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  
22  import javax.jcr.AccessDeniedException;
23  import javax.jcr.Node;
24  import javax.jcr.RepositoryException;
25  import javax.jcr.query.Query;
26  import javax.jcr.query.QueryResult;
27  
28  import org.exoplatform.commons.utils.LazyPageList;
29  import org.exoplatform.commons.utils.ListAccess;
30  import org.exoplatform.commons.utils.ListAccessImpl;
31  import org.exoplatform.ecm.webui.component.explorer.UIDocumentWorkspace;
32  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
33  import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
34  import org.exoplatform.portal.webui.util.Util;
35  import org.exoplatform.services.cms.queries.QueryService;
36  import org.exoplatform.services.wcm.core.NodeLocation;
37  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
38  import org.exoplatform.web.application.ApplicationMessage;
39  import org.exoplatform.webui.application.WebuiRequestContext;
40  import org.exoplatform.webui.application.portlet.PortletRequestContext;
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.UIComponent;
45  import org.exoplatform.webui.core.UIContainer;
46  import org.exoplatform.webui.core.UIPageIterator;
47  import org.exoplatform.webui.core.UIPopupComponent;
48  import org.exoplatform.webui.core.UIPopupContainer;
49  import org.exoplatform.webui.core.UIPopupWindow;
50  import org.exoplatform.webui.event.Event;
51  import org.exoplatform.webui.event.EventListener;
52  /**
53   * Created by The eXo Platform SARL
54   * Author : Dang Van Minh
55   *          minh.dang@exoplatform.com
56   * Jan 4, 2006
57   * 16:37:15
58   */
59  @ComponentConfig(
60      template =  "app:/groovy/webui/component/explorer/search/UISavedQuery.gtmpl",
61      events = {
62          @EventConfig(listeners = UISavedQuery.ExecuteActionListener.class),
63          @EventConfig(listeners = UISavedQuery.DeleteActionListener.class, confirm = "UISavedQuery.msg.confirm-delete-query"),
64          @EventConfig(listeners = UISavedQuery.EditActionListener.class)
65      }
66  )
67  
68  public class UISavedQuery extends UIContainer implements UIPopupComponent {
69  
70    final static public String EDIT_FORM = "EditSavedQueryForm";
71  
72    private UIPageIterator uiPageIterator_;
73  
74    private boolean isQuickSearch_ = false;
75  
76    public UISavedQuery() throws Exception {
77      uiPageIterator_ = addChild(UIPageIterator.class, null, "SavedQueryIterator");
78    }
79  
80    public void updateGrid(int currentPage) throws Exception {
81      ListAccess<Object> queryList = new ListAccessImpl<Object>(Object.class,
82                                                                NodeLocation.getLocationsByNodeList(queryList()));
83      LazyPageList<Object> pageList = new LazyPageList<Object>(queryList, 10);
84      uiPageIterator_.setPageList(pageList);
85      if (currentPage > uiPageIterator_.getAvailablePage())
86        uiPageIterator_.setCurrentPage(uiPageIterator_.getAvailablePage());
87      else
88        uiPageIterator_.setCurrentPage(currentPage);
89    }
90  
91    public List<Object> queryList() throws Exception {
92      List<Object> objectList = new ArrayList<Object>();
93      List<Node> sharedQueries = getSharedQueries();
94      if(!sharedQueries.isEmpty()) {
95        for(Node node : sharedQueries) {
96          objectList.add(node);
97        }
98      }
99      List<Query> queries = getQueries();
100     if(!queries.isEmpty()) {
101       for(Query query : queries) {
102         objectList.add(new QueryData(query));
103       }
104     }
105     return objectList;
106   }
107 
108   public UIPageIterator getUIPageIterator() { return uiPageIterator_; }
109 
110   public List getQueryList() throws Exception {
111     return NodeLocation.getNodeListByLocationList(uiPageIterator_.getCurrentPageData());
112   }
113 
114   public void initPopupEditForm(Query query) throws Exception {
115     removeChildById(EDIT_FORM);
116     UIPopupWindow uiPopup = addChild(UIPopupWindow.class, null, EDIT_FORM);
117     uiPopup.setWindowSize(500,0);
118     UIJCRAdvancedSearch uiJAdvancedSearch =
119       createUIComponent(UIJCRAdvancedSearch.class, null, "EditQueryForm");
120     uiJAdvancedSearch.setActions(new String[] {"Save", "Cancel"});
121     uiPopup.setUIComponent(uiJAdvancedSearch);
122     uiPopup.setRendered(true);
123     uiJAdvancedSearch.setIsEdit(true);
124     uiJAdvancedSearch.setQuery(query);
125     uiJAdvancedSearch.update(query);
126     uiPopup.setShow(true);
127     uiPopup.setResizable(true);
128   }
129 
130   public List<Query> getQueries() throws Exception {
131     QueryService queryService = getApplicationComponent(QueryService.class);
132     try {
133       return queryService.getQueries(getCurrentUserId(), WCMCoreUtils.getUserSessionProvider());
134     } catch(AccessDeniedException ace) {
135       return new ArrayList<Query>();
136     }
137   }
138 
139   public String getCurrentUserId() { return Util.getPortalRequestContext().getRemoteUser();}
140 
141   public List<Node> getSharedQueries() throws Exception {
142     PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
143     QueryService queryService = getApplicationComponent(QueryService.class);
144     String userId = pcontext.getRemoteUser();
145     return queryService.getSharedQueries(userId, WCMCoreUtils.getSystemSessionProvider());
146   }
147 
148   //public List<Node> getSharedQueries() { return sharedQueries_; }
149 
150   public void activate() { }
151 
152   public void deActivate() { }
153 
154   public void setIsQuickSearch(boolean isQuickSearch) { isQuickSearch_ = isQuickSearch; }
155 
156   static public class ExecuteActionListener extends EventListener<UISavedQuery> {
157     public void execute(Event<UISavedQuery> event) throws Exception {
158       UISavedQuery uiQuery = event.getSource();
159       UIJCRExplorer uiExplorer = uiQuery.getAncestorOfType(UIJCRExplorer.class);
160       String wsName = uiQuery.getAncestorOfType(UIJCRExplorer.class).getCurrentWorkspace();
161       UIApplication uiApp = uiQuery.getAncestorOfType(UIApplication.class);
162       QueryService queryService = uiQuery.getApplicationComponent(QueryService.class);
163       String queryPath = event.getRequestContext().getRequestParameter(OBJECTID);
164       UIComponent uiSearch = null;
165       UISearchResult uiSearchResult = null;
166       if(uiQuery.isQuickSearch_) {
167         uiSearch = uiExplorer.getChild(UIWorkingArea.class).getChild(UIDocumentWorkspace.class);
168         uiSearchResult = ((UIDocumentWorkspace)uiSearch).getChild(UISearchResult.class);
169       } else {
170         uiSearch = uiQuery.getParent();
171         uiSearchResult = ((UIECMSearch)uiSearch).getChild(UISearchResult.class);
172         ((UIECMSearch)uiSearch).setSelectedTab(uiSearchResult.getId());
173       }
174       Query query = null;
175       QueryResult queryResult = null;
176       try {
177         query = queryService.getQuery(queryPath,
178                                        wsName,
179                                        WCMCoreUtils.getSystemSessionProvider(),
180                                        uiQuery.getCurrentUserId());
181         queryResult = query.execute();
182       } catch(Exception e) {
183         uiApp.addMessage(new ApplicationMessage("UISearchResult.msg.query-invalid", null,
184                                                 ApplicationMessage.WARNING));
185         if(!uiQuery.isQuickSearch_) ((UIECMSearch)uiSearch).setSelectedTab(uiQuery.getId());
186         return;
187       } finally {
188         if(queryResult == null || queryResult.getNodes().getSize() ==0) {
189           uiApp.addMessage(new ApplicationMessage("UISavedQuery.msg.not-result-found", null));
190           
191           if(!uiQuery.isQuickSearch_) ((UIECMSearch)uiSearch).setSelectedTab(uiQuery.getId());
192           return;
193         }
194         uiSearchResult.setQuery(query.getStatement(), wsName, query.getLanguage(), true, null);
195         uiSearchResult.updateGrid();
196       }
197       if(uiQuery.isQuickSearch_) {
198         ((UIDocumentWorkspace)uiSearch).setRenderedChild(UISearchResult.class);
199         UIPopupContainer uiPopup = uiExplorer.getChild(UIPopupContainer.class);
200         uiPopup.deActivate();
201       }
202     }
203   }
204 
205   static public class EditActionListener extends EventListener<UISavedQuery> {
206     public void execute(Event<UISavedQuery> event) throws Exception {
207       UISavedQuery uiQuery = event.getSource();
208       String userName = Util.getPortalRequestContext().getRemoteUser();
209       QueryService queryService = uiQuery.getApplicationComponent(QueryService.class);
210       String queryPath = event.getRequestContext().getRequestParameter(OBJECTID);
211       Query query = queryService.getQueryByPath(queryPath,
212                                                 userName,
213                                                 WCMCoreUtils.getSystemSessionProvider());
214       uiQuery.initPopupEditForm(query);
215       if(!uiQuery.isQuickSearch_) {
216         UIECMSearch uiECSearch = uiQuery.getParent();
217         uiECSearch.setSelectedTab(uiQuery.getId());
218         event.getRequestContext().addUIComponentToUpdateByAjax(uiECSearch);
219       } else {
220         event.getRequestContext().addUIComponentToUpdateByAjax(uiQuery.getParent());
221       }
222     }
223   }
224 
225   static public class DeleteActionListener extends EventListener<UISavedQuery> {
226     public void execute(Event<UISavedQuery> event) throws Exception {
227       UISavedQuery uiQuery = event.getSource();
228       String userName = Util.getPortalRequestContext().getRemoteUser();
229       QueryService queryService = uiQuery.getApplicationComponent(QueryService.class);
230       String path = event.getRequestContext().getRequestParameter(OBJECTID);
231       queryService.removeQuery(path, userName);
232       uiQuery.updateGrid(uiQuery.getUIPageIterator().getCurrentPage());
233       event.getRequestContext().addUIComponentToUpdateByAjax(uiQuery);
234     }
235   }
236   
237   public class QueryData {
238 
239     private String language_;
240     private String statement_;
241     private String storedQueryPath_;
242     
243     public QueryData(Query query) {
244       language_ = query.getLanguage();
245       statement_ = query.getStatement();
246       try {
247         storedQueryPath_ = query.getStoredQueryPath();
248       } catch (RepositoryException e) {
249         storedQueryPath_ = "";
250       }
251     }
252 
253     public String getLanguage() {
254       return language_;
255     }
256 
257     public void setLanguage(String language) {
258       language_ = language;
259     }
260 
261     public String getStatement() {
262       return statement_;
263     }
264 
265     public void setStatement(String statement) {
266       statement_ = statement;
267     }
268 
269     public String getStoredQueryPath() {
270       return storedQueryPath_;
271     }
272 
273     public void setStoredQueryPath(String storedQueryPath) {
274       storedQueryPath_ = storedQueryPath;
275     }
276   }
277 }