1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.ecm.webui.component.explorer.search;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
21 import org.exoplatform.ecm.webui.form.validator.ECMNameValidator;
22 import org.exoplatform.webui.form.validator.MandatoryValidator;
23 import org.exoplatform.portal.webui.util.Util;
24 import org.exoplatform.services.cms.queries.QueryService;
25 import org.exoplatform.services.jcr.RepositoryService;
26 import org.exoplatform.services.jcr.core.ManageableRepository;
27 import org.exoplatform.services.security.IdentityConstants;
28 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
29 import org.exoplatform.web.application.ApplicationMessage;
30 import org.exoplatform.webui.config.annotation.ComponentConfig;
31 import org.exoplatform.webui.config.annotation.EventConfig;
32 import org.exoplatform.webui.core.UIApplication;
33 import org.exoplatform.webui.core.UIPopupComponent;
34 import org.exoplatform.webui.core.UIPopupContainer;
35 import org.exoplatform.webui.core.UIPopupWindow;
36 import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
37 import org.exoplatform.webui.core.model.SelectItemOption;
38 import org.exoplatform.webui.event.Event;
39 import org.exoplatform.webui.event.Event.Phase;
40 import org.exoplatform.webui.event.EventListener;
41 import org.exoplatform.webui.form.UIForm;
42 import org.exoplatform.webui.form.UIFormSelectBox;
43 import org.exoplatform.webui.form.UIFormStringInput;
44 import org.exoplatform.webui.form.UIFormTextAreaInput;
45
46 import javax.jcr.Node;
47 import javax.jcr.Session;
48 import javax.jcr.query.Query;
49 import javax.jcr.query.QueryManager;
50 import java.util.ArrayList;
51 import java.util.List;
52
53
54
55
56
57
58
59
60 @ComponentConfig(
61 lifecycle = UIFormLifecycle.class,
62 template = "system:/groovy/webui/form/UIForm.gtmpl",
63 events = {
64 @EventConfig(listeners = UIJCRAdvancedSearch.SaveActionListener.class),
65 @EventConfig(listeners = UIJCRAdvancedSearch.SearchActionListener.class, phase = Phase.DECODE),
66 @EventConfig(listeners = UIJCRAdvancedSearch.CancelActionListener.class, phase = Phase.DECODE),
67 @EventConfig(phase=Phase.DECODE, listeners = UIJCRAdvancedSearch.ChangeOptionActionListener.class)
68 }
69 )
70 public class UIJCRAdvancedSearch extends UIForm implements UIPopupComponent {
71 public static final String FIELD_NAME = "name" ;
72 public static final String FIELD_QUERY = "query" ;
73 public static final String FIELD_SELECT_BOX = "selectBox" ;
74
75 private static final String ROOT_SQL_QUERY = "select * from nt:base order by exo:dateCreated DESC" ;
76 private static final String SQL_QUERY = "select * from nt:base where jcr:path like '$0/%' order by exo:dateCreated DESC" ;
77 private static final String ROOT_XPATH_QUERY = "//* order by @exo:dateCreated descending" ;
78 private static final String XPATH_QUERY = "/jcr:root$0//* order by @exo:dateCreated descending" ;
79 private static final String CHANGE_OPTION = "ChangeOption" ;
80 private boolean isEdit_ = false ;
81 private String queryPath_ ;
82 private String queryStatement_;
83 private String queryLanguage_;
84
85 public UIJCRAdvancedSearch() throws Exception {
86 addUIFormInput(new UIFormStringInput(FIELD_NAME, FIELD_NAME, null).addValidator(ECMNameValidator.class)
87 .addValidator(MandatoryValidator.class));
88 List<SelectItemOption<String>> ls = new ArrayList<SelectItemOption<String>>() ;
89 ls.add(new SelectItemOption<String>("SQL", "sql")) ;
90 ls.add(new SelectItemOption<String>("xPath", "xpath")) ;
91 UIFormSelectBox uiSelectBox = new UIFormSelectBox(FIELD_SELECT_BOX, FIELD_SELECT_BOX, ls) ;
92 uiSelectBox.setOnChange("Change") ;
93 addUIFormInput(uiSelectBox) ;
94 addUIFormInput(new UIFormTextAreaInput(FIELD_QUERY, FIELD_QUERY, null)) ;
95 setActions(new String[]{"Search", "Save", "Cancel"}) ;
96 }
97
98 public void update(Query query) throws Exception {
99 if(query == null) {
100 UIJCRExplorer uiExplorer = getAncestorOfType(UIJCRExplorer.class) ;
101 Node selectedNode = uiExplorer.getCurrentNode() ;
102 String path = selectedNode.getPath() ;
103 String queryText = StringUtils.replace(SQL_QUERY, "$0", path) ;
104 if ("/".equals(path)) queryText = ROOT_SQL_QUERY ;
105 getUIStringInput(FIELD_NAME).setValue(null) ;
106 getUIStringInput(FIELD_NAME).setReadOnly(false) ;
107 getUIFormSelectBox(FIELD_SELECT_BOX).setOnChange(CHANGE_OPTION) ;
108 getUIFormSelectBox(FIELD_SELECT_BOX).setValue("sql") ;
109 getUIFormTextAreaInput(FIELD_QUERY).setValue(queryText) ;
110 } else {
111 String storedPath = query.getStoredQueryPath() ;
112 queryPath_ = storedPath ;
113 storedPath = storedPath.substring(storedPath.lastIndexOf("/") + 1, storedPath.length()) ;
114 getUIStringInput(FIELD_NAME).setValue(storedPath) ;
115 getUIStringInput(FIELD_NAME).setReadOnly(true);
116 getUIFormSelectBox(FIELD_SELECT_BOX).setOnChange(CHANGE_OPTION) ;
117 getUIFormSelectBox(FIELD_SELECT_BOX).setValue(query.getLanguage()) ;
118 getUIFormTextAreaInput(FIELD_QUERY).setValue(query.getStatement()) ;
119 }
120 }
121
122 public void setQuery(Query query) {
123 if (query != null) {
124 queryLanguage_ = query.getLanguage();
125 queryStatement_ = query.getStatement();
126 } else {
127 queryLanguage_ = null;
128 queryStatement_ = null;
129 }
130 }
131
132 public void setIsEdit(boolean isEdit) { isEdit_ = isEdit ; }
133 public boolean isEdit() { return isEdit_ ; }
134
135 public void activate() {}
136
137 public void deActivate() {}
138
139 static public class CancelActionListener extends EventListener<UIJCRAdvancedSearch> {
140 public void execute(Event<UIJCRAdvancedSearch> event) throws Exception {
141 UIJCRAdvancedSearch uiJAdvancedSearch = event.getSource() ;
142 if(uiJAdvancedSearch.isEdit_) {
143 UIPopupWindow uiPopup = uiJAdvancedSearch.getParent() ;
144 uiPopup.setShow(false) ;
145 uiPopup.setRendered(false) ;
146 event.getRequestContext().addUIComponentToUpdateByAjax(uiJAdvancedSearch.getParent()) ;
147 } else {
148 uiJAdvancedSearch.getAncestorOfType(UIPopupContainer.class).deActivate() ;
149 }
150 }
151 }
152
153 static public class SearchActionListener extends EventListener<UIJCRAdvancedSearch> {
154 public void execute(Event<UIJCRAdvancedSearch> event) throws Exception {
155 UIJCRAdvancedSearch uiForm = event.getSource() ;
156 UIJCRExplorer uiExplorer = uiForm.getAncestorOfType(UIJCRExplorer.class) ;
157 StringBuffer queryS = new StringBuffer();
158 queryS.append(uiForm.getUIFormTextAreaInput(FIELD_QUERY).getValue());
159 String searchType = uiForm.getUIFormSelectBox(FIELD_SELECT_BOX).getValue() ;
160 UIECMSearch uiSearch = uiForm.getParent() ;
161 long startTime = System.currentTimeMillis();
162 try {
163 if(queryS.toString().toLowerCase().indexOf("order by") < 0) {
164 if(searchType.equals("sql")) {
165 queryS.append(" order by exo:dateCreated DESC");
166 } else if(searchType.equals("xpath")) {
167 queryS.append(" order by @exo:dateCreated descending");
168 }
169 }
170 UISearchResult uiSearchResult = uiSearch.getChild(UISearchResult.class) ;
171 uiSearchResult.setQuery(queryS.toString(), uiExplorer.getTargetSession().getWorkspace().getName(), searchType,
172 IdentityConstants.SYSTEM.equals(WCMCoreUtils.getRemoteUser()), null);
173 uiSearchResult.updateGrid() ;
174 long time = System.currentTimeMillis() - startTime;
175 uiSearchResult.setSearchTime(time);
176 uiSearch.setSelectedTab(UIECMSearch.ADVANCED_RESULT) ;
177 } catch (Exception e){
178 UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class) ;
179 uiApp.addMessage(new ApplicationMessage("UIJCRAdvancedSearch.msg.invalid-queryStatement", null,
180 ApplicationMessage.WARNING)) ;
181 event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
182 return ;
183 }
184 }
185 }
186
187 static public class ChangeOptionActionListener extends EventListener<UIJCRAdvancedSearch> {
188 public void execute(Event<UIJCRAdvancedSearch> event) throws Exception {
189 UIJCRAdvancedSearch uiForm = event.getSource() ;
190 UIJCRExplorer uiExplorer = uiForm.getAncestorOfType(UIJCRExplorer.class) ;
191 String currentPath = uiExplorer.getCurrentNode().getPath() ;
192 String queryText = "" ;
193 String searchType = uiForm.getUIFormSelectBox(FIELD_SELECT_BOX).getValue() ;
194 if(searchType.equals(Query.SQL)){
195 if ("/".equals(currentPath)) queryText = ROOT_SQL_QUERY ;
196 else queryText = StringUtils.replace(SQL_QUERY, "$0", currentPath) ;
197 uiForm.getUIFormTextAreaInput(FIELD_QUERY).setValue(queryText) ;
198 } else {
199 if ("/".equals(currentPath)) queryText = ROOT_XPATH_QUERY ;
200 else queryText = StringUtils.replace(XPATH_QUERY, "$0", currentPath) ;
201 uiForm.getUIFormTextAreaInput(FIELD_QUERY).setValue(queryText) ;
202 }
203 if(uiForm.isEdit_ && uiForm.queryLanguage_ != null) {
204 if(searchType.equals(uiForm.queryLanguage_)) {
205 uiForm.getUIFormTextAreaInput(FIELD_QUERY).setValue(uiForm.queryStatement_) ;
206 }
207 }
208 event.getRequestContext().addUIComponentToUpdateByAjax(uiForm) ;
209 }
210 }
211
212 static public class SaveActionListener extends EventListener<UIJCRAdvancedSearch> {
213 public void execute(Event<UIJCRAdvancedSearch> event) throws Exception {
214 UIJCRAdvancedSearch uiForm = event.getSource() ;
215 String statement = uiForm.getUIFormTextAreaInput(FIELD_QUERY).getValue() ;
216 String queryLang = uiForm.getUIFormSelectBox(FIELD_SELECT_BOX).getValue() ;
217 UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class) ;
218
219 if(statement == null || statement.trim().length() ==0) {
220 uiApp.addMessage(new ApplicationMessage("UIJCRAdvancedSearch.msg.value-save-null", null,
221 ApplicationMessage.WARNING)) ;
222 event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
223 return ;
224 }
225
226 if(!uiForm.isEdit_) {
227 QueryService queryService = uiForm.getApplicationComponent(QueryService.class) ;
228 String name = uiForm.getUIStringInput(FIELD_NAME).getValue() ;
229 String userName = Util.getPortalRequestContext().getRemoteUser() ;
230 try {
231 queryService.addQuery(name, statement, queryLang, userName) ;
232 } catch(Exception e){
233 uiApp.addMessage(new ApplicationMessage("UIJCRAdvancedSearch.msg.save_unSuccessful", null,
234 ApplicationMessage.WARNING)) ;
235 event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
236 return ;
237 }
238 UIECMSearch uiSearch = uiForm.getParent() ;
239 uiSearch.getChild(UISavedQuery.class).updateGrid(1);
240 uiForm.update(null) ;
241 uiSearch.setSelectedTab(uiSearch.getChild(UISavedQuery.class).getId()) ;
242 event.getRequestContext().addUIComponentToUpdateByAjax(uiSearch) ;
243 } else {
244 UIJCRExplorer uiExplorer = uiForm.getAncestorOfType(UIJCRExplorer.class) ;
245 QueryManager queryManager = uiExplorer.getTargetSession().getWorkspace().getQueryManager() ;
246 try {
247 queryManager.createQuery(statement, queryLang) ;
248 } catch(Exception e) {
249 uiApp.addMessage(new ApplicationMessage("UIJCRAdvancedSearch.msg.save_unSuccessful", null,
250 ApplicationMessage.WARNING)) ;
251 event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
252 return ;
253 }
254 ManageableRepository repository =
255 uiForm.getApplicationComponent(RepositoryService.class).getRepository(uiExplorer.getRepositoryName()) ;
256 Session session = repository.getSystemSession(repository.getConfiguration().getDefaultWorkspaceName()) ;
257 Node queryNode = (Node) session.getItem(uiForm.queryPath_) ;
258 queryNode.setProperty("jcr:language", queryLang) ;
259 queryNode.setProperty("jcr:statement", statement) ;
260 queryNode.save() ;
261 session.save() ;
262 session.logout() ;
263 UISavedQuery uiSavedQuery = uiForm.getAncestorOfType(UISavedQuery.class) ;
264 uiSavedQuery.updateGrid(1);
265 uiSavedQuery.removeChildById(UISavedQuery.EDIT_FORM) ;
266 event.getRequestContext().addUIComponentToUpdateByAjax(uiSavedQuery.getParent()) ;
267 }
268 }
269 }
270 }