15
15
*/
16
16
package org .springframework .data .ldap .repository .support ;
17
17
18
+ import static org .springframework .ldap .query .LdapQueryBuilder .*;
19
+
20
+ import java .util .List ;
21
+ import java .util .Optional ;
22
+ import java .util .stream .Collectors ;
23
+ import java .util .stream .StreamSupport ;
24
+
25
+ import javax .naming .Name ;
26
+
18
27
import org .springframework .dao .EmptyResultDataAccessException ;
19
28
import org .springframework .data .domain .Persistable ;
20
29
import org .springframework .data .ldap .repository .LdapRepository ;
28
37
import org .springframework .ldap .query .LdapQuery ;
29
38
import org .springframework .util .Assert ;
30
39
31
- import javax .naming .Name ;
32
- import java .util .List ;
33
- import java .util .Optional ;
34
- import java .util .stream .Collectors ;
35
- import java .util .stream .StreamSupport ;
36
-
37
- import static org .springframework .ldap .query .LdapQueryBuilder .*;
38
-
39
40
/**
40
41
* Base repository implementation for LDAP.
41
42
*
@@ -55,8 +56,8 @@ public class SimpleLdapRepository<T> implements LdapRepository<T> {
55
56
* Creates a new {@link SimpleLdapRepository}.
56
57
*
57
58
* @param ldapOperations must not be {@literal null}.
58
- * @param odm must not be {@literal null}.
59
- * @param entityType must not be {@literal null}.
59
+ * @param odm must not be {@literal null}.
60
+ * @param entityType must not be {@literal null}.
60
61
*/
61
62
public SimpleLdapRepository (LdapOperations ldapOperations , ObjectDirectoryMapper odm , Class <T > entityType ) {
62
63
@@ -69,31 +70,12 @@ public SimpleLdapRepository(LdapOperations ldapOperations, ObjectDirectoryMapper
69
70
this .entityType = entityType ;
70
71
}
71
72
72
- /* (non-Javadoc)
73
- * @see org.springframework.data.repository.CrudRepository#count()
74
- */
75
- @ Override
76
- public long count () {
77
-
78
- Filter filter = odm .filterFor (entityType , null );
79
- CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler ();
80
- LdapQuery query = query ().attributes (OBJECTCLASS_ATTRIBUTE ).filter (filter );
81
- ldapOperations .search (query , callback );
82
-
83
- return callback .getNoOfRows ();
84
- }
85
-
86
- private <S extends T > boolean isNew (S entity , @ Nullable Name id ) {
73
+ // -------------------------------------------------------------------------
74
+ // Methods from CrudRepository
75
+ // -------------------------------------------------------------------------
87
76
88
- if (entity instanceof Persistable ) {
89
- Persistable <?> persistable = (Persistable <?>) entity ;
90
- return persistable .isNew ();
91
- } else {
92
- return id == null ;
93
- }
94
- }
95
-
96
- /* (non-Javadoc)
77
+ /*
78
+ * (non-Javadoc)
97
79
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
98
80
*/
99
81
@ Override
@@ -112,18 +94,20 @@ public <S extends T> S save(S entity) {
112
94
return entity ;
113
95
}
114
96
115
- /* (non-Javadoc)
97
+ /*
98
+ * (non-Javadoc)
116
99
* @see org.springframework.data.repository.CrudRepository#saveAll(java.lang.Iterable)
117
100
*/
118
101
@ Override
119
- public <S extends T > Iterable <S > saveAll (Iterable <S > entities ) {
102
+ public <S extends T > List <S > saveAll (Iterable <S > entities ) {
120
103
121
104
return StreamSupport .stream (entities .spliterator (), false ) //
122
105
.map (this ::save ) //
123
106
.collect (Collectors .toList ());
124
107
}
125
108
126
- /* (non-Javadoc)
109
+ /*
110
+ * (non-Javadoc)
127
111
* @see org.springframework.data.repository.CrudRepository#findById(java.io.Serializable)
128
112
*/
129
113
@ Override
@@ -138,32 +122,8 @@ public Optional<T> findById(Name name) {
138
122
}
139
123
}
140
124
141
- /* (non-Javadoc)
142
- * @see org.springframework.data.ldap.repository.LdapRepository#findAll(org.springframework.ldap.query.LdapQuery)
143
- */
144
- @ Override
145
- public List <T > findAll (LdapQuery ldapQuery ) {
146
-
147
- Assert .notNull (ldapQuery , "LdapQuery must not be null" );
148
- return ldapOperations .find (ldapQuery , entityType );
149
- }
150
-
151
- /* (non-Javadoc)
152
- * @see org.springframework.data.ldap.repository.LdapRepository#findOne(org.springframework.ldap.query.LdapQuery)
153
- */
154
- @ Override
155
- public Optional <T > findOne (LdapQuery ldapQuery ) {
156
-
157
- Assert .notNull (ldapQuery , "LdapQuery must not be null" );
158
-
159
- try {
160
- return Optional .ofNullable (ldapOperations .findOne (ldapQuery , entityType ));
161
- } catch (EmptyResultDataAccessException e ) {
162
- return Optional .empty ();
163
- }
164
- }
165
-
166
- /* (non-Javadoc)
125
+ /*
126
+ * (non-Javadoc)
167
127
* @see org.springframework.data.repository.CrudRepository#existsById(java.io.Serializable)
168
128
*/
169
129
@ Override
@@ -174,27 +134,45 @@ public boolean existsById(Name name) {
174
134
return findById (name ).isPresent ();
175
135
}
176
136
177
- /* (non-Javadoc)
137
+ /*
138
+ * (non-Javadoc)
178
139
* @see org.springframework.data.repository.CrudRepository#findAll()
179
140
*/
180
141
@ Override
181
142
public List <T > findAll () {
182
143
return ldapOperations .findAll (entityType );
183
144
}
184
145
185
- /* (non-Javadoc)
146
+ /*
147
+ * (non-Javadoc)
186
148
* @see org.springframework.data.repository.CrudRepository#findAllById(java.lang.Iterable)
187
149
*/
188
150
@ Override
189
- public List <T > findAllById (final Iterable <Name > names ) {
151
+ public List <T > findAllById (Iterable <Name > names ) {
190
152
191
153
return StreamSupport .stream (names .spliterator (), false ) //
192
154
.map (this ::findById ) //
193
155
.flatMap (Optionals ::toStream ) //
194
156
.collect (Collectors .toList ());
195
157
}
196
158
197
- /* (non-Javadoc)
159
+ /*
160
+ * (non-Javadoc)
161
+ * @see org.springframework.data.repository.CrudRepository#count()
162
+ */
163
+ @ Override
164
+ public long count () {
165
+
166
+ Filter filter = odm .filterFor (entityType , null );
167
+ CountNameClassPairCallbackHandler callback = new CountNameClassPairCallbackHandler ();
168
+ LdapQuery query = query ().attributes (OBJECTCLASS_ATTRIBUTE ).filter (filter );
169
+ ldapOperations .search (query , callback );
170
+
171
+ return callback .getNoOfRows ();
172
+ }
173
+
174
+ /*
175
+ * (non-Javadoc)
198
176
* @see org.springframework.data.repository.CrudRepository#deleteById(java.io.Serializable)
199
177
*/
200
178
@ Override
@@ -205,7 +183,8 @@ public void deleteById(Name name) {
205
183
ldapOperations .unbind (name );
206
184
}
207
185
208
- /* (non-Javadoc)
186
+ /*
187
+ * (non-Javadoc)
209
188
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
210
189
*/
211
190
@ Override
@@ -216,7 +195,20 @@ public void delete(T entity) {
216
195
ldapOperations .delete (entity );
217
196
}
218
197
219
- /* (non-Javadoc)
198
+ /*
199
+ * (non-Javadoc)
200
+ * @see org.springframework.data.repository.CrudRepository#deleteAllById(java.lang.Iterable)
201
+ */
202
+ @ Override
203
+ public void deleteAllById (Iterable <? extends Name > names ) {
204
+
205
+ Assert .notNull (names , "Names must not be null." );
206
+
207
+ names .forEach (this ::deleteById );
208
+ }
209
+
210
+ /*
211
+ * (non-Javadoc)
220
212
* @see org.springframework.data.repository.CrudRepository#deleteAll(java.lang.Iterable)
221
213
*/
222
214
@ Override
@@ -227,19 +219,56 @@ public void deleteAll(Iterable<? extends T> entities) {
227
219
entities .forEach (this ::delete );
228
220
}
229
221
222
+ /*
223
+ * (non-Javadoc)
224
+ * @see org.springframework.data.repository.CrudRepository#deleteAll()
225
+ */
230
226
@ Override
231
- public void deleteAllById (Iterable <? extends Name > names ) {
227
+ public void deleteAll () {
228
+ deleteAll (findAll ());
229
+ }
232
230
233
- Assert .notNull (names , "Names must not be null." );
231
+ // -------------------------------------------------------------------------
232
+ // Methods from LdapRepository
233
+ // ------------------------------------------------------------------------
234
234
235
- names .forEach (this ::deleteById );
235
+ /*
236
+ * (non-Javadoc)
237
+ * @see org.springframework.data.ldap.repository.LdapRepository#findOne(org.springframework.ldap.query.LdapQuery)
238
+ */
239
+ @ Override
240
+ public Optional <T > findOne (LdapQuery ldapQuery ) {
241
+
242
+ Assert .notNull (ldapQuery , "LdapQuery must not be null" );
243
+
244
+ try {
245
+ return Optional .ofNullable (ldapOperations .findOne (ldapQuery , entityType ));
246
+ } catch (EmptyResultDataAccessException e ) {
247
+ return Optional .empty ();
248
+ }
236
249
}
237
250
238
- /* (non-Javadoc)
239
- * @see org.springframework.data.repository.CrudRepository#deleteAll()
251
+ /*
252
+ * (non-Javadoc)
253
+ * @see org.springframework.data.ldap.repository.LdapRepository#findAll(org.springframework.ldap.query.LdapQuery)
240
254
*/
241
255
@ Override
242
- public void deleteAll () {
243
- deleteAll (findAll ());
256
+ public List <T > findAll (LdapQuery ldapQuery ) {
257
+
258
+ Assert .notNull (ldapQuery , "LdapQuery must not be null" );
259
+ return ldapOperations .find (ldapQuery , entityType );
244
260
}
261
+
262
+
263
+ private <S extends T > boolean isNew (S entity , @ Nullable Name id ) {
264
+
265
+ if (entity instanceof Persistable ) {
266
+ Persistable <?> persistable = (Persistable <?>) entity ;
267
+ return persistable .isNew ();
268
+ } else {
269
+ return id == null ;
270
+ }
271
+ }
272
+
273
+
245
274
}
0 commit comments