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.category;
18  
19  import java.io.UnsupportedEncodingException;
20  import java.net.URLDecoder;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.jcr.Node;
25  import javax.jcr.NodeIterator;
26  import javax.portlet.PortletPreferences;
27  import javax.servlet.http.HttpServletRequestWrapper;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.exoplatform.ecm.resolver.JCRResourceResolver;
31  import org.exoplatform.ecm.webui.utils.Utils;
32  import org.exoplatform.portal.mop.SiteType;
33  import org.exoplatform.portal.webui.util.Util;
34  import org.exoplatform.resolver.ResourceResolver;
35  import org.exoplatform.services.cms.impl.DMSConfiguration;
36  import org.exoplatform.services.cms.taxonomy.TaxonomyService;
37  import org.exoplatform.services.log.ExoLogger;
38  import org.exoplatform.services.log.Log;
39  import org.exoplatform.services.wcm.friendly.FriendlyService;
40  import org.exoplatform.services.wcm.portal.LivePortalManagerService;
41  import org.exoplatform.web.url.navigation.NavigationResource;
42  import org.exoplatform.web.url.navigation.NodeURL;
43  import org.exoplatform.webui.application.WebuiRequestContext;
44  import org.exoplatform.webui.application.portlet.PortletRequestContext;
45  import org.exoplatform.webui.config.annotation.ComponentConfig;
46  import org.exoplatform.webui.config.annotation.EventConfig;
47  import org.exoplatform.webui.core.UIRightClickPopupMenu;
48  import org.exoplatform.webui.core.UITree;
49  
50  /**
51   * Created by The eXo Platform SAS
52   * Author : eXoPlatform
53   * chuong.phan@exoplatform.com, phan.le.thanh.chuong@gmail.com
54   * Comment: Change objId from node's path to category's path
55   * Jun 30, 2009
56   */
57  @ComponentConfig(
58      events = @EventConfig(listeners = UITree.ChangeNodeActionListener.class)
59  )
60  public class UICategoryNavigationTreeBase extends UITree {
61  
62    private static final Log         LOG            = ExoLogger.getLogger(UICategoryNavigationTreeBase.class.getName());
63    
64    /* (non-Javadoc)
65     * @see org.exoplatform.webui.core.UITree#renderNode(java.lang.Object)
66     */
67    public String renderNode(Object obj) throws Exception {
68      Node node = (Node) obj;
69      String nodeTypeIcon = Utils.getNodeTypeIcon(node,"16x16Icon");
70      String nodeIcon = this.getExpandIcon();
71      String iconGroup = this.getIcon();
72      String note = "" ;
73      if(isSelected(obj)) {
74        nodeIcon = getColapseIcon();
75        iconGroup = getSelectedIcon();
76        note = " NodeSelected" ;
77      }
78      String beanIconField = getBeanIconField();
79      if(beanIconField != null && beanIconField.length() > 0) {
80        if(getFieldValue(obj, beanIconField) != null)
81          iconGroup = (String)getFieldValue(obj, beanIconField);
82      }
83      renderCategoryLink(node);
84      String objId = String.valueOf(getId(obj));
85      StringBuilder builder = new StringBuilder();
86      if (nodeIcon.equals(getExpandIcon())) {
87        builder.append(" <a class=\"")
88               .append(nodeIcon)
89               .append(" ")
90               .append(nodeTypeIcon)
91               .append("\" href=\"")
92               .append(objId)
93               .append("\">");
94      } else {
95        builder.append(" <a class=\"")
96               .append(nodeIcon)
97               .append(" ")
98               .append(nodeTypeIcon)
99               .append("\" onclick=\"eXo.portal.UIPortalControl.collapseTree(this)")
100              .append("\">");
101     }
102     UIRightClickPopupMenu popupMenu = getUiPopupMenu();
103     String beanLabelField = getBeanLabelField();
104     String className="NodeIcon";
105     boolean flgSymlink = false;
106     if (Utils.isSymLink(node)) {
107       flgSymlink = true;
108       className = "NodeIconLink";
109     }
110     if (popupMenu == null) {
111       builder.append(" <div class=\"")
112              .append(className)
113              .append(" ")
114              .append(iconGroup)
115              .append(" ")
116              .append(nodeTypeIcon)
117              .append(note)
118              .append("\"")
119              .append(" title=\"")
120              .append(getFieldValue(obj, beanLabelField))
121              .append("\"")
122              .append(">");
123       if (flgSymlink) {
124         builder.append("  <div class=\"LinkSmall\">")
125                .append(getFieldValue(obj, beanLabelField))
126                .append("</div>");
127       } else {
128         builder.append(getFieldValue(obj, beanLabelField));
129       }
130       builder.append("</div>");
131     } else {
132       builder.append(" <div class=\"")
133              .append(className)
134              .append(" ")
135              .append(iconGroup)
136              .append(" ")
137              .append(nodeTypeIcon)
138              .append(note)
139              .append("\" ")
140              .append(popupMenu.getJSOnclickShowPopup(objId, null))
141              .append(" title=\"")
142              .append(getFieldValue(obj, beanLabelField))
143              .append("\"")
144              .append(">");
145       if (flgSymlink) {
146         builder.append("  <div class=\"LinkSmall\">")
147                .append(getFieldValue(obj, beanLabelField))
148                .append("</div>");
149       } else {
150         builder.append(getFieldValue(obj, beanLabelField));
151       }
152       builder.append("</div>");
153     }
154     builder.append(" </a>");
155     return builder.toString();
156   }
157 
158   /* (non-Javadoc)
159    * @see org.exoplatform.webui.core.UIComponent#getTemplate()
160    */
161   public String getTemplate() {
162     return UICategoryNavigationUtils.getPortletPreferences()
163                                     .getValue(UICategoryNavigationConstant.PREFERENCE_TEMPLATE_PATH,
164                                               null);
165   }
166 
167   /*
168    * (non-Javadoc)
169    * @see
170    * org.exoplatform.webui.core.UIComponent#getTemplateResourceResolver(org.
171    * exoplatform.webui.application.WebuiRequestContext, java.lang.String)
172    */
173   public ResourceResolver getTemplateResourceResolver(WebuiRequestContext context, String template) {
174     DMSConfiguration dmsConfiguration = getApplicationComponent(DMSConfiguration.class);
175     String workspace = dmsConfiguration.getConfig().getSystemWorkspace();
176     return new JCRResourceResolver(workspace);
177   }
178 
179   /* (non-Javadoc)
180    * @see org.exoplatform.webui.core.UITree#getActionLink()
181    */
182   public String getActionLink() throws Exception {
183     PortletRequestContext porletRequestContext = (PortletRequestContext) WebuiRequestContext.getCurrentInstance();
184     HttpServletRequestWrapper requestWrapper = (HttpServletRequestWrapper) porletRequestContext.getRequest();
185     String requestURI = requestWrapper.getRequestURI();
186     PortletPreferences portletPreferences = UICategoryNavigationUtils.getPortletPreferences();
187     String preferenceTreeName = portletPreferences.getValue(UICategoryNavigationConstant.PREFERENCE_TREE_NAME, "");
188     String preferenceTargetPage = portletPreferences.getValue(UICategoryNavigationConstant.PREFERENCE_TARGET_PAGE, "");
189     String backPath = requestURI.substring(0, requestURI.lastIndexOf("/"));
190     if (backPath.endsWith(preferenceTargetPage)
191         || requestURI.endsWith(Util.getUIPortal().getSelectedUserNode().getURI()))
192       backPath = "javascript:void(0)";
193     else if (backPath.endsWith(preferenceTreeName))
194       backPath = backPath.substring(0, backPath.lastIndexOf("/"));
195     return backPath;
196   }
197 
198   /* (non-Javadoc)
199    * @see org.exoplatform.webui.core.UITree#isSelected(java.lang.Object)
200    */
201   public boolean isSelected(Object obj) throws Exception {
202     Node selectedNode = this.getSelected();
203     Node node = (Node) obj;
204     if(selectedNode == null) return false;
205     return selectedNode.getPath().equals(node.getPath());
206   }
207 
208   public boolean isMovedTreeToTrash(String rootCategory) throws Exception {
209     Node categoryNode = getCategoryNode(rootCategory);
210     if (Utils.isInTrash(categoryNode))
211       return true;
212     return false;
213   }
214 
215   /**
216    * Gets the subcategories.
217    *
218    * @param categoryPath the category path
219    *
220    * @return the subcategories
221    *
222    * @throws Exception the exception
223    */
224   public List<Node> getSubcategories(String categoryPath) throws Exception {
225     Node categoryNode = getCategoryNode(categoryPath);
226     NodeIterator nodeIterator = categoryNode.getNodes();
227     List<Node> subcategories = new ArrayList<Node>();
228     while (nodeIterator.hasNext()) {
229       Node subcategory = nodeIterator.nextNode();
230       if (subcategory.isNodeType("exo:taxonomy"))
231         subcategories.add(subcategory);
232     }
233     return subcategories;
234   }
235 
236   /**
237    * Resolve category path by uri.
238    *
239    * @param context the context
240    *
241    * @return the string
242    */
243   public String resolveCategoryPathByUri(WebuiRequestContext context) throws Exception {
244     String parameters = null;
245     try {
246       // parameters: Classic/News/France/Blah/Bom
247       parameters = URLDecoder.decode(StringUtils.substringAfter(Util.getPortalRequestContext()
248                                                                     .getNodePath(),
249                                                                 Util.getUIPortal()
250                                                                     .getSelectedUserNode()
251                                                                     .getURI()), "UTF-8");
252     } catch (UnsupportedEncodingException e) {
253       if (LOG.isWarnEnabled()) {
254         LOG.warn(e.getMessage());
255       }
256     }
257 
258     // categoryPath: /News/France/Blah/Bom
259     String categoryPath = parameters.indexOf("/") >= 0 ? parameters.substring(parameters.indexOf("/")) : "";
260 
261     String gpath  = Util.getPortalRequestContext().getRequestParameter("path");
262     if (gpath != null) {
263       PortletPreferences portletPreferences = UICategoryNavigationUtils.getPortletPreferences();
264       String preferenceTreeName = portletPreferences.getValue(UICategoryNavigationConstant.PREFERENCE_TREE_NAME,
265                                                               "");
266       categoryPath = gpath.substring(gpath.indexOf(preferenceTreeName)
267           + preferenceTreeName.length());
268     }
269 
270 
271     return categoryPath;
272   }
273 
274   /**
275    * Gets the categories by uri.
276    *
277    * @param categoryUri the category uri
278    *
279    * @return the categories by uri
280    *
281    * @throws Exception the exception
282    */
283   public List<String> getCategoriesByUri(String categoryUri) throws Exception {
284     PortletPreferences portletPreferences = UICategoryNavigationUtils.getPortletPreferences();
285     String preferenceTreeName = portletPreferences.getValue(UICategoryNavigationConstant.PREFERENCE_TREE_NAME, "");
286     if (preferenceTreeName.equals(categoryUri)) categoryUri = "";
287 
288     // categories: {"/", "News", "News/France", "News/France/Blah", "News/France/Blah/Bom"}
289     List<String> categories = new ArrayList<String>();
290     String[] tempCategories = categoryUri.split("/");
291     StringBuffer tempCategory = new StringBuffer();
292     for (int i = 0; i < tempCategories.length; i++) {
293       if (i == 0)
294         tempCategory = new StringBuffer("");
295       else if (i == 1)
296         tempCategory = new StringBuffer(tempCategories[1]);
297       else
298         tempCategory.append("/").append(tempCategories[i]);
299       categories.add(tempCategory.toString());
300     }
301     return categories;
302   }
303 
304   /**
305    * Render category link.
306    *
307    * @param node the node
308    *
309    * @return the string
310    *
311    * @throws Exception the exception
312    */
313   public String renderCategoryLink(Node node) throws Exception {
314     // preferenceTargetPage: products/presentation/pclv
315     PortletPreferences portletPreferences = UICategoryNavigationUtils.getPortletPreferences();
316     String preferenceTargetPage = portletPreferences.getValue(UICategoryNavigationConstant.PREFERENCE_TARGET_PAGE, "");
317 
318     LivePortalManagerService livePortalManagerService = getApplicationComponent(LivePortalManagerService.class);
319     Node portalNode = livePortalManagerService.getLivePortalByChild(node);
320     String preferenceTreeName = portletPreferences.getValue(UICategoryNavigationConstant.PREFERENCE_TREE_NAME, "");
321     String categoryPath = node.getPath().replaceFirst(portalNode.getPath(), "");
322 //    categoryPath = categoryPath.substring(categoryPath.indexOf(preferenceTreeName) + preferenceTreeName.length());
323     categoryPath = categoryPath.substring(categoryPath.indexOf(preferenceTreeName)-1);
324 
325     NodeURL nodeURL = Util.getPortalRequestContext().createURL(NodeURL.TYPE);
326     NavigationResource resource = new NavigationResource(SiteType.PORTAL,
327                                                          Util.getPortalRequestContext()
328                                                              .getPortalOwner(),
329                                                          preferenceTargetPage);
330     nodeURL.setResource(resource).setQueryParameterValue("path", categoryPath);
331     String link = nodeURL.toString();
332 
333     FriendlyService friendlyService = getApplicationComponent(FriendlyService.class);
334     link = friendlyService.getFriendlyUri(link);
335 
336     return link;
337   }
338 
339   /**
340    * get content's title if exists (from exo:title property)
341    *
342    * @param node The node
343    * @return the title
344    * @throws Exception
345    */
346   public String getTitle(Node node) throws Exception {
347     if (node.hasProperty("exo:title"))
348       return node.getProperty("exo:title").getString();
349     else
350       return node.getName();
351   }
352 
353   public String getTreeTitle() {
354     return UICategoryNavigationUtils.getPortletPreferences().getValue(UICategoryNavigationConstant.PREFERENCE_TREE_TITLE, "");
355   }
356 
357   private Node getCategoryNode(String categoryPath) throws Exception {
358   TaxonomyService taxonomyService = getApplicationComponent(TaxonomyService.class);
359   PortletPreferences portletPreferences = UICategoryNavigationUtils.getPortletPreferences();
360   String preferenceTreeName = portletPreferences.getValue(UICategoryNavigationConstant.PREFERENCE_TREE_NAME, "");
361   Node treeNode = taxonomyService.getTaxonomyTree(preferenceTreeName);
362   Node categoryNode = null;
363   if ("".equals(categoryPath)) categoryNode = treeNode;
364   else categoryNode = treeNode.getNode(categoryPath);
365   return categoryNode;
366   }
367 }