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.templates;
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.Node;
25  import javax.jcr.NodeIterator;
26  import javax.jcr.PathNotFoundException;
27  import javax.jcr.nodetype.NodeType;
28  import javax.jcr.nodetype.NodeTypeIterator;
29  import javax.jcr.nodetype.NodeTypeManager;
30  
31  import org.exoplatform.commons.utils.LazyPageList;
32  import org.exoplatform.commons.utils.ListAccess;
33  import org.exoplatform.commons.utils.ListAccessImpl;
34  import org.exoplatform.ecm.webui.core.UIPagingGrid;
35  import org.exoplatform.services.cms.templates.TemplateService;
36  import org.exoplatform.services.context.DocumentContext;
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.core.UIPopupWindow;
43  import org.exoplatform.webui.event.Event;
44  import org.exoplatform.webui.event.EventListener;
45  
46  /**
47   * Created by The eXo Platform SARL
48   * Author : pham tuan
49   *          phamtuanchip@yahoo.de
50   * Oct 03, 2006
51   * 9:43:23 AM
52   */
53  @ComponentConfig(
54                   template = "app:/groovy/webui/component/admin/template/UITemplateList.gtmpl",
55                   events = {
56                       @EventConfig(listeners = UITemplateList.EditActionListener.class),
57                       @EventConfig(listeners = UITemplateList.DeleteActionListener.class, confirm = "UITemplateList.msg.confirm-delete"),
58                       @EventConfig(listeners = UITemplateList.AddNewActionListener.class)
59                   }
60      )
61  
62  public class UITemplateList extends UIPagingGrid {
63  
64    private static String[] NODETYPE_BEAN_FIELD = {"icon", "label", "name"} ;
65    private static String[] NODETYPE_ACTION = {"Edit", "Delete"} ;
66    public static final String DOCUMENTS_TEMPLATE_TYPE = "templates";
67    public static final String ACTIONS_TEMPLATE_TYPE = "actions";
68    public static final String OTHERS_TEMPLATE_TYPE = "others";
69    public static final String LABEL_PROPERTY = "label";
70    public static final String ICON_FIELD = "icon";
71    public static final String LABEL_FIELD = "label";
72  
73    private String filter = DOCUMENTS_TEMPLATE_TYPE;
74  
75    public void setTemplateFilter(String filter) {
76      this.filter = filter;
77    }  
78    public String getTemplateFilter() {
79      return this.filter;
80    }
81  
82    public UITemplateList() throws Exception {
83      configure("name", NODETYPE_BEAN_FIELD, NODETYPE_ACTION) ;
84    }
85  
86    public String[] getActions() {
87      return new String[] {"AddNew"} ;
88    }
89  
90    static public class TemplateComparator implements Comparator<TemplateData> {
91      public int compare(TemplateData t1, TemplateData t2) throws ClassCastException {
92        String name1 = t1.getName();
93        String name2 = t2.getName();
94        return name1.compareToIgnoreCase(name2);
95      }
96    }
97  
98    static public class EditActionListener extends EventListener<UITemplateList> {
99      public void execute(Event<UITemplateList> event) throws Exception {
100       UITemplateList nodeTemplateList = event.getSource() ;
101 
102       UITemplatesManager uiManager = nodeTemplateList.getAncestorOfType(UITemplatesManager.class);
103       UITemplateContainer uiTemplateContainer = uiManager.getChildById(uiManager.getSelectedTabId()) ;
104 
105       String nodeType = event.getRequestContext().getRequestParameter(OBJECTID) ;
106       UIViewTemplate uiViewTemplate = uiTemplateContainer.createUIComponent(UIViewTemplate.class, null, 
107                                                                             "UIViewTemplate" + "_" + uiManager.getSelectedTabId()) ;
108       uiViewTemplate.getChild(UITemplateEditForm.class).update(nodeType) ;
109       uiViewTemplate.setNodeTypeName(nodeType) ;
110       UIDialogTab uiDialogTab = uiViewTemplate.findFirstComponentOfType(UIDialogTab.class) ;
111       uiDialogTab.updateGrid(nodeType) ;
112       UITemplateContent uiDialogTabForm = uiViewTemplate.findComponentById(UIDialogTab.DIALOG_FORM_NAME) ;
113       uiDialogTabForm.setNodeTypeName(nodeType) ;
114       uiDialogTabForm.update(null) ;
115       UIViewTab uiViewTab = uiViewTemplate.findFirstComponentOfType(UIViewTab.class) ;
116       uiViewTab.updateGrid(nodeType) ;
117       UITemplateContent uiViewTabForm = uiViewTemplate.findComponentById(UIViewTab.VIEW_FORM_NAME) ;
118       uiViewTabForm.setNodeTypeName(nodeType) ;
119       uiViewTabForm.update(null) ;
120       UISkinTab uiSkinTab = uiViewTemplate.findFirstComponentOfType(UISkinTab.class) ;
121       uiSkinTab.updateGrid(nodeType) ;
122       UITemplateContent uiSkinTabForm = uiViewTemplate.findComponentById(UISkinTab.SKIN_FORM_NAME) ;
123       uiSkinTabForm.setNodeTypeName(nodeType) ;
124       uiSkinTabForm.update(null);      
125       uiManager.initPopup(uiViewTemplate);
126       UIPopupWindow uiPopup = uiManager.getChildById(UITemplatesManager.POPUP_TEMPLATE_ID);
127       event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup) ;
128     }
129 
130   }
131 
132   static public class DeleteActionListener extends EventListener<UITemplateList> {
133     public void execute(Event<UITemplateList> event) throws Exception {
134       UITemplatesManager uiTemplatesManager = event.getSource().getAncestorOfType(UITemplatesManager.class);
135       UITemplateContainer uiTemplateContainer = uiTemplatesManager.getChildById(uiTemplatesManager.getSelectedTabId());
136       UITemplateList uiTemplateList = uiTemplateContainer.getChild(UITemplateList.class);
137 
138       if (uiTemplatesManager.isEditingTemplate()) {
139         UIApplication uiApp = event.getSource().getAncestorOfType(UIApplication.class) ;
140         uiApp.addMessage(new ApplicationMessage("UITemplateList.msg.editing-template", null, ApplicationMessage.WARNING)) ;
141         return;
142       }
143 
144       String nodeType = event.getRequestContext().getRequestParameter(OBJECTID);
145       TemplateService templateService = uiTemplateList.getApplicationComponent(TemplateService.class);
146       try {
147         DocumentContext.getCurrent().getAttributes().put(DocumentContext.IS_SKIP_RAISE_ACT, true);
148         templateService.removeManagedNodeType(nodeType);
149       } catch (PathNotFoundException ex) {
150         UIApplication uiApp = event.getSource().getAncestorOfType(UIApplication.class) ;
151         uiApp.addMessage(new ApplicationMessage("UITemplateList.msg.template-not-exist", null, ApplicationMessage.WARNING)) ;
152         return;
153       }
154       uiTemplateList.refresh(uiTemplateList.getUIPageIterator().getCurrentPage());
155       event.getRequestContext().addUIComponentToUpdateByAjax(uiTemplatesManager);
156     }
157   }
158 
159   static public class AddNewActionListener extends EventListener<UITemplateList> {
160     public void execute(Event<UITemplateList> event) throws Exception {
161       UITemplatesManager uiTemplatesManager = event.getSource().getAncestorOfType(UITemplatesManager.class) ;      
162       UITemplateContainer uiTemplateContainer = uiTemplatesManager.getChildById(uiTemplatesManager.getSelectedTabId());
163       UITemplateList uiList = uiTemplateContainer.getChild(UITemplateList.class);
164       UITemplateForm uiTemplateForm = uiTemplateContainer.createUIComponent(UITemplateForm.class, null, null) ;
165       uiTemplateForm.setFilter(uiList.getTemplateFilter());         
166       uiTemplateForm.refresh();
167       uiTemplatesManager.initPopup(uiTemplateForm);
168       UIPopupWindow uiPopup = uiTemplatesManager.getChildById(UITemplatesManager.POPUP_TEMPLATE_ID);
169       event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup) ;
170     }
171   }
172 
173   static public class TemplateData {
174     private String name ;
175     private String label;
176     private String icon;
177 
178     public TemplateData(String dataName, String dataLabel, String dataIcon) { 
179       name = dataName ;
180       label = dataLabel;
181       icon = dataIcon;
182     }
183     public String getName() { return name ; }
184     public String getLabel() { return label; }
185     public String getIcon() { return icon; }
186   }
187 
188   @Override
189   public void refresh(int currentPage) throws Exception {
190     TemplateService templateService = getApplicationComponent(TemplateService.class);
191     List<String> documentNodeTypes = templateService.getAllDocumentNodeTypes();
192     Node templatesHome = templateService.getTemplatesHome(WCMCoreUtils.getSystemSessionProvider());
193     List<TemplateData> templateData = new ArrayList<TemplateData>();
194     if (templatesHome != null) {
195       NodeTypeManager ntManager = templatesHome.getSession().getWorkspace().getNodeTypeManager();      
196       NodeTypeIterator nodetypeIter = ntManager.getAllNodeTypes();      
197       List<String> listNodeTypeName = new ArrayList<String>();
198       while (nodetypeIter.hasNext()) {
199         NodeType n1 = nodetypeIter.nextNodeType();
200         listNodeTypeName.add(n1.getName());
201       }
202       NodeIterator nodes = templatesHome.getNodes();
203       String label = "";
204       String icon = "";
205       while (nodes.hasNext()) {
206         Node node = nodes.nextNode();        
207         if (listNodeTypeName.contains(node.getName())) {
208           label = node.hasProperty(LABEL_PROPERTY) ? node.getProperty(LABEL_PROPERTY).getString() : node.getName();
209           icon = "uiIcon16x16Template" + label.replaceAll(" ", "");
210           icon = icon + " uiIconFile";
211           if(filter.equals(DOCUMENTS_TEMPLATE_TYPE)) {        		
212             if(documentNodeTypes.contains(node.getName()))
213               templateData.add(new TemplateData(node.getName(), label, icon));
214           } else if(filter.equals(ACTIONS_TEMPLATE_TYPE)) {
215             if(ntManager.getNodeType(node.getName()).isNodeType("exo:action")) templateData.add(new TemplateData(node.getName(), 
216                                                                                                                  label, icon ));
217           } else {
218             if(!ntManager.getNodeType(node.getName()).isNodeType("exo:action") && !documentNodeTypes.contains(node.getName())) 
219               templateData.add(new TemplateData(node.getName(), label, icon ));
220           }          
221         }
222       }
223       Collections.sort(templateData, new TemplateComparator());
224     }
225     ListAccess<TemplateData> dataList = new ListAccessImpl<TemplateData>(TemplateData.class,
226         templateData);
227     LazyPageList<TemplateData> pageList = new LazyPageList<TemplateData>(dataList,
228         getUIPageIterator().getItemsPerPage());
229     getUIPageIterator().setTotalItems(templateData.size());
230     getUIPageIterator().setPageList(pageList);
231     if (currentPage > getUIPageIterator().getAvailablePage())
232       getUIPageIterator().setCurrentPage(getUIPageIterator().getAvailablePage());
233     else
234       getUIPageIterator().setCurrentPage(currentPage);
235 
236   }
237 }