View Javadoc
1   /*
2    * Copyright (C) 2003-2008 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.sidebar;
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  
27  import org.exoplatform.ecm.utils.text.Text;
28  import org.exoplatform.ecm.webui.component.explorer.UIDocumentWorkspace;
29  import org.exoplatform.ecm.webui.component.explorer.UIDrivesArea;
30  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
31  import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
32  import org.exoplatform.ecm.webui.component.explorer.search.UIContentNameSearch;
33  import org.exoplatform.ecm.webui.component.explorer.search.UIECMSearch;
34  import org.exoplatform.ecm.webui.component.explorer.search.UISavedQuery;
35  import org.exoplatform.ecm.webui.component.explorer.search.UISearchResult;
36  import org.exoplatform.ecm.webui.component.explorer.search.UISimpleSearch;
37  import org.exoplatform.portal.webui.util.Util;
38  import org.exoplatform.services.cms.queries.QueryService;
39  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
40  import org.exoplatform.web.application.ApplicationMessage;
41  import org.exoplatform.webui.application.WebuiRequestContext;
42  import org.exoplatform.webui.application.portlet.PortletRequestContext;
43  import org.exoplatform.webui.config.annotation.ComponentConfig;
44  import org.exoplatform.webui.config.annotation.EventConfig;
45  import org.exoplatform.webui.core.UIApplication;
46  import org.exoplatform.webui.core.UIComponent;
47  import org.exoplatform.webui.core.UIPopupContainer;
48  import org.exoplatform.webui.event.Event;
49  import org.exoplatform.webui.event.EventListener;
50  
51  /**
52   * Created by The eXo Platform SARL
53   * Author : Dang Van Minh
54   *          minh.dang@exoplatform.com
55   * Oct 23, 2009
56   * 4:01:53 AM
57   */
58  @ComponentConfig(
59      template =  "app:/groovy/webui/component/explorer/sidebar/UISavedSearches.gtmpl",
60      events = {
61          @EventConfig(listeners = UISavedSearches.ExecuteActionListener.class),
62          @EventConfig(listeners = UISavedSearches.AdvanceSearchActionListener.class),
63          @EventConfig(listeners = UISavedSearches.SavedQueriesActionListener.class)
64      }
65  )
66  public class UISavedSearches extends UIComponent {
67  
68    public final static String ACTION_TAXONOMY = "exo:taxonomyAction";
69    public final static String EXO_TARGETPATH = "exo:targetPath";
70    public final static String EXO_TARGETWORKSPACE = "exo:targetWorkspace";
71  
72    private String queryPath;
73    
74    public UISavedSearches() throws Exception {
75    }
76  
77    public List<Object> queryList() throws Exception {
78      List<Object> objectList = new ArrayList<Object>();
79      List<Node> sharedQueries = getSharedQueries();
80      if(!sharedQueries.isEmpty()) {
81        for(Node node : sharedQueries) {
82          objectList.add(new NodeData(node));
83        }
84      }
85      List<Query> queries = getQueries();
86      if(!queries.isEmpty()) {
87        for(Query query : queries) {
88          objectList.add(new QueryData(query));
89        }
90      }
91      return objectList;
92    }
93  
94    public String getCurrentUserId() { return Util.getPortalRequestContext().getRemoteUser();}
95  
96    public List<Query> getQueries() throws Exception { 
97      QueryService queryService = getApplicationComponent(QueryService.class);
98      try {
99        return  queryService.getQueries(getCurrentUserId(), WCMCoreUtils.getUserSessionProvider());
100     } catch(AccessDeniedException ace) {
101       return new ArrayList<Query>();
102     }
103   }
104 
105   public List<Node> getSharedQueries() throws Exception { 
106     PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
107     QueryService queryService = getApplicationComponent(QueryService.class);
108     String userId = pcontext.getRemoteUser();
109     return queryService.getSharedQueries(userId, WCMCoreUtils.getSystemSessionProvider());
110   }
111 
112   public void setQueryPath(String queryPath) throws Exception {
113     this.queryPath = queryPath;
114   }
115 
116   public String getQueryPath() throws Exception {
117     return this.queryPath;
118   }
119 
120   static public class ExecuteActionListener extends EventListener<UISavedSearches> {
121     public void execute(Event<UISavedSearches> event) throws Exception {
122       UISavedSearches uiSavedSearches = event.getSource();
123       UIJCRExplorer uiExplorer = uiSavedSearches.getAncestorOfType(UIJCRExplorer.class);
124       String queryPath = event.getRequestContext().getRequestParameter(OBJECTID);
125       uiSavedSearches.setQueryPath(queryPath);
126 
127       uiExplorer.setPathToAddressBar(Text.unescapeIllegalJcrChars(
128                                                            uiExplorer.filterPath(queryPath)));
129       String wsName = uiSavedSearches.getAncestorOfType(UIJCRExplorer.class).getCurrentWorkspace();
130       UIApplication uiApp = uiSavedSearches.getAncestorOfType(UIApplication.class);
131       QueryService queryService = uiSavedSearches.getApplicationComponent(QueryService.class);
132       UIWorkingArea uiWorkingArea = uiExplorer.getChild(UIWorkingArea.class);
133       UIComponent uiSearch = uiWorkingArea.getChild(UIDocumentWorkspace.class);
134       UIDrivesArea uiDrivesArea = uiWorkingArea.getChild(UIDrivesArea.class);
135       UISearchResult uiSearchResult = ((UIDocumentWorkspace)uiSearch).getChild(UISearchResult.class);
136       Query query = null;
137       try {
138         query = queryService.getQuery(queryPath,
139                                        wsName,
140                                        WCMCoreUtils.getSystemSessionProvider(),
141                                        uiSavedSearches.getCurrentUserId());
142         query.execute();
143       } catch(Exception e) {
144         uiApp.addMessage(new ApplicationMessage("UISearchResult.msg.query-invalid", null,
145                                                 ApplicationMessage.WARNING));
146       } finally {
147         uiSearchResult.setQuery(query.getStatement(), wsName, query.getLanguage(), false, null);
148         uiSearchResult.updateGrid();
149       }   
150       if (uiDrivesArea != null) uiDrivesArea.setRendered(false);
151       uiWorkingArea.getChild(UIDocumentWorkspace.class).setRendered(true);      
152       ((UIDocumentWorkspace)uiSearch).setRenderedChild(UISearchResult.class);
153     }
154   }
155 
156   static public class AdvanceSearchActionListener extends EventListener<UISavedSearches> {
157     public void execute(Event<UISavedSearches> event) throws Exception {
158       UIJCRExplorer uiJCRExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
159       UIPopupContainer UIPopupContainer = uiJCRExplorer.getChild(UIPopupContainer.class);
160       UIECMSearch uiECMSearch = event.getSource().createUIComponent(UIECMSearch.class, null, null);
161       UIContentNameSearch contentNameSearch = uiECMSearch.findFirstComponentOfType(UIContentNameSearch.class);
162       String currentNodePath = uiJCRExplorer.getCurrentNode().getPath();
163       contentNameSearch.setLocation(currentNodePath);
164       UISimpleSearch uiSimpleSearch = uiECMSearch.findFirstComponentOfType(UISimpleSearch.class);
165       uiSimpleSearch.getUIFormInputInfo(UISimpleSearch.NODE_PATH).setValue(currentNodePath);
166       UIPopupContainer.activate(uiECMSearch, 700, 500, false);
167       event.getRequestContext().addUIComponentToUpdateByAjax(UIPopupContainer);
168     }
169   }
170 
171   static public class SavedQueriesActionListener extends EventListener<UISavedSearches> {
172     public void execute(Event<UISavedSearches> event) throws Exception {
173       UIJCRExplorer uiJCRExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
174       UIPopupContainer UIPopupContainer = uiJCRExplorer.getChild(UIPopupContainer.class);
175       UISavedQuery uiSavedQuery = event.getSource().createUIComponent(UISavedQuery.class, null, null);
176       uiSavedQuery.setIsQuickSearch(true);
177       uiSavedQuery.updateGrid(1);
178       UIPopupContainer.activate(uiSavedQuery, 700, 400);
179       event.getRequestContext().addUIComponentToUpdateByAjax(UIPopupContainer);
180     }
181   }
182 
183   public class QueryData {
184 
185     private String storedQueryPath_;
186     
187     public QueryData(Query query) {
188       try {
189         storedQueryPath_ = query.getStoredQueryPath();
190       } catch (RepositoryException e) {
191         storedQueryPath_ = "";
192       }
193     }
194 
195     public String getStoredQueryPath() {
196       return storedQueryPath_;
197     }
198 
199     public void setStoredQueryPath(String storedQueryPath) {
200       storedQueryPath_ = storedQueryPath;
201     }
202   }
203   
204   public class NodeData {
205     private String path_;
206     private String name_;
207     
208     public NodeData(Node node) throws RepositoryException {
209       this.path_ = node.getPath();
210       this.name_ = node.getName();
211     }
212 
213     public String getPath() {
214       return path_;
215     }
216 
217     public void setPath(String path) {
218       path_ = path;
219     }
220 
221     public String getName() {
222       return name_;
223     }
224 
225     public void setName(String name) {
226       name_ = name;
227     }
228     
229   }
230 }