View Javadoc
1   /*
2    * Copyright (C) 2003-2009 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.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  
24  import javax.jcr.Node;
25  import javax.jcr.query.Query;
26  import javax.portlet.PortletPreferences;
27  
28  import org.exoplatform.ecm.utils.text.Text;
29  import org.exoplatform.ecm.utils.comparator.PropertyValueComparator;
30  import org.exoplatform.portal.webui.application.UIPortlet;
31  import org.exoplatform.portal.webui.util.Util;
32  import org.exoplatform.resolver.ResourceResolver;
33  import org.exoplatform.services.cms.link.LinkManager;
34  import org.exoplatform.services.jcr.ext.common.SessionProvider;
35  import org.exoplatform.services.log.ExoLogger;
36  import org.exoplatform.services.log.Log;
37  import org.exoplatform.services.wcm.core.NodeLocation;
38  import org.exoplatform.services.wcm.core.NodetypeConstant;
39  import org.exoplatform.services.wcm.publication.PaginatedResultIterator;
40  import org.exoplatform.services.wcm.publication.Result;
41  import org.exoplatform.services.wcm.publication.WCMComposer;
42  import org.exoplatform.services.wcm.search.base.AbstractPageList;
43  import org.exoplatform.services.wcm.search.base.PageListFactory;
44  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
45  import org.exoplatform.wcm.webui.Utils;
46  import org.exoplatform.wcm.webui.scv.UISingleContentViewerPortlet;
47  import org.exoplatform.webui.application.WebuiRequestContext;
48  import org.exoplatform.webui.application.portlet.PortletRequestContext;
49  import org.exoplatform.webui.config.annotation.ComponentConfig;
50  import org.exoplatform.webui.config.annotation.EventConfig;
51  import org.exoplatform.webui.core.lifecycle.Lifecycle;
52  
53  /**
54   * Created by The eXo Platform SAS
55   * Author : anh.do
56   * anh.do@exoplatform.com, anhdn86@gmail.com
57   * Feb 23, 2009
58   */
59  @ComponentConfig(
60                   lifecycle = Lifecycle.class,
61                   template = "system:/groovy/ContentListViewer/UICLVContainer.gtmpl",
62                   events = {
63                     @EventConfig(listeners = UICLVManualMode.PreferencesActionListener.class)
64                   }
65      )
66  @SuppressWarnings("deprecation")
67  public class UICLVManualMode extends UICLVContainer {
68  
69    /** The log. */
70    private static final Log LOG = ExoLogger.getLogger(UICLVManualMode.class.getName());
71  
72    /* (non-Javadoc)
73     * @see org.exoplatform.wcm.webui.clv.UICLVContainer#init()
74     */
75    @SuppressWarnings("unchecked")
76    public void init() throws Exception {
77      PortletPreferences portletPreferences = Utils.getAllPortletPreferences();
78      String query = portletPreferences.getValue(UICLVPortlet.PREFERENCE_CONTENTS_BY_QUERY, "");
79      String contextualMode = portletPreferences.getValue(UICLVPortlet.PREFERENCE_CONTEXTUAL_FOLDER, null);
80      String workspace = portletPreferences.getValue(UICLVPortlet.PREFERENCE_WORKSPACE, null);
81      List<Node> nodes = new ArrayList<Node>();
82      String folderPath="";
83  
84      HashMap<String, String> filters = new HashMap<String, String>();
85      if (UICLVPortlet.PREFERENCE_CONTEXTUAL_FOLDER_ENABLE.equals(contextualMode)) {
86        String folderParamName = portletPreferences.getValue(UICLVPortlet.PREFERENCE_SHOW_CLV_BY, null);
87        if (folderParamName == null || folderParamName.length() == 0)
88          folderParamName = UICLVPortlet.DEFAULT_SHOW_CLV_BY;
89        folderPath = Util.getPortalRequestContext().getRequestParameter(folderParamName);
90      }
91      String sharedCache = portletPreferences.getValue(UISingleContentViewerPortlet.ENABLE_CACHE, "true");
92      sharedCache = "true".equals(sharedCache) ? WCMComposer.VISIBILITY_PUBLIC:WCMComposer.VISIBILITY_USER;
93      int itemsPerPage = Integer.parseInt(portletPreferences.getValue(UICLVPortlet.PREFERENCE_ITEMS_PER_PAGE, null));
94  
95      String strQuery = this.getAncestorOfType(UICLVPortlet.class).getQueryStatement(query);
96      if (strQuery != null) strQuery = strQuery.replaceAll("\"", "'");
97      String[] contentList = null;
98      if (portletPreferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null) != null) {
99        contentList = portletPreferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null).split(";");
100     }
101     if (this.getAncestorOfType(UICLVPortlet.class).isQueryApplication()
102         && UICLVPortlet.PREFERENCE_CONTEXTUAL_FOLDER_ENABLE.equals(contextualMode)
103         && org.exoplatform.wcm.webui.Utils.checkQuery(workspace, strQuery, Query.SQL)) {
104       if (contentList != null && contentList.length != 0) {
105         for (String itemPath : contentList) {
106           itemPath = itemPath.replace("{siteName}", Util.getPortalRequestContext().getSiteName());
107           Node currentNode = NodeLocation.getNodeByExpression(itemPath);
108           NodeLocation nodeLocation = new NodeLocation();
109           if (currentNode != null) {
110             String path = currentNode.getPath();
111             nodeLocation.setPath(path);
112             nodeLocation.setWorkspace(workspace);
113             nodeLocation.setSystemSession(false);
114           }
115 
116           filters.put(WCMComposer.FILTER_QUERY_FULL, strQuery);
117           Result rNodes = WCMCoreUtils.getService(WCMComposer.class)
118                   .getPaginatedContents(nodeLocation, filters, WCMCoreUtils.getUserSessionProvider());
119           if (rNodes.getNumTotal() == 0) messageKey = "UICLVContainer.msg.non-contents";
120           PaginatedResultIterator paginatedResultIterator = new PaginatedResultIterator(rNodes, itemsPerPage);
121           getChildren().clear();
122           ResourceResolver resourceResolver = getTemplateResourceResolver();
123           PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
124           UICLVPresentation clvPresentation =
125                   addChild(UICLVPresentation.class,
126                           null,
127                           UICLVPresentation.class.getSimpleName() + "_" + pContext.getWindowId()
128                   );
129           clvPresentation.init(resourceResolver, paginatedResultIterator);
130 
131           return;
132         }
133       }
134     } else {
135       LinkManager linkManager = WCMCoreUtils.getService(LinkManager.class);
136       //get node to sort
137       List<Node> originalList = new ArrayList<Node>();
138       if (contentList != null && contentList.length != 0) {
139         for (String itemPath : contentList) {
140           itemPath = itemPath.replace("{siteName}", Util.getPortalRequestContext().getSiteName());
141           Node currentNode = NodeLocation.getNodeByExpression(itemPath);
142           if(currentNode != null){
143             try {
144               linkManager.updateSymlink(currentNode);
145               currentNode = NodeLocation.getNodeByExpression(itemPath);
146             } catch (Exception e) {
147               if (LOG.isErrorEnabled()) {
148                 LOG.error("Can not update symlink: " + currentNode.getPath(), e);
149               }
150             }
151             originalList.add(currentNode);
152           }
153         }
154       }
155       //sort nodes
156       String orderBy = portletPreferences.getValue(UICLVPortlet.PREFERENCE_ORDER_BY, NodetypeConstant.EXO_TITLE);
157       String orderType = portletPreferences.getValue(UICLVPortlet.PREFERENCE_ORDER_TYPE, "ASC");
158 
159       Collections.sort(originalList, new PropertyValueComparator(orderBy, "ASC".equals(orderType) ? "Ascending" : "Descending"));
160       //get real node by portlet mode
161       for (Node node : originalList) {
162         Node viewNode = Utils.getViewableNodeByComposer(WCMCoreUtils.getRepository().getConfiguration().getName(),
163                                                         Text.escapeIllegalJcrChars(node.getSession().getWorkspace().getName()), 
164                                                         Text.escapeIllegalJcrChars(node.getPath()),
165                                                         null,
166                                                         sharedCache);
167         if (viewNode != null) nodes.add(viewNode);
168       }
169     }
170     if (nodes.size() == 0) {
171       messageKey = "UICLVContainer.msg.non-contents";
172     }
173     getChildren().clear();
174     AbstractPageList<NodeLocation> pageList = 
175         PageListFactory.createPageList(nodes, itemsPerPage, null, 
176                                        new CLVNodeCreator());
177     ResourceResolver resourceResolver = getTemplateResourceResolver();
178     PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
179     UICLVPresentation clvPresentation =
180         addChild(UICLVPresentation.class,
181                  null,
182                  UICLVPresentation.class.getSimpleName() + "_" + pContext.getWindowId()
183             );
184     clvPresentation.init(resourceResolver, pageList);
185   }
186   /**
187    * Gets the bar info show.
188    *
189    * @return the value for info bar setting
190    *
191    * @throws Exception the exception
192    */
193   public boolean isShowInfoBar() throws Exception {
194     if (UIPortlet.getCurrentUIPortlet().getShowInfoBar())
195       return true;
196     return false;
197   }
198   /**
199    * Get portlet name.
200    *
201    * @throws Exception the exception
202    */
203   public String getPortletName() throws Exception {
204     return UICLVManualMode.class.getSimpleName();
205   }
206 }