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.component.explorer.versions;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.jcr.Node;
23  import javax.jcr.Property;
24  import javax.jcr.PropertyIterator;
25  import javax.jcr.PropertyType;
26  import javax.jcr.Session;
27  import javax.jcr.Value;
28  import javax.jcr.nodetype.NodeType;
29  import javax.jcr.nodetype.NodeTypeManager;
30  import javax.jcr.nodetype.PropertyDefinition;
31  
32  import org.exoplatform.container.PortalContainer;
33  import org.exoplatform.ecm.webui.component.explorer.UIDocumentWorkspace;
34  import org.exoplatform.services.jcr.RepositoryService;
35  import org.exoplatform.services.jcr.core.ManageableRepository;
36  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
37  import org.exoplatform.webui.config.annotation.ComponentConfig;
38  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
39  import org.exoplatform.webui.form.UIForm;
40  
41  /**
42   * Created by The eXo Platform SARL
43   * Author : Le Bien Thuy
44   *          lebienthuy@gmail.com
45   * Oct 20, 2006
46   */
47  
48  @ComponentConfig(
49      lifecycle = UIFormLifecycle.class,
50      template = "app:/groovy/webui/component/explorer/versions/UINodeProperty.gtmpl"
51  )
52  public class UINodeProperty extends UIForm{
53  
54    public UINodeProperty() {}
55  
56    public List<Property> getVersionedNodeProperties() throws Exception{
57      RepositoryService repositoryService =
58        (RepositoryService)PortalContainer.getComponent(RepositoryService.class) ;
59      List<Property> list = new ArrayList<Property>() ;
60      NodeTypeManager nodeTypeManager = repositoryService.getCurrentRepository().getNodeTypeManager();
61      NodeType jcrFrozenNode = nodeTypeManager.getNodeType("nt:frozenNode") ;
62      NodeType ntVersion = nodeTypeManager.getNodeType("nt:version") ;
63      NodeType ntVersionHistory = nodeTypeManager.getNodeType("nt:versionHistory") ;
64      NodeType mixVersionable = nodeTypeManager.getNodeType("mix:versionable") ;
65      UIVersionInfo uiVersionInfo = getAncestorOfType(UIDocumentWorkspace.class).getChild(UIVersionInfo.class) ;
66      Node frozenNode = uiVersionInfo.getCurrentVersionNode().getNode("jcr:frozenNode") ;
67      for(PropertyIterator propertyIter = frozenNode.getProperties(); propertyIter.hasNext() ;) {
68        Property property = propertyIter.nextProperty() ;
69        boolean isDefinition = false ;
70        for(PropertyDefinition propDef : jcrFrozenNode.getPropertyDefinitions()) {
71          if(propDef.getName().equals(property.getName())) isDefinition = true ;
72        }
73        for(PropertyDefinition propDef : ntVersion.getPropertyDefinitions()) {
74          if(propDef.getName().equals(property.getName())) isDefinition = true ;
75        }
76        for(PropertyDefinition propDef : ntVersionHistory.getPropertyDefinitions()) {
77          if(propDef.getName().equals(property.getName())) isDefinition = true ;
78        }
79        for(PropertyDefinition propDef : mixVersionable.getPropertyDefinitions()) {
80          if(propDef.getName().equals(property.getName())) isDefinition = true ;
81        }
82        if(!isDefinition) list.add(property) ;
83      }
84      return list ;
85    }
86  
87    public String getPropertyValue(Property property) throws Exception{
88      switch(property.getType()) {
89      case PropertyType.BINARY: return Integer.toString(PropertyType.BINARY) ;
90      case PropertyType.BOOLEAN :return Boolean.toString(property.getValue().getBoolean()) ;
91      case PropertyType.DATE : return property.getValue().getDate().getTime().toString() ;
92      case PropertyType.DOUBLE : return Double.toString(property.getValue().getDouble()) ;
93      case PropertyType.LONG : return Long.toString(property.getValue().getLong()) ;
94      case PropertyType.NAME : return property.getValue().getString() ;
95      case PropertyType.STRING : return property.getValue().getString() ;
96      case PropertyType.REFERENCE : {
97        if(property.getName().equals("exo:category") || property.getName().equals("exo:relation")) {
98          Session session = getSystemSession() ;
99          Node referenceNode = session.getNodeByUUID(property.getValue().getString()) ;
100         String path = referenceNode.getPath();
101         return path ;
102       }
103       return property.getValue().getString() ;
104     }
105     }
106     return null ;
107   }
108 
109 
110   public List<String> getPropertyMultiValues(Property property) throws Exception {
111     String propName = property.getName() ;
112     if(propName.equals("exo:category")) return getCategoriesValues(property) ;
113     else if(propName.equals("exo:relation")) return getRelationValues(property) ;
114     List<String> values = new ArrayList<String>() ;
115     for(Value value:property.getValues()) {
116       values.add(value.getString()) ;
117     }
118     return values ;
119   }
120 
121   public boolean isMultiValue(Property prop) throws Exception{
122     PropertyDefinition propDef = prop.getDefinition() ;
123     return propDef.isMultiple() ;
124   }
125 
126   private List<String> getReferenceValues(Property property) throws Exception {
127     Session session = getSystemSession() ;
128     List<String> pathList = new ArrayList<String>() ;
129     Value[] values = property.getValues() ;
130     for(Value value:values) {
131       Node referenceNode = session.getNodeByUUID(value.getString()) ;
132       pathList.add(referenceNode.getPath()) ;
133     }
134     return pathList ;
135   }
136 
137   private List<String> getRelationValues(Property relationProp) throws Exception {
138     return getReferenceValues(relationProp) ;
139   }
140 
141   private List<String> getCategoriesValues(Property categoryProp) throws Exception {
142     return getReferenceValues(categoryProp) ;
143   }
144 
145   private Session getSystemSession() throws Exception {
146     ManageableRepository manageableRepository = getApplicationComponent(RepositoryService.class).getCurrentRepository();
147     String systemWorksapce = manageableRepository.getConfiguration().getDefaultWorkspaceName();
148     Session session = WCMCoreUtils.getSystemSessionProvider().getSession(systemWorksapce, manageableRepository) ;
149     return session ;
150   }
151 }