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.Collections;
20  import java.util.Comparator;
21  import java.util.List;
22  
23  import javax.jcr.Node;
24  import javax.jcr.PathNotFoundException;
25  import javax.jcr.Session;
26  
27  import org.exoplatform.commons.utils.LazyPageList;
28  import org.exoplatform.commons.utils.ListAccess;
29  import org.exoplatform.commons.utils.ListAccessImpl;
30  import org.exoplatform.ecm.webui.core.UIPagingGridDecorator;
31  import org.exoplatform.portal.webui.util.Util;
32  import org.exoplatform.services.cms.queries.QueryService;
33  import org.exoplatform.services.jcr.access.PermissionType;
34  import org.exoplatform.services.jcr.core.ManageableRepository;
35  import org.exoplatform.services.jcr.ext.common.SessionProvider;
36  import org.exoplatform.services.log.ExoLogger;
37  import org.exoplatform.services.log.Log;
38  import org.exoplatform.services.wcm.core.NodeLocation;
39  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
40  import org.exoplatform.web.application.ApplicationMessage;
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.event.Event;
45  import org.exoplatform.webui.event.EventListener;
46  
47  /**
48   * Created by The eXo Platform SARL
49   * Author : Dang Van Minh
50   *          minh.dang@exoplatform.com
51   * Dec 29, 2006
52   * 11:30:17 AM
53   */
54  @ComponentConfig(
55      template = "app:/groovy/webui/component/admin/queries/UIQueriesList.gtmpl",
56      events = {
57          @EventConfig(listeners = UIQueriesList.AddQueryActionListener.class),
58          @EventConfig(listeners = UIQueriesList.EditActionListener.class),
59          @EventConfig(listeners = UIQueriesList.DeleteActionListener.class, confirm = "UIQueriesList.msg.confirm-delete")
60      }
61  )
62  public class UIQueriesList extends UIPagingGridDecorator {
63    
64    private static final Log LOG  = ExoLogger.getLogger(UIQueriesList.class.getName());
65  
66    final static public String[] ACTIONS = {"AddQuery"} ;
67    final static public String ST_ADD = "AddQueryForm" ;
68    final static public String ST_EDIT = "EditQueryForm" ;
69  
70    public UIQueriesList() throws Exception {
71      getUIPageIterator().setId("QueriesListIterator");
72    }
73  
74    public String[] getActions() { return ACTIONS ; }
75  
76    public void refresh(int currentPage) throws Exception {
77      ListAccess<Object> sharedQueryList = new ListAccessImpl<Object>(Object.class,
78                                                                      NodeLocation.getLocationsByNodeList(getAllSharedQueries()));
79      LazyPageList<Object> pageList = new LazyPageList<Object>(sharedQueryList,
80                                                               getUIPageIterator().getItemsPerPage());
81      getUIPageIterator().setPageList(pageList);
82      if (currentPage > getUIPageIterator().getAvailablePage())
83        getUIPageIterator().setCurrentPage(getUIPageIterator().getAvailablePage());
84      else
85        getUIPageIterator().setCurrentPage(currentPage);
86    }
87  
88    public boolean canEditNode(Node node) {
89      SessionProvider sProvider = WCMCoreUtils.getSystemSessionProvider();
90      try {
91        ManageableRepository manageableRepository = ((ManageableRepository)node.getSession().getRepository());
92        Session session = sProvider.getSession(node.getSession().getWorkspace().getName(), manageableRepository);
93        session.checkPermission(node.getPath(), PermissionType.SET_PROPERTY);
94      }catch (Exception e){
95        return false;
96      }
97      return true;
98    }
99    public boolean canRemoveNode(Node node) {
100     SessionProvider sProvider = WCMCoreUtils.getSystemSessionProvider();
101     try {
102       ManageableRepository manageableRepository = ((ManageableRepository)node.getSession().getRepository());
103       Session session = sProvider.getSession(node.getSession().getWorkspace().getName(), manageableRepository);
104       session.checkPermission(node.getPath(), PermissionType.REMOVE);
105     }catch (Exception e){
106       return false;
107     }
108     return true;
109   }
110   public List getQueryList() throws Exception { 
111     return NodeLocation.getNodeListByLocationList(getUIPageIterator().getCurrentPageData()); 
112   }
113 
114   @SuppressWarnings("unchecked")
115   public List<Node> getAllSharedQueries() throws Exception {
116     QueryService queryService = getApplicationComponent(QueryService.class) ;
117     List<Node> queries = queryService.getSharedQueries(WCMCoreUtils.getSystemSessionProvider()) ;
118     Collections.sort(queries, new QueryComparator()) ;
119     return queries ;
120   }
121 
122   static public class QueryComparator implements Comparator {
123     public int compare(Object o1, Object o2) throws ClassCastException {
124       try {
125         String name1 = ((Node) o1).getName() ;
126         String name2 = ((Node) o2).getName() ;
127         return name1.compareToIgnoreCase(name2) ;
128       } catch(Exception e) {
129         return 0;
130       }
131     }
132   }
133 
134   static public class AddQueryActionListener extends EventListener<UIQueriesList> {
135     public void execute(Event<UIQueriesList> event) throws Exception {
136       UIQueriesManager uiQueriesMan = event.getSource().getParent() ;
137       uiQueriesMan.removeChildById(UIQueriesList.ST_EDIT) ;
138       uiQueriesMan.initFormPopup(UIQueriesList.ST_ADD) ;
139       event.getRequestContext().addUIComponentToUpdateByAjax(uiQueriesMan) ;
140     }
141   }
142 
143   static public class EditActionListener extends EventListener<UIQueriesList> {
144     public void execute(Event<UIQueriesList> event) throws Exception {
145       UIQueriesManager uiQueriesMan = event.getSource().getParent() ;
146       uiQueriesMan.removeChildById(UIQueriesList.ST_ADD) ;
147       uiQueriesMan.initFormPopup(UIQueriesList.ST_EDIT ) ;
148       String queryPath = event.getRequestContext().getRequestParameter(OBJECTID) ;
149       UIQueriesForm uiForm = uiQueriesMan.findFirstComponentOfType(UIQueriesForm.class) ;
150       String queryName = queryPath.substring(queryPath.lastIndexOf("/") + 1) ;
151       uiForm.update(queryName) ;
152       event.getRequestContext().addUIComponentToUpdateByAjax(uiQueriesMan) ;
153     }
154   }
155 
156   static public class DeleteActionListener extends EventListener<UIQueriesList> {
157     public void execute(Event<UIQueriesList> event) throws Exception {
158       UIQueriesList uiQueriesList = event.getSource();
159       UIApplication uiApp = uiQueriesList.getAncestorOfType(UIApplication.class);
160       UIQueriesManager uiQueriesMan = event.getSource().getParent() ;
161       String userName = Util.getPortalRequestContext().getRemoteUser() ;
162       String queryName = event.getRequestContext().getRequestParameter(OBJECTID) ;
163       QueryService queryService = event.getSource().getApplicationComponent(QueryService.class) ;
164       try {
165         queryService.removeQuery(queryName, userName) ;
166       } catch (PathNotFoundException pe) {
167         uiApp.addMessage(new ApplicationMessage("UIQueriesList.msg.query-not-existed",
168                                                 null,ApplicationMessage.WARNING));
169         return;
170       } catch (Exception ex) {
171         if (LOG.isErrorEnabled()) {
172           LOG.error("cannot remove the query", ex);
173         }
174         uiApp.addMessage(new ApplicationMessage("UIQueriesList.msg.can-not-remove",
175                                                 null,ApplicationMessage.ERROR));
176         return;
177       }
178       
179       event.getSource().refresh(uiQueriesList.getUIPageIterator().getCurrentPage());
180       event.getRequestContext().addUIComponentToUpdateByAjax(uiQueriesMan) ;
181     }
182   }
183 }