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.GregorianCalendar;
21  import java.util.List;
22  import java.util.MissingResourceException;
23  import java.util.ResourceBundle;
24  
25  import javax.jcr.Node;
26  import javax.jcr.PropertyType;
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.apache.commons.lang.StringUtils;
33  import org.exoplatform.ecm.webui.utils.Utils;
34  import org.exoplatform.services.cms.metadata.MetadataService;
35  import org.exoplatform.web.application.ApplicationMessage;
36  import org.exoplatform.webui.config.annotation.ComponentConfig;
37  import org.exoplatform.webui.config.annotation.EventConfig;
38  import org.exoplatform.webui.core.UIApplication;
39  import org.exoplatform.webui.core.UIPopupWindow;
40  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
41  import org.exoplatform.webui.event.Event;
42  import org.exoplatform.webui.event.Event.Phase;
43  import org.exoplatform.webui.event.EventListener;
44  import org.exoplatform.webui.form.UIForm;
45  import org.exoplatform.webui.form.input.UICheckBoxInput;
46  
47  /**
48   * Created by The eXo Platform SARL
49   * Author : Dang Van Minh
50   *          minh.dang@exoplatform.com
51   * May 25, 2007 3:58:09 PM
52   */
53  @ComponentConfig(lifecycle = UIFormLifecycle.class, template = "system:/groovy/webui/form/UIForm.gtmpl", events = {
54      @EventConfig(listeners = UIExternalMetadataForm.AddActionListener.class),
55      @EventConfig(phase = Phase.DECODE, listeners = UIExternalMetadataForm.CancelActionListener.class) })
56  public class UIExternalMetadataForm extends UIForm {
57  
58    public UIExternalMetadataForm() throws Exception {
59    }
60  
61    public void renderExternalList() throws Exception {
62      MetadataService metadataService = getApplicationComponent(MetadataService.class) ;
63      UICheckBoxInput uiCheckBox ;
64      for(NodeType nodeType : metadataService.getAllMetadatasNodeType()) {
65        uiCheckBox = new UICheckBoxInput(nodeType.getName(), nodeType.getName(), null) ;
66        if(!isInternalUse(nodeType)) {
67          if(hasExternalMetadata(nodeType.getName())) {
68            uiCheckBox.setChecked(true);
69            uiCheckBox.setDisabled(true) ;
70          } else {
71            uiCheckBox.setChecked(false) ;
72            uiCheckBox.setDisabled(false);
73          }
74          addUIFormInput(uiCheckBox) ;
75        }
76      }
77    }
78  
79    private boolean isInternalUse(NodeType nodeType) throws Exception{
80      for(PropertyDefinition pro : nodeType.getPropertyDefinitions()) {
81        if(pro.getName().equals("exo:internalUse")) {
82          return pro.getDefaultValues()[0].getBoolean();
83        }
84      }
85      return false;
86    }
87  
88    private boolean hasExternalMetadata(String name) throws Exception {
89      UIUploadManager uiUploadManager = getAncestorOfType(UIUploadManager.class) ;
90      UIUploadContainer uiUploadContainer = uiUploadManager.getChild(UIUploadContainer.class) ;
91      Node uploaded = uiUploadContainer.getUploadedNode() ;
92      for(NodeType mixin : uploaded.getMixinNodeTypes()) {
93        if(mixin.getName().equals(name)) return true ;
94      }
95      if(uploaded.hasNode(Utils.JCR_CONTENT)) {
96        for(NodeType mixin : uploaded.getNode(Utils.JCR_CONTENT).getMixinNodeTypes()) {
97          if(mixin.getName().equals(name)) return true ;
98        }
99      }
100     return false ;
101   }
102 
103   public String getLabel(ResourceBundle res, String id)  {
104     try {
105       return res.getString("UIExternalMetadataForm.label." + id) ;
106     } catch (MissingResourceException ex) {
107       return '_' + id ;
108     }
109   }
110 
111   static  public class CancelActionListener extends EventListener<UIExternalMetadataForm> {
112     public void execute(Event<UIExternalMetadataForm> event) throws Exception {
113       UIUploadManager uiUploadManager = event.getSource().getAncestorOfType(UIUploadManager.class) ;
114       UIPopupWindow uiPopup = uiUploadManager.getChildById(UIUploadManager.EXTARNAL_METADATA_POPUP) ;
115       uiPopup.setShow(false) ;
116       uiPopup.setRendered(false) ;
117       event.getRequestContext().addUIComponentToUpdateByAjax(uiUploadManager) ;
118     }
119   }
120 
121   static  public class AddActionListener extends EventListener<UIExternalMetadataForm> {
122     public void execute(Event<UIExternalMetadataForm> event) throws Exception {
123       UIExternalMetadataForm uiExternalMetadataForm = event.getSource() ;
124       List<UICheckBoxInput> listCheckbox =  new ArrayList<UICheckBoxInput>();
125       uiExternalMetadataForm.findComponentOfType(listCheckbox, UICheckBoxInput.class);
126       UIUploadManager uiUploadManager = event.getSource().getAncestorOfType(UIUploadManager.class) ;
127       UIUploadContainer uiContainer = uiUploadManager.getChild(UIUploadContainer.class) ;
128       String metadataName = null ;
129       Node uploadedNode = uiContainer.getUploadedNode() ;
130       for(int i = 0; i < listCheckbox.size(); i ++) {
131         if(listCheckbox.get(i).isChecked() && !listCheckbox.get(i).isDisabled()) {
132           metadataName = listCheckbox.get(i).getName() ;
133           if(!uploadedNode.canAddMixin(metadataName)) {
134             UIApplication uiApp = uiExternalMetadataForm.getAncestorOfType(UIApplication.class) ;
135             uiApp.addMessage(new ApplicationMessage("UIExternalMetadataForm.msg.can-not-add", null,
136                                                     ApplicationMessage.WARNING)) ;
137 
138             return ;
139           }
140           uploadedNode.addMixin(metadataName);
141           createMandatoryPropertyValue(uploadedNode, metadataName);
142           uploadedNode.save() ;
143           UIUploadContent uiUploadContent = uiContainer.getChild(UIUploadContent.class) ;
144           uiUploadContent.externalList_.add(metadataName) ;
145         }
146       }
147       uploadedNode.getSession().save() ;
148       UIPopupWindow uiPopup = uiUploadManager.getChildById(UIUploadManager.EXTARNAL_METADATA_POPUP) ;
149       uiPopup.setShow(false) ;
150       uiPopup.setRendered(false) ;
151       uiContainer.setRenderedChild(UIUploadContent.class) ;
152       event.getRequestContext().addUIComponentToUpdateByAjax(uiUploadManager) ;
153     }
154 
155     private void createMandatoryPropertyValue(Node node, String nodeTypeName) throws Exception {
156       NodeTypeManager nodeTypeManager = node.getSession().getWorkspace().getNodeTypeManager();
157       NodeType nodeType = nodeTypeManager.getNodeType(nodeTypeName);
158       for (PropertyDefinition propertyDefinition : nodeType.getPropertyDefinitions()) {
159         if (propertyDefinition.isMandatory() &&
160             (!propertyDefinition.isAutoCreated() || !node.hasProperty(propertyDefinition.getName()))&&
161             !propertyDefinition.isProtected()) {
162           String propertyName = propertyDefinition.getName();
163           int requiredType = propertyDefinition.getRequiredType();
164           if (!propertyDefinition.isMultiple()) {
165             switch (requiredType) {
166             case PropertyType.STRING:
167               node.setProperty(propertyName, StringUtils.EMPTY);
168               break;
169             case PropertyType.BINARY:
170               node.setProperty(propertyName, "");
171               break;
172             case PropertyType.BOOLEAN:
173               node.setProperty(propertyName, false);
174               break;
175             case PropertyType.LONG:
176               node.setProperty(propertyName, 0);
177               break;
178             case PropertyType.DOUBLE:
179               node.setProperty(propertyName, 0);
180               break;
181             case PropertyType.DATE:
182               node.setProperty(propertyName, new GregorianCalendar());
183               break;
184             case PropertyType.REFERENCE:
185               node.setProperty(propertyName, "");
186               break;
187             }
188           } else {
189             switch (requiredType) {
190             case PropertyType.STRING:
191               node.setProperty(propertyName, new String[] { StringUtils.EMPTY });
192               break;
193             case PropertyType.BINARY:
194               node.setProperty(propertyName, new String[] { "" });
195               break;
196             case PropertyType.BOOLEAN:
197               node.setProperty(propertyName, new Value[] { node.getSession()
198                                                                .getValueFactory()
199                                                                .createValue(false) });
200               break;
201             case PropertyType.LONG:
202               node.setProperty(propertyName, new Value[] { node.getSession()
203                                                                .getValueFactory()
204                                                                .createValue(0L) });
205               break;
206             case PropertyType.DOUBLE:
207               node.setProperty(propertyName, new Value[] { node.getSession()
208                                                                .getValueFactory()
209                                                                .createValue(0) });
210               break;
211             case PropertyType.DATE:
212               node.setProperty(propertyName,
213                                new Value[] { node.getSession()
214                                                  .getValueFactory()
215                                                  .createValue(new GregorianCalendar()) });
216               break;
217             case PropertyType.REFERENCE:
218               node.setProperty(propertyName, new String[] {});
219               break;
220             }
221           }
222         }
223       }
224     }
225   }
226 }