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.admin.queries;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.jcr.AccessDeniedException;
23  import javax.jcr.Node;
24  import javax.jcr.Value;
25  import javax.jcr.query.InvalidQueryException;
26  import javax.jcr.query.Query;
27  
28  import org.exoplatform.ecm.webui.form.UIFormInputSetWithAction;
29  import org.exoplatform.ecm.webui.form.validator.ECMNameValidator;
30  import org.exoplatform.ecm.webui.selector.UISelectable;
31  import org.exoplatform.ecm.webui.utils.Utils;
32  import org.exoplatform.services.cms.queries.QueryService;
33  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
34  import org.exoplatform.web.application.ApplicationMessage;
35  import org.exoplatform.webui.application.WebuiRequestContext;
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.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.UIFormSelectBox;
47  import org.exoplatform.webui.form.UIFormStringInput;
48  import org.exoplatform.webui.form.UIFormTextAreaInput;
49  import org.exoplatform.webui.form.input.UICheckBoxInput;
50  import org.exoplatform.webui.form.validator.MandatoryValidator;
51  
52  /**
53   * Created by The eXo Platform SARL
54   * Author : Dang Van Minh
55   *          minh.dang@exoplatform.com
56   * Dec 29, 2006
57   * 11:30:29 AM
58   */
59  @ComponentConfig(
60      lifecycle = UIFormLifecycle.class,
61      template = "system:/groovy/webui/form/UIForm.gtmpl",
62      events = {
63        @EventConfig(listeners = UIQueriesForm.SaveActionListener.class),
64        @EventConfig(phase = Phase.DECODE, listeners = UIQueriesForm.CancelActionListener.class),
65        @EventConfig(phase = Phase.DECODE, listeners = UIQueriesForm.ChangeQueryTypeActionListener.class),
66        @EventConfig(phase = Phase.DECODE, listeners = UIQueriesForm.AddPermissionActionListener.class)
67      }
68  )
69  public class UIQueriesForm extends UIForm implements UISelectable {
70  
71    final static public String QUERY_NAME = "name" ;
72    final static public String QUERY_TYPE = "type" ;
73    final static public String STATEMENT = "statement" ;
74    final static public String PERMISSIONS = "permissions" ;
75    final static public String CACHE_RESULT = "cache" ;
76    final static public String[] ACTIONS = {"Save", "Cancel"} ;
77    final static public String SQL_QUERY = "select * from nt:file where jcr:path like '/Documents/Live/%'" ;
78    final static public String XPATH_QUERY = "/jcr:root/Documents/Live//element(*, nt:file)" ;
79    final static public String[] REG_EXPRESSION = {"[", "]", ":", "&"} ;
80  
81    private boolean isAddNew_ = false ;
82  
83    public UIQueriesForm() throws Exception {
84      addUIFormInput(new UIFormStringInput(QUERY_NAME, QUERY_NAME, null).
85        addValidator(MandatoryValidator.class).addValidator(ECMNameValidator.class)) ;
86      List<SelectItemOption<String>> ls = new ArrayList<SelectItemOption<String>>() ;
87      ls.add(new SelectItemOption<String>("xPath", "xpath")) ;
88      ls.add(new SelectItemOption<String>("SQL", "sql")) ;
89      UIFormSelectBox uiSelectBox = new UIFormSelectBox(QUERY_TYPE, QUERY_TYPE, ls) ;
90      uiSelectBox.setOnChange("ChangeQueryType") ;
91      addUIFormInput(uiSelectBox) ;
92      UIFormTextAreaInput uiStatement = new UIFormTextAreaInput(STATEMENT, STATEMENT, null) ;
93      uiStatement.setValue(XPATH_QUERY) ;
94      uiStatement.addValidator(MandatoryValidator.class) ;
95      addUIFormInput(uiStatement) ;
96      addUIFormInput(new UICheckBoxInput(CACHE_RESULT, CACHE_RESULT, null)) ;
97      UIFormInputSetWithAction uiInputAct = new UIFormInputSetWithAction("PermissionButton") ;
98      uiInputAct.addUIFormInput(new UIFormStringInput(PERMISSIONS, PERMISSIONS, null).setDisabled(true)
99                                                                                     .addValidator(MandatoryValidator.class));
100     uiInputAct.setActionInfo(PERMISSIONS, new String[] {"AddPermission"}) ;
101     addUIComponentInput(uiInputAct) ;
102   }
103 
104   public String[] getActions() { return ACTIONS ; }
105 
106   public void doSelect(String selectField, Object value) {
107     getUIStringInput(selectField).setValue(value.toString());
108     UIQueriesManager uiManager = getAncestorOfType(UIQueriesManager.class);
109     UIPopupWindow uiPopup = uiManager.getChildById("PermissionPopup");
110     uiPopup.setShowMask(true);
111     uiManager.removeChildById("PermissionPopup");
112   }
113 
114   public void setIsAddNew(boolean isAddNew) { isAddNew_ = isAddNew ; }
115 
116   public void update(String queryName)throws Exception{
117     isAddNew_ = false ;
118     QueryService queryService = getApplicationComponent(QueryService.class) ;
119     if(queryName == null) {
120       isAddNew_ = true ;
121       reset() ;
122       return ;
123     }
124     Node query = queryService.getSharedQuery(queryName, WCMCoreUtils.getSystemSessionProvider());
125     getUIStringInput(QUERY_NAME).setValue(queryName) ;
126     getUIStringInput(QUERY_NAME).setDisabled(true);
127     if(query.hasProperty("exo:cachedResult")) {
128       getUICheckBoxInput(CACHE_RESULT).setChecked(query.getProperty("exo:cachedResult").getBoolean()) ;
129     } else {
130       getUICheckBoxInput(CACHE_RESULT).setChecked(false) ;
131     }
132     if(query.hasProperty("jcr:statement")) {
133       getUIFormTextAreaInput(STATEMENT).setValue(query.getProperty("jcr:statement").getString()) ;
134     }
135     if(query.hasProperty("jcr:language")) {
136       getUIFormSelectBox(QUERY_TYPE).setValue(query.getProperty("jcr:language").getString()) ;
137     }
138     if(query.hasProperty("exo:accessPermissions")) {
139       Value[] values = query.getProperty("exo:accessPermissions").getValues() ;
140       StringBuilder strValues = new StringBuilder() ;
141       for(int i = 0; i < values.length; i ++) {
142         if(strValues.length() > 0) strValues = strValues.append(",") ;
143         strValues = strValues.append(values[i].getString()) ;
144       }
145       getUIStringInput(PERMISSIONS).setValue(strValues.toString()) ;
146     }
147   }
148 
149   static public class CancelActionListener extends EventListener<UIQueriesForm> {
150     public void execute(Event<UIQueriesForm> event) throws Exception {
151       UIQueriesForm uiForm = event.getSource() ;
152       UIQueriesManager uiManager = uiForm.getAncestorOfType(UIQueriesManager.class) ;
153       uiManager.removeChildById(UIQueriesList.ST_ADD) ;
154       uiManager.removeChildById(UIQueriesList.ST_EDIT);
155       event.getRequestContext().getJavascriptManager()
156               .require("SHARED/jquery", "gj")
157               .addScripts("gj(document).ready(function() { gj(\"*[rel='tooltip']\").tooltip();});");
158       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
159     }
160   }
161 
162   static public class SaveActionListener extends EventListener<UIQueriesForm> {
163     public void execute(Event<UIQueriesForm> event) throws Exception {
164       UIQueriesForm uiForm = event.getSource() ;
165       QueryService queryService = uiForm.getApplicationComponent(QueryService.class) ;
166       UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class) ;
167       String queryName = uiForm.getUIStringInput(QUERY_NAME).getValue().trim();
168       if(uiForm.isAddNew_) {
169         for(Node queryNode : queryService.getSharedQueries(WCMCoreUtils.getSystemSessionProvider())) {
170           if(queryNode.getName().equals(queryName)) {
171             uiApp.addMessage(new ApplicationMessage("UIQueriesForm.msg.name-existing", null,
172                 ApplicationMessage.WARNING)) ;            
173             return ;
174           }
175         }
176       }
177      
178       String statement = uiForm.getUIFormTextAreaInput(STATEMENT).getValue() ;
179       UIFormInputSetWithAction permField = uiForm.getChildById("PermissionButton") ;
180       String permissions = permField.getUIStringInput(PERMISSIONS).getValue() ;
181       if((permissions == null)||(permissions.trim().length() == 0)) {
182         uiApp.addMessage(new ApplicationMessage("UIQueriesForm.msg.permission-require", null,
183                                                 ApplicationMessage.WARNING)) ;        
184         return ;
185       }
186       String queryType = uiForm.getUIFormSelectBox(QUERY_TYPE).getValue() ;
187       boolean cacheResult = uiForm.getUICheckBoxInput(CACHE_RESULT).isChecked() ;
188       try {
189         if(permissions.indexOf(",") > -1) {
190           queryService.addSharedQuery(queryName,
191                                       statement,
192                                       queryType,
193                                       permissions.split(","),
194                                       cacheResult);
195         } else {
196           queryService.addSharedQuery(queryName,
197                                       statement,
198                                       queryType,
199                                       new String[] { permissions },
200                                       cacheResult);
201         }
202       } catch(InvalidQueryException qe) {
203         uiApp.addMessage(new ApplicationMessage("UIQueriesForm.msg.invalid-query", null,
204                                                 ApplicationMessage.WARNING)) ;        
205         return ;
206       } catch (AccessDeniedException ade) {
207         uiApp.addMessage(new ApplicationMessage("UIQueriesForm.msg.access-denied", null,
208                   ApplicationMessage.WARNING)) ;        
209         return ;
210       }
211       UIQueriesManager uiManager = uiForm.getAncestorOfType(UIQueriesManager.class) ;
212       uiManager.getChild(UIQueriesList.class).refresh(1);
213       uiManager.removeChildById(UIQueriesList.ST_ADD) ;
214       uiManager.removeChildById(UIQueriesList.ST_EDIT) ;
215       event.getRequestContext().getJavascriptManager()
216               .require("SHARED/jquery", "gj")
217               .addScripts("gj(document).ready(function() { gj(\"*[rel='tooltip']\").tooltip();});");
218       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
219     }
220   }
221 
222   static public class ChangeQueryTypeActionListener extends EventListener<UIQueriesForm> {
223     public void execute(Event<UIQueriesForm> event) throws Exception {
224       UIQueriesForm uiForm= event.getSource() ;
225       String queryType = uiForm.getUIFormSelectBox(QUERY_TYPE).getValue() ;
226       if(queryType.equals(Query.XPATH)) {
227         uiForm.getUIFormTextAreaInput(STATEMENT).setValue(XPATH_QUERY) ;
228       } else {
229         uiForm.getUIFormTextAreaInput(STATEMENT).setValue(SQL_QUERY) ;
230       }
231     }
232   }
233 
234   static public class AddPermissionActionListener extends EventListener<UIQueriesForm> {
235     public void execute(Event<UIQueriesForm> event) throws Exception {
236       UIQueriesManager uiManager = event.getSource().getAncestorOfType(UIQueriesManager.class) ;
237       String membership = event.getSource().getUIStringInput(PERMISSIONS).getValue() ;
238       uiManager.initPermissionPopup(membership) ;
239       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
240     }
241   }
242 }