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.search;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.ResourceBundle;
22  
23  import javax.jcr.Node;
24  import javax.jcr.NodeIterator;
25  import javax.jcr.Session;
26  import javax.jcr.nodetype.NodeType;
27  import javax.jcr.nodetype.NodeTypeManager;
28  import javax.jcr.nodetype.PropertyDefinition;
29  
30  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
31  import org.exoplatform.services.cms.BasePath;
32  import org.exoplatform.services.cms.impl.DMSConfiguration;
33  import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
34  import org.exoplatform.services.log.ExoLogger;
35  import org.exoplatform.services.log.Log;
36  import org.exoplatform.webui.config.annotation.ComponentConfig;
37  import org.exoplatform.webui.config.annotation.EventConfig;
38  import org.exoplatform.webui.core.UIPopupComponent;
39  import org.exoplatform.webui.core.UIPopupContainer;
40  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
41  import org.exoplatform.webui.core.model.SelectItemOption;
42  import org.exoplatform.webui.event.Event;
43  import org.exoplatform.webui.event.Event.Phase;
44  import org.exoplatform.webui.event.EventListener;
45  import org.exoplatform.webui.form.UIForm;
46  import org.exoplatform.webui.form.UIFormRadioBoxInput;
47  import org.exoplatform.webui.form.UIFormSelectBox;
48  
49  /**
50   * Created by The eXo Platform SARL
51   * Author : Tran The Trong
52   *          trong.tran@exoplatform.com
53   * May 6, 2007
54   * 10:18:56 AM
55   */
56  @ComponentConfig(
57      lifecycle = UIFormLifecycle.class,
58      template =  "system:/groovy/webui/form/UIForm.gtmpl",
59      events = {
60        @EventConfig(phase=Phase.DECODE, listeners = UISelectPropertyForm.CancelActionListener.class),
61        @EventConfig(listeners = UISelectPropertyForm.AddActionListener.class),
62        @EventConfig(listeners = UISelectPropertyForm.ChangeMetadataTypeActionListener.class)
63      }
64  )
65  public class UISelectPropertyForm extends UIForm implements UIPopupComponent {
66  
67    private static final Log LOG = ExoLogger.getLogger(UISelectPropertyForm.class.getName());
68  
69    final static public String METADATA_TYPE= "metadataType" ;
70    final static public String PROPERTY = "property" ;
71  
72    private String fieldName_ = null ;
73  
74    private List<SelectItemOption<String>> properties_ = new ArrayList<SelectItemOption<String>>() ;
75  
76    public UISelectPropertyForm() throws Exception {
77      setActions(new String[] {"Add", "Cancel"}) ;
78    }
79  
80    public String getLabel(ResourceBundle res, String id)  {
81      try {
82        return super.getLabel(res, id) ;
83      } catch (Exception ex) {
84        return id ;
85      }
86    }
87  
88    public void activate() {
89      try {
90        List<SelectItemOption<String>> options = new ArrayList<SelectItemOption<String>>() ;
91        NodeHierarchyCreator nodeHierarchyCreator = getApplicationComponent(NodeHierarchyCreator.class) ;
92        UIFormSelectBox uiSelect = new UIFormSelectBox(METADATA_TYPE, METADATA_TYPE, options) ;
93        uiSelect.setOnChange("ChangeMetadataType") ;
94        addUIFormInput(uiSelect) ;
95        String metadataPath = nodeHierarchyCreator.getJcrPath(BasePath.METADATA_PATH) ;
96        UIJCRExplorer uiExplorer = getAncestorOfType(UIJCRExplorer.class) ;
97        DMSConfiguration dmsConfiguration = getApplicationComponent(DMSConfiguration.class);
98        String workspaceName = dmsConfiguration.getConfig().getSystemWorkspace();
99        Session session = uiExplorer.getSystemProvider().getSession(workspaceName, uiExplorer.getRepository());
100       Node homeNode = (Node) session.getItem(metadataPath) ;
101       NodeIterator nodeIter = homeNode.getNodes() ;
102       Node meta = nodeIter.nextNode() ;
103       renderProperties(meta.getName()) ;
104       options.add(new SelectItemOption<String>(meta.getName(), meta.getName())) ;
105       while(nodeIter.hasNext()) {
106         meta = nodeIter.nextNode() ;
107         options.add(new SelectItemOption<String>(meta.getName(), meta.getName())) ;
108       }
109       addUIFormInput(new UIFormRadioBoxInput(PROPERTY, null, properties_).
110                          setAlign(UIFormRadioBoxInput.VERTICAL_ALIGN)) ;
111     } catch (Exception e) {
112       if (LOG.isErrorEnabled()) {
113         LOG.error("Unexpected error!", e.getMessage());
114       }
115     }
116   }
117 
118   public void deActivate() {}
119 
120   public void setFieldName(String fieldName) { fieldName_ = fieldName ; }
121 
122   public void renderProperties(String metadata) throws Exception {
123     properties_.clear() ;
124     UIJCRExplorer uiExpolrer = getAncestorOfType(UIJCRExplorer.class) ;
125     NodeTypeManager ntManager = uiExpolrer.getSession().getWorkspace().getNodeTypeManager() ;
126     NodeType nt = ntManager.getNodeType(metadata) ;
127     PropertyDefinition[] properties = nt.getPropertyDefinitions() ;
128     for(PropertyDefinition property : properties) {
129       String name = property.getName() ;
130       if(!name.equals("exo:internalUse")) properties_.add(new SelectItemOption<String>(name, name)) ;
131     }
132   }
133 
134   static  public class CancelActionListener extends EventListener<UISelectPropertyForm> {
135     public void execute(Event<UISelectPropertyForm> event) throws Exception {
136       UISearchContainer uiSearchContainer = event.getSource().getAncestorOfType(UISearchContainer.class) ;
137       UIPopupContainer uiPopup = uiSearchContainer.getChild(UIPopupContainer.class) ;
138       uiPopup.deActivate() ;
139       event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup) ;
140     }
141   }
142 
143   static  public class AddActionListener extends EventListener<UISelectPropertyForm> {
144     public void execute(Event<UISelectPropertyForm> event) throws Exception {
145       UISelectPropertyForm uiForm = event.getSource() ;
146       String property = uiForm.<UIFormRadioBoxInput>getUIInput(PROPERTY).getValue();
147       UIPopupContainer UIPopupContainer = uiForm.getAncestorOfType(UIPopupContainer.class);
148       UISearchContainer uiSearchContainer = UIPopupContainer.getParent() ;
149       UIConstraintsForm uiConstraintsForm =
150         uiSearchContainer.findFirstComponentOfType(UIConstraintsForm.class) ;
151       /* Set value for textbox */
152       uiConstraintsForm.getUIStringInput(uiForm.fieldName_).setValue(property) ;
153       /*  Set value of checkbox is checked when choose value of property */
154       if (uiForm.fieldName_.equals(UIConstraintsForm.PROPERTY1)) {
155         uiConstraintsForm.getUICheckBoxInput(UIConstraintsForm.EXACTLY_PROPERTY).setChecked(true);
156       } else if (uiForm.fieldName_.equals(UIConstraintsForm.PROPERTY2)) {
157         uiConstraintsForm.getUICheckBoxInput(UIConstraintsForm.CONTAIN_PROPERTY).setChecked(true);
158       } else if (uiForm.fieldName_.equals(UIConstraintsForm.PROPERTY3)) {
159         uiConstraintsForm.getUICheckBoxInput(UIConstraintsForm.NOT_CONTAIN_PROPERTY).setChecked(true);
160       }
161       UIPopupContainer.deActivate() ;
162       event.getRequestContext().addUIComponentToUpdateByAjax(UIPopupContainer) ;
163       event.getRequestContext().addUIComponentToUpdateByAjax(uiConstraintsForm) ;
164     }
165   }
166 
167   static  public class ChangeMetadataTypeActionListener extends EventListener<UISelectPropertyForm> {
168     public void execute(Event<UISelectPropertyForm> event) throws Exception {
169       UISelectPropertyForm uiForm = event.getSource() ;
170       uiForm.renderProperties(uiForm.getUIFormSelectBox(METADATA_TYPE).getValue()) ;
171       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm) ;
172     }
173   }
174 }