/
ServiceNavigationImpl.java
289 lines (258 loc) · 11.5 KB
/
ServiceNavigationImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.silverpeas.mobile.server.services;
import com.silverpeas.mobile.server.common.SpMobileLogModule;
import com.silverpeas.mobile.server.services.helpers.FavoritesHelper;
import com.silverpeas.mobile.server.services.helpers.NewsHelper;
import com.silverpeas.mobile.shared.dto.ContentsTypes;
import com.silverpeas.mobile.shared.dto.HomePageDTO;
import com.silverpeas.mobile.shared.dto.RightDTO;
import com.silverpeas.mobile.shared.dto.documents.PublicationDTO;
import com.silverpeas.mobile.shared.dto.navigation.ApplicationInstanceDTO;
import com.silverpeas.mobile.shared.dto.navigation.Apps;
import com.silverpeas.mobile.shared.dto.navigation.SilverpeasObjectDTO;
import com.silverpeas.mobile.shared.dto.navigation.SpaceDTO;
import com.silverpeas.mobile.shared.exceptions.AuthenticationException;
import com.silverpeas.mobile.shared.exceptions.NavigationException;
import com.silverpeas.mobile.shared.services.navigation.ServiceNavigation;
import org.apache.commons.lang3.EnumUtils;
import org.silverpeas.components.gallery.model.Media;
import org.silverpeas.components.gallery.model.MediaPK;
import org.silverpeas.components.gallery.service.MediaServiceProvider;
import org.silverpeas.core.admin.component.model.ComponentInstLight;
import org.silverpeas.core.admin.service.Administration;
import org.silverpeas.core.admin.service.OrganizationController;
import org.silverpeas.core.admin.space.SpaceInstLight;
import org.silverpeas.core.contribution.publication.model.PublicationDetail;
import org.silverpeas.core.contribution.publication.model.PublicationPK;
import org.silverpeas.core.contribution.publication.service.PublicationService;
import org.silverpeas.core.mylinks.model.LinkDetail;
import org.silverpeas.core.util.ResourceLocator;
import org.silverpeas.core.util.SettingBundle;
import org.silverpeas.core.util.logging.SilverLogger;
import org.silverpeas.core.web.look.PublicationHelper;
import org.silverpeas.core.web.util.viewgenerator.html.GraphicElementFactory;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Service de gestion de la navigation dans les espaces et apps.
* @author svuillet
*/
public class ServiceNavigationImpl extends AbstractAuthenticateService implements ServiceNavigation {
private static final long serialVersionUID = 1L;
private static boolean showLastPublicationsOnHomePage;
private static boolean showLastPublicationsOnSpaceHomePage;
private OrganizationController organizationController = OrganizationController.get();
static {
SettingBundle mobileSettings = ResourceLocator.getSettingBundle("org.silverpeas.mobile.mobileSettings");
showLastPublicationsOnHomePage = mobileSettings.getBoolean("homepage.lastpublications", true);
showLastPublicationsOnSpaceHomePage = mobileSettings.getBoolean("spacehomepage.lastpublications", true);
}
@Override
public HomePageDTO getHomePageData(String spaceId) throws NavigationException, AuthenticationException {
checkUserInSession();
HomePageDTO data = new HomePageDTO();
data.setId(spaceId);
try {
if (spaceId != null) {
SpaceInstLight space = Administration.get().getSpaceInstLightById(spaceId);
data.setSpaceName(space.getName(getUserInSession().getUserPreferences().getLanguage()));
}
} catch (Exception e) {
throw new NavigationException(e);
}
List<PublicationDetail> lastNews = NewsHelper.getInstance().getLastNews(getUserInSession().getId(), spaceId);
data.setNews(NewsHelper.getInstance().populate(lastNews, false));
if (spaceId == null || spaceId.isEmpty()) {
List<LinkDetail> links = FavoritesHelper.getInstance().getBookmarkPersoVisible(getUserInSession().getId());
data.setFavorites(FavoritesHelper.getInstance().populate(links));
}
data.setSpacesAndApps(getSpacesAndApps(spaceId));
// last publications
if ((spaceId == null && showLastPublicationsOnHomePage) || (spaceId != null && showLastPublicationsOnSpaceHomePage)) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM");
ArrayList<PublicationDTO> lastPubs = new ArrayList<PublicationDTO>();
SettingBundle settings = GraphicElementFactory.getLookSettings(GraphicElementFactory.defaultLookName);
int max;
if (spaceId == null) {
max = settings.getInteger("home.publications.nb", 3);
} else {
max = settings.getInteger("space.homepage.latestpublications.nb", 3);
}
List<PublicationDetail> pubs = getPublicationHelper().getPublications(spaceId, max);
for (PublicationDetail pub : pubs) {
PublicationDTO dto = new PublicationDTO();
dto.setId(pub.getId());
dto.setName(pub.getName());
dto.setUpdateDate(sdf.format(pub.getUpdateDate()));
dto.setInstanceId(pub.getInstanceId());
lastPubs.add(dto);
}
data.setLastPublications(lastPubs);
} catch (Exception e) {
e.printStackTrace();
}
}
return data;
}
private boolean isSupportedApp(String appType) {
return EnumUtils.isValidEnum(Apps.class, appType);
}
//TODO : remove appType
@Override
public List<SilverpeasObjectDTO> getSpacesAndApps(String rootSpaceId) throws NavigationException, AuthenticationException {
checkUserInSession();
ArrayList<SilverpeasObjectDTO> results = new ArrayList<SilverpeasObjectDTO>();
try {
if (rootSpaceId == null) {
String [] spaceIds = Administration.get().getAllSpaceIds(getUserInSession().getId());
for (String spaceId : spaceIds) {
SpaceInstLight space = Administration.get().getSpaceInstLightById(spaceId);
if (space.getFatherId().equals("0")) {
if (containApp(space)) {
results.add(populate(space));
}
}
}
Collections.sort(results);
} else {
String [] spaceIds = Administration.get().getAllowedSubSpaceIds(getUserInSession().getId(), rootSpaceId);
for (String spaceId : spaceIds) {
SpaceInstLight space = Administration.get().getSpaceInstLightById(spaceId);
if (("WA"+space.getFatherId()).equals(rootSpaceId)) {
if (containApp(space)) {
results.add(populate(space));
}
}
}
Collections.sort(results);
ArrayList<SilverpeasObjectDTO> partialResults = new ArrayList<SilverpeasObjectDTO>();
String [] appsIds = Administration.get().getAvailCompoIds(rootSpaceId, getUserInSession().getId());
for (String appId : appsIds) {
ComponentInstLight app = Administration.get().getComponentInstLight(appId);
if (isSupportedApp(app.getName()) && app.getDomainFatherId().equals(rootSpaceId)) {
partialResults.add(populate(app));
}
}
Collections.sort(partialResults);
results.addAll(partialResults);
}
} catch (Exception e) {
SilverLogger.getLogger(SpMobileLogModule.getName()).error("ServiceNavigationImpl.getSpacesAndApps", "root.EX_NO_MESSAGE", e);
}
return results;
}
@Override
public ApplicationInstanceDTO getApp(String instanceId, String contentId, String contentType) throws NavigationException, AuthenticationException {
if (instanceId == null) {
if (contentType.equals(ContentsTypes.Publication.name())) {
PublicationDetail pub = PublicationService.get().getDetail(new PublicationPK(contentId));
instanceId = pub.getInstanceId();
} else if(contentType.equals(ContentsTypes.Media.name())) {
Media media = MediaServiceProvider.getMediaService().getMedia(new MediaPK(contentId));
instanceId = media.getInstanceId();
}
}
return getApplicationInstanceDTO(instanceId);
}
private ApplicationInstanceDTO getApplicationInstanceDTO(final String instanceId) {
ApplicationInstanceDTO dto = null;
try {
ComponentInstLight app = Administration.get().getComponentInstLight(instanceId);
dto = populate(app);
} catch (Exception e) {
SilverLogger.getLogger(SpMobileLogModule.getName())
.error("ServiceNavigationImpl.getApp", "root.EX_NO_MESSAGE", e);
}
return dto;
}
private boolean containApp(SpaceInstLight space) throws Exception {
String [] appsIds = Administration.get().getAvailCompoIds(space.getId(), getUserInSession().getId());
for (String appId : appsIds) {
ComponentInstLight app = Administration.get().getComponentInstLight(appId);
if (isSupportedApp(app.getName())) {
return true;
}
}
return false;
}
private String[] getUserRoles(String componentId, String userId) {
return organizationController.getUserProfiles(userId, componentId);
}
private SpaceDTO populate(SpaceInstLight space) {
SpaceDTO dto = new SpaceDTO();
dto.setId(space.getId());
dto.setLabel(space.getName());
dto.setPersonal(space.isPersonalSpace());
dto.setOrderNum(space.getOrderNum());
return dto;
}
private ApplicationInstanceDTO populate(ComponentInstLight app) {
ApplicationInstanceDTO dto = new ApplicationInstanceDTO();
dto.setId(app.getId());
dto.setLabel(app.getLabel());
dto.setType(app.getName());
dto.setOrderNum(app.getOrderNum());
RightDTO rights = new RightDTO();
String[] roles = getUserRoles(app.getId(), getUserInSession().getId());
for (int i = 0; i < roles.length; i++) {
if (roles[i].equals("admin")) {
rights.setManager(true);
}
if (roles[i].equals("publisher")) {
rights.setPublisher(true);
}
if (roles[i].equals("writer")) {
rights.setWriter(true);
}
if (roles[i].equals("user")) {
rights.setReader(true);
}
}
dto.setRights(rights);
try {
String value = "";
try {
value = getMainSessionController().getComponentParameterValue(app.getId(), "notifications");
dto.setNotifiable(value.equals("yes"));
} catch(Exception e) {
dto.setNotifiable(false);
}
if (app.getName().equals("kmelia")) {
value = getMainSessionController().getComponentParameterValue(app.getId(), "tabComments");
} else if (app.getName().equals("gallery")) {
value = getMainSessionController().getComponentParameterValue(app.getId(), "comments");
}
if (app.getName().equals("kmelia")) {
dto.setCommentable(value.equals("yes"));
} else if (app.getName().equals("gallery")) {
dto.setCommentable(true);
dto.setNotifiable(true);
} else if (app.getName().equals("blog")) {
dto.setCommentable(true);
dto.setNotifiable(true);
}
} catch (Exception e) {
dto.setCommentable(false);
}
try {
String value = "";
if (app.getName().equals("kmelia")) {
value = getMainSessionController().getComponentParameterValue(app.getId(), "tabContent");
}
dto.setAbleToStoreContent(value.equals("yes"));
} catch (Exception e) {
dto.setAbleToStoreContent(false);
}
return dto;
}
private PublicationHelper getPublicationHelper() throws Exception {
SettingBundle settings = GraphicElementFactory.getLookSettings(GraphicElementFactory.defaultLookName);
String helperClassName = settings.getString("publicationHelper", "org.silverpeas.components.kmelia.KmeliaTransversal");
Class<?> helperClass = Class.forName(helperClassName);
PublicationHelper kmeliaTransversal = (PublicationHelper) helperClass.newInstance();
kmeliaTransversal.setMainSessionController(getMainSessionController());
return kmeliaTransversal;
}
}