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  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.MissingResourceException;
23  
24  import javax.jcr.Node;
25  import javax.jcr.PathNotFoundException;
26  import javax.jcr.RepositoryException;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.exoplatform.ecm.webui.tree.UIBaseNodeTreeSelector;
30  import org.exoplatform.ecm.webui.tree.UITreeTaxonomyBuilder;
31  import org.exoplatform.ecm.webui.utils.Utils;
32  import org.exoplatform.services.cms.link.NodeFinder;
33  import org.exoplatform.services.cms.taxonomy.TaxonomyService;
34  import org.exoplatform.services.cms.templates.TemplateService;
35  import org.exoplatform.services.ecm.publication.PublicationService;
36  import org.exoplatform.services.jcr.RepositoryService;
37  import org.exoplatform.services.jcr.core.ManageableRepository;
38  import org.exoplatform.services.jcr.ext.common.SessionProvider;
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  import org.exoplatform.webui.form.UIFormSelectBox;
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/UIOneTaxonomySelector.gtmpl"
58        ),
59        @ComponentConfig(
60            type = UIBreadcumbs.class, id = "BreadcumbOneTaxonomy",
61            template = "system:/groovy/webui/core/UIBreadcumbs.gtmpl",
62            events = @EventConfig(listeners = UIOneTaxonomySelector.SelectPathActionListener.class)
63        )
64      }
65  )
66  
67  public class UIOneTaxonomySelector extends UIBaseNodeTreeSelector {
68  
69    private String[] acceptedNodeTypesInTree = {};
70    private String[] acceptedNodeTypesInPathPanel = {};
71    private String[] acceptedMimeTypes = {};
72  
73    private String[] exceptedNodeTypesInPathPanel = {};
74  
75    private String repositoryName = null;
76    private String workspaceName = null;
77    private String rootTreePath = null;
78    private boolean isDisable = false;
79    private boolean allowPublish = false;
80  
81    private boolean alreadyChangePath = false;
82  
83    private String rootTaxonomyName = null;
84  
85    private String[] defaultExceptedNodeTypes = {"exo:symlink"};
86  
87    public UIOneTaxonomySelector() throws Exception {
88      addChild(UIBreadcumbs.class, "BreadcumbOneTaxonomy", "BreadcumbOneTaxonomy");
89      addChild(UITreeTaxonomyList.class, null, null);
90      addChild(UITreeTaxonomyBuilder.class, null, UITreeTaxonomyBuilder.class.getSimpleName()+hashCode());
91      addChild(UISelectTaxonomyPanel.class, null, null);
92    }
93  
94    public String getRootTaxonomyName() { return rootTaxonomyName; }
95  
96    public void setRootTaxonomyName(String rootTaxonomyName) {
97      this.rootTaxonomyName = rootTaxonomyName;
98    }
99  
100   Node getTaxoTreeNode(String taxoTreeName) throws RepositoryException {
101     TaxonomyService taxonomyService = getApplicationComponent(TaxonomyService.class);
102     return taxonomyService.getTaxonomyTree(taxoTreeName);
103   }
104 
105   public String[] getDefaultExceptedNodeTypes() { return defaultExceptedNodeTypes; }
106 
107   public void init(SessionProvider sessionProvider) throws Exception {
108     RepositoryService repositoryService = getApplicationComponent(RepositoryService.class);
109     ManageableRepository manageableRepository = repositoryService.getCurrentRepository();
110     PublicationService publicationService = getApplicationComponent(PublicationService.class);
111     TemplateService templateService = getApplicationComponent(TemplateService.class);
112     List<String> templates = templateService.getDocumentTemplates();
113     Node rootNode;
114     if (rootTreePath.trim().equals("/")) {
115       rootNode = sessionProvider.getSession(workspaceName, manageableRepository)
116           .getRootNode();
117     } else {
118       NodeFinder nodeFinder = getApplicationComponent(NodeFinder.class);
119       try {
120         rootNode = (Node) nodeFinder.getItem(workspaceName, rootTreePath);
121       } catch (PathNotFoundException pathNotFoundException) {
122         rootNode = null;
123       }
124     }
125 
126     UITreeTaxonomyList uiTreeTaxonomyList = getChild(UITreeTaxonomyList.class);
127     uiTreeTaxonomyList.setTaxonomyTreeList();
128     UITreeTaxonomyBuilder builder = getChild(UITreeTaxonomyBuilder.class);
129     builder.setAllowPublish(allowPublish, publicationService, templates);
130     builder.setAcceptedNodeTypes(acceptedNodeTypesInTree);
131     builder.setDefaultExceptedNodeTypes(defaultExceptedNodeTypes);
132     if (rootNode != null) builder.setRootTreeNode(rootNode);
133     UISelectTaxonomyPanel selectPathPanel = getChild(UISelectTaxonomyPanel.class);
134     selectPathPanel.setAllowPublish(allowPublish, publicationService, templates);
135     selectPathPanel.setAcceptedNodeTypes(acceptedNodeTypesInPathPanel);
136     selectPathPanel.setAcceptedMimeTypes(acceptedMimeTypes);
137     selectPathPanel.setExceptedNodeTypes(exceptedNodeTypesInPathPanel);
138     selectPathPanel.setDefaultExceptedNodeTypes(defaultExceptedNodeTypes);
139     selectPathPanel.updateGrid();
140     String taxoTreeName = ((UIFormSelectBox)this.findComponentById(UITreeTaxonomyList.TAXONOMY_TREE)).getValue();
141     Node taxoTreeNode = this.getTaxoTreeNode(taxoTreeName);
142     this.setWorkspaceName(taxoTreeNode.getSession().getWorkspace().getName());
143     this.setRootTaxonomyName(taxoTreeNode.getName());
144     this.setRootTreePath(taxoTreeNode.getPath());
145     UITreeTaxonomyBuilder uiTreeJCRExplorer = this.findFirstComponentOfType(UITreeTaxonomyBuilder.class);
146     uiTreeJCRExplorer.setRootTreeNode(taxoTreeNode);
147     uiTreeJCRExplorer.buildTree();
148   }
149 
150   public boolean isAllowPublish() {
151     return allowPublish;
152   }
153 
154   public void setAllowPublish(boolean allowPublish) {
155     this.allowPublish = allowPublish;
156   }
157 
158   public void setRootNodeLocation(String repository, String workspace, String rootPath) throws Exception {
159     this.repositoryName = repository;
160     this.workspaceName = workspace;
161     this.rootTreePath = rootPath;
162   }
163 
164   public void setIsDisable(String wsName, boolean isDisable) {
165     setWorkspaceName(wsName);
166     this.isDisable = isDisable;
167   }
168 
169   public boolean isDisable() { return isDisable; }
170 
171   public void setIsShowSystem(boolean isShowSystem) {
172     getChild(UITreeTaxonomyList.class).setIsShowSystem(isShowSystem);
173   }
174 
175   public void setShowRootPathSelect(boolean isRendered) {
176     UITreeTaxonomyList uiTreeTaxonomyList = getChild(UITreeTaxonomyList.class);
177     uiTreeTaxonomyList.setShowRootPathSelect(isRendered);
178   }
179 
180   public String[] getAcceptedNodeTypesInTree() {
181     return acceptedNodeTypesInTree;
182   }
183 
184   public void setAcceptedNodeTypesInTree(String[] acceptedNodeTypesInTree) {
185     this.acceptedNodeTypesInTree = acceptedNodeTypesInTree;
186   }
187 
188   public String[] getAcceptedNodeTypesInPathPanel() {
189     return acceptedNodeTypesInPathPanel;
190   }
191 
192   public void setAcceptedNodeTypesInPathPanel(String[] acceptedNodeTypesInPathPanel) {
193     this.acceptedNodeTypesInPathPanel = acceptedNodeTypesInPathPanel;
194   }
195 
196   public String[] getExceptedNodeTypesInPathPanel() {
197     return exceptedNodeTypesInPathPanel;
198   }
199 
200   public void setExceptedNodeTypesInPathPanel(String[] exceptedNodeTypesInPathPanel) {
201     this.exceptedNodeTypesInPathPanel = exceptedNodeTypesInPathPanel;
202   }
203 
204   public String[] getAcceptedMimeTypes() { return acceptedMimeTypes; }
205 
206   public void setAcceptedMimeTypes(String[] acceptedMimeTypes) { this.acceptedMimeTypes = acceptedMimeTypes; }
207 
208   public String getRepositoryName() { return repositoryName; }
209   public void setRepositoryName(String repositoryName) {
210     this.repositoryName = repositoryName;
211   }
212 
213   public String getWorkspaceName() { return workspaceName; }
214 
215   public void setWorkspaceName(String workspaceName) {
216     this.workspaceName = workspaceName;
217   }
218 
219   public String getRootTreePath() { return rootTreePath; }
220 
221   public void setRootTreePath(String rootTreePath) {
222     this.rootTreePath = rootTreePath;
223     getChild(UISelectTaxonomyPanel.class).setTaxonomyTreePath(rootTreePath);
224   }
225 
226   public String getTaxonomyLabel(Node node) throws RepositoryException {
227     try {
228       String display = node.getName();
229       if (rootTaxonomyName == null) rootTaxonomyName = rootTreePath.substring(rootTreePath.lastIndexOf("/") + 1);
230       display = rootTaxonomyName.concat(node.getPath().replace(rootTreePath, "")).replaceAll("/", ".");
231       return Utils.getResourceBundle(("eXoTaxonomies.").concat(display).concat(".label"));
232     } catch (MissingResourceException me) {
233       return node.getName();
234     }
235   }
236 
237   public void onChange(final Node currentNode, Object context) throws Exception {
238     UISelectTaxonomyPanel selectPathPanel = getChild(UISelectTaxonomyPanel.class);
239     UITreeTaxonomyList uiTreeTaxonomyList = getChild(UITreeTaxonomyList.class);
240     String taxoTreeName = uiTreeTaxonomyList.getUIFormSelectBox(UITreeTaxonomyList.TAXONOMY_TREE).getValue();
241     if (StringUtils.isEmpty(taxoTreeName)) return;
242     Node parentRoot = getTaxoTreeNode(taxoTreeName);
243     selectPathPanel.setParentNode(currentNode);
244     selectPathPanel.updateGrid();
245     UIBreadcumbs uiBreadcumbs = getChild(UIBreadcumbs.class);
246     String pathName = currentNode.getName();
247     if (currentNode.equals(parentRoot)) {
248       pathName = "";
249     }
250     String label = pathName.length() > 0 ? getTaxonomyLabel(currentNode) : pathName;
251     UIBreadcumbs.LocalPath localPath = new UIBreadcumbs.LocalPath(pathName, label);
252     List<LocalPath> listLocalPath = uiBreadcumbs.getPath();
253     StringBuilder buffer = new StringBuilder(1024);
254     for(LocalPath iterLocalPath: listLocalPath) {
255       buffer.append("/").append(iterLocalPath.getId());
256     }
257     if (!alreadyChangePath) {
258       String path = buffer.toString();
259       path = path.replaceAll("/+", "/");
260       if (!path.startsWith(rootTreePath)) {
261         StringBuffer buf = new StringBuffer();
262         if (currentNode.getPath().contains(parentRoot.getPath())) {
263           buf.append(currentNode.getPath());
264         } else {
265           buf.append(rootTreePath).append(path);
266         }
267         path = buf.toString();
268       }
269       if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
270       if (path.length() == 0) path = "/";
271       String breadcumbPath = rootTreePath + buffer.toString();
272       if (!breadcumbPath.equals(path)) {
273         if (breadcumbPath.startsWith(path)) {
274           if (listLocalPath != null && listLocalPath.size() > 0) {
275             listLocalPath.remove(listLocalPath.size() - 1);
276           }
277         } else {
278           if (!currentNode.equals(parentRoot)) {
279             listLocalPath.add(localPath);
280           }
281         }
282       }
283     }
284     alreadyChangePath = false;
285     uiBreadcumbs.setPath(listLocalPath);
286   }
287 
288   private void changeNode(String stringPath, Object context) throws Exception {
289     UITreeTaxonomyBuilder builder = getChild(UITreeTaxonomyBuilder.class);
290     builder.changeNode(stringPath, context);
291   }
292 
293   public void changeGroup(String groupId, Object context) throws Exception {
294     StringBuffer stringPath = new StringBuffer(rootTreePath);
295     if (!rootTreePath.equals("/")) {
296       stringPath.append("/");
297     }
298     UIBreadcumbs uiBreadcumb = getChild(UIBreadcumbs.class);
299     if (groupId == null) groupId = "";
300     List<LocalPath> listLocalPath = uiBreadcumb.getPath();
301     if (listLocalPath == null || listLocalPath.size() == 0) return;
302     List<String> listLocalPathString = new ArrayList<String>();
303     for (LocalPath localPath : listLocalPath) {
304       listLocalPathString.add(localPath.getId().trim());
305     }
306     if (listLocalPathString.contains(groupId)) {
307       int index = listLocalPathString.indexOf(groupId);
308       alreadyChangePath = false;
309       if (index == listLocalPathString.size() - 1) return;
310       for (int i = listLocalPathString.size() - 1; i > index; i--) {
311         listLocalPathString.remove(i);
312         listLocalPath.remove(i);
313       }
314       alreadyChangePath = true;
315       uiBreadcumb.setPath(listLocalPath);
316       for (int i = 0; i < listLocalPathString.size(); i++) {
317         String pathName = listLocalPathString.get(i);
318         if (pathName != null && pathName.trim().length() != 0) {
319           stringPath.append(pathName.trim());
320           if (i < listLocalPathString.size() - 1) stringPath.append("/");
321         }
322       }
323       changeNode(stringPath.toString(), context);
324     }
325   }
326 
327   static  public class SelectPathActionListener extends EventListener<UIBreadcumbs> {
328     public void execute(Event<UIBreadcumbs> event) throws Exception {
329       UIBreadcumbs uiBreadcumbs = event.getSource();
330       UIOneTaxonomySelector uiOneNodePathSelector = uiBreadcumbs.getParent();
331       String objectId =  event.getRequestContext().getRequestParameter(OBJECTID);
332       uiBreadcumbs.setSelectPath(objectId);
333       String selectGroupId = uiBreadcumbs.getSelectLocalPath().getId();
334       uiOneNodePathSelector.changeGroup(selectGroupId, event.getRequestContext());
335       event.getRequestContext().addUIComponentToUpdateByAjax(uiOneNodePathSelector);
336     }
337   }
338 }