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.List;
21  
22  import javax.jcr.Node;
23  import javax.jcr.NodeIterator;
24  import javax.jcr.Value;
25  
26  import org.exoplatform.commons.utils.LazyPageList;
27  import org.exoplatform.commons.utils.ListAccess;
28  import org.exoplatform.commons.utils.ListAccessImpl;
29  import org.exoplatform.ecm.webui.utils.Utils;
30  import org.exoplatform.services.cms.templates.TemplateService;
31  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
32  import org.exoplatform.web.application.ApplicationMessage;
33  import org.exoplatform.webui.config.annotation.ComponentConfig;
34  import org.exoplatform.webui.config.annotation.EventConfig;
35  import org.exoplatform.webui.core.UIApplication;
36  import org.exoplatform.webui.core.UIContainer;
37  import org.exoplatform.webui.core.UIGrid;
38  import org.exoplatform.webui.core.lifecycle.UIContainerLifecycle;
39  import org.exoplatform.webui.event.Event;
40  import org.exoplatform.webui.event.EventListener;
41  
42  /**
43   * Created by The eXo Platform SARL
44   * Author : pham tuan
45   *          phamtuanchip@yahoo.de
46   * Oct 03, 2006
47   * 9:43:23 AM
48   */
49  @ComponentConfig(
50      lifecycle = UIContainerLifecycle.class,
51      events = {
52        @EventConfig(listeners = UIViewTab.EditActionListener.class),
53        @EventConfig(listeners = UIViewTab.DeleteActionListener.class, confirm = "UIViewTab.msg.confirm-delete")
54      }
55  )
56  
57  public class UIViewTab extends UIContainer {
58  
59    final private static String[] BEAN_FIELD = {"name", "roles", "baseVersion"} ;
60    final private static String[] ACTIONS = {"Edit", "Delete"} ;
61    final public static String VIEW_LIST_NAME = "VewList" ;
62    final public static String VIEW_FORM_NAME = "ViewForm" ;
63  
64    private List<String> listView_ = new ArrayList<String>() ;
65  
66    public UIViewTab() throws Exception {
67      UIGrid uiGrid = addChild(UIGrid.class, null, VIEW_LIST_NAME) ;
68      uiGrid.getUIPageIterator().setId("ViewListIterator") ;
69      uiGrid.configure("name", BEAN_FIELD, ACTIONS) ;
70      UITemplateContent uiForm = addChild(UITemplateContent.class, null , VIEW_FORM_NAME) ;
71      uiForm.setTemplateType(TemplateService.VIEWS);
72      uiForm.update(null);
73    }
74  
75    public List<String> getListView() { return listView_ ; }
76  
77    public void updateGrid(String nodeName) throws Exception {
78      TemplateService tempService = getApplicationComponent(TemplateService.class) ;
79      NodeIterator iter = tempService.getAllTemplatesOfNodeType(false, nodeName, WCMCoreUtils.getSystemSessionProvider()) ;
80      List<ViewData> data = new ArrayList<ViewData>() ;
81      ViewData item  ;
82      if(iter == null) return;
83      while(iter.hasNext()) {
84        Node node = (Node) iter.next() ;
85        String version = "" ;
86        StringBuilder rule = new StringBuilder() ;
87        Value[] rules = node.getNode(Utils.JCR_CONTENT).getProperty(Utils.EXO_ROLES).getValues() ;
88        for(int i = 0; i < rules.length; i++) {
89          rule.append("["+rules[i].getString()+"]") ;
90        }
91        if(node.isNodeType(Utils.MIX_VERSIONABLE) && !node.isNodeType(Utils.NT_FROZEN)) {
92          version = node.getBaseVersion().getName();
93        }
94        listView_.add(node.getName()) ;
95        item = new ViewData(node.getName(), rule.toString(), version) ;
96        data.add(item);
97      }
98      UIGrid uiGrid = getChild(UIGrid.class) ;
99      ListAccess<ViewData> viewDataList = new ListAccessImpl<ViewData>(ViewData.class, data);
100     LazyPageList<ViewData> dataPageList = new LazyPageList<ViewData>(viewDataList, 4);
101     uiGrid.getUIPageIterator().setPageList(dataPageList);
102   }
103 
104   public void setTabRendered() {
105     UIViewTemplate uiViewTemplate = getAncestorOfType(UIViewTemplate.class) ;
106 
107     uiViewTemplate.setSelectedTab(UIViewTab.class.getSimpleName()) ;
108   }
109 
110   static public class EditActionListener extends EventListener<UIViewTab> {
111     public void execute(Event<UIViewTab> event) throws Exception {
112       UIViewTab viewTab = event.getSource() ;
113       String viewName = event.getRequestContext().getRequestParameter(OBJECTID) ;
114       UITemplateContent uiForm = viewTab.getChild(UITemplateContent.class) ;
115       uiForm.update(viewName) ;
116       viewTab.setTabRendered() ;
117       UITemplatesManager uiManager = viewTab.getAncestorOfType(UITemplatesManager.class) ;
118       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
119     }
120   }
121 
122   static public class DeleteActionListener extends EventListener<UIViewTab> {
123     public void execute(Event<UIViewTab> event) throws Exception {
124       UIViewTab viewTab = event.getSource() ;
125       UIViewTemplate uiViewTemplate = event.getSource().getAncestorOfType(UIViewTemplate.class) ;
126       String nodeTypeName = uiViewTemplate.getNodeTypeName() ;
127       String templateName = event.getRequestContext().getRequestParameter(OBJECTID) ;
128       TemplateService templateService = viewTab.getApplicationComponent(TemplateService.class) ;
129       for(String template : TemplateService.UNDELETABLE_TEMPLATES) {
130         if(template.equals(templateName)){
131           UIApplication app = viewTab.getAncestorOfType(UIApplication.class) ;
132           Object[] args = {template} ;
133           app.addMessage(new ApplicationMessage("UIViewTab.msg.undeletable", args, ApplicationMessage.WARNING)) ;
134           viewTab.setTabRendered() ;
135           return ;
136         }
137       }
138       templateService.removeTemplate(TemplateService.VIEWS, nodeTypeName, templateName) ;
139       UITemplateContent uiForm = viewTab.findFirstComponentOfType(UITemplateContent.class) ;
140       uiForm.update(null);
141       uiForm.reset();
142       viewTab.updateGrid(nodeTypeName) ;
143       viewTab.setTabRendered() ;
144       UITemplatesManager uiManager = viewTab.getAncestorOfType(UITemplatesManager.class) ;
145       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
146     }
147   }
148 
149   public static class ViewData {
150     private String name ;
151     private String roles ;
152     private String baseVersion ;
153 
154     public ViewData(String name, String roles, String version) {
155       this.name = name ;
156       this.roles = roles ;
157       baseVersion = version ;
158     }
159     public String getName(){return name ; }
160     public String getRoles(){return roles ; }
161     public String getBaseVersion(){return baseVersion ; }
162   }
163 }