View Javadoc
1   /*
2    * Copyright (C) 2003-2007 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.Arrays;
21  import java.util.List;
22  
23  import javax.jcr.Item;
24  import javax.jcr.Node;
25  import javax.jcr.Session;
26  
27  import org.exoplatform.ecm.webui.tree.UIBaseNodeTreeSelector;
28  import org.exoplatform.ecm.webui.tree.UINodeTreeBuilder;
29  import org.exoplatform.ecm.webui.utils.Utils;
30  import org.exoplatform.portal.webui.util.Util;
31  import org.exoplatform.services.cms.link.NodeFinder;
32  import org.exoplatform.services.cms.templates.TemplateService;
33  import org.exoplatform.services.ecm.publication.PublicationService;
34  import org.exoplatform.services.jcr.RepositoryService;
35  import org.exoplatform.services.jcr.core.ManageableRepository;
36  import org.exoplatform.services.jcr.ext.common.SessionProvider;
37  import org.exoplatform.services.security.IdentityConstants;
38  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
39  import org.exoplatform.webui.config.annotation.ComponentConfig;
40  import org.exoplatform.webui.config.annotation.ComponentConfigs;
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.event.Event;
45  import org.exoplatform.webui.event.EventListener;
46  
47  /**
48   * Created by The eXo Platform SARL
49   * Author : Dang Van Minh
50   *          minh.dang@exoplatform.com
51   * Oct 18, 2006
52   * 2:12:26 PM
53   */
54  @ComponentConfigs(
55      {
56        @ComponentConfig(
57            template = "classpath:groovy/ecm/webui/UIOneNodePathSelector.gtmpl"
58        ),
59        @ComponentConfig(
60            type = UIBreadcumbs.class, id = "BreadcumbCategoriesOne",
61            template = "system:/groovy/webui/core/UIBreadcumbs.gtmpl",
62            events = @EventConfig(listeners = UIOneNodePathSelector.SelectPathActionListener.class)
63        )
64      }
65  )
66  
67  public class UIOneNodePathSelector extends UIBaseNodeTreeSelector {
68  
69    private String[] acceptedNodeTypesInTree = {};
70    private String[] acceptedNodeTypesInPathPanel = {};
71    private String[] acceptedMimeTypes = {};
72  
73    private String[] exceptedNodeTypesInPathPanel = {};
74    private String[] exceptedNodeTypesInTree = {};
75  
76    private String[] defaultExceptedNodeTypes = {"exo:symlink"};
77  
78    private String repositoryName = null;
79    private String workspaceName = null;
80    private String rootTreePath = null;
81    private boolean isDisable = false;
82    private boolean allowPublish = false;
83    private boolean alreadyChangePath = false;
84    private boolean showOnlyFolderNodeInTree = true;
85  
86    private String rootTaxonomyName = null;
87  
88    public UIOneNodePathSelector() throws Exception {
89      addChild(UIBreadcumbs.class, "BreadcumbCategoriesOne", "BreadcumbCategoriesOne");
90      addChild(UIWorkspaceList.class, null, null);
91      addChild(UINodeTreeBuilder.class, null, UINodeTreeBuilder.class.getSimpleName()+hashCode());
92      addChild(UISelectPathPanel.class, null, null).setShowTrashHomeNode(false);
93    }
94  
95    public String getRootTaxonomyName() { return rootTaxonomyName; }
96  
97    public void setRootTaxonomyName(String rootTaxonomyName) {
98      this.rootTaxonomyName = rootTaxonomyName;
99    }
100 
101   public void init(SessionProvider sessionProvider) throws Exception {
102     RepositoryService repositoryService = getApplicationComponent(RepositoryService.class);
103     ManageableRepository manageableRepository = repositoryService.getCurrentRepository();
104     PublicationService publicationService = getApplicationComponent(PublicationService.class);
105     TemplateService templateService  = getApplicationComponent(TemplateService.class);
106     List<String> templates = templateService.getDocumentTemplates();
107     Node rootNode;
108     if (rootTreePath.trim().equals("/")) {
109       rootNode = sessionProvider.getSession(workspaceName, manageableRepository).getRootNode();
110     } else {
111       NodeFinder nodeFinder = getApplicationComponent(NodeFinder.class);
112       if (rootTreePath.indexOf("${userId}") > -1) {
113         String userId = Util.getPortalRequestContext().getRemoteUser();
114         String rootTreeOfSpecialDriver = 
115           org.exoplatform.services.cms.impl.Utils.getPersonalDrivePath(rootTreePath , userId);
116         rootTreePath = rootTreeOfSpecialDriver;
117       }
118       rootNode = (Node) nodeFinder.getItem(workspaceName, rootTreePath);
119     }
120 
121     UIWorkspaceList uiWorkspaceList = getChild(UIWorkspaceList.class);
122     uiWorkspaceList.setWorkspaceList();
123     uiWorkspaceList.setIsDisable(workspaceName, isDisable);
124     UINodeTreeBuilder builder = getChild(UINodeTreeBuilder.class);
125     builder.setAllowPublish(allowPublish, publicationService, templates);
126     if (this.showOnlyFolderNodeInTree) {
127       List<String> nodeTypesInTree = new ArrayList<String>(Arrays.asList(acceptedNodeTypesInTree));
128       if (!nodeTypesInTree.contains(Utils.NT_UNSTRUCTURED))
129         nodeTypesInTree.add(Utils.NT_UNSTRUCTURED);
130       if (!nodeTypesInTree.contains(Utils.NT_FOLDER))
131         nodeTypesInTree.add(Utils.NT_FOLDER);
132       if (!nodeTypesInTree.contains(Utils.EXO_TAXONOMY))
133         nodeTypesInTree.add(Utils.EXO_TAXONOMY);
134       this.acceptedNodeTypesInTree = nodeTypesInTree.toArray(new String[]{});
135     }
136     builder.setAcceptedNodeTypes(acceptedNodeTypesInTree);
137     builder.setDefaultExceptedNodeTypes(defaultExceptedNodeTypes);
138     builder.setRootTreeNode(rootNode);
139 
140     UISelectPathPanel selectPathPanel = getChild(UISelectPathPanel.class);
141     selectPathPanel.setAllowPublish(allowPublish, publicationService, templates);
142     selectPathPanel.setAcceptedNodeTypes(acceptedNodeTypesInPathPanel);
143     selectPathPanel.setAcceptedMimeTypes(acceptedMimeTypes);
144     selectPathPanel.setExceptedNodeTypes(exceptedNodeTypesInPathPanel);
145     selectPathPanel.setDefaultExceptedNodeTypes(defaultExceptedNodeTypes);
146     selectPathPanel.updateGrid();
147   }
148 
149   public boolean isAllowPublish() {
150     return allowPublish;
151   }
152 
153   public void setAllowPublish(boolean allowPublish) {
154     this.allowPublish = allowPublish;
155   }
156 
157   public void setRootNodeLocation(String repository, String workspace, String rootPath) throws Exception {
158     this.repositoryName = repository;
159     this.workspaceName = workspace;
160     this.rootTreePath = rootPath;
161   }
162 
163   public void setIsDisable(String wsName, boolean isDisable) {
164     setWorkspaceName(wsName);
165     this.isDisable = isDisable;
166   }
167 
168   public boolean isDisable() { return isDisable; }
169 
170   public void setIsShowSystem(boolean isShowSystem) {
171     getChild(UIWorkspaceList.class).setIsShowSystem(isShowSystem);
172   }
173 
174   public void setShowRootPathSelect(boolean isRendered) {
175     UIWorkspaceList uiWorkspaceList = getChild(UIWorkspaceList.class);
176     uiWorkspaceList.setShowRootPathSelect(isRendered);
177   }
178 
179   public String[] getAcceptedNodeTypesInTree() {
180     return acceptedNodeTypesInTree;
181   }
182 
183   public void setAcceptedNodeTypesInTree(String[] acceptedNodeTypesInTree) {
184     this.acceptedNodeTypesInTree = acceptedNodeTypesInTree;
185   }
186 
187   public String[] getAcceptedNodeTypesInPathPanel() {
188     return acceptedNodeTypesInPathPanel;
189   }
190 
191   public void setAcceptedNodeTypesInPathPanel(String[] acceptedNodeTypesInPathPanel) {
192     this.acceptedNodeTypesInPathPanel = acceptedNodeTypesInPathPanel;
193   }
194 
195   public String[] getExceptedNodeTypesInTree() {
196     return exceptedNodeTypesInTree;
197   }
198 
199   public void setExceptedNodeTypesInTree(String[] exceptedNodeTypesInTree) {
200     this.exceptedNodeTypesInTree = exceptedNodeTypesInTree;
201   }
202 
203   public String[] getExceptedNodeTypesInPathPanel() {
204     return exceptedNodeTypesInPathPanel;
205   }
206 
207   public void setExceptedNodeTypesInPathPanel(String[] exceptedNodeTypesInPathPanel) {
208     this.exceptedNodeTypesInPathPanel = exceptedNodeTypesInPathPanel;
209   }
210 
211   public String[] getDefaultExceptedNodeTypes() { return defaultExceptedNodeTypes; }
212 
213   public String[] getAcceptedMimeTypes() { return acceptedMimeTypes; }
214 
215   public void setAcceptedMimeTypes(String[] acceptedMimeTypes) { this.acceptedMimeTypes = acceptedMimeTypes; }
216 
217   public boolean isShowOnlyFolderNodeInTree() { return showOnlyFolderNodeInTree; }
218 
219   public void setShowOnlyFolderNodeInTree(boolean value) {
220     showOnlyFolderNodeInTree = value;
221   }
222 
223   public String getRepositoryName() { return repositoryName; }
224   public void setRepositoryName(String repositoryName) {
225     this.repositoryName = repositoryName;
226   }
227 
228   public String getWorkspaceName() { return workspaceName; }
229 
230   public void setWorkspaceName(String workspaceName) {
231     this.workspaceName = workspaceName;
232   }
233 
234   public String getRootTreePath() { return rootTreePath; }
235 
236   public void setRootTreePath(String rootTreePath) { this.rootTreePath = rootTreePath;
237   }
238 
239   public void onChange(final Node currentNode, Object context) throws Exception {
240     UISelectPathPanel selectPathPanel = getChild(UISelectPathPanel.class);
241     selectPathPanel.setParentNode(currentNode);
242     selectPathPanel.updateGrid();
243     UIBreadcumbs uiBreadcumbs = getChild(UIBreadcumbs.class);
244     String pathName = currentNode.getName();
245     String pathTitle = pathName;
246     if (currentNode.hasProperty("exo:title")){
247       pathTitle = currentNode.getProperty("exo:title").getString();
248     } 
249     NodeFinder nodeFinder = getApplicationComponent(NodeFinder.class);
250     Session session;
251     if(currentNode.getSession().getUserID().equals(IdentityConstants.SYSTEM)) {
252       // use system session to fetch node if current session is a system session
253       session = WCMCoreUtils.getSystemSessionProvider().getSession(workspaceName, WCMCoreUtils.getRepository());
254     } else {
255       session = WCMCoreUtils.getUserSessionProvider().getSession(workspaceName, WCMCoreUtils.getRepository());
256     }
257     Node rootNode = (Node) nodeFinder.getItem(session, rootTreePath);
258 
259 
260     if (currentNode.equals(rootNode)) {
261       pathName = "";
262     }
263     UIBreadcumbs.LocalPath localPath = new UIBreadcumbs.LocalPath(pathName, pathTitle);
264     List<LocalPath> listLocalPath = uiBreadcumbs.getPath();
265     StringBuilder buffer = new StringBuilder(1024);
266     for(LocalPath iterLocalPath: listLocalPath) {
267       buffer.append("/").append(iterLocalPath.getId());
268     }
269     if (!alreadyChangePath) {
270       String path = buffer.toString();
271       if (path.startsWith("//"))
272         path = path.substring(1);
273       if (!path.startsWith(rootTreePath)) {
274         StringBuffer buf = new StringBuffer();
275         buf.append(rootTreePath).append(path);
276         path = buf.toString();
277       }
278       if (path.endsWith("/"))
279         path = path.substring(0, path.length() - 1);
280       if (path.length() == 0)
281         path = "/";
282       Node currentBreadcumbsNode = getNodeByVirtualPath(path, session);
283       if (currentNode.equals(rootNode)
284           || ((!currentBreadcumbsNode.equals(rootNode) && currentBreadcumbsNode.getParent()
285                                                                                .equals(currentNode)))) {
286         if (listLocalPath != null && listLocalPath.size() > 0) {
287           listLocalPath.remove(listLocalPath.size() - 1);
288         }
289       } else {
290         listLocalPath.add(localPath);
291       }
292     }
293     alreadyChangePath = false;
294     uiBreadcumbs.setPath(listLocalPath);
295   }
296 
297   private Node getNodeByVirtualPath(String pathLinkNode, Session session) throws Exception{
298     NodeFinder nodeFinder_ = getApplicationComponent(NodeFinder.class);
299     Item item = nodeFinder_.getItem(session, pathLinkNode);
300     return (Node)item;
301   }
302 
303   private void changeNode(String stringPath, Object context) throws Exception {
304     UINodeTreeBuilder builder = getChild(UINodeTreeBuilder.class);
305     builder.changeNode(stringPath, context);
306   }
307 
308   public void changeGroup(String groupId, Object context) throws Exception {
309     StringBuffer stringPath = new StringBuffer(rootTreePath);
310     if (!rootTreePath.equals("/")) {
311       stringPath.append("/");
312     }
313     UIBreadcumbs uiBreadcumb = getChild(UIBreadcumbs.class);
314     if (groupId == null) groupId = "";
315     List<LocalPath> listLocalPath = uiBreadcumb.getPath();
316     if (listLocalPath == null || listLocalPath.size() == 0) return;
317     List<String> listLocalPathString = new ArrayList<String>();
318     for (LocalPath localPath : listLocalPath) {
319       listLocalPathString.add(localPath.getId().trim());
320     }
321     if (listLocalPathString.contains(groupId)) {
322       int index = listLocalPathString.indexOf(groupId);
323       alreadyChangePath = false;
324       if (index == listLocalPathString.size() - 1) return;
325       for (int i = listLocalPathString.size() - 1; i > index; i--) {
326         listLocalPathString.remove(i);
327         listLocalPath.remove(i);
328       }
329       alreadyChangePath = true;
330       uiBreadcumb.setPath(listLocalPath);
331       for (int i = 0; i < listLocalPathString.size(); i++) {
332         String pathName = listLocalPathString.get(i);
333         if (pathName != null && pathName.trim().length() != 0) {
334           stringPath.append(pathName.trim());
335           if (i < listLocalPathString.size() - 1) stringPath.append("/");
336         }
337       }
338       changeNode(stringPath.toString(), context);
339     }
340   }
341 
342   static  public class SelectPathActionListener extends EventListener<UIBreadcumbs> {
343     public void execute(Event<UIBreadcumbs> event) throws Exception {
344       UIBreadcumbs uiBreadcumbs = event.getSource();
345       UIOneNodePathSelector uiOneNodePathSelector = uiBreadcumbs.getParent();
346       String objectId =  event.getRequestContext().getRequestParameter(OBJECTID);
347       uiBreadcumbs.setSelectPath(objectId);
348       String selectGroupId = uiBreadcumbs.getSelectLocalPath().getId();
349       uiOneNodePathSelector.changeGroup(selectGroupId, event.getRequestContext());
350       event.getRequestContext().addUIComponentToUpdateByAjax(uiOneNodePathSelector);
351     }
352   }
353 }