1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.exoplatform.ecm.webui.component.admin.templates.clv;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.exoplatform.ecm.webui.form.validator.ECMNameValidator;
22 import org.exoplatform.ecm.webui.utils.Utils;
23 import org.exoplatform.services.cms.templates.TemplateService;
24 import org.exoplatform.services.cms.views.ApplicationTemplateManagerService;
25 import org.exoplatform.services.wcm.core.NodetypeConstant;
26 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
27 import org.exoplatform.web.application.ApplicationMessage;
28 import org.exoplatform.web.application.RequestContext;
29 import org.exoplatform.webui.config.annotation.ComponentConfig;
30 import org.exoplatform.webui.config.annotation.EventConfig;
31 import org.exoplatform.webui.core.UIApplication;
32 import org.exoplatform.webui.core.UIPopupWindow;
33 import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
34 import org.exoplatform.webui.core.model.SelectItemOption;
35 import org.exoplatform.webui.event.Event;
36 import org.exoplatform.webui.event.Event.Phase;
37 import org.exoplatform.webui.event.EventListener;
38 import org.exoplatform.webui.form.UIForm;
39 import org.exoplatform.webui.form.UIFormSelectBox;
40 import org.exoplatform.webui.form.UIFormStringInput;
41 import org.exoplatform.webui.form.UIFormTextAreaInput;
42 import org.exoplatform.webui.form.validator.MandatoryValidator;
43
44 import javax.jcr.Node;
45 import javax.jcr.PathNotFoundException;
46 import java.io.ByteArrayInputStream;
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.ResourceBundle;
50
51
52
53
54
55
56
57
58 @ComponentConfig(
59 lifecycle = UIFormLifecycle.class,
60 template = "system:/groovy/webui/form/UIForm.gtmpl",
61 events = {
62 @EventConfig(listeners = UICLVTemplateForm.SaveActionListener.class),
63 @EventConfig(listeners = UICLVTemplateForm.ResetActionListener.class, phase=Phase.DECODE),
64 @EventConfig(listeners = UICLVTemplateForm.CancelActionListener.class, phase=Phase.DECODE)
65 }
66 )
67 public class UICLVTemplateForm extends UIForm {
68
69 final static public String FIELD_TITLE = "title" ;
70 final static public String FIELD_TEMPLATE_NAME = "template" ;
71 final static public String FIELD_CONTENT = "content" ;
72 final static public String FIELD_CONTENT_TYPE = "type" ;
73
74 private boolean isAddNew;
75 private String selectedCategory;
76
77 public UICLVTemplateForm() throws Exception {
78
79 UIFormTextAreaInput contentInput = new UIFormTextAreaInput(FIELD_CONTENT, FIELD_CONTENT, null);
80 addUIFormInput(contentInput);
81 UIFormStringInput nameInput = new UIFormStringInput(FIELD_TITLE, FIELD_TITLE, null);
82 nameInput.addValidator(ECMNameValidator.class);
83 addUIFormInput(nameInput);
84 UIFormStringInput tempateNameInput = new UIFormStringInput(FIELD_TEMPLATE_NAME, FIELD_TEMPLATE_NAME, null);
85 tempateNameInput.addValidator(MandatoryValidator.class).addValidator(ECMNameValidator.class);
86 addUIFormInput(tempateNameInput);
87 List<SelectItemOption<String>> templateOptions = new ArrayList<SelectItemOption<String>>();
88 RequestContext context = RequestContext.getCurrentInstance();
89 ResourceBundle res = context.getApplicationResourceBundle();
90 templateOptions.add(new SelectItemOption<String>(
91 res.getString("UICLVTemplateForm.label." + ApplicationTemplateManagerService.CLV_LIST_TEMPLATE_CATEGORY),
92 ApplicationTemplateManagerService.CLV_LIST_TEMPLATE_CATEGORY));
93 templateOptions.add(new SelectItemOption<String>(
94 res.getString("UICLVTemplateForm.label." + ApplicationTemplateManagerService.CLV_NAVIGATION_TEMPLATE_CATEGORY),
95 ApplicationTemplateManagerService.CLV_NAVIGATION_TEMPLATE_CATEGORY));
96 templateOptions.add(new SelectItemOption<String>(
97 res.getString("UICLVTemplateForm.label." + ApplicationTemplateManagerService.CLV_PAGINATOR_TEMPLATE_CATEGORY),
98 ApplicationTemplateManagerService.CLV_PAGINATOR_TEMPLATE_CATEGORY ));
99 UIFormSelectBox templateType = new UIFormSelectBox(FIELD_CONTENT_TYPE, FIELD_CONTENT_TYPE, templateOptions);
100 addUIFormInput(templateType);
101 }
102
103 public void refresh(String category) throws Exception {
104 isAddNew = true;
105 selectedCategory = category;
106 setActions(new String[] {"Save", "Reset", "Cancel"});
107 getUIStringInput(FIELD_TITLE).setValue(StringUtils.EMPTY);
108 getUIStringInput(FIELD_TEMPLATE_NAME).setValue(StringUtils.EMPTY);
109 getUIStringInput(FIELD_TEMPLATE_NAME).setDisabled(false);
110 getUIFormTextAreaInput(FIELD_CONTENT).setValue(StringUtils.EMPTY);
111 getUIFormSelectBox(FIELD_CONTENT_TYPE).setValue(selectedCategory);
112 }
113
114 public void update(String category, String name) throws Exception {
115 this.isAddNew = false;
116 selectedCategory = category;
117 setActions(new String[] {"Save", "Cancel"});
118 ApplicationTemplateManagerService templateManager = WCMCoreUtils.getService(ApplicationTemplateManagerService.class);
119 Node templateNode = templateManager.getTemplateByName(ApplicationTemplateManagerService.CLV_TEMPLATE_STORAGE_FOLDER,
120 category, name, WCMCoreUtils.getSystemSessionProvider());
121 Node content = templateNode.getNode(Utils.JCR_CONTENT);
122 try {
123 getUIStringInput(FIELD_TITLE).setValue(content.getProperty(NodetypeConstant.DC_TITLE).getValues()[0].getString());
124 } catch(PathNotFoundException pne) {
125 getUIStringInput(FIELD_TITLE).setValue(templateNode.getName());
126 } catch(ArrayIndexOutOfBoundsException aoe) {
127 getUIStringInput(FIELD_TITLE).setValue(templateNode.getName());
128 }
129 getUIStringInput(FIELD_TEMPLATE_NAME).setValue(templateNode.getName());
130 getUIStringInput(FIELD_TEMPLATE_NAME).setDisabled(true);
131 getUIFormTextAreaInput(FIELD_CONTENT).setValue(content.getProperty(Utils.JCR_DATA).getString());
132 getUIFormSelectBox(FIELD_CONTENT_TYPE).setValue(category);
133 }
134
135 private void addTemplate(String category, String title, String template, String content) throws Exception {
136 TemplateService templateService = WCMCoreUtils.getService(TemplateService.class);
137 ApplicationTemplateManagerService appTemplateManager = WCMCoreUtils.getService(ApplicationTemplateManagerService.class);
138 if(content == null) content = StringUtils.EMPTY;
139 if(isAddNew) {
140 if(!template.contains(".gtmpl")) template = template + ".gtmpl";
141 if(title == null || title.length() == 0) title = template;
142 templateService.createTemplate(getCategoryByName(category), title,
143 template, new ByteArrayInputStream(content.getBytes()), new String[] { "*" });
144 } else {
145 if(hasTemplate(category, template)) {
146 if(!selectedCategory.equals(category)) {
147 UIApplication uiApp = getAncestorOfType(UIApplication.class);
148 uiApp.addMessage(new ApplicationMessage("UICLVTemplateForm.msg.template-existing", null, ApplicationMessage.WARNING));
149 return;
150 } else {
151 Node templateNode = getCategoryByName(category).getNode(template);
152 Node contentNode = templateNode.getNode(NodetypeConstant.JCR_CONTENT);
153 contentNode.setProperty(NodetypeConstant.JCR_DATA, new ByteArrayInputStream(content.getBytes()));
154 if(title == null || title.length() == 0) title = templateNode.getName();
155 contentNode.setProperty(NodetypeConstant.DC_TITLE, new String[] { title });
156 templateNode.save();
157 }
158 } else {
159 templateService.createTemplate(getCategoryByName(category), title,
160 template, new ByteArrayInputStream(content.getBytes()), new String[] { "*" });
161 appTemplateManager.removeTemplate(ApplicationTemplateManagerService.CLV_TEMPLATE_STORAGE_FOLDER,
162 selectedCategory, template, WCMCoreUtils.getSystemSessionProvider());
163 }
164
165 }
166 }
167
168 private boolean hasTemplate(String category, String template) throws Exception {
169 if(!template.contains(".gtmpl")) template = template + ".gtmpl";
170 return getCategoryByName(category).hasNode(template);
171 }
172
173 private Node getCategoryByName(String category) throws Exception {
174 ApplicationTemplateManagerService templateManager = WCMCoreUtils.getService(ApplicationTemplateManagerService.class);
175 Node templateHome = templateManager.getApplicationTemplateHome(
176 ApplicationTemplateManagerService.CLV_TEMPLATE_STORAGE_FOLDER, WCMCoreUtils.getSystemSessionProvider());
177 return templateHome.getNode(category);
178 }
179
180 static public class SaveActionListener extends EventListener<UICLVTemplateForm> {
181 public void execute(Event<UICLVTemplateForm> event) throws Exception {
182 UICLVTemplateForm uiForm = event.getSource() ;
183 UICLVTemplatesManager uiManager = uiForm.getAncestorOfType(UICLVTemplatesManager.class);
184 String title = uiForm.getUIStringInput(FIELD_TITLE).getValue();
185 if(title != null) title = title.trim();
186 String template = uiForm.getUIStringInput(FIELD_TEMPLATE_NAME).getValue();
187 if(template != null) template = template.trim();
188 String category = uiForm.getUIFormSelectBox(FIELD_CONTENT_TYPE).getValue();
189 String content = uiForm.getUIFormTextAreaInput(FIELD_CONTENT).getValue();
190 if(content != null) content = content.trim();
191 if(uiForm.isAddNew & uiForm.hasTemplate(category, template)) {
192 UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class);
193 uiApp.addMessage(new ApplicationMessage("UICLVTemplateForm.msg.template-existing", null, ApplicationMessage.WARNING));
194 return;
195 }
196 uiForm.addTemplate(category, title, template, content);
197 uiManager.refresh();
198 UIPopupWindow uiPopup = uiForm.getParent();
199 uiPopup.setRendered(false);
200 uiPopup.setShow(false);
201 event.getRequestContext().addUIComponentToUpdateByAjax(uiManager);
202 }
203 }
204
205 static public class ResetActionListener extends EventListener<UICLVTemplateForm> {
206 public void execute(Event<UICLVTemplateForm> event) throws Exception {
207 UICLVTemplateForm uiForm = event.getSource() ;
208 uiForm.refresh(uiForm.selectedCategory);
209 event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
210 }
211 }
212
213 static public class CancelActionListener extends EventListener<UICLVTemplateForm> {
214 public void execute(Event<UICLVTemplateForm> event) throws Exception {
215 UICLVTemplateForm uiForm = event.getSource() ;
216 UICLVTemplatesManager uiManager = uiForm.getAncestorOfType(UICLVTemplatesManager.class);
217 UIPopupWindow uiPopup = uiForm.getParent();
218 uiPopup.setRendered(false);
219 uiPopup.setShow(false);
220 event.getRequestContext().addUIComponentToUpdateByAjax(uiManager);
221 }
222 }
223 }