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 javax.jcr.Node;
20  
21  import org.exoplatform.services.cms.templates.TemplateService;
22  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
23  import org.exoplatform.webui.config.annotation.ComponentConfig;
24  import org.exoplatform.webui.config.annotation.EventConfig;
25  import org.exoplatform.webui.core.UIPopupWindow;
26  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
27  import org.exoplatform.webui.event.Event;
28  import org.exoplatform.webui.event.Event.Phase;
29  import org.exoplatform.webui.event.EventListener;
30  import org.exoplatform.webui.form.UIForm;
31  import org.exoplatform.webui.form.UIFormStringInput;
32  import org.exoplatform.webui.form.input.UICheckBoxInput;
33  
34  /**
35   * Created by The eXo Platform SARL
36   * Author : Pham Tuan
37   *          phamtuanchip@yahoo.de
38   * Dec 4, 2006 9:50:06 AM
39   */
40  @ComponentConfig(
41                   lifecycle = UIFormLifecycle.class,
42                   template = "system:/groovy/webui/form/UIForm.gtmpl",
43                   events = {
44                     @EventConfig(listeners = UITemplateEditForm.SaveActionListener.class),
45                     @EventConfig(phase=Phase.DECODE, listeners = UITemplateEditForm.CancelActionListener.class)
46                   }
47      )
48  
49  public class UITemplateEditForm extends UIForm {
50  
51    final static public String FIELD_NAME = "name" ;
52    final static public String FIELD_LABEL = "label" ;
53    final static public String FIELD_ISTEMPLATE = "isDocumentTemplate" ;
54  
55    public UITemplateEditForm() {
56      addChild(new UIFormStringInput(FIELD_NAME, null)) ;
57      addChild(new UIFormStringInput(FIELD_LABEL, null)) ;
58      addChild(new UICheckBoxInput(FIELD_ISTEMPLATE, null, null)) ;
59    }
60  
61    private boolean isDocumentTemplate(String nodeType)throws Exception {
62      TemplateService templateService = getApplicationComponent(TemplateService.class) ;
63      return templateService.getDocumentTemplates().contains(nodeType) ;
64    }
65  
66    public void update(String nodeType) throws Exception {
67      TemplateService tempService = getApplicationComponent(TemplateService.class) ;
68      Node node = tempService.getTemplatesHome(WCMCoreUtils.getSystemSessionProvider()).getNode(nodeType) ;
69      String label = null ;
70      if(node.hasProperty(TemplateService.TEMPLATE_LABEL)) {
71        label = node.getProperty(TemplateService.TEMPLATE_LABEL).getString() ;
72      }
73      getUICheckBoxInput(FIELD_ISTEMPLATE).setChecked(isDocumentTemplate(nodeType)) ;
74      getUIStringInput(FIELD_NAME).setValue(nodeType) ;
75      getUIStringInput(FIELD_LABEL).setValue(label) ;
76      getUICheckBoxInput(FIELD_ISTEMPLATE).setDisabled(true);
77      getUIStringInput(FIELD_NAME).setDisabled(true);
78    }
79  
80    static public class SaveActionListener extends EventListener<UITemplateEditForm> {
81      public void execute(Event<UITemplateEditForm> event) throws Exception {
82        UITemplateEditForm uiForm = event.getSource() ;
83        TemplateService tempService = uiForm.getApplicationComponent(TemplateService.class) ;
84        String nodeType = ((UIFormStringInput)(event.getSource().getChildById("name"))).getValue();
85        Node node = tempService.getTemplatesHome(WCMCoreUtils.getSystemSessionProvider()).getNode(nodeType) ;
86        node.setProperty(TemplateService.TEMPLATE_LABEL,uiForm.getUIStringInput(FIELD_LABEL).getValue()) ;
87        node.save() ;
88        uiForm.reset() ;
89        UITemplatesManager uiManager = uiForm.getAncestorOfType(UITemplatesManager.class) ;
90        UITemplateContainer uiTemplateContainer = uiManager.getChildById(uiManager.getSelectedTabId());
91        UITemplateList uiList = uiTemplateContainer.getChild(UITemplateList.class);
92        uiList.refresh(uiList.getUIPageIterator().getCurrentPage());
93        UIPopupWindow uiPopupWindow = uiManager.getChildById(UITemplatesManager.POPUP_TEMPLATE_ID) ;
94        uiPopupWindow.setShow(false) ;
95        event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
96  
97      }
98    }
99  
100   static  public class CancelActionListener extends EventListener<UITemplateEditForm> {
101     public void execute(Event<UITemplateEditForm> event) throws Exception {    	     
102       UITemplatesManager uiManager = event.getSource().getAncestorOfType(UITemplatesManager.class) ;
103       UIPopupWindow uiPopupWindow = uiManager.getChildById(UITemplatesManager.POPUP_TEMPLATE_ID) ;
104       uiPopupWindow.setShow(false) ;
105       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
106     }
107   }
108 }