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 = UIDialogTab.EditActionListener.class),
53        @EventConfig(listeners = UIDialogTab.DeleteActionListener.class, confirm = "UIDialogTab.msg.confirm-delete")
54      }
55  )
56  
57  public class UIDialogTab 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 DIALOG_LIST_NAME = "DialogList" ;
62    final public static String DIALOG_FORM_NAME = "DialogForm" ;
63  
64    private List<String> listDialog_ = new ArrayList<String>() ;
65  
66    public UIDialogTab() throws Exception {
67      UIGrid uiGrid = addChild(UIGrid.class, null, DIALOG_LIST_NAME) ;
68      uiGrid.getUIPageIterator().setId("DialogListIterator") ;
69      uiGrid.configure("name", BEAN_FIELD, ACTIONS) ;
70      UITemplateContent uiForm = addChild(UITemplateContent.class, null , DIALOG_FORM_NAME) ;
71      uiForm.setTemplateType(TemplateService.DIALOGS);
72      uiForm.update(null) ;
73    }
74  
75    public List<String> getListDialog() { return listDialog_ ; }
76    
77    public void updateGrid(String nodeName) throws Exception {
78      TemplateService tempService = getApplicationComponent(TemplateService.class) ;
79      NodeIterator iter = tempService.getAllTemplatesOfNodeType(true, nodeName, WCMCoreUtils.getSystemSessionProvider()) ;
80      List<DialogData> data = new ArrayList<DialogData>() ;
81      DialogData 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        listDialog_.add(node.getName()) ;
95        item = new DialogData(node.getName(), rule.toString(), version) ;
96        data.add(item) ;
97      }
98      UIGrid uiGrid = getChild(UIGrid.class) ;
99      ListAccess<DialogData> dialogDataList = new ListAccessImpl<DialogData>(DialogData.class, data);
100     LazyPageList<DialogData> dataPageList = new LazyPageList<DialogData>(dialogDataList, 4);
101     uiGrid.getUIPageIterator().setPageList(dataPageList);
102   }
103 
104   public void setTabRendered() {
105     UIViewTemplate uiViewTemplate = getAncestorOfType(UIViewTemplate.class) ;
106     uiViewTemplate.setSelectedTab(UIDialogTab.class.getSimpleName()) ;
107   }
108 
109   static public class EditActionListener extends EventListener<UIDialogTab> {
110     public void execute(Event<UIDialogTab> event) throws Exception {
111       UIDialogTab dialogTab = event.getSource() ;
112       String dialogName = event.getRequestContext().getRequestParameter(OBJECTID) ;
113       UITemplateContent uiForm = dialogTab.getChild(UITemplateContent.class) ;
114       uiForm.update(dialogName) ;
115       dialogTab.setTabRendered() ;
116       UITemplatesManager uiManager = dialogTab.getAncestorOfType(UITemplatesManager.class) ;
117       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
118     }
119   }
120 
121   static public class DeleteActionListener extends EventListener<UIDialogTab> {
122     public void execute(Event<UIDialogTab> event) throws Exception {
123       UIDialogTab dialogTab = event.getSource() ;
124       UIViewTemplate uiViewTemplate = event.getSource().getAncestorOfType(UIViewTemplate.class) ;
125       String nodeTypeName = uiViewTemplate.getNodeTypeName() ;
126       String templateName = event.getRequestContext().getRequestParameter(OBJECTID) ;
127       TemplateService templateService = dialogTab.getApplicationComponent(TemplateService.class) ;
128       UITemplateContent uiForm = dialogTab.findFirstComponentOfType(UITemplateContent.class) ;
129       for(String template : TemplateService.UNDELETABLE_TEMPLATES) {
130         if(template.equals(templateName)) {
131           UIApplication app = dialogTab.getAncestorOfType(UIApplication.class) ;
132           Object[] args = {template} ;
133           app.addMessage(new ApplicationMessage("UIDialogTab.msg.undeletable", args, ApplicationMessage.WARNING)) ;
134           dialogTab.setTabRendered() ;
135           return ;
136         }
137       }
138       templateService.removeTemplate(TemplateService.DIALOGS, nodeTypeName, templateName) ;
139       uiForm.update(null);
140       uiForm.reset();
141 
142       dialogTab.updateGrid(nodeTypeName) ;
143       dialogTab.setTabRendered() ;
144       UITemplatesManager uiManager = dialogTab.getAncestorOfType(UITemplatesManager.class) ;
145       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
146     }
147   }
148 
149   public static class DialogData {
150     private String name ;
151     private String roles ;
152     private String baseVersion ;
153 
154     public DialogData(String name, String roles, String version) {
155       this.name = name ;
156       this.roles = roles ;
157       baseVersion = version ;
158     }
159 
160     public String getName(){return name ;}
161     public String getRoles(){return roles ;}
162     public String getBaseVersion(){return baseVersion ;}
163   }
164 }