View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
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   * Created by The eXo Platform SAS Author : Anh Do Ngoc anh.do@exoplatform.com
49   * Oct 15, 2008
50   */
51  /**
52   * The Class UICLVPortlet.
53   */
54  @ComponentConfig(lifecycle = UIApplicationLifecycle.class, template = "system:/groovy/ContentListViewer/UICLVPortlet.gtmpl")
55  public class UICLVPortlet extends UIPortletApplication {
56  
57    /** The Constant PREFERENCE_DISPLAY_MODE. */
58    public static final String PREFERENCE_DISPLAY_MODE              = "mode";
59  
60    /** The Constant PREFERENCE_ITEM_PATH. */
61    public final static String PREFERENCE_ITEM_PATH                 = "folderPath";
62  
63    /** The Constant PREFERENCE_ITEM_PATH. */
64    public final static String PREFERENCE_ITEM_DRIVE                = "nodeDrive";
65  
66    /** The Constant PREFERENCE_ORDER_BY. */
67    public static final String PREFERENCE_ORDER_BY                  = "orderBy";
68  
69    /** The Constant ORDER_BY_TITLE. */
70    public static final String ORDER_BY_TITLE                       = "OrderByTitle";
71  
72    /** The Constant ORDER_BY_DATE_CREATED. */
73    public static final String ORDER_BY_DATE_CREATED                = "OrderByDateCreated";
74  
75    /** The Constant ORDER_BY_DATE_MODIFIED. */
76    public static final String ORDER_BY_DATE_MODIFIED               = "OrderByDateModified";
77  
78    /** The Constant ORDER_BY_DATE_PUBLISHED. */
79    public static final String ORDER_BY_DATE_PUBLISHED              = "OrderByDatePublished";
80  
81    /** The Constant ORDER_BY_DATE_START_EVENT. */
82    public static final String ORDER_BY_DATE_START_EVENT            = "OrderByDateStartEvent";
83  
84    /** The Constant ORDER_BY_INDEX. */
85    public static final String ORDER_BY_INDEX                       = "OrderByIndex";
86  
87    /** The Constant PREFERENCE_ORDER_TYPE. */
88    public static final String PREFERENCE_ORDER_TYPE                = "orderType";
89  
90    /** The Constant ORDER_TYPE_DESCENDENT. */
91    public static final String ORDER_TYPE_DESCENDENT                = "OrderDesc";
92  
93    /** The Constant ORDER_TYPE_ASCENDENT. */
94    public static final String ORDER_TYPE_ASCENDENT                 = "OrderAsc";
95  
96    /** The Constant PREFERENCE_HEADER. */
97    public final static String PREFERENCE_HEADER                    = "header";
98  
99    /** The Constant PREFERENCE_AUTOMATIC_DETECTION. */
100   public final static String PREFERENCE_AUTOMATIC_DETECTION       = "automaticDetection";
101 
102   /** The Constant PREFERENCE_DISPLAY_TEMPLATE. */
103   public final static String PREFERENCE_DISPLAY_TEMPLATE          = "formViewTemplatePath";
104 
105   /** The Constant PREFERENCE_PAGINATOR_TEMPLATE. */
106   public final static String PREFERENCE_PAGINATOR_TEMPLATE        = "paginatorTemplatePath";
107 
108   /** The Constant PREFERENCE_ITEMS_PER_PAGE. */
109   public final static String PREFERENCE_ITEMS_PER_PAGE            = "itemsPerPage";
110 
111   /** The Constant PREFERENCE_SHOW_TITLE. */
112   public final static String PREFERENCE_SHOW_TITLE                = "showTitle";
113 
114   /** The Constant PREFERENCE_SHOW_HEADER. */
115   public final static String PREFERENCE_SHOW_HEADER               = "showHeader";
116 
117   /** The Constant PREFERENCE_SHOW_REFRESH_BUTTON. */
118   public final static String PREFERENCE_SHOW_REFRESH_BUTTON       = "showRefreshButton";
119 
120   /** The Constant PREFERENCE_SHOW_ILLUSTRATION. */
121   /** The Constant PREFERENCE_SHOW_IMAGE. */
122   public final static String PREFERENCE_SHOW_ILLUSTRATION         = "showThumbnailsView";
123 
124   /** The Constant PREFERENCE_SHOW_DATE_CREATED. */
125   public final static String PREFERENCE_SHOW_DATE_CREATED         = "showDateCreated";
126 
127   /** The Constant PREFERENCE_SHOW_MORE_LINK. */
128   public final static String PREFERENCE_SHOW_READMORE             = "showReadmore";
129 
130   /** The Constant PREFERNECE_SHOW_SUMMARY. */
131   public final static String PREFERENCE_SHOW_SUMMARY              = "showSummary";
132 
133   /** The Constant PREFERENCE_SHOW_LINK. */
134   public final static String PREFERENCE_SHOW_LINK                 = "showLink";
135 
136   /** The Constant PREFERENCE_SHOW_RSSLINK. */
137   public final static String PREFERENCE_SHOW_RSSLINK              = "showRssLink";
138 
139   /** The Constant PREFERENCE_CONTEXTUAL_FOLDER. */
140   public final static String PREFERENCE_CONTEXTUAL_FOLDER         = "contextualFolder";
141 
142   /** The Constant PREFERENCE_CONTEXTUAL_FOLDER_ENABLE. */
143   public final static String PREFERENCE_CONTEXTUAL_FOLDER_ENABLE  = "contextualEnable";
144 
145   /** The Constant PREFERENCE_CONTEXTUAL_FOLDER_DISABLE. */
146   public final static String PREFERENCE_CONTEXTUAL_FOLDER_DISABLE = "contextualDisable";
147 
148   /** The Constant PREFERENCE_TARGET_PAGE. */
149   public final static String PREFERENCE_TARGET_PAGE               = "basePath";
150 
151   /** The Constant PREFERENCE_SHOW_SCL_WITH. */
152   public final static String PREFERENCE_SHOW_SCV_WITH             = "showScvWith";
153 
154   /** The Constant PREFERENCE_SHOW_CLV_BY. */
155   public final static String PREFERENCE_SHOW_CLV_BY               = "showClvBy";
156 
157   /** The Constant PREFERENCE_CACHE_ENABLED. */
158   public final static String PREFERENCE_CACHE_ENABLED             = "sharedCache";
159 
160   /** The Constant CONTENT_BY_QUERY. */
161   public final static String PREFERENCE_CONTENTS_BY_QUERY         = "query";
162 
163   /** The Constant PREFERENCE_WORKSPACE. */
164   public final static String PREFERENCE_WORKSPACE                 = "workspace";
165 
166   /** The Constant DISPLAY_MODE_MANUAL. */
167   public static final String DISPLAY_MODE_MANUAL                  = "ManualViewerMode";
168 
169   /** The Constant DISPLAY_MODE_AUTOMATIC. */
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   /* Dynamic parameter for CLV by query */
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    * Instantiates a new uICLV portlet.
203    *
204    * @throws Exception the exception
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    * @param params
286    * @return
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    * (non-Javadoc)
308    * @see
309    * org.exoplatform.webui.core.UIPortletApplication#processRender(org.exoplatform
310    * .webui.application.WebuiApplication,
311    * org.exoplatform.webui.application.WebuiRequestContext)
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    * Decide which element will be displayed for correspond PortletMode/DisplayMode
347    * @param npMode: View/Edit
348    * @param nDisplayMode : FolderMode/ManualMode
349    * @throws Exception : Exception will be throws if child addition action fails
350    */
351   private void activateMode(PortletMode npMode, String nDisplayMode) throws Exception {
352     PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
353     if (npMode.equals(cpMode)) {
354       // Switch manual/auto
355       // Not reach in the case of queryMode.
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)) { //Change from edit to iew
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         // Change from view to edit
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    * Force porlet to change to ViewMode
410    *
411    * @throws Exception
412    */
413   public void changeToViewMode() throws Exception {
414     PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
415     portletRequestContext.setApplicationMode(PortletMode.VIEW);
416     updatePortlet();
417   }
418 
419   /**
420    * Update the current portlet if config/data changed.
421    * @throws Exception
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    * @param sqlQuery
434    * @return
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    * @return
446    */
447   public boolean isQueryApplication() {
448     return APPLICATION_CLV_BY_QUERY.equals(currentApplicationMode);
449   }
450 }