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.metadata;
18  
19  import org.exoplatform.commons.utils.HTMLSanitizer;
20  import org.exoplatform.ecm.resolver.JCRResourceResolver;
21  import org.exoplatform.ecm.webui.component.admin.UIECMAdminPortlet;
22  import org.exoplatform.ecm.webui.form.UIFormInputSetWithAction;
23  import org.exoplatform.ecm.webui.selector.UISelectable;
24  import org.exoplatform.groovyscript.text.TemplateService;
25  import org.exoplatform.services.cms.metadata.MetadataService;
26  import org.exoplatform.wcm.webui.form.UIFormInputSetWithNoLabel;
27  import org.exoplatform.webui.config.annotation.ComponentConfig;
28  import org.exoplatform.webui.config.annotation.EventConfig;
29  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
30  import org.exoplatform.webui.event.Event;
31  import org.exoplatform.webui.event.Event.Phase;
32  import org.exoplatform.webui.event.EventListener;
33  import org.exoplatform.webui.form.UIFormStringInput;
34  import org.exoplatform.webui.form.UIFormTabPane;
35  import org.exoplatform.webui.form.UIFormTextAreaInput;
36  import org.exoplatform.webui.form.validator.MandatoryValidator;
37  
38  /**
39   * Created by The eXo Platform SARL Author : Dang Van Minh
40   * minh.dang@exoplatform.com Sep 19, 2006 5:31:04 PM
41   */
42  @ComponentConfig(lifecycle = UIFormLifecycle.class, template = "system:/groovy/webui/form/UIFormTabPane.gtmpl", events = {
43      @EventConfig(listeners = UIMetadataForm.SaveActionListener.class),
44      @EventConfig(listeners = UIMetadataForm.CancelActionListener.class, phase = Phase.DECODE),
45      @EventConfig(listeners = UIMetadataForm.AddPermissionActionListener.class, phase = Phase.DECODE),
46      @EventConfig(listeners = UIMetadataForm.SelectTabActionListener.class, phase = Phase.DECODE) })
47  public class UIMetadataForm extends UIFormTabPane implements UISelectable {
48  
49    final static public String METADATA_PATH    = "metadataPath";
50  
51    final static public String MAPPING          = "mapping";
52  
53    final static public String METADATA_MAPPING = "metadataMapping";
54  
55    final static public String NT_UNSTRUCTURED  = "nt:unstructured";
56  
57    final static public String DIALOG_TEMPLATE  = "dialogTemplate";
58  
59    final static public String VIEW_TEMPLATE    = "viewTemplate";
60  
61    final static public String METADATA_NAME    = "metadataName";
62  
63    final static public String METADATA_LABEL   = "metadataLabel";
64  
65    final static public String VIEW_PERMISSION  = "viewPermission";
66  
67    final static public String METADATA_TAB     = "metadataTypeTab";
68  
69    final static public String DIALOG_TAB       = "dialogTab";
70  
71    final static public String VIEW_TAB         = "viewTab";
72  
73    private boolean            isAddNew_        = false;
74  
75    private String             metadataName_;
76  
77    private String             workspaceName_;
78  
79    public UIMetadataForm() throws Exception {
80      super("UIMetadataForm");
81      UIFormInputSetWithAction uiMetadataType = new UIFormInputSetWithAction(METADATA_TAB);
82      uiMetadataType.addUIFormInput(new UIFormStringInput(METADATA_NAME, METADATA_NAME, null));
83      uiMetadataType.addUIFormInput(new UIFormStringInput(VIEW_PERMISSION, VIEW_PERMISSION, null).addValidator(MandatoryValidator.class)
84                                                                                                 .setDisabled(true));
85      uiMetadataType.addUIFormInput(new UIFormStringInput(METADATA_LABEL, METADATA_LABEL, null));
86      uiMetadataType.setActionInfo(VIEW_PERMISSION, new String[] { "AddPermission" });
87      addUIComponentInput(uiMetadataType);
88      setSelectedTab(uiMetadataType.getId());
89      UIFormInputSetWithNoLabel uiDialogTab = new UIFormInputSetWithNoLabel(DIALOG_TAB);
90      uiDialogTab.addUIFormInput(new UIFormTextAreaInput(DIALOG_TEMPLATE, DIALOG_TEMPLATE, null));
91      addUIComponentInput(uiDialogTab);
92      UIFormInputSetWithNoLabel uiViewTab = new UIFormInputSetWithNoLabel(VIEW_TAB);
93      uiViewTab.addUIFormInput(new UIFormTextAreaInput(VIEW_TEMPLATE, VIEW_TEMPLATE, null));
94      addUIComponentInput(uiViewTab);
95      setActions(new String[] { "Save", "Cancel" });
96    }
97  
98    public void doSelect(String selectField, Object value) {
99      getUIStringInput(VIEW_PERMISSION).setValue(value.toString());
100     UIMetadataManager uiManager = getAncestorOfType(UIMetadataManager.class);
101     uiManager.removeChildById(UIMetadataManager.PERMISSION_POPUP);
102   }
103 
104   public void update(String metadata) throws Exception {
105     metadataName_ = metadata;
106     MetadataService metadataService = getApplicationComponent(MetadataService.class);
107     workspaceName_ = getAncestorOfType(UIECMAdminPortlet.class).getPreferenceWorkspace();
108     getUIStringInput(METADATA_NAME).setValue(metadata);
109     getUIStringInput(METADATA_LABEL).setValue(metadataService.getMetadataLabel(metadata));
110     String dialogTemplate = metadataService.getMetadataTemplate(metadata, true);
111     String viewTemplate = metadataService.getMetadataTemplate(metadata, false);
112     String role = metadataService.getMetadataRoles(metadata, true);
113     getUIStringInput(METADATA_NAME).setDisabled(true);
114     getUIStringInput(VIEW_PERMISSION).setValue(role);
115     getUIFormTextAreaInput(DIALOG_TEMPLATE).setValue(dialogTemplate);
116     getUIFormTextAreaInput(VIEW_TEMPLATE).setValue(viewTemplate);
117   }
118 
119   static public class SaveActionListener extends EventListener<UIMetadataForm> {
120     public void execute(Event<UIMetadataForm> event) throws Exception {
121       UIMetadataForm uiForm = event.getSource();
122       UIMetadataManager uiMetaManager = uiForm.getAncestorOfType(UIMetadataManager.class);
123       MetadataService metadataService = uiForm.getApplicationComponent(MetadataService.class);
124       String roles = uiForm.getUIStringInput(VIEW_PERMISSION).getValue();
125       String dialogTemplate = uiForm.getUIFormTextAreaInput(DIALOG_TEMPLATE).getValue();
126       if (dialogTemplate == null)
127         dialogTemplate = "";
128       String viewTemplate = uiForm.getUIFormTextAreaInput(VIEW_TEMPLATE).getValue();
129       if (viewTemplate == null)
130         viewTemplate = "";
131       if (!metadataService.hasMetadata(uiForm.metadataName_))
132         uiForm.isAddNew_ = true;
133       else
134         uiForm.isAddNew_ = false;
135       String metaLabel = HTMLSanitizer.sanitize(uiForm.getUIStringInput(METADATA_LABEL).getValue());
136       JCRResourceResolver resourceResolver = new JCRResourceResolver(uiForm.workspaceName_);
137       TemplateService templateService = uiForm.getApplicationComponent(TemplateService.class);
138       String path = metadataService.addMetadata(uiForm.metadataName_, true, roles, dialogTemplate, metaLabel, uiForm.isAddNew_);
139       if (path != null)
140         templateService.invalidateTemplate(path, resourceResolver);
141       path = metadataService.addMetadata(uiForm.metadataName_, false, roles, viewTemplate, metaLabel, uiForm.isAddNew_);
142       if (path != null)
143         templateService.invalidateTemplate(path, resourceResolver);
144       uiForm.reset();
145       uiMetaManager.getChild(UIMetadataList.class).refresh(1);
146       uiMetaManager.removeChildById(UIMetadataManager.METADATA_POPUP);
147       event.getRequestContext().addUIComponentToUpdateByAjax(uiMetaManager);
148     }
149   }
150 
151   static public class AddPermissionActionListener extends EventListener<UIMetadataForm> {
152     public void execute(Event<UIMetadataForm> event) throws Exception {
153       UIMetadataForm uiView = event.getSource();
154       UIMetadataManager uiManager = uiView.getAncestorOfType(UIMetadataManager.class);
155       String membership = uiView.getUIStringInput(VIEW_PERMISSION).getValue();
156       uiManager.initPopupPermission(membership);
157       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager);
158     }
159   }
160 
161   static public class CancelActionListener extends EventListener<UIMetadataForm> {
162     public void execute(Event<UIMetadataForm> event) throws Exception {
163       UIMetadataForm uiView = event.getSource();
164       UIMetadataManager uiMetaManager = uiView.getAncestorOfType(UIMetadataManager.class);
165       uiMetaManager.removeChildById(UIMetadataManager.METADATA_POPUP);
166       event.getRequestContext().addUIComponentToUpdateByAjax(uiMetaManager);
167     }
168   }
169 
170   static public class SelectTabActionListener extends EventListener<UIMetadataForm> {
171     public void execute(Event<UIMetadataForm> event) throws Exception {
172       UIMetadataForm uiView = event.getSource();
173       UIMetadataManager uiMetaManager = uiView.getAncestorOfType(UIMetadataManager.class);
174       event.getRequestContext().addUIComponentToUpdateByAjax(uiMetaManager);
175     }
176   }
177 }