37
37
import org .silverpeas .core .contact .model .CompleteContact ;
38
38
import org .silverpeas .core .contact .model .ContactDetail ;
39
39
import org .silverpeas .core .contact .model .ContactPK ;
40
- import org .silverpeas .core .contribution .content .form .PagesContext ;
41
40
import org .silverpeas .core .contribution .template .publication .PublicationTemplateManager ;
42
41
import org .silverpeas .core .index .search .SearchEngineProvider ;
43
42
import org .silverpeas .core .index .search .model .MatchingIndexEntry ;
44
43
import org .silverpeas .core .index .search .model .ParseException ;
45
44
import org .silverpeas .core .index .search .model .QueryDescription ;
46
45
import org .silverpeas .core .socialnetwork .relationship .RelationShipService ;
46
+ import org .silverpeas .core .web .rs .UserPrivilegeValidation ;
47
+ import org .silverpeas .core .web .rs .annotation .Authorized ;
47
48
import org .silverpeas .kernel .bundle .ResourceLocator ;
48
49
import org .silverpeas .kernel .bundle .SettingBundle ;
49
- import org .silverpeas .kernel .util .StringUtil ;
50
50
import org .silverpeas .kernel .logging .SilverLogger ;
51
- import org .silverpeas .core .web .rs .RESTWebService ;
52
- import org .silverpeas .core .web .rs .UserPrivilegeValidation ;
53
- import org .silverpeas .core .web .rs .annotation .Authorized ;
51
+ import org .silverpeas .kernel .util .StringUtil ;
54
52
import org .silverpeas .mobile .server .common .SpMobileLogModule ;
55
53
import org .silverpeas .mobile .server .helpers .DataURLHelper ;
54
+ import org .silverpeas .mobile .server .services .helpers .UserHelper ;
56
55
import org .silverpeas .mobile .shared .dto .DetailUserDTO ;
57
56
import org .silverpeas .mobile .shared .dto .contact .ContactFilters ;
58
57
import org .silverpeas .mobile .shared .dto .contact .ContactScope ;
59
58
59
+ import javax .inject .Inject ;
60
60
import javax .servlet .http .HttpServletRequest ;
61
- import javax .ws .rs .GET ;
62
- import javax .ws .rs .Path ;
63
- import javax .ws .rs .PathParam ;
64
- import javax .ws .rs .Produces ;
65
- import javax .ws .rs .QueryParam ;
61
+ import javax .ws .rs .*;
66
62
import javax .ws .rs .core .Context ;
67
63
import javax .ws .rs .core .MediaType ;
68
- import java .util .ArrayList ;
69
- import java .util .Arrays ;
70
- import java .util .Collections ;
71
- import java .util .Comparator ;
72
- import java .util .HashSet ;
73
- import java .util .List ;
74
- import java .util .Map ;
75
- import java .util .StringTokenizer ;
64
+ import java .util .*;
76
65
77
66
@ WebService
78
67
@ Authorized
@@ -84,11 +73,13 @@ public class ServiceContact extends AbstractRestWebService {
84
73
85
74
static final String PATH = "mobile/contact" ;
86
75
87
- private OrganizationController organizationController = OrganizationController .get ();
88
- private RelationShipService relationShipService = RelationShipService .get ();
76
+ @ Inject
77
+ private OrganizationController organizationController ;
78
+ @ Inject
79
+ private RelationShipService relationShipService ;
89
80
90
81
private List <String > getUserProperties () {
91
- List <String > userProperties = new ArrayList <String >();
82
+ List <String > userProperties = new ArrayList <>();
92
83
String properties = getSettings ().getString ("directory.user.properties" , "" );
93
84
StringTokenizer stkU = new StringTokenizer (properties , "," );
94
85
while (stkU .hasMoreTokens ()) {
@@ -98,7 +89,7 @@ private List<String> getUserProperties() {
98
89
}
99
90
100
91
private List <String > getContactProperties () {
101
- List <String > contactProperties = new ArrayList <String >();
92
+ List <String > contactProperties = new ArrayList <>();
102
93
String formProperties = getSettings ().getString ("directory.contact.properties" , "" );
103
94
StringTokenizer stkC = new StringTokenizer (formProperties , "," );
104
95
while (stkC .hasMoreTokens ()) {
@@ -108,7 +99,7 @@ private List<String> getContactProperties() {
108
99
}
109
100
110
101
private List <String > getdomainsIds () {
111
- List <String > domainsIds = new ArrayList <String >();
102
+ List <String > domainsIds = new ArrayList <>();
112
103
String domains = getSettings ().getString ("directory.domains" , "" );
113
104
114
105
StringTokenizer stk = new StringTokenizer (domains , "," );
@@ -122,10 +113,9 @@ private List<String> getdomainsIds() {
122
113
@ GET
123
114
@ Produces (MediaType .APPLICATION_JSON )
124
115
@ Path ("/contact/{userId}/" )
125
- public DetailUserDTO getContact (@ PathParam ("userId" ) String userId ) throws Exception {
116
+ public DetailUserDTO getContact (@ PathParam ("userId" ) String userId ) throws AdminException {
126
117
UserDetail u = Administration .get ().getUserDetail (userId );
127
- DetailUserDTO d = populate (u , false );
128
- return d ;
118
+ return populate (u , false );
129
119
}
130
120
131
121
/**
@@ -138,39 +128,47 @@ public DetailUserDTO getContact(@PathParam("userId") String userId) throws Excep
138
128
@ Path ("paging/{type}/" )
139
129
public List <DetailUserDTO > getContacts (@ PathParam ("type" ) String type ,
140
130
@ QueryParam ("filter" ) String filter , @ QueryParam ("pageSize" ) int pageSize ,
141
- @ QueryParam ("startIndex" ) int startIndex ) throws Exception {
142
- ArrayList <DetailUserDTO > listUsers = new ArrayList <DetailUserDTO >();
131
+ @ QueryParam ("startIndex" ) int startIndex ) {
132
+ ArrayList <DetailUserDTO > listUsers = new ArrayList <>();
143
133
try {
144
- if (type .equals (ContactFilters .ALL )) {
145
- List tabUserDetail = getUsersByQuery (filter , "UserFull" );
146
- for (int i = 0 ; i < tabUserDetail .size (); i ++) {
147
- if (i >= startIndex && i < startIndex + pageSize ) {
148
- DetailUserDTO dto = populate (tabUserDetail .get (i ), true );
149
- if (dto != null ) listUsers .add (dto );
150
- }
151
- }
152
- } else if (type .equals (ContactFilters .ALL_EXT )) {
153
- List tabUserDetail = getUsersByQuery (filter , "Contact" );
154
- for (int i = 0 ; i < tabUserDetail .size (); i ++) {
155
- if (i >= startIndex && i < startIndex + pageSize ) {
156
- listUsers .add (populate (tabUserDetail .get (i ), true ));
157
- }
134
+ switch (type ) {
135
+ case ContactFilters .ALL : {
136
+ List tabUserDetail = getUsersByQuery (filter , "UserFull" );
137
+ for (int i = 0 ; i < tabUserDetail .size (); i ++) {
138
+ if (i >= startIndex && i < startIndex + pageSize ) {
139
+ DetailUserDTO dto = populate (tabUserDetail .get (i ), true );
140
+ if (dto != null ) listUsers .add (dto );
158
141
}
159
- } else if (type .equals (ContactFilters .MY )) {
160
- List <String > contactsIds =
161
- relationShipService .getMyContactsIds (Integer .parseInt (getUser ().getId ()));
162
-
163
- for (int j = 0 ; j < contactsIds .size (); j ++) {
164
- if (j >= startIndex && j < startIndex + pageSize ) {
165
- String id = contactsIds .get (j );
166
- UserDetail userDetail = organizationController .getUserDetail (id );
167
- DetailUserDTO userDTO = populate (userDetail , true );
168
- listUsers .add (userDTO );
169
- }
142
+ }
143
+ break ;
144
+ }
145
+ case ContactFilters .ALL_EXT : {
146
+ List tabUserDetail = getUsersByQuery (filter , "Contact" );
147
+ for (int i = 0 ; i < tabUserDetail .size (); i ++) {
148
+ if (i >= startIndex && i < startIndex + pageSize ) {
149
+ listUsers .add (populate (tabUserDetail .get (i ), true ));
170
150
}
151
+ }
152
+ break ;
171
153
}
154
+ case ContactFilters .MY :
155
+ List <String > contactsIds =
156
+ relationShipService .getMyContactsIds (Integer .parseInt (getUser ().getId ()));
157
+
158
+ for (int j = 0 ; j < contactsIds .size (); j ++) {
159
+ if (j >= startIndex && j < startIndex + pageSize ) {
160
+ String id = contactsIds .get (j );
161
+ UserDetail userDetail = organizationController .getUserDetail (id );
162
+ DetailUserDTO userDTO = populate (userDetail , true );
163
+ listUsers .add (userDTO );
164
+ }
165
+ }
166
+ break ;
167
+ default :
168
+ break ;
169
+ }
172
170
defaultSortContacts (listUsers );
173
- } catch (Throwable e ) {
171
+ } catch (Exception e ) {
174
172
SilverLogger .getLogger (this ).error ("ServiceContact.getContacts" , "root.EX_NO_MESSAGE" , e );
175
173
throw e ;
176
174
}
@@ -182,8 +180,8 @@ public List<DetailUserDTO> getContacts(@PathParam("type") String type,
182
180
@ Produces (MediaType .APPLICATION_JSON )
183
181
@ Path ("{type}/" )
184
182
public List <DetailUserDTO > getContactsFiltered (@ PathParam ("type" ) String type , @ QueryParam ("filter" ) String filter )
185
- throws Exception {
186
- ArrayList <DetailUserDTO > listUsers = new ArrayList <DetailUserDTO >();
183
+ throws AdminException {
184
+ ArrayList <DetailUserDTO > listUsers = new ArrayList <>();
187
185
try {
188
186
if (type .equals (ContactScope .group .name ())) {
189
187
String [] groupsIds = filter .split ("," );
@@ -192,7 +190,7 @@ public List<DetailUserDTO> getContactsFiltered(@PathParam("type") String type, @
192
190
for (GroupDetail group : groups ) {
193
191
users .addAll (group .getAllUsers ());
194
192
}
195
- users = new ArrayList <User >(new HashSet <>(users ));
193
+ users = new ArrayList <>(new HashSet <>(users ));
196
194
for (User user : users ) {
197
195
DetailUserDTO userDTO = populate (user , true );
198
196
listUsers .add (userDTO );
@@ -208,6 +206,7 @@ public List<DetailUserDTO> getContactsFiltered(@PathParam("type") String type, @
208
206
}
209
207
}
210
208
209
+ Objects .requireNonNull (users );
211
210
for (User user : users ) {
212
211
DetailUserDTO userDTO = populate (user , true );
213
212
listUsers .add (userDTO );
@@ -219,7 +218,7 @@ public List<DetailUserDTO> getContactsFiltered(@PathParam("type") String type, @
219
218
listUsers .add (userDTO );
220
219
}
221
220
}
222
- } catch (Throwable e ) {
221
+ } catch (Exception e ) {
223
222
SilverLogger .getLogger (this )
224
223
.error ("ServiceContact.getContactsFiltered" , "root.EX_NO_MESSAGE" , e );
225
224
throw e ;
@@ -228,13 +227,8 @@ public List<DetailUserDTO> getContactsFiltered(@PathParam("type") String type, @
228
227
return listUsers ;
229
228
}
230
229
231
- private void defaultSortContacts (final List listUsers ) {
232
- Collections .sort (listUsers , new Comparator <DetailUserDTO >() {
233
- @ Override
234
- public int compare (final DetailUserDTO u1 , final DetailUserDTO u2 ) {
235
- return u1 .getLastName ().compareTo (u2 .getLastName ());
236
- }
237
- });
230
+ private void defaultSortContacts (final List <DetailUserDTO > listUsers ) {
231
+ listUsers .sort (Comparator .comparing (DetailUserDTO ::getLastName ));
238
232
}
239
233
240
234
@ GET
@@ -248,23 +242,12 @@ public ContactFilters hasContacts() {
248
242
try {
249
243
contactsIds = relationShipService .getMyContactsIds (Integer .parseInt (getUser ().getId ()));
250
244
} catch (Exception e ) {
245
+ // don't treat this case
251
246
}
252
247
result .setHasPersonnalContacts (!contactsIds .isEmpty ());
253
248
return result ;
254
249
}
255
250
256
- private List getFilteredUserList (final String filter , String type ) {
257
- UserDetail [] tabUserDetail = organizationController .getAllUsers ();
258
- List filteredUserList = null ;
259
- if (filter .isEmpty ()) {
260
- filteredUserList = new ArrayList <UserDetail >(Arrays .asList (tabUserDetail ));
261
- return filteredUserList ;
262
- }
263
- filteredUserList = getUsersByQuery (filter , type );
264
-
265
- return filteredUserList ;
266
- }
267
-
268
251
private List getUsersByQuery (String query , String type ) {
269
252
if (!query .isEmpty ()) {
270
253
query += '*' ;
@@ -311,7 +294,7 @@ private List getUsersByQuery(String query, String type) {
311
294
private List <String > getContactComponentIds () {
312
295
String [] appIds =
313
296
organizationController .getComponentIdsForUser (getUser ().getId (), "yellowpages" );
314
- List <String > result = new ArrayList <String >();
297
+ List <String > result = new ArrayList <>();
315
298
for (String appId : appIds ) {
316
299
String param =
317
300
organizationController .getComponentParameterValue (appId , "displayedInDirectory" );
@@ -322,28 +305,16 @@ private List<String> getContactComponentIds() {
322
305
return result ;
323
306
}
324
307
325
- /**
326
- * Populate user or contact DTO.
327
- *
328
- * @param
329
- * @return
330
- */
331
308
private DetailUserDTO populate (Object user , boolean filtered ) {
332
- if (user != null && user instanceof UserDetail ) {
309
+ if (user instanceof UserDetail ) {
333
310
UserDetail userDetail = (UserDetail ) user ;
334
311
SilverLogger .getLogger (this ).debug (SpMobileLogModule .getName (), "ServiceContact.populate" ,
335
312
"User id=" + userDetail .getId ());
336
313
UserFull userFull = UserFull .getById (userDetail .getId ());
337
314
DetailUserDTO dto = null ;
338
315
if (userFull != null ) {
339
316
dto = new DetailUserDTO ();
340
- dto .setId (userFull .getId ());
341
- dto .setFirstName (userFull .getFirstName ());
342
- dto .setLastName (userFull .getLastName ());
343
- dto .seteMail (userFull .getEmailAddress ());
344
- dto .setStatus (userFull .getStatus ());
345
- dto .setAvatar (userFull .getAvatar ());
346
- dto .setLanguage (userFull .getUserPreferences ().getLanguage ());
317
+ UserHelper .initUserDTO (userFull , dto );
347
318
dto .setConnected (userFull .isConnected ());
348
319
String avatar ;
349
320
@@ -354,9 +325,8 @@ private DetailUserDTO populate(Object user, boolean filtered) {
354
325
dto .addPropertyLabel (prop , label );
355
326
}
356
327
Map <String , String > labels = PublicationTemplateManager .getInstance ().getDirectoryFormLabels (userFull .getId (), userFull .getDomainId (), dto .getLanguage ());
357
- for (String prop : labels .keySet ()) {
358
- String value = labels .get (prop );
359
- dto .addPropertyLabel (prop , value );
328
+ for (Map .Entry <String , String > prop : labels .entrySet ()) {
329
+ dto .addPropertyLabel (prop .getKey (), prop .getValue ());
360
330
}
361
331
362
332
if (filtered ) {
@@ -368,14 +338,14 @@ private DetailUserDTO populate(Object user, boolean filtered) {
368
338
} else {
369
339
avatar = DataURLHelper .convertAvatarToUrlData (userDetail .getAvatarFileName (), "96x" );
370
340
Map <String , String > fields = userFull .getAllDefinedValues (userFull .getUserPreferences ().getLanguage ());
371
- for (String prop : fields .keySet ()) {
372
- dto .addProperty (prop , fields . get ( prop ));
341
+ for (Map . Entry < String , String > prop : fields .entrySet ()) {
342
+ dto .addProperty (prop . getKey (), prop . getValue ( ));
373
343
}
374
344
}
375
345
dto .setAvatar (avatar );
376
346
}
377
347
return dto ;
378
- } else if (user != null && user instanceof ContactDetail ) {
348
+ } else if (user instanceof ContactDetail ) {
379
349
ContactDetail contactDetail = (ContactDetail ) user ;
380
350
SilverLogger .getLogger (this ).debug (SpMobileLogModule .getName (), "ServiceContact.populate" ,
381
351
"Contact id=" + contactDetail .getPK ().getId () + "app id=" +
@@ -402,8 +372,8 @@ private DetailUserDTO populate(Object user, boolean filtered) {
402
372
dto .addProperty (prop , fields .get (prop ));
403
373
}
404
374
} else {
405
- for (String prop : fields .keySet ()) {
406
- dto .addProperty (prop , fields . get ( prop ));
375
+ for (Map . Entry < String , String > prop : fields .entrySet ()) {
376
+ dto .addProperty (prop . getKey (), prop . getValue ( ));
407
377
}
408
378
}
409
379
}
0 commit comments