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.popup.admin;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import javax.jcr.AccessDeniedException;
26  import javax.jcr.Node;
27  import javax.jcr.Property;
28  import javax.jcr.PropertyIterator;
29  import javax.jcr.PropertyType;
30  import javax.jcr.Session;
31  import javax.jcr.Value;
32  import javax.jcr.ValueFormatException;
33  import javax.jcr.nodetype.NodeType;
34  import javax.jcr.nodetype.PropertyDefinition;
35  
36  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
37  import org.exoplatform.ecm.webui.utils.PermissionUtil;
38  import org.exoplatform.services.log.ExoLogger;
39  import org.exoplatform.services.log.Log;
40  import org.exoplatform.web.application.ApplicationMessage;
41  import org.exoplatform.webui.config.annotation.ComponentConfig;
42  import org.exoplatform.webui.config.annotation.EventConfig;
43  import org.exoplatform.webui.core.UIApplication;
44  import org.exoplatform.webui.core.UIContainer;
45  import org.exoplatform.webui.event.Event;
46  import org.exoplatform.webui.event.EventListener;
47  
48  /**
49   * Created by The eXo Platform SARL
50   * Author : pham tuan
51   *          phamtuanchip@yahoo.de
52   * September 13, 2006
53   * 10:07:15 AM
54   */
55  
56  @ComponentConfig(
57      template =  "app:/groovy/webui/component/explorer/popup/info/UIPropertyTab.gtmpl",
58      events = {
59          @EventConfig(listeners = UIPropertyTab.CloseActionListener.class),
60          @EventConfig(listeners = UIPropertyTab.EditActionListener.class),
61          @EventConfig(listeners = UIPropertyTab.DeleteActionListener.class, confirm="UIPropertyTab.confirm.remove-property")
62      }
63  )
64  
65  public class UIPropertyTab extends UIContainer {
66  
67    private static String[] PRO_BEAN_FIELD = {"icon", "name", "multiValue", "value", "action"} ;
68    private final static String PRO_KEY_BINARYTYPE = "binary" ;
69    private final static String PRO_KEY_CANNOTGET = "cannotget" ;
70    private static final Log LOG  = ExoLogger.getLogger(UIPropertyTab.class.getName());
71    private Set<String> propertiesName_ = new HashSet<String>();
72  
73    public String[] getBeanFields() { return PRO_BEAN_FIELD ;}
74  
75    public String[] getActions() {return  new String[] {"Close"} ;}
76  
77    private Node getCurrentNode() throws Exception {
78      UIPropertiesManager uiManager = getParent();
79      return uiManager.getCurrentNode();
80    }
81  
82    public PropertyIterator getProperties() throws Exception {
83      return getCurrentNode().getProperties() ;
84    }
85  
86    private Set<String> propertiesName() throws Exception {
87      if(propertiesName_.size() == 0) {
88        Node currentNode = getCurrentNode();
89        NodeType nodetype = currentNode.getPrimaryNodeType() ;
90        Collection<NodeType> types = new ArrayList<NodeType>() ;
91        types.add(nodetype) ;
92        NodeType[] mixins = currentNode.getMixinNodeTypes() ;
93        if (mixins != null) types.addAll(Arrays.asList(mixins)) ;
94        for(NodeType nodeType : types) {
95          for(PropertyDefinition property : nodeType.getPropertyDefinitions()) {
96            propertiesName_.add(property.getName());
97          }
98        }
99      }
100     return propertiesName_;
101   }
102 
103   public boolean addedByUser(String propertyName) throws Exception {
104     if(propertiesName().contains(propertyName)) return false;
105     return true;
106   }
107 
108   public boolean isCanbeRemoved(String propertyName) throws Exception {
109     Property property = getCurrentNode().getProperty(propertyName);
110     if (property == null || !PermissionUtil.canSetProperty(property.getParent()) ||
111         property.getDefinition().isMandatory() || property.getDefinition().isProtected())
112       return false;
113     return true;
114   }
115 
116   public boolean isCanbeEdit(Property property) throws Exception {
117     if(!PermissionUtil.canSetProperty(property.getParent()) ||
118        property.getDefinition().isProtected()) {
119       return false;
120     }
121     return true;
122   }
123 
124   public String getPropertyValue(Property prop) throws Exception {
125     if(prop.getType() == PropertyType.BINARY) return PRO_KEY_BINARYTYPE ;
126     boolean flag = true;
127     try {
128       if(prop.getDefinition() != null && prop.getDefinition().isMultiple()) {
129         Value[] values =  prop.getValues();
130         StringBuilder sB = new StringBuilder();
131         for (int i = 0; i < values.length; i++) {
132           if (prop.getType() == PropertyType.REFERENCE) {
133             String uuid = values[i].getString();
134             Node node = this.getNodeByUUID(uuid);
135             if (node == null) {
136               if (i == 0) flag = false;
137               continue;
138             }
139 
140           }
141           if ((i > 0) && flag)
142             sB.append("; ");
143           sB.append(values[i].getString());
144           flag = true;
145         }
146         return sB.toString();
147       }
148       return prop.getString() ;
149     } catch(ValueFormatException ve) {
150       return PRO_KEY_CANNOTGET ;
151     } catch(Exception e) {
152       return PRO_KEY_CANNOTGET ;
153     }
154   }
155 
156   public Node getNodeByUUID(String uuid) {
157     Node node = null;
158     try {
159       UIJCRExplorer uiExplorer = getAncestorOfType(UIJCRExplorer.class);
160       Session session = uiExplorer.getSession();
161       node = session.getNodeByUUID(uuid);
162     } catch (Exception e) {
163       if (LOG.isWarnEnabled()) {
164         LOG.warn(e.getMessage());
165       }
166     }
167     return node;
168 
169   }
170   static public class CloseActionListener extends EventListener<UIPropertyTab> {
171     public void execute(Event<UIPropertyTab> event) throws Exception {
172       event.getSource().getAncestorOfType(UIJCRExplorer.class).cancelAction() ;
173     }
174   }
175 
176   static public class EditActionListener extends EventListener<UIPropertyTab> {
177     public void execute(Event<UIPropertyTab> event) throws Exception {
178       UIPropertyTab uiPropertyTab = event.getSource();
179       UIPropertiesManager uiManager = uiPropertyTab.getParent();
180       UIApplication uiApp = uiManager.getAncestorOfType(UIApplication.class);
181       UIJCRExplorer uiExplorer = uiManager.getAncestorOfType(UIJCRExplorer.class);
182       Node currentNode = uiExplorer.getCurrentNode();
183       if(!PermissionUtil.canSetProperty(currentNode)) {
184         uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.access-denied", null,
185             ApplicationMessage.WARNING));
186 
187         return;
188       }
189       if(uiExplorer.nodeIsLocked(currentNode)) {
190         Object[] arg = { currentNode.getPath() };
191         uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.node-locked", arg));
192 
193         return;
194       }
195       if(!currentNode.isCheckedOut()) {
196         uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.node-checkedin", null));
197 
198         return;
199       }
200       String propertyName = event.getRequestContext().getRequestParameter(OBJECTID);
201       UIPropertyForm uiForm = uiManager.getChild(UIPropertyForm.class);
202       if(uiForm == null) {
203         uiForm = uiManager.addChild(UIPropertyForm.class, null, null);
204         uiForm.init(currentNode);
205       }
206       uiForm.loadForm(propertyName);
207       uiManager.setIsEditProperty(true);
208       uiManager.setSelectedTab(2);
209     }
210   }
211 
212   static public class DeleteActionListener extends EventListener<UIPropertyTab> {
213     public void execute(Event<UIPropertyTab> event) throws Exception {
214       UIPropertyTab uiPropertyTab = event.getSource();
215       String propertyName = event.getRequestContext().getRequestParameter(OBJECTID);
216       Node currentNode = uiPropertyTab.getCurrentNode();
217       UIApplication uiApp = uiPropertyTab.getAncestorOfType(UIApplication.class);
218       try {
219         if(currentNode.hasProperty(propertyName)) currentNode.getProperty(propertyName).remove();
220         currentNode.save();
221         
222         UIPropertiesManager uiManager = uiPropertyTab.getParent();
223         uiManager.setRenderedChild(UIPropertyTab.class);
224         event.getRequestContext().addUIComponentToUpdateByAjax(uiPropertyTab);
225         return;
226       } catch(AccessDeniedException ace) {
227         uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.access-denied", null,
228             ApplicationMessage.WARNING));
229 
230         return;
231       } catch(Exception e) {
232         if (LOG.isErrorEnabled()) {
233           LOG.error("Unexpected error", e);
234         }
235         return;
236       }
237     }
238   }
239 }