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.ArrayList;
20  import java.util.HashMap;
21  
22  import javax.jcr.AccessDeniedException;
23  import javax.jcr.ItemNotFoundException;
24  import javax.jcr.Node;
25  import javax.jcr.query.Query;
26  import javax.portlet.PortletPreferences;
27  
28  import org.exoplatform.portal.webui.application.UIPortlet;
29  import org.exoplatform.portal.webui.util.Util;
30  import org.exoplatform.resolver.ResourceResolver;
31  import org.exoplatform.services.wcm.core.NodeLocation;
32  import org.exoplatform.services.wcm.publication.NodeLocationPaginatedResultIterator;
33  import org.exoplatform.services.wcm.publication.PaginatedResultIterator;
34  import org.exoplatform.services.wcm.publication.Result;
35  import org.exoplatform.services.wcm.publication.WCMComposer;
36  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
37  import org.exoplatform.wcm.webui.Utils;
38  import org.exoplatform.webui.application.WebuiRequestContext;
39  import org.exoplatform.webui.application.portlet.PortletRequestContext;
40  import org.exoplatform.webui.config.annotation.ComponentConfig;
41  import org.exoplatform.webui.config.annotation.EventConfig;
42  import org.exoplatform.webui.core.lifecycle.Lifecycle;
43  
44  /*
45   * Created by The eXo Platform SAS Author : Anh Do Ngoc anh.do@exoplatform.com
46   * Oct 15, 2008
47   */
48  
49  /**
50   * The Class UICLVFolderMode.
51   */
52  @ComponentConfig(
53                   lifecycle = Lifecycle.class,
54                   template = "system:/groovy/ContentListViewer/UICLVContainer.gtmpl",
55                   events = {
56                     @EventConfig(listeners = UICLVFolderMode.PreferencesActionListener.class)
57                   }
58      )
59  public class UICLVFolderMode extends UICLVContainer {
60  
61    private UICLVPresentation clvPresentation;
62  
63    /* (non-Javadoc)
64     * @see org.exoplatform.wcm.webui.clv.UICLVContainer#init()
65     */
66    public void init() throws Exception {
67      PortletPreferences portletPreferences = Utils.getAllPortletPreferences();
68  
69      Result result = null;
70      messageKey = null;
71      try {
72        result = getRenderedContentNodes();
73      } catch (ItemNotFoundException e) {
74        messageKey = "UICLVContainer.msg.item-not-found";
75        return;
76      } catch (AccessDeniedException e) {
77        messageKey = "UICLVContainer.msg.no-permission";
78        result = new Result(new ArrayList<Node>(), 0, 0, null, null);
79      }
80      if (result.getNumTotal() == 0) {
81        messageKey = "UICLVContainer.msg.non-contents";
82      }
83      int itemsPerPage = Integer.parseInt(portletPreferences.getValue(UICLVPortlet.PREFERENCE_ITEMS_PER_PAGE, null));
84      PaginatedResultIterator paginatedResultIterator = new NodeLocationPaginatedResultIterator(result, itemsPerPage); 
85      getChildren().clear();
86      PortletRequestContext pContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
87      clvPresentation =
88          addChild(UICLVPresentation.class,
89                   null,
90                   UICLVPresentation.class.getSimpleName() + "_" + pContext.getWindowId()
91              );
92      ResourceResolver resourceResolver = getTemplateResourceResolver();
93      clvPresentation.init(resourceResolver, paginatedResultIterator);
94    }
95  
96    /**
97     * Gets the rendered content nodes.
98     *
99     * @return the rendered content nodes
100    *
101    * @throws Exception the exception
102    */
103   public Result getRenderedContentNodes() throws Exception {
104     PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
105     PortletPreferences preferences = portletRequestContext.getRequest().getPreferences();
106     WCMComposer wcmComposer = getApplicationComponent(WCMComposer.class);
107     HashMap<String, String> filters = new HashMap<String, String>();
108     filters.put(WCMComposer.FILTER_MODE, Utils.getCurrentMode());
109     String orderBy = preferences.getValue(UICLVPortlet.PREFERENCE_ORDER_BY, null);
110     String orderType = preferences.getValue(UICLVPortlet.PREFERENCE_ORDER_TYPE, null);
111     String itemsPerPage = preferences.getValue(UICLVPortlet.PREFERENCE_ITEMS_PER_PAGE, null);
112     String sharedCache = preferences.getValue(UICLVPortlet.PREFERENCE_SHARED_CACHE, "true");
113     String contextualMode = preferences.getValue(UICLVPortlet.PREFERENCE_CONTEXTUAL_FOLDER, "true");
114     String workspace = preferences.getValue(UICLVPortlet.PREFERENCE_WORKSPACE, null);
115     String query = preferences.getValue(UICLVPortlet.PREFERENCE_CONTENTS_BY_QUERY, null);
116     if (orderType == null) orderType = "DESC";
117     if (orderBy == null) orderBy = "exo:title";
118     filters.put(WCMComposer.FILTER_ORDER_BY, orderBy);
119     filters.put(WCMComposer.FILTER_ORDER_TYPE, orderType);
120     StringBuffer filterLang = new StringBuffer(Util.getPortalRequestContext().getLocale().getLanguage());
121     String country = Util.getPortalRequestContext().getLocale().getCountry();
122     if (country != null && country.length() > 0) {
123       filterLang.append("_").append(country);
124     }
125     filters.put(WCMComposer.FILTER_LANGUAGE, filterLang.toString());
126     filters.put(WCMComposer.FILTER_LIMIT, itemsPerPage);
127     filters.put(WCMComposer.FILTER_VISIBILITY, ("true".equals(sharedCache))?
128                                                                             WCMComposer.VISIBILITY_PUBLIC:WCMComposer.VISIBILITY_USER);
129 
130 
131     if (this.getAncestorOfType(UICLVPortlet.class).isQueryApplication()) {
132       String folderPath = preferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null);
133       if (folderPath == null) {
134         return new Result(new ArrayList<Node>(), 0, 0, null, null);
135       }
136       folderPath = folderPath.replace("{siteName}", Util.getPortalRequestContext().getSiteName());
137       String strQuery = this.getAncestorOfType(UICLVPortlet.class).getQueryStatement(query);
138       if (strQuery != null) strQuery = strQuery.replaceAll("\"", "'");
139       if (UICLVPortlet.PREFERENCE_CONTEXTUAL_FOLDER_ENABLE.equals(contextualMode)
140               && org.exoplatform.wcm.webui.Utils.checkQuery(workspace, strQuery, Query.SQL)) {
141         String[] contentList = null;
142         if (preferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null) != null) {
143           contentList = preferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null).split(";");
144         }
145         if (contentList != null && contentList.length != 0) {
146           for (String itemPath : contentList) {
147             itemPath = itemPath.replace("{siteName}", Util.getPortalRequestContext().getSiteName());
148             Node currentNode = NodeLocation.getNodeByExpression(itemPath);
149             NodeLocation nodeLocation = new NodeLocation();
150             if (currentNode != null) {
151               String path = currentNode.getPath();
152               nodeLocation.setPath(path);
153               nodeLocation.setWorkspace(workspace);
154               nodeLocation.setSystemSession(false);
155             }
156             filters.put(WCMComposer.FILTER_QUERY_FULL, strQuery);
157             return wcmComposer.getPaginatedContents(nodeLocation,
158                     filters,
159                     WCMCoreUtils.getUserSessionProvider());
160           }
161         }
162       }
163     }
164     String folderPath = this.getAncestorOfType(UICLVPortlet.class).getFolderPath();
165     if (folderPath == null) {
166       folderPath = preferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null);
167     }
168     if (folderPath != null) { 
169       folderPath = folderPath.replace("{siteName}", Util.getPortalRequestContext().getSiteName()); 
170     }
171     if (folderPath == null) {
172       return new Result(new ArrayList<Node>(), 0, 0, null, null);
173     }
174     NodeLocation nodeLocation = NodeLocation.getNodeLocationByExpression(folderPath);
175     Node targetNode = NodeLocation.getNodeByLocation(nodeLocation);
176     //check if folder is empty, return empty result
177     if (targetNode == null || !targetNode.hasNodes()) {
178       return new Result(new ArrayList<Node>(), 0, 0, nodeLocation, filters);
179     } else {
180       return wcmComposer.getPaginatedContents(nodeLocation,
181                                               filters,
182                                               WCMCoreUtils.getUserSessionProvider());
183     }
184   }
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 UICLVFolderMode.class.getSimpleName();
205   }
206 }