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   **************************************************************************/
18  package org.exoplatform.ecm.webui.component.admin.taxonomy;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.jcr.Node;
24  import javax.jcr.Session;
25  
26  import org.exoplatform.ecm.webui.utils.Utils;
27  import org.exoplatform.services.jcr.RepositoryService;
28  import org.exoplatform.services.jcr.core.ManageableRepository;
29  import org.exoplatform.services.wcm.core.NodeLocation;
30  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
31  import org.exoplatform.webui.config.annotation.ComponentConfig;
32  import org.exoplatform.webui.config.annotation.ComponentConfigs;
33  import org.exoplatform.webui.config.annotation.EventConfig;
34  import org.exoplatform.webui.core.UIBreadcumbs;
35  import org.exoplatform.webui.core.UIBreadcumbs.LocalPath;
36  import org.exoplatform.webui.core.UIContainer;
37  import org.exoplatform.webui.core.UIPopupContainer;
38  import org.exoplatform.webui.core.UIPopupWindow;
39  import org.exoplatform.webui.core.lifecycle.UIContainerLifecycle;
40  import org.exoplatform.webui.event.Event;
41  import org.exoplatform.webui.event.EventListener;
42  
43  /**
44   * Created by The eXo Platform SARL
45   * Author : Hoang Van Hung
46   *          hunghvit@gmail.com
47   * Apr 5, 2009
48   */
49  
50  @ComponentConfigs(
51      {
52        @ComponentConfig(lifecycle = UIContainerLifecycle.class),
53        @ComponentConfig(
54            type = UIBreadcumbs.class, id = "BreadcumbTaxonomyTreeECMAdmin",
55            template = "system:/groovy/webui/core/UIBreadcumbs.gtmpl",
56            events = @EventConfig(listeners = UITaxonomyTreeCreateChild.SelectPathActionListener.class)
57        )
58      }
59  )
60  
61  public class UITaxonomyTreeCreateChild extends UIContainer {
62  
63    private String workspace;
64  
65    public static final String PERMISSION_ID_POPUP = "TaxonomyTreeViewPermissionPopup";
66  
67    private String selectedPath_ = null ;
68  
69    private NodeLocation taxonomyTreeNode = null;
70  
71    public UITaxonomyTreeCreateChild() throws Exception {
72      addChild(UIBreadcumbs.class, "BreadcumbTaxonomyTreeECMAdmin", "BreadcumbTaxonomyTreeECMAdmin");
73      UITaxonomyTreeBrowser uiTaxonomyTreeBrowser = addChild(UITaxonomyTreeBrowser.class, null, null);
74      uiTaxonomyTreeBrowser.setAcceptedNodeTypes(new String[] {Utils.EXO_TAXONOMY});
75      UITaxonomyTreeWorkingArea uiTaxonomyTreeWorkingArea = addChild(UITaxonomyTreeWorkingArea.class, null, null);
76      uiTaxonomyTreeWorkingArea.setAcceptedNodeTypes(new String[] {Utils.EXO_TAXONOMY});
77    }
78  
79    public void update() throws Exception {
80      UITaxonomyTreeBrowser uiTree = getChild(UITaxonomyTreeBrowser.class);
81      uiTree.update();
82      UITaxonomyTreeWorkingArea uiTaxonomyTreeWorkingArea = getChild(UITaxonomyTreeWorkingArea.class);
83      uiTaxonomyTreeWorkingArea.update();
84    }
85  
86    public void update(String path) throws Exception {
87      UITaxonomyTreeBrowser uiTree = getChild(UITaxonomyTreeBrowser.class);
88      uiTree.update();
89      uiTree.setNodeSelect(path);
90      UITaxonomyTreeWorkingArea uiTaxonomyTreeWorkingArea = getChild(UITaxonomyTreeWorkingArea.class);
91      uiTaxonomyTreeWorkingArea.setSelectedPath(path);
92      uiTaxonomyTreeWorkingArea.update();
93      setSelectedPath(path);
94    }
95  
96    public Node getRootNode() throws Exception {
97      return getTaxonomyTreeNode().getParent();
98    }
99  
100   public Node getTaxonomyTreeNode() {
101     return NodeLocation.getNodeByLocation(taxonomyTreeNode);
102   }
103 
104   public void setSelectedPath(String selectedPath) {
105     selectedPath_ = selectedPath;
106   }
107 
108   public String getSelectedPath() {
109     return selectedPath_;
110   }
111 
112   public Node getNodeByPath(String path) throws Exception {
113     return (Node) getSession().getItem(path) ;
114   }
115 
116   Session getSession() throws Exception {
117     return WCMCoreUtils.getSystemSessionProvider().getSession(workspace, getRepository());
118   }
119   
120   public ManageableRepository getRepository() throws Exception {
121     RepositoryService repositoryService = getApplicationComponent(RepositoryService.class);
122     return repositoryService.getCurrentRepository();
123   } 
124 
125   public void initPopup(String path) throws Exception {
126     removeChildById("TaxonomyPopupCreateChild");
127     UIPopupWindow uiPopup = addChild(UIPopupWindow.class, null, "TaxonomyPopupCreateChild");
128     uiPopup.setShowMask(true);
129     uiPopup.setWindowSize(600, 250);
130     UITaxonomyTreeCreateChildForm uiTaxoForm = createUIComponent(UITaxonomyTreeCreateChildForm.class, null, null);
131     uiTaxoForm.setParent(path);
132     uiPopup.setUIComponent(uiTaxoForm);
133     uiPopup.setRendered(true);
134     uiPopup.setShow(true);
135   }
136 
137   public UIPopupContainer initPopupPermission(String id) throws Exception {
138     removeChildById(id) ;
139     return addChild(UIPopupContainer.class, null, id) ;
140   }
141 
142   public void onChange(Node currentNode) throws Exception {
143     UIBreadcumbs uiBreadcumbs = getChild(UIBreadcumbs.class);
144     List<LocalPath> listLocalPath = new ArrayList<LocalPath>();
145     String path = currentNode.getPath().trim();
146     String taxonomyPath = getTaxonomyTreeNode().getPath();
147     if (path.startsWith(taxonomyPath)) {
148       String subTaxonomy = path.substring(taxonomyPath.length(), path.length());
149       String[] arrayPath = subTaxonomy.split("/");
150       if (arrayPath.length > 0) {
151         for (int i = 0; i < arrayPath.length; i++) {
152           if (!arrayPath[i].trim().equals("")) {
153             UIBreadcumbs.LocalPath localPath1 = new UIBreadcumbs.LocalPath(arrayPath[i].trim(),
154                 arrayPath[i].trim());
155             listLocalPath.add(localPath1);
156           }
157         }
158       }
159     }
160     uiBreadcumbs.setPath(listLocalPath);
161   }
162 
163   public void changeGroup(String groupId, Object context) throws Exception {
164     StringBuffer sbPath =  new StringBuffer();
165     sbPath.append(getTaxonomyTreeNode().getPath()).append("/");
166     UIBreadcumbs uiBreadcumb = getChild(UIBreadcumbs.class);
167     if (groupId == null) groupId = "";
168     List<LocalPath> listLocalPath = uiBreadcumb.getPath();
169     if (listLocalPath == null || listLocalPath.size() == 0) return;
170     List<String> listLocalPathString = new ArrayList<String>();
171     for (LocalPath localPath : listLocalPath) {
172       listLocalPathString.add(localPath.getId().trim());
173     }
174     if (listLocalPathString.contains(groupId)) {
175       int index = listLocalPathString.indexOf(groupId);
176       if (index == listLocalPathString.size() - 1) return;
177       for (int i = listLocalPathString.size() - 1; i > index; i--) {
178         listLocalPathString.remove(i);
179         listLocalPath.remove(i);
180       }
181       uiBreadcumb.setPath(listLocalPath);
182       for (int i = 0; i < listLocalPathString.size(); i++) {
183         String pathName = listLocalPathString.get(i);
184         if (pathName != null && pathName.length() > 0) {
185           sbPath.append(pathName.trim());
186           if (i < listLocalPathString.size() - 1) sbPath.append("/");
187         }
188       }
189       UITaxonomyTreeBrowser uiTaxonomyTree = getChild(UITaxonomyTreeBrowser.class);
190       uiTaxonomyTree.setNodeSelect(sbPath.toString());
191     }
192   }
193 
194   public String getWorkspace() {
195     return workspace;
196   }
197 
198   public void setWorkspace(String workspace) {
199     this.workspace = workspace;
200   }
201 
202   public void setTaxonomyTreeNode(Node taxonomyTreeNode) {
203     this.taxonomyTreeNode = NodeLocation.getNodeLocationByNode(taxonomyTreeNode);
204   }
205 
206   public static class SelectPathActionListener extends EventListener<UIBreadcumbs> {
207     public void execute(Event<UIBreadcumbs> event) throws Exception {
208       UIBreadcumbs uiBreadcumbs = event.getSource();
209       UITaxonomyTreeCreateChild uiTaxonomyTreeCreateChild = uiBreadcumbs.getParent();
210       String objectId = event.getRequestContext().getRequestParameter(OBJECTID);
211       uiBreadcumbs.setSelectPath(objectId);
212       String selectGroupId = uiBreadcumbs.getSelectLocalPath().getId();
213       uiTaxonomyTreeCreateChild.changeGroup(selectGroupId, event.getRequestContext());
214       event.getRequestContext().addUIComponentToUpdateByAjax(uiTaxonomyTreeCreateChild);
215     }
216   }
217 
218 }