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.views;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.List;
23  
24  import javax.jcr.AccessDeniedException;
25  import javax.jcr.Node;
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.UIPagingGrid;
31  import org.exoplatform.services.cms.BasePath;
32  import org.exoplatform.services.cms.drives.ManageDriveService;
33  import org.exoplatform.services.cms.impl.Utils;
34  import org.exoplatform.services.cms.views.ManageViewService;
35  import org.exoplatform.services.cms.views.impl.ManageViewPlugin;
36  import org.exoplatform.services.jcr.ext.common.SessionProvider;
37  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
38  import org.exoplatform.web.application.ApplicationMessage;
39  import org.exoplatform.webui.config.annotation.ComponentConfig;
40  import org.exoplatform.webui.config.annotation.EventConfig;
41  import org.exoplatform.webui.core.UIApplication;
42  import org.exoplatform.webui.event.Event;
43  import org.exoplatform.webui.event.EventListener;
44  
45  /**
46   * Created by The eXo Platform SARL
47   * Author : Tran The Trong
48   *          trongtt@exoplatform.com
49   * Sep 19, 2006
50   * 11:45:11 AM
51   */
52  @ComponentConfig(
53      template = "system:/groovy/ecm/webui/UIGridWithButton.gtmpl",
54      events = {
55          @EventConfig(listeners = UIECMTemplateList.DeleteActionListener.class, confirm = "UIECMTemplateList.msg.confirm-delete"),
56          @EventConfig(listeners = UIECMTemplateList.EditInfoActionListener.class),
57          @EventConfig(listeners = UIECMTemplateList.AddActionListener.class)
58      }
59  )
60  public class UIECMTemplateList extends UIPagingGrid {
61    private static String[] VIEW_BEAN_FIELD = {"name"} ;
62    private static String[] VIEW_ACTION = {"EditInfo","Delete"} ;
63    public static String ST_ecmTempForm = "ecmTempForm" ;
64    public static String ST_ECMTemp = "ECMTemplate" ;
65  
66    public UIECMTemplateList() throws Exception {
67      getUIPageIterator().setId("UIECMTemplateGrid") ;
68      configure("path", VIEW_BEAN_FIELD, VIEW_ACTION) ;
69    }
70  
71    public String[] getActions() { return new String[] {"Add"} ; }
72  
73    public void refresh(int currentPage) throws Exception {
74      List<Node> nodes = getApplicationComponent(ManageViewService.class)
75                                                 .getAllTemplates(
76                                                                  BasePath.ECM_EXPLORER_TEMPLATES, 
77                                                                  WCMCoreUtils.getSystemSessionProvider());
78      List<TemplateBean> tempBeans = new ArrayList<TemplateBean>();
79      for (Node node : nodes) {
80        tempBeans.add(new TemplateBean(node.getName(), node.getPath()));
81      }
82      Collections.sort(tempBeans, new ECMViewComparator());
83      ListAccess<TemplateBean> tmplBeanList = new ListAccessImpl<TemplateBean>(TemplateBean.class,
84                                                                               tempBeans);
85      getUIPageIterator().setPageList(new LazyPageList<TemplateBean>(tmplBeanList,
86                                                                     getUIPageIterator().getItemsPerPage()));
87      getUIPageIterator().setTotalItems(tempBeans.size());
88      if (currentPage > getUIPageIterator().getAvailablePage())
89        getUIPageIterator().setCurrentPage(getUIPageIterator().getAvailablePage());
90      else
91        getUIPageIterator().setCurrentPage(currentPage);
92    }
93  
94    static public class ECMViewComparator implements Comparator<TemplateBean> {
95      public int compare(TemplateBean o1, TemplateBean o2) throws ClassCastException {
96        String name1 = o1.getName();
97        String name2 = o2.getName();
98        return name1.compareToIgnoreCase(name2);
99      }
100   }
101 
102   static  public class AddActionListener extends EventListener<UIECMTemplateList> {
103     public void execute(Event<UIECMTemplateList> event) throws Exception {
104       UIECMTemplateList uiECMTempList = event.getSource() ;
105       SessionProvider provider = WCMCoreUtils.getSystemSessionProvider();
106       Node ecmTemplateHome = uiECMTempList.getApplicationComponent(ManageViewService.class)
107                                           .getTemplateHome(BasePath.ECM_EXPLORER_TEMPLATES,
108                                                            provider);
109       if (ecmTemplateHome == null) {
110         UIApplication uiApp = event.getSource().getAncestorOfType(UIApplication.class);
111         uiApp.addMessage(new ApplicationMessage("UIECMTemplateList.msg.access-denied",
112                                                 null,
113                                                 ApplicationMessage.WARNING));
114         
115         return;
116       }
117       UIViewManager uiViewManager = uiECMTempList.getAncestorOfType(UIViewManager.class) ;
118       UITemplateContainer uiECMTempContainer = uiViewManager.getChildById(UIECMTemplateList.ST_ECMTemp) ;
119       uiECMTempContainer.removeChildById(UIECMTemplateList.ST_ecmTempForm + "Edit") ;
120       uiECMTempContainer.initPopup(UIECMTemplateList.ST_ecmTempForm, "Add") ;
121       event.getRequestContext().addUIComponentToUpdateByAjax(uiECMTempContainer) ;
122     }
123   }
124 
125   static  public class DeleteActionListener extends EventListener<UIECMTemplateList> {
126     public void execute(Event<UIECMTemplateList> event) throws Exception {
127       UIECMTemplateList uiECMTemp = event.getSource() ;
128       ManageViewService vservice = uiECMTemp.getApplicationComponent(ManageViewService.class) ;
129       uiECMTemp.setRenderSibling(UIECMTemplateList.class);
130       String templatePath = event.getRequestContext().getRequestParameter(OBJECTID) ;
131       String templateName = templatePath.substring(templatePath.lastIndexOf("/") + 1) ;
132       if (uiECMTemp.getApplicationComponent(ManageDriveService.class).isUsedView(templateName)) {
133         UIApplication app = uiECMTemp.getAncestorOfType(UIApplication.class);
134         Object[] args = { templateName };
135         app.addMessage(new ApplicationMessage("UIECMTemplateList.msg.template-in-use", args));
136         return;
137       }
138       try {
139         vservice.removeTemplate(templatePath, WCMCoreUtils.getSystemSessionProvider());
140         Utils.addEditedConfiguredData(templateName, ManageViewPlugin.class.getSimpleName(), ManageViewPlugin.EDITED_CONFIGURED_VIEWS_TEMPLATES, true);
141       } catch (AccessDeniedException ex) {
142         UIApplication app = uiECMTemp.getAncestorOfType(UIApplication.class);
143         Object[] args = { "UIViewFormTabPane.label.option." + templateName };
144         app.addMessage(new ApplicationMessage("UIECMTemplateList.msg.delete-permission-denied",
145                                               args,
146                                               ApplicationMessage.WARNING));
147         return;
148       }
149       uiECMTemp.refresh(uiECMTemp.getUIPageIterator().getCurrentPage()) ;
150       UITemplateContainer uiTempContainer = uiECMTemp.getParent() ;
151       event.getRequestContext().addUIComponentToUpdateByAjax(uiTempContainer) ;
152     }
153   }
154 
155   static  public class EditInfoActionListener extends EventListener<UIECMTemplateList> {
156     public void execute(Event<UIECMTemplateList> event) throws Exception {
157       UIECMTemplateList uiECMTemp = event.getSource() ;
158       String tempPath = event.getRequestContext().getRequestParameter(OBJECTID) ;
159       UITemplateContainer uiTempContainer = uiECMTemp.getParent() ;
160       uiTempContainer.removeChildById(UIECMTemplateList.ST_ecmTempForm + "Add") ;
161       uiTempContainer.initPopup(UIECMTemplateList.ST_ecmTempForm, "Edit") ;
162       UITemplateForm uiTempForm = uiTempContainer.findComponentById(UIECMTemplateList.ST_ecmTempForm) ;
163       uiTempForm.update(tempPath, null) ;
164       event.getRequestContext().addUIComponentToUpdateByAjax(uiTempContainer) ;
165     }
166   }
167 }