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.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 : Dang Van Minh
45   *          minh.dang@exoplatform.com
46   * Nov 18, 2009
47   * 10:37:42 AM
48   */
49  @ComponentConfig(
50      lifecycle = UIContainerLifecycle.class,
51      events = {
52        @EventConfig(listeners = UISkinTab.EditActionListener.class),
53        @EventConfig(listeners = UISkinTab.DeleteActionListener.class, confirm = "UIDialogTab.msg.confirm-delete")
54      }
55  )
56  public class UISkinTab extends UIContainer {
57  
58    final private static String[] BEAN_FIELD = {"name", "roles", "baseVersion"} ;
59    final private static String[] ACTIONS = {"Edit", "Delete"} ;
60    final public static String SKIN_LIST_NAME = "SkinList" ;
61    final public static String SKIN_FORM_NAME = "SkinForm" ;
62  
63    private List<String> listSkin_ = new ArrayList<String>() ;
64  
65    public UISkinTab() throws Exception {
66      UIGrid uiGrid = addChild(UIGrid.class, null, SKIN_LIST_NAME) ;
67      uiGrid.getUIPageIterator().setId("SkinListIterator") ;
68      uiGrid.configure("name", BEAN_FIELD, ACTIONS) ;
69      UITemplateContent uiForm = addChild(UITemplateContent.class, null , SKIN_FORM_NAME) ;
70      uiForm.setTemplateType(TemplateService.SKINS);
71      uiForm.update(null);
72    }
73  
74    public void setTabRendered() {
75      UIViewTemplate uiViewTemplate = getAncestorOfType(UIViewTemplate.class) ;
76      uiViewTemplate.setSelectedTab(UISkinTab.class.getSimpleName()) ;
77    }
78  
79    public List<String> getListSkin() { return listSkin_ ; }
80    
81    public void updateGrid(String nodeName) throws Exception {
82      TemplateService tempService = getApplicationComponent(TemplateService.class) ;
83      Node templateHome = tempService.getTemplatesHome(WCMCoreUtils.getSystemSessionProvider()).getNode(nodeName);
84      if(!templateHome.hasNode(TemplateService.SKINS)) return;
85      NodeIterator iter = templateHome.getNode(TemplateService.SKINS).getNodes();
86      List<SkinData> data = new ArrayList<SkinData>() ;
87      SkinData item  ;
88      Node node = null;
89      while (iter.hasNext()){
90        node = (Node) iter.next() ;
91        String version = "" ;
92        StringBuilder rule = new StringBuilder() ;
93        Value[] rules = node.getNode(Utils.JCR_CONTENT).getProperty(Utils.EXO_ROLES).getValues() ;
94        for(int i = 0; i < rules.length; i++) {
95          rule.append("["+rules[i].getString()+"]") ;
96        }
97        if(node.isNodeType(Utils.MIX_VERSIONABLE) && !node.isNodeType(Utils.NT_FROZEN)){
98          version = node.getBaseVersion().getName() ;
99        }
100       listSkin_.add(node.getName()) ;
101       item = new SkinData(node.getName(), rule.toString(), version) ;
102       data.add(item) ;
103     }
104     UIGrid uiGrid = getChild(UIGrid.class) ;
105     ListAccess<SkinData> skinDataList = new ListAccessImpl<SkinData>(SkinData.class, data);
106     LazyPageList<SkinData> dataPageList = new LazyPageList<SkinData>(skinDataList, 4);
107     uiGrid.getUIPageIterator().setPageList(dataPageList);
108   }  
109 
110   static public class EditActionListener extends EventListener<UISkinTab> {
111     public void execute(Event<UISkinTab> event) throws Exception {
112       UISkinTab skinTab = event.getSource() ;
113       String skinName = event.getRequestContext().getRequestParameter(OBJECTID) ;
114       UITemplateContent uiForm = skinTab.getChild(UITemplateContent.class) ;
115       uiForm.update(skinName) ;
116       skinTab.setTabRendered() ;
117       UITemplatesManager uiManager = skinTab.getAncestorOfType(UITemplatesManager.class) ;
118       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
119     }
120   }
121 
122   static public class DeleteActionListener extends EventListener<UISkinTab> {
123     public void execute(Event<UISkinTab> event) throws Exception {
124       UISkinTab skinTab = 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 = skinTab.getApplicationComponent(TemplateService.class) ;
129       UITemplateContent uiForm = skinTab.findFirstComponentOfType(UITemplateContent.class) ;
130       for(String template : TemplateService.UNDELETABLE_TEMPLATES) {
131         if(template.equals(templateName)) {
132           UIApplication app = skinTab.getAncestorOfType(UIApplication.class) ;
133           Object[] args = {template} ;
134           app.addMessage(new ApplicationMessage("UIDialogTab.msg.undeletable", args)) ;
135           skinTab.setTabRendered() ;
136           return ;
137         }
138       }
139       templateService.removeTemplate(TemplateService.SKINS, nodeTypeName, templateName) ;
140       uiForm.update(null);
141       uiForm.reset();
142 
143       skinTab.updateGrid(nodeTypeName) ;
144       skinTab.setTabRendered() ;
145       UITemplatesManager uiManager = skinTab.getAncestorOfType(UITemplatesManager.class) ;
146       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
147     }
148   }
149 
150   public static class SkinData {
151     private String name ;
152     private String roles ;
153     private String baseVersion ;
154 
155     public SkinData(String name, String roles, String version) {
156       this.name = name ;
157       this.roles = roles ;
158       baseVersion = version ;
159     }
160 
161     public String getName(){return name ;}
162     public String getRoles(){return roles ;}
163     public String getBaseVersion(){return baseVersion ;}
164   }
165 
166 }