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.upload;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.jcr.Node;
23  import javax.jcr.nodetype.NodeType;
24  import javax.jcr.nodetype.PropertyDefinition;
25  
26  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
27  import org.exoplatform.ecm.webui.utils.Utils;
28  import org.exoplatform.services.cms.metadata.MetadataService;
29  //import org.exoplatform.services.jcr.core.nodetype.ExtendedNodeType;
30  import org.exoplatform.web.application.ApplicationMessage;
31  import org.exoplatform.webui.config.annotation.ComponentConfig;
32  import org.exoplatform.webui.config.annotation.EventConfig;
33  import org.exoplatform.webui.core.UIApplication;
34  import org.exoplatform.webui.core.UIContainer;
35  import org.exoplatform.webui.event.Event;
36  import org.exoplatform.webui.event.EventListener;
37  
38  /**
39   * Created by The eXo Platform SARL
40   * Author : Dang Van Minh
41   *          minh.dang@exoplatform.com
42   * May 24, 2007 5:48:57 PM
43   */
44  @ComponentConfig(
45      template = "app:/groovy/webui/component/explorer/upload/UIUploadContent.gtmpl",
46      events = {
47          @EventConfig(listeners = UIUploadContent.EditActionListener.class),
48          @EventConfig(listeners = UIUploadContent.ManageMetadataActionListener.class)
49      }
50  )
51  public class UIUploadContent extends UIContainer {
52  
53    private String[] arrValues_ ;
54    public List<String> externalList_ = new ArrayList<String>() ;
55  
56    private List<String[]> listArrValues_ = new ArrayList<String[]>();
57    private boolean isExternalMetadata = false;
58  
59    public UIUploadContent() throws Exception {
60    }
61  
62    public Node getUploadedNode() { return ((UIUploadContainer)getParent()).getUploadedNode() ; }
63  
64    public List<Node> getListUploadedNode() {
65      return ((UIUploadContainer)getParent()).getListUploadedNode();
66    }
67  
68    public List<String> getExternalList() throws Exception {
69      NodeType[] mixinTypes = getUploadedNode().getMixinNodeTypes();
70      for (NodeType nodeType : mixinTypes) {
71        if (nodeType.getName().equals(Utils.EXO_METADATA) && isExternalUse(nodeType)
72            && !externalList_.contains(nodeType.getName())) {
73          externalList_.add(nodeType.getName());
74        }
75        for (NodeType superType : nodeType.getSupertypes()) {
76          if (superType.getName().equals(Utils.EXO_METADATA) && isExternalUse(nodeType)
77              && !externalList_.contains(nodeType.getName())) {
78            externalList_.add(nodeType.getName());
79          }
80        }
81      }
82      if (getUploadedNode().hasNode(Utils.JCR_CONTENT)) {
83        for (NodeType nodeType : getUploadedNode().getNode(Utils.JCR_CONTENT).getMixinNodeTypes()) {
84          if (nodeType.isNodeType(Utils.EXO_METADATA) && isExternalUse(nodeType)
85              && !externalList_.contains(nodeType.getName())) {
86            externalList_.add(nodeType.getName());
87          }
88        }
89      }
90  
91      return externalList_ ;
92    }
93  
94    private boolean isExternalUse(NodeType nodeType) throws Exception{
95      for(PropertyDefinition pro : nodeType.getPropertyDefinitions()) {
96        if(pro.getName().equals("exo:internalUse")) {
97          return pro.getDefaultValues()[0].getBoolean();
98        }
99      }
100     return false;
101   }
102 
103   public String[] arrUploadValues() { return arrValues_ ; }
104 
105   public void setUploadValues(String[] arrValues) { arrValues_ = arrValues ; }
106 
107   public List<String[]> listUploadValues() {
108     return listArrValues_;
109   }
110 
111   public void setListUploadValues(List<String[]> listArrValues) {
112     listArrValues_ = listArrValues;
113   }
114 
115   public void setIsExternalMetadata(boolean isExternal) {
116     isExternalMetadata = isExternal;
117   }
118 
119   public boolean getIsExternalMetadata() {
120     return isExternalMetadata;
121   }
122 
123   static public class EditActionListener extends EventListener<UIUploadContent> {
124     public void execute(Event<UIUploadContent> event) throws Exception {
125       UIUploadContent uiUploadContent = event.getSource() ;
126       UIUploadContainer uiUploadContainer = uiUploadContent.getParent() ;
127       MetadataService metadataService = uiUploadContent.getApplicationComponent(MetadataService.class) ;
128       UIJCRExplorer uiExplorer = uiUploadContent.getAncestorOfType(UIJCRExplorer.class) ;
129       String nodeType = event.getRequestContext().getRequestParameter(OBJECTID) ;
130       String template = metadataService.getMetadataTemplate(nodeType, true) ;
131       if(template == null || template.trim().length() == 0) {
132         UIApplication uiApp = uiUploadContent.getAncestorOfType(UIApplication.class) ;
133         Object[] args = {nodeType} ;
134         uiApp.addMessage(new ApplicationMessage("UIUploadContent.msg.has-not-template", args,
135                          ApplicationMessage.WARNING)) ;
136         
137         return ;
138       }
139       uiUploadContainer.removeChild(UIAddMetadataForm.class) ;
140       UIAddMetadataForm uiAddMetadataForm =
141         uiUploadContainer.createUIComponent(UIAddMetadataForm.class, null, null) ;
142       uiAddMetadataForm.getChildren().clear() ;
143       uiAddMetadataForm.setNodeType(nodeType) ;
144       uiAddMetadataForm.setIsNotEditNode(true) ;
145       Node currentNode = uiExplorer.getCurrentNode() ;
146       uiAddMetadataForm.setWorkspace(currentNode.getSession().getWorkspace().getName()) ;
147       uiAddMetadataForm.setStoredPath(currentNode.getPath()) ;
148       uiAddMetadataForm.setChildPath(uiUploadContainer.getEditNode(nodeType).getPath()) ;
149       uiUploadContainer.addChild(uiAddMetadataForm) ;
150       uiUploadContainer.setRenderedChild(UIAddMetadataForm.class) ;
151       event.getRequestContext().addUIComponentToUpdateByAjax(uiUploadContainer) ;
152     }
153   }
154 
155   static public class ManageMetadataActionListener extends EventListener<UIUploadContent> {
156     public void execute(Event<UIUploadContent> event) throws Exception {
157       UIUploadContent uiUploadContent = event.getSource() ;
158       UIUploadContainer uiUploadContainer = uiUploadContent.getParent() ;
159       MetadataService metadataService = uiUploadContent.getApplicationComponent(MetadataService.class) ;
160       UIJCRExplorer uiExplorer = uiUploadContent.getAncestorOfType(UIJCRExplorer.class);
161       uiUploadContent.setIsExternalMetadata(true);
162       String uploadedNodePath = event.getRequestContext().getRequestParameter(OBJECTID);
163       Node uploadedNode = (Node) uiExplorer.getCurrentNode().getSession().getItem(uploadedNodePath);
164       uiUploadContainer.setUploadedNode(uploadedNode);
165       String nodeType = "dc:elementSet";
166       if(uploadedNode.hasNode(Utils.JCR_CONTENT)) {
167         for(NodeType itemNodeType : uploadedNode.getNode(Utils.JCR_CONTENT).getMixinNodeTypes()) {
168           if(itemNodeType.isNodeType(Utils.EXO_METADATA) && uiUploadContent.isExternalUse(itemNodeType)) {
169             nodeType = itemNodeType.getName();
170           }
171         }
172       }
173       String template = metadataService.getMetadataTemplate(nodeType, true) ;
174       if(template == null || template.trim().length() == 0) {
175         UIApplication uiApp = uiUploadContent.getAncestorOfType(UIApplication.class) ;
176         Object[] args = {nodeType} ;
177         uiApp.addMessage(new ApplicationMessage("UIUploadContent.msg.has-not-template", args,
178                          ApplicationMessage.WARNING)) ;
179         
180         return ;
181       }
182       uiUploadContainer.setActions(new String[] {"AddMetadata","Close"});
183       UIAddMetadataForm uiAddMetadataForm = uiUploadContainer.getChild(UIAddMetadataForm.class);
184       if (uiAddMetadataForm != null) uiUploadContainer.setRenderedChild(UIAddMetadataForm.class);
185       UIListMetadata uiListMetadata = uiUploadContainer.getChild(UIListMetadata.class);
186       if (uiListMetadata==null) {
187         uiListMetadata = uiUploadContainer.createUIComponent(UIListMetadata.class, null, null) ;
188         uiUploadContainer.addChild(uiListMetadata);
189       }
190       uiListMetadata.setIsExternalMetadata(true);
191       uiUploadContainer.setRenderedChild(UIListMetadata.class) ;
192       event.getRequestContext().addUIComponentToUpdateByAjax(uiUploadContainer) ;
193     }
194   }
195 }