Skip to content

Commit 2fafa7a

Browse files
committed
Fix in UserHelper the call to Group#getNbUsers() that has been renamed
to Group#getDirectUsersCount(). Refine the code of ServiceContact and ServiceNotification by removing some code duplication and by fixing some code quality issues reported by Sonar.
1 parent 2656126 commit 2fafa7a

File tree

3 files changed

+168
-228
lines changed

3 files changed

+168
-228
lines changed

mobile-war/src/main/java/org/silverpeas/mobile/server/services/ServiceContact.java

Lines changed: 70 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,31 @@
3737
import org.silverpeas.core.contact.model.CompleteContact;
3838
import org.silverpeas.core.contact.model.ContactDetail;
3939
import org.silverpeas.core.contact.model.ContactPK;
40-
import org.silverpeas.core.contribution.content.form.PagesContext;
4140
import org.silverpeas.core.contribution.template.publication.PublicationTemplateManager;
4241
import org.silverpeas.core.index.search.SearchEngineProvider;
4342
import org.silverpeas.core.index.search.model.MatchingIndexEntry;
4443
import org.silverpeas.core.index.search.model.ParseException;
4544
import org.silverpeas.core.index.search.model.QueryDescription;
4645
import org.silverpeas.core.socialnetwork.relationship.RelationShipService;
46+
import org.silverpeas.core.web.rs.UserPrivilegeValidation;
47+
import org.silverpeas.core.web.rs.annotation.Authorized;
4748
import org.silverpeas.kernel.bundle.ResourceLocator;
4849
import org.silverpeas.kernel.bundle.SettingBundle;
49-
import org.silverpeas.kernel.util.StringUtil;
5050
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;
5452
import org.silverpeas.mobile.server.common.SpMobileLogModule;
5553
import org.silverpeas.mobile.server.helpers.DataURLHelper;
54+
import org.silverpeas.mobile.server.services.helpers.UserHelper;
5655
import org.silverpeas.mobile.shared.dto.DetailUserDTO;
5756
import org.silverpeas.mobile.shared.dto.contact.ContactFilters;
5857
import org.silverpeas.mobile.shared.dto.contact.ContactScope;
5958

59+
import javax.inject.Inject;
6060
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.*;
6662
import javax.ws.rs.core.Context;
6763
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.*;
7665

