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.Collections;
21  import java.util.List;
22  
23  import javax.jcr.Node;
24  import javax.jcr.NodeIterator;
25  import javax.jcr.Property;
26  import javax.jcr.Value;
27  import javax.jcr.query.QueryResult;
28  
29  import org.exoplatform.webui.core.UIPopupComponent;
30  import org.exoplatform.webui.core.UIPopupContainer;
31  import org.exoplatform.webui.config.annotation.ComponentConfig;
32  import org.exoplatform.webui.config.annotation.EventConfig;
33  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
34  import org.exoplatform.webui.core.model.SelectItemOption;
35  import org.exoplatform.webui.event.Event;
36  import org.exoplatform.webui.event.EventListener;
37  import org.exoplatform.webui.event.Event.Phase;
38  import org.exoplatform.webui.form.UIForm;
39  import org.exoplatform.webui.form.UIFormSelectBox;
40  import org.exoplatform.webui.form.UIFormStringInput;
41  import org.exoplatform.webui.form.validator.MandatoryValidator;
42  
43  /**
44   * Created by The eXo Platform SARL
45   * Author : Dang Van Minh
46   *          minh.dang@exoplatform.com
47   * May 6, 2007
48   * 10:18:56 AM
49   */
50  @ComponentConfig(
51      lifecycle = UIFormLifecycle.class,
52      template =  "app:/groovy/webui/component/explorer/search/UICompareExactlyForm.gtmpl",
53      events = {
54        @EventConfig(listeners = UICompareExactlyForm.SelectActionListener.class),
55        @EventConfig(phase=Phase.DECODE, listeners = UICompareExactlyForm.CancelActionListener.class)
56      }
57  )
58  public class UICompareExactlyForm extends UIForm implements UIPopupComponent {
59    private static final String FILTER = "filter" ;
60    private static final String RESULT = "result";
61    private static final String TEMP_RESULT = "tempSel";
62    private List<String> listValue_ ;
63  
64    public UICompareExactlyForm() throws Exception {}
65  
66    public void activate() {}
67    public void deActivate() {}
68  
69    public void init(String properties, QueryResult result) throws Exception {
70      listValue_ = new ArrayList<String>() ;
71      List<SelectItemOption<String>> opts = new ArrayList<SelectItemOption<String>>();
72      addUIFormInput(new UIFormStringInput(FILTER, FILTER, null)) ;
73      addUIFormInput(new UIFormSelectBox(RESULT, RESULT, opts).setSize(15).addValidator(MandatoryValidator.class)) ;
74      addUIFormInput(new UIFormSelectBox(TEMP_RESULT, TEMP_RESULT, opts)) ;
75  
76      NodeIterator iter = result.getNodes() ;
77      String[] props = {} ;
78      if(properties.indexOf(",") > -1) props = properties.split(",") ;
79      while(iter.hasNext()) {
80        Node node = iter.nextNode() ;
81        if(props.length > 0) {
82          for(String pro : props) {
83            if(node.hasProperty(pro)) {
84              Property property = node.getProperty(pro) ;
85              setPropertyResult(property) ;
86            }
87          }
88        } else {
89          if(node.hasProperty(properties)) {
90            Property property = node.getProperty(properties) ;
91            setPropertyResult(property) ;
92          }
93        }
94      }
95      Collections.sort(listValue_) ;
96      for(String value : listValue_) {
97        opts.add(new SelectItemOption<String>(value, value)) ;
98      }
99    }
100 
101   public void setPropertyResult(Property property) throws Exception {
102     if(property.getDefinition().isMultiple()) {
103       Value[] values = property.getValues() ;
104       for(Value value : values) {
105         if(!listValue_.contains(value.getString())) listValue_.add(value.getString()) ;
106       }
107     } else {
108       Value value = property.getValue() ;
109       if(!listValue_.contains(value.getString())) listValue_.add(value.getString()) ;
110     }
111   }
112 
113   static  public class CancelActionListener extends EventListener<UICompareExactlyForm> {
114     public void execute(Event<UICompareExactlyForm> event) throws Exception {
115       UISearchContainer uiSearchContainer = event.getSource().getAncestorOfType(UISearchContainer.class) ;
116       UIPopupContainer uiPopup = uiSearchContainer.getChild(UIPopupContainer.class) ;
117       uiPopup.deActivate() ;
118       event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup) ;
119     }
120   }
121 
122   static  public class SelectActionListener extends EventListener<UICompareExactlyForm> {
123     public void execute(Event<UICompareExactlyForm> event) throws Exception {
124       UICompareExactlyForm uiForm = event.getSource() ;
125       String value = uiForm.getUIFormSelectBox(RESULT).getValue();
126       UIPopupContainer uiPopupAction = uiForm.getAncestorOfType(UIPopupContainer.class);
127       UISearchContainer uiSearchContainer = uiPopupAction.getParent() ;
128       UIConstraintsForm uiConstraintsForm =
129         uiSearchContainer.findFirstComponentOfType(UIConstraintsForm.class) ;
130       uiConstraintsForm.getUIStringInput(UIConstraintsForm.CONTAIN_EXACTLY).setValue(value) ;
131       uiPopupAction.deActivate() ;
132       event.getRequestContext().addUIComponentToUpdateByAjax(uiPopupAction) ;
133       event.getRequestContext().addUIComponentToUpdateByAjax(uiConstraintsForm) ;
134     }
135   }
136 }