1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.wcm.webui.clv;
18
19 import java.util.Date;
20 import java.util.HashMap;
21 import java.util.HashSet;
22
23 import javax.jcr.ItemNotFoundException;
24 import javax.jcr.NoSuchWorkspaceException;
25 import javax.jcr.Node;
26 import javax.jcr.PathNotFoundException;
27 import javax.jcr.RepositoryException;
28 import javax.portlet.MimeResponse;
29 import javax.portlet.PortletMode;
30 import javax.portlet.PortletPreferences;
31
32 import org.exoplatform.portal.application.PortalRequestContext;
33 import org.exoplatform.portal.webui.util.Util;
34 import org.exoplatform.services.wcm.core.NodeLocation;
35 import org.exoplatform.services.wcm.core.WCMService;
36 import org.exoplatform.services.wcm.publication.WCMComposer;
37 import org.exoplatform.wcm.webui.Utils;
38 import org.exoplatform.webui.application.WebuiApplication;
39 import org.exoplatform.webui.application.WebuiRequestContext;
40 import org.exoplatform.webui.application.portlet.PortletRequestContext;
41 import org.exoplatform.webui.config.annotation.ComponentConfig;
42 import org.exoplatform.webui.core.UIPopupContainer;
43 import org.exoplatform.webui.core.UIPortletApplication;
44 import org.exoplatform.webui.core.lifecycle.UIApplicationLifecycle;
45 import org.gatein.portal.controller.resource.ResourceScope;
46
47
48
49
50
51
52
53
54 @ComponentConfig(lifecycle = UIApplicationLifecycle.class, template = "system:/groovy/ContentListViewer/UICLVPortlet.gtmpl")
55 public class UICLVPortlet extends UIPortletApplication {
56
57
58 public static final String PREFERENCE_DISPLAY_MODE = "mode";
59
60
61 public final static String PREFERENCE_ITEM_PATH = "folderPath";
62
63
64 public final static String PREFERENCE_ITEM_DRIVE = "nodeDrive";
65
66
67 public static final String PREFERENCE_ORDER_BY = "orderBy";
68
69
70 public static final String ORDER_BY_TITLE = "OrderByTitle";
71
72
73 public static final String ORDER_BY_DATE_CREATED = "OrderByDateCreated";
74
75
76 public static final String ORDER_BY_DATE_MODIFIED = "OrderByDateModified";
77
78
79 public static final String ORDER_BY_DATE_PUBLISHED = "OrderByDatePublished";
80
81
82 public static final String ORDER_BY_DATE_START_EVENT = "OrderByDateStartEvent";
83
84
85 public static final String ORDER_BY_INDEX = "OrderByIndex";
86
87
88 public static final String PREFERENCE_ORDER_TYPE = "orderType";
89
90
91 public static final String ORDER_TYPE_DESCENDENT = "OrderDesc";
92
93
94 public static final String ORDER_TYPE_ASCENDENT = "OrderAsc";
95
96
97 public final static String PREFERENCE_HEADER = "header";
98
99
100 public final static String PREFERENCE_AUTOMATIC_DETECTION = "automaticDetection";
101
102
103 public final static String PREFERENCE_DISPLAY_TEMPLATE = "formViewTemplatePath";
104
105
106 public final static String PREFERENCE_PAGINATOR_TEMPLATE = "paginatorTemplatePath";
107
108
109 public final static String PREFERENCE_ITEMS_PER_PAGE = "itemsPerPage";
110
111
112 public final static String PREFERENCE_SHOW_TITLE = "showTitle";
113
114
115 public final static String PREFERENCE_SHOW_HEADER = "showHeader";
116
117
118 public final static String PREFERENCE_SHOW_REFRESH_BUTTON = "showRefreshButton";
119
120
121
122 public final static String PREFERENCE_SHOW_ILLUSTRATION = "showThumbnailsView";
123
124
125 public final static String PREFERENCE_SHOW_DATE_CREATED = "showDateCreated";
126
127
128 public final static String PREFERENCE_SHOW_READMORE = "showReadmore";
129
130
131 public final static String PREFERENCE_SHOW_SUMMARY = "showSummary";
132
133
134 public final static String PREFERENCE_SHOW_LINK = "showLink";
135
136
137 public final static String PREFERENCE_SHOW_RSSLINK = "showRssLink";
138
139
140 public final static String PREFERENCE_CONTEXTUAL_FOLDER = "contextualFolder";
141
142
143 public final static String PREFERENCE_CONTEXTUAL_FOLDER_ENABLE = "contextualEnable";
144
145
146 public final static String PREFERENCE_CONTEXTUAL_FOLDER_DISABLE = "contextualDisable";
147
148
149 public final static String PREFERENCE_TARGET_PAGE = "basePath";
150
151
152 public final static String PREFERENCE_SHOW_SCV_WITH = "showScvWith";
153
154
155 public final static String PREFERENCE_SHOW_CLV_BY = "showClvBy";
156
157
158 public final static String PREFERENCE_CACHE_ENABLED = "sharedCache";
159
160
161 public final static String PREFERENCE_CONTENTS_BY_QUERY = "query";
162
163
164 public final static String PREFERENCE_WORKSPACE = "workspace";
165
166
167 public static final String DISPLAY_MODE_MANUAL = "ManualViewerMode";
168
169
170 public static final String DISPLAY_MODE_AUTOMATIC = "AutoViewerMode";
171
172 public static final String DEFAULT_SHOW_CLV_BY = "folder-id";
173
174 public static final String DEFAULT_SHOW_SCV_WITH = "content-id";
175
176 public static final String PREFERENCE_APPLICATION_TYPE = "application";
177
178 public static final String APPLICATION_CLV_BY_QUERY = "ContentsByQuery";
179
180 public static final String PREFERENCE_SHARED_CACHE = "sharedCache";
181
182 public static final String QUERY_USER_PARAMETER = "user";
183 public static final String QUERY_LANGUAGE_PARAMETER = "lang";
184
185 private PortletMode cpMode;
186
187 private UICLVFolderMode folderMode;
188
189 private UICLVManualMode manualMode;
190
191 private UICLVConfig clvConfig;
192
193 private String currentFolderPath;
194
195 private String header;
196
197 private String currentDisplayMode;
198
199 private String currentApplicationMode;
200
201
202
203
204
205
206 public UICLVPortlet() throws Exception {
207 addChild(UIPopupContainer.class, null, "UIPopupContainer-" + new Date().getTime());
208 currentFolderPath = getFolderPath();
209 }
210
211 public String getHeader() {
212 return header;
213 }
214
215 public void setCurrentFolderPath(String value) {
216 currentFolderPath = value;
217 }
218
219 public String getFolderPath() {
220 PortalRequestContext preq = Util.getPortalRequestContext();
221 currentFolderPath = "";
222 if (!preq.useAjax()) {
223 currentFolderPath = getFolderPathParamValue();
224 }
225 try {
226 if (currentFolderPath != null && currentFolderPath.length() > 0) {
227 Node folderNode = null;
228 NodeLocation folderLocation = NodeLocation.getNodeLocationByExpression(currentFolderPath);
229 folderNode = NodeLocation.getNodeByLocation(folderLocation);
230 if (folderNode == null) {
231 header = null;
232 } else {
233 if (folderNode.hasProperty(org.exoplatform.ecm.webui.utils.Utils.EXO_TITLE))
234 header = folderNode.getProperty(org.exoplatform.ecm.webui.utils.Utils.EXO_TITLE).getString();
235 else header = folderNode.getName();
236 }
237 } else header = null;
238 } catch(IllegalArgumentException ex) {
239 header = null;
240 } catch(ItemNotFoundException ex) {
241 header = null;
242 } catch(PathNotFoundException ex) {
243 header = null;
244 } catch(NoSuchWorkspaceException ex) {
245 header = null;
246 } catch(RepositoryException ex) {
247 header = null;
248 }
249 PortletPreferences preferences = Utils.getAllPortletPreferences();
250 currentDisplayMode = preferences.getValue(PREFERENCE_DISPLAY_MODE, null);
251 currentApplicationMode = preferences.getValue(PREFERENCE_APPLICATION_TYPE, null);
252 if (DISPLAY_MODE_AUTOMATIC.equals(currentDisplayMode)) {
253 if (currentFolderPath == null || currentFolderPath.length() == 0) {
254 currentFolderPath = Utils.getPortletPreference(UICLVPortlet.PREFERENCE_ITEM_PATH);
255 }
256 }
257 return currentFolderPath;
258 }
259
260 public String getFolderPathParamValue() {
261 PortletPreferences preferences = Utils.getAllPortletPreferences();
262 String contextualMode = preferences.getValue(PREFERENCE_CONTEXTUAL_FOLDER, null);
263 Node folderNode = null;
264 String folderPath = null;
265 if (PREFERENCE_CONTEXTUAL_FOLDER_ENABLE.equals(contextualMode)) {
266 String folderParamName = preferences.getValue(PREFERENCE_SHOW_CLV_BY, null);
267 if (folderParamName == null || folderParamName.length() == 0)
268 folderParamName = DEFAULT_SHOW_CLV_BY;
269 folderPath = Util.getPortalRequestContext().getRequestParameter(folderParamName);
270 try {
271 NodeLocation folderLocation = NodeLocation.getNodeLocationByExpression(folderPath);
272 folderNode = NodeLocation.getNodeByLocation(folderLocation);
273 if (folderNode == null)
274 return null;
275 } catch (Exception e) {
276 folderNode = null;
277 folderPath = null;
278 }
279 }
280 return folderPath;
281 }
282
283
284
285
286
287
288 public HashMap<String, String> getQueryParammeter(HashSet<String> params) {
289
290 HashMap<String, String> paramMap = new HashMap<String, String>();
291 PortalRequestContext context = Util.getPortalRequestContext();
292 for (String param : params) {
293 String value = context.getRequestParameter(param);
294 if (value != null) {
295 paramMap.put(param, value);
296 } else {
297 paramMap.put(param, "");
298 }
299 }
300 paramMap.put(UICLVPortlet.QUERY_USER_PARAMETER, context.getRemoteUser());
301 paramMap.put(UICLVPortlet.QUERY_LANGUAGE_PARAMETER, context.getLocale().getLanguage());
302
303 return paramMap;
304 }
305
306
307
308
309
310
311
312
313 public void processRender(WebuiApplication app, WebuiRequestContext context) throws Exception {
314 PortletRequestContext pContext = (PortletRequestContext) context;
315 PortletPreferences preferences = pContext.getRequest().getPreferences();
316
317 Boolean sharedCache = "true".equals(preferences.getValue(PREFERENCE_SHARED_CACHE, "true"));
318
319 if (context.getRemoteUser() == null
320 || (Utils.isLiveMode() && sharedCache && !Utils.isPortalEditMode() && Utils.isPortletViewMode(pContext))) {
321 WCMService wcmService = getApplicationComponent(WCMService.class);
322 pContext.getResponse().setProperty(MimeResponse.EXPIRATION_CACHE,
323 "" + wcmService.getPortletExpirationCache());
324 if (log.isTraceEnabled())
325 log.trace("CLV rendering : cache set to " + wcmService.getPortletExpirationCache());
326 }
327 String nDisplayMode = preferences.getValue(PREFERENCE_DISPLAY_MODE, null);
328 PortletMode npMode = pContext.getApplicationMode();
329 if (!nDisplayMode.equals(currentDisplayMode)) {
330 activateMode(npMode, nDisplayMode);
331 } else {
332 if (!npMode.equals(cpMode)) {
333 activateMode(npMode, nDisplayMode);
334 }
335 }
336 setId(UICLVPortlet.class.getSimpleName() + "_" + pContext.getWindowId());
337
338 if (context.getRemoteUser() != null && WCMComposer.MODE_EDIT.equals(Utils.getCurrentMode())) {
339 pContext.getJavascriptManager().loadScriptResource(ResourceScope.SHARED, "content-selector");
340 pContext.getJavascriptManager().loadScriptResource(ResourceScope.SHARED, "quick-edit");
341 }
342 super.processRender(app, context);
343 }
344
345
346
347
348
349
350
351 private void activateMode(PortletMode npMode, String nDisplayMode) throws Exception {
352 PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
353 if (npMode.equals(cpMode)) {
354
355
356 removeChildren();
357 if (Utils.isPortalEditMode()){
358 clvConfig = addChild(UICLVConfig.class, null, null);
359 clvConfig.setModeInternal(false);
360 }else {
361 if (nDisplayMode.equals(DISPLAY_MODE_AUTOMATIC)) {
362 folderMode = addChild(UICLVFolderMode.class, null, UICLVFolderMode.class.getSimpleName() +
363 "_" + pContext.getWindowId());
364 folderMode.init();
365 folderMode.setRendered(true);
366 } else {
367 manualMode = addChild(UICLVManualMode.class, null, UICLVManualMode.class.getSimpleName() +
368 "_" + pContext.getWindowId());
369 manualMode.init();
370 manualMode.setRendered(true);
371 }
372 }
373 } else {
374 if (npMode.equals(PortletMode.VIEW)) {
375 removeChildren();
376 if (nDisplayMode.equals(DISPLAY_MODE_AUTOMATIC)) {
377 folderMode = addChild(UICLVFolderMode.class, null, UICLVFolderMode.class.getSimpleName() +
378 "_" + pContext.getWindowId());
379 folderMode.init();
380 folderMode.setRendered(true);
381 } else {
382 manualMode = addChild(UICLVManualMode.class, null, UICLVManualMode.class.getSimpleName() +
383 "_" + pContext.getWindowId());
384 manualMode.init();
385 manualMode.setRendered(true);
386 }
387 } else {
388
389 removeChildren();
390 clvConfig = addChild(UICLVConfig.class, null, null);
391 clvConfig.setModeInternal(true);
392 }
393 }
394 cpMode = npMode;
395 currentDisplayMode = nDisplayMode;
396 }
397 private void removeChildren() {
398 clvConfig = getChild(UICLVConfig.class);
399 if (clvConfig != null)
400 removeChild(UICLVConfig.class);
401 folderMode = getChild(UICLVFolderMode.class);
402 if (folderMode != null)
403 removeChild(UICLVFolderMode.class);
404 manualMode = getChild(UICLVManualMode.class);
405 if (manualMode != null)
406 removeChild(UICLVManualMode.class);
407 }
408
409
410
411
412
413 public void changeToViewMode() throws Exception {
414 PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
415 portletRequestContext.setApplicationMode(PortletMode.VIEW);
416 updatePortlet();
417 }
418
419
420
421
422
423 public void updatePortlet() throws Exception {
424 PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
425 PortletMode npMode = portletRequestContext.getApplicationMode();
426 PortletPreferences preferences = Utils.getAllPortletPreferences();
427 String nDisplayMode = preferences.getValue(PREFERENCE_DISPLAY_MODE, null);
428 activateMode(npMode, nDisplayMode);
429 }
430
431
432
433
434
435
436 public String getQueryStatement(String sqlQuery) {
437 HashSet<String> params = Utils.getQueryParams(sqlQuery);
438 HashMap<String, String> queryParam = getQueryParammeter(params);
439
440 return Utils.buildQuery(sqlQuery, queryParam);
441 }
442
443
444
445
446
447 public boolean isQueryApplication() {
448 return APPLICATION_CLV_BY_QUERY.equals(currentApplicationMode);
449 }
450 }