7766
@WebService
7867
@Authorized
@@ -84,11 +73,13 @@ public class ServiceContact extends AbstractRestWebService {
8473

8574
static final String PATH = "mobile/contact";
8675

87-
private OrganizationController organizationController = OrganizationController.get();
88-
private RelationShipService relationShipService = RelationShipService.get();
76+
@Inject
77+
private OrganizationController organizationController;
78+
@Inject
79+
private RelationShipService relationShipService;
8980

9081
private List<String> getUserProperties() {
91-
List<String> userProperties = new ArrayList<String>();
82+
List<String> userProperties = new ArrayList<>();
9283
String properties = getSettings().getString("directory.user.properties", "");
9384
StringTokenizer stkU = new StringTokenizer(properties, ",");
9485
while (stkU.hasMoreTokens()) {
@@ -98,7 +89,7 @@ private List<String> getUserProperties() {
9889
}
9990

10091
private List<String> getContactProperties() {
101-
List<String> contactProperties = new ArrayList<String>();
92+
List<String> contactProperties = new ArrayList<>();
10293
String formProperties = getSettings().getString("directory.contact.properties", "");
10394
StringTokenizer stkC = new StringTokenizer(formProperties, ",");
10495
while (stkC.hasMoreTokens()) {
@@ -108,7 +99,7 @@ private List<String> getContactProperties() {
10899
}
109100

110101
private List<String> getdomainsIds() {
111-
List<String> domainsIds = new ArrayList<String>();
102+
List<String> domainsIds = new ArrayList<>();
112103
String domains = getSettings().getString("directory.domains", "");
113104

114105
StringTokenizer stk = new StringTokenizer(domains, ",");
@@ -122,10 +113,9 @@ private List<String> getdomainsIds() {
122113
@GET
123114
@Produces(MediaType.APPLICATION_JSON)
124115
@Path("/contact/{userId}/")
125-
public DetailUserDTO getContact(@PathParam("userId") String userId) throws Exception {
116+
public DetailUserDTO getContact(@PathParam("userId") String userId) throws AdminException {
126117
UserDetail u = Administration.get().getUserDetail(userId);
127-
DetailUserDTO d = populate(u, false);
128-
return d;
118+
return populate(u, false);
129119
}
130120

131121
/**
@@ -138,39 +128,47 @@ public DetailUserDTO getContact(@PathParam("userId") String userId) throws Excep
138128
@Path("paging/{type}/")
139129
public List<DetailUserDTO> getContacts(@PathParam("type") String type,
140130
@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<>();
143133
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);
158141
}
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));
170150
}
151+
}
152+
break;
171153
}
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+
}
172170
defaultSortContacts(listUsers);
173-
} catch (Throwable e) {
171+
} catch (Exception e) {
174172
SilverLogger.getLogger(this).error("ServiceContact.getContacts", "root.EX_NO_MESSAGE", e);
175173
throw e;
176174
}
@@ -182,8 +180,8 @@ public List<DetailUserDTO> getContacts(@PathParam("type") String type,
182180
@Produces(MediaType.APPLICATION_JSON)
183181
@Path("{type}/")
184182
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<>();
187185
try {
188186
if (type.equals(ContactScope.group.name())) {
189187
String[] groupsIds = filter.split(",");
@@ -192,7 +190,7 @@ public List<DetailUserDTO> getContactsFiltered(@PathParam("type") String type, @
192190
for (GroupDetail group : groups) {
193191
users.addAll(group.getAllUsers());
194192
}
195-
users = new ArrayList<User>(new HashSet<>(users));
193+
users = new ArrayList<>(new HashSet<>(users));
196194
for (User user : users) {
197195
DetailUserDTO userDTO = populate(user, true);
198196
listUsers.add(userDTO);
@@ -208,6 +206,7 @@ public List<DetailUserDTO> getContactsFiltered(@PathParam("type") String type, @
208206
}
209207
}
210208

209+
Objects.requireNonNull(users);
211210
for (User user : users) {
212211
DetailUserDTO userDTO = populate(user, true);
213212
listUsers.add(userDTO);
@@ -219,7 +218,7 @@ public List<DetailUserDTO> getContactsFiltered(@PathParam("type") String type, @
219218
listUsers.add(userDTO);
220219
}
221220
}
222-
} catch (Throwable e) {
221+
} catch (Exception e) {
223222
SilverLogger.getLogger(this)
224223
.error("ServiceContact.getContactsFiltered", "root.EX_NO_MESSAGE", e);
225224
throw e;
@@ -228,13 +227,8 @@ public List<DetailUserDTO> getContactsFiltered(@PathParam("type") String type, @
228227
return listUsers;
229228
}
230229

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));
238232
}
239233

240234
@GET
@@ -248,23 +242,12 @@ public ContactFilters hasContacts() {
248242
try {
249243
contactsIds = relationShipService.getMyContactsIds(Integer.parseInt(getUser().getId()));
250244
} catch (Exception e) {
245+
// don't treat this case
251246
}
252247
result.setHasPersonnalContacts(!contactsIds.isEmpty());
253248
return result;
254249
}
255250

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-
268251
private List getUsersByQuery(String query, String type) {
269252
if (!query.isEmpty()) {
270253
query += '*';
@@ -311,7 +294,7 @@ private List getUsersByQuery(String query, String type) {
311294
private List<String> getContactComponentIds() {
312295
String[] appIds =
313296
organizationController.getComponentIdsForUser(getUser().getId(), "yellowpages");
314-
List<String> result = new ArrayList<String>();
297+
List<String> result = new ArrayList<>();
315298
for (String appId : appIds) {
316299
String param =
317300
organizationController.getComponentParameterValue(appId, "displayedInDirectory");
@@ -322,28 +305,16 @@ private List<String> getContactComponentIds() {
322305
return result;
323306
}
324307

325-
/**
326-
* Populate user or contact DTO.
327-
*
328-
* @param
329-
* @return
330-
*/
331308
private DetailUserDTO populate(Object user, boolean filtered) {
332-
if (user != null && user instanceof UserDetail) {
309+
if (user instanceof UserDetail) {
333310
UserDetail userDetail = (UserDetail) user;
334311
SilverLogger.getLogger(this).debug(SpMobileLogModule.getName(), "ServiceContact.populate",
335312
"User id=" + userDetail.getId());
336313
UserFull userFull = UserFull.getById(userDetail.getId());
337314
DetailUserDTO dto = null;
338315
if (userFull != null) {
339316
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);
347318
dto.setConnected(userFull.isConnected());
348319
String avatar;
349320

@@ -354,9 +325,8 @@ private DetailUserDTO populate(Object user, boolean filtered) {
354325
dto.addPropertyLabel(prop, label);
355326
}
356327
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());
360330
}
361331

362332
if (filtered) {
@@ -368,14 +338,14 @@ private DetailUserDTO populate(Object user, boolean filtered) {
368338
} else {
369339
avatar = DataURLHelper.convertAvatarToUrlData(userDetail.getAvatarFileName(), "96x");
370340
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());
373343
}
374344
}
375345
dto.setAvatar(avatar);
376346
}
377347
return dto;
378-
} else if (user != null && user instanceof ContactDetail) {
348+
} else if (user instanceof ContactDetail) {
379349
ContactDetail contactDetail = (ContactDetail) user;
380350
SilverLogger.getLogger(this).debug(SpMobileLogModule.getName(), "ServiceContact.populate",
381351
"Contact id=" + contactDetail.getPK().getId() + "app id=" +
@@ -402,8 +372,8 @@ private DetailUserDTO populate(Object user, boolean filtered) {
402372
dto.addProperty(prop, fields.get(prop));
403373
}
404374
} 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());
407377
}
408378
}
409379
}

0 commit comments

Comments
 (0)