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.ecm.webui.tree.selectone;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import javax.jcr.Node;
24  import javax.jcr.NodeIterator;
25  import javax.jcr.RepositoryException;
26  import javax.jcr.nodetype.NodeType;
27  
28  import org.exoplatform.commons.utils.LazyPageList;
29  import org.exoplatform.commons.utils.ListAccess;
30  import org.exoplatform.commons.utils.ListAccessImpl;
31  import org.exoplatform.ecm.webui.comparator.NodeTitleComparator;
32  import org.exoplatform.ecm.webui.selector.UISelectable;
33  import org.exoplatform.ecm.webui.tree.UIBaseNodeTreeSelector;
34  import org.exoplatform.ecm.webui.utils.Utils;
35  import org.exoplatform.portal.webui.container.UIContainer;
36  import org.exoplatform.services.cms.BasePath;
37  import org.exoplatform.services.ecm.publication.PublicationService;
38  import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
39  import org.exoplatform.services.wcm.core.NodeLocation;
40  import org.exoplatform.webui.config.annotation.ComponentConfig;
41  import org.exoplatform.webui.config.annotation.EventConfig;
42  import org.exoplatform.webui.core.UIBreadcumbs;
43  import org.exoplatform.webui.core.UIBreadcumbs.LocalPath;
44  import org.exoplatform.webui.core.UIComponent;
45  import org.exoplatform.webui.core.UIPageIterator;
46  import org.exoplatform.webui.core.UIPopupWindow;
47  import org.exoplatform.webui.event.Event;
48  import org.exoplatform.webui.event.EventListener;
49  
50  /**
51   * Created by The eXo Platform SAS
52   * @author : Hoa.Pham
53   *          hoa.pham@exoplatform.com
54   * Jun 23, 2008
55   */
56  
57  @ComponentConfig(
58      template =  "classpath:groovy/ecm/webui/tree/selectone/UISelectPathPanel.gtmpl",
59      events = {
60          @EventConfig(listeners = UISelectPathPanel.SelectActionListener.class)
61      }
62  )
63  public class UISelectPathPanel extends UIContainer {
64    private UIPageIterator uiPageIterator_;
65    public String[] acceptedMimeTypes = {};
66    protected NodeLocation parentNode;
67    private String[] acceptedNodeTypes = {};
68    private String[] exceptedNodeTypes = {};
69    private String[] defaultExceptedNodeTypes = {};
70    private boolean allowPublish = false;
71    private PublicationService publicationService_ = null;
72    private List<String> templates_ = null;
73    private boolean _showTrashHomeNode = true;
74  
75    public UISelectPathPanel() throws Exception {
76      uiPageIterator_ = addChild(UIPageIterator.class, null, "UISelectPathIterate");
77    }
78  
79    public UIPageIterator getUIPageIterator() { return uiPageIterator_; }
80  
81    public boolean isAllowPublish() {
82      return allowPublish;
83    }
84  
85    public void setShowTrashHomeNode(boolean value) {
86      _showTrashHomeNode = value;
87    }
88  
89    public void setAllowPublish(boolean allowPublish, PublicationService publicationService, List<String> templates) {
90      this.allowPublish = allowPublish;
91      publicationService_ = publicationService;
92      templates_ = templates;
93    }
94  
95    private void addNodePublish(List<Node> listNode, Node node, PublicationService publicationService) throws Exception {
96      if (isAllowPublish()) {
97        NodeType nt = node.getPrimaryNodeType();
98        if (templates_.contains(nt.getName())) {
99          Node nodecheck = publicationService.getNodePublish(node, null);
100         if (nodecheck != null) {
101           listNode.add(nodecheck);
102         }
103       } else {
104         listNode.add(node);
105       }
106     } else {
107       listNode.add(node);
108     }
109   }
110 
111   public void setParentNode(Node node) { 
112     this.parentNode = NodeLocation.getNodeLocationByNode(node); 
113   }
114 
115   public Node getParentNode() { 
116     return NodeLocation.getNodeByLocation(parentNode); 
117   }
118 
119   public String[] getAcceptedNodeTypes() { return acceptedNodeTypes; }
120 
121   public void setAcceptedNodeTypes(String[] acceptedNodeTypes) {
122     this.acceptedNodeTypes = acceptedNodeTypes;
123   }
124 
125   public String[] getExceptedNodeTypes() { return exceptedNodeTypes; }
126 
127   public void setExceptedNodeTypes(String[] exceptedNodeTypes) {
128     this.exceptedNodeTypes = exceptedNodeTypes;
129   }
130 
131   public String[] getDefaultExceptedNodeTypes() { return defaultExceptedNodeTypes; }
132 
133   public void setDefaultExceptedNodeTypes(String[] defaultExceptedNodeTypes) {
134     this.defaultExceptedNodeTypes = defaultExceptedNodeTypes;
135   }
136 
137 
138   public String[] getAcceptedMimeTypes() { return acceptedMimeTypes; }
139   public void setAcceptedMimeTypes(String[] acceptedMimeTypes) { this.acceptedMimeTypes = acceptedMimeTypes; }
140 
141   public List getSelectableNodes() throws Exception { 
142     return NodeLocation.getNodeListByLocationList(uiPageIterator_.getCurrentPageData()); 
143   }
144 
145   public void updateGrid() throws Exception {
146     ListAccess<Object> selectableNodeList = new ListAccessImpl<Object>(Object.class, 
147         NodeLocation.getLocationsByNodeList(getListSelectableNodes()));
148     LazyPageList<Object> objPageList = new LazyPageList<Object>(selectableNodeList, 10);
149     uiPageIterator_.setPageList(objPageList);
150   }
151 
152   public List<Node> getListSelectableNodes() throws Exception {
153     List<Node> list = new ArrayList<Node>();
154     if (parentNode == null) return list;
155     Node realNode = Utils.getNodeSymLink(NodeLocation.getNodeByLocation(parentNode));
156     for (NodeIterator iterator = realNode.getNodes();iterator.hasNext();) {
157       Node child = iterator.nextNode();
158       if(child.isNodeType("exo:hiddenable") || !child.isCheckedOut()) continue;
159       if(Utils.isTrashHomeNode(child) && !_showTrashHomeNode) continue;
160       if(matchMimeType(Utils.getNodeSymLink(child)) && matchNodeType(Utils.getNodeSymLink(child)) && !isExceptedNodeType(child)) {
161         list.add(child);
162       }
163     }
164     Collections.sort(list, new NodeTitleComparator(NodeTitleComparator.ASCENDING_ORDER));
165     List<Node> listNodeCheck = new ArrayList<Node>();
166     for (Node node : list) {
167       addNodePublish(listNodeCheck, node, publicationService_);
168     }
169     return listNodeCheck;
170   }
171 
172   protected boolean matchNodeType(Node node) throws Exception {
173     if(acceptedNodeTypes == null || acceptedNodeTypes.length == 0) return true;
174     for(String nodeType: acceptedNodeTypes) {
175       if((node != null) && node.isNodeType(nodeType)) return true;
176     }
177     return false;
178   }
179 
180   protected boolean isExceptedNodeType(Node node) throws RepositoryException {
181     if(defaultExceptedNodeTypes.length > 0) {
182       for(String nodeType: defaultExceptedNodeTypes) {
183         if((node != null) && node.isNodeType(nodeType)) return true;
184       }
185     }
186     if(exceptedNodeTypes == null || exceptedNodeTypes.length == 0) return false;
187     for(String nodeType: exceptedNodeTypes) {
188       if((node != null) && node.isNodeType(nodeType)) return true;
189     }
190     return false;
191   }
192 
193   protected boolean matchMimeType(Node node) throws Exception {
194     if(acceptedMimeTypes == null || acceptedMimeTypes.length == 0) return true;
195     if(!node.isNodeType("nt:file")) return true;
196     String mimeType = node.getNode("jcr:content").getProperty("jcr:mimeType").getString();
197     for(String type: acceptedMimeTypes) {
198       if(type.equalsIgnoreCase(mimeType))
199         return true;
200     }
201     return false;
202   }
203 
204   public String getPathTaxonomy() throws Exception {
205     NodeHierarchyCreator nodeHierarchyCreator = getApplicationComponent(NodeHierarchyCreator.class);
206     return nodeHierarchyCreator.getJcrPath(BasePath.TAXONOMIES_TREE_STORAGE_PATH);
207   }
208 
209   public String getDisplayName(Node node) throws RepositoryException {
210     return node.getName();
211   }
212 
213   static public class SelectActionListener extends EventListener<UISelectPathPanel> {
214     public void execute(Event<UISelectPathPanel> event) throws Exception {
215       UISelectPathPanel uiSelectPathPanel = event.getSource();
216       UIContainer uiTreeSelector = uiSelectPathPanel.getParent();
217       UIBreadcumbs uiBreadcumbs = uiTreeSelector.getChild(UIBreadcumbs.class);
218       StringBuffer breadcumbsPaths = new StringBuffer();
219       for(LocalPath iterLocalPath: uiBreadcumbs.getPath()) {
220         breadcumbsPaths.append("/").append(iterLocalPath.getId());
221       }
222       StringBuffer sbPath = new StringBuffer();
223       String objectid = event.getRequestContext().getRequestParameter(OBJECTID);
224 
225       sbPath.append(breadcumbsPaths).append(objectid.substring(objectid.lastIndexOf("/")));
226       if(uiTreeSelector instanceof UIOneNodePathSelector) {
227         if(!((UIOneNodePathSelector)uiTreeSelector).isDisable()) {
228           sbPath.insert(0, ":").insert(0, ((UIOneNodePathSelector)uiTreeSelector).getWorkspaceName());
229         }
230       }
231       String returnField = ((UIBaseNodeTreeSelector)uiTreeSelector).getReturnFieldName();
232       ((UISelectable)((UIBaseNodeTreeSelector)uiTreeSelector).getSourceComponent()).doSelect(returnField, sbPath.toString()) ;
233 
234       UIComponent uiOneNodePathSelector = uiSelectPathPanel.getParent();
235       if (uiOneNodePathSelector instanceof UIOneNodePathSelector) {
236         UIComponent uiComponent = uiOneNodePathSelector.getParent();
237         if (uiComponent instanceof UIPopupWindow) {
238           UIComponent uiParentOfPopup = uiComponent.getParent();
239           ((UIPopupWindow)uiComponent).setShow(false);
240           ((UIPopupWindow)uiComponent).setRendered(false);
241           if(uiParentOfPopup != null) {
242             event.getRequestContext().addUIComponentToUpdateByAjax(uiParentOfPopup);
243           }
244         }
245         UIComponent component = ((UIOneNodePathSelector)uiOneNodePathSelector).getSourceComponent().getParent();
246         if (component != null) {
247           event.getRequestContext().addUIComponentToUpdateByAjax(component);
248         }
249       }
250     }
251   }
252 }