1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.wcm.webui.fastcontentcreator.config;
18
19 import org.exoplatform.ecm.webui.selector.UISelectable;
20 import org.exoplatform.ecm.webui.tree.selectone.UIOneNodePathSelector;
21 import org.exoplatform.services.cms.templates.TemplateService;
22 import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
23 import org.exoplatform.services.jcr.core.ManageableRepository;
24 import org.exoplatform.services.log.ExoLogger;
25 import org.exoplatform.services.log.Log;
26 import org.exoplatform.services.wcm.core.NodeLocation;
27 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
28 import org.exoplatform.wcm.webui.Utils;
29 import org.exoplatform.wcm.webui.container.UIFormFieldSet;
30 import org.exoplatform.wcm.webui.fastcontentcreator.UIFCCUtils;
31 import org.exoplatform.wcm.webui.fastcontentcreator.config.action.UIFCCActionList;
32 import org.exoplatform.wcm.webui.fastcontentcreator.UIFCCConstant;
33 import org.exoplatform.web.application.ApplicationMessage;
34 import org.exoplatform.webui.config.annotation.ComponentConfig;
35 import org.exoplatform.webui.config.annotation.EventConfig;
36 import org.exoplatform.webui.core.UIApplication;
37 import org.exoplatform.webui.core.UIGrid;
38 import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
39 import org.exoplatform.webui.core.model.SelectItemOption;
40 import org.exoplatform.webui.event.Event;
41 import org.exoplatform.webui.event.Event.Phase;
42 import org.exoplatform.webui.event.EventListener;
43 import org.exoplatform.webui.form.UIFormInputSet;
44 import org.exoplatform.webui.form.UIFormSelectBox;
45 import org.exoplatform.webui.form.UIFormStringInput;
46 import org.exoplatform.webui.form.UIFormTabPane;
47 import org.exoplatform.webui.form.UIFormTextAreaInput;
48 import org.exoplatform.webui.form.ext.UIFormInputSetWithAction;
49 import org.exoplatform.webui.form.input.UICheckBoxInput;
50
51 import javax.jcr.Node;
52 import javax.jcr.PathNotFoundException;
53 import javax.jcr.RepositoryException;
54 import javax.jcr.Session;
55 import javax.jcr.nodetype.NodeDefinition;
56 import javax.jcr.nodetype.NodeType;
57 import javax.jcr.nodetype.NodeTypeManager;
58 import javax.portlet.PortletPreferences;
59 import java.util.ArrayList;
60 import java.util.List;
61
62
63
64
65
66
67
68
69 @ComponentConfig(
70 lifecycle = UIFormLifecycle.class,
71 template = "system:/groovy/FastContentCreatorPortlet/UIFormWithFieldSet.gtmpl",
72 events = {
73 @EventConfig(listeners = UIFCCConfig.SaveActionListener.class),
74 @EventConfig(listeners = UIFCCConfig.SelectPathActionListener.class, phase=Phase.DECODE),
75 @EventConfig(listeners = UIFCCConfig.ChangeWorkspaceActionListener.class, phase=Phase.DECODE)
76 }
77 )
78 public class UIFCCConfig extends UIFormTabPane implements UISelectable {
79
80
81 private static final Log LOG = ExoLogger.getLogger(UIFCCConfig.class.getName());
82
83
84 private static final String BASIC_MODE = "basic";
85
86
87 private NodeLocation savedLocationNode;
88
89
90
91
92
93
94 public UIFCCConfig() throws Exception {
95 super("UIFCCConfig");
96 PortletPreferences portletPreferences = UIFCCUtils.getPortletPreferences();
97 String preferenceMode = portletPreferences.getValue(UIFCCConstant.PREFERENCE_MODE, "");
98 String preferenceWorkspace = portletPreferences.getValue(UIFCCConstant.PREFERENCE_WORKSPACE, "");
99 String preferencePath = portletPreferences.getValue(UIFCCConstant.PREFERENCE_PATH, "");
100
101 List<SelectItemOption<String>> options = new ArrayList<SelectItemOption<String>>() ;
102
103 UIFormInputSetWithAction saveLocationField = new UIFormInputSetWithAction(UIFCCConstant.SAVE_LOCATION_FIELD);
104
105 if (!BASIC_MODE.equals(preferenceMode)) {
106 UIFormSelectBox workspaceSelectBox = new UIFormSelectBox(UIFCCConstant.WORKSPACE_FORM_SELECTBOX,
107 UIFCCConstant.WORKSPACE_FORM_SELECTBOX,
108 options);
109 workspaceSelectBox.setOnChange("ChangeWorkspace") ;
110 saveLocationField.addChild(workspaceSelectBox) ;
111 }
112 UIFormInputSetWithAction folderSelectorInput = new UIFormInputSetWithAction(UIFCCConstant.LOCATION_FORM_INPUT_ACTION);
113 folderSelectorInput.addUIFormInput(new UIFormStringInput(UIFCCConstant.LOCATION_FORM_STRING_INPUT,
114 UIFCCConstant.LOCATION_FORM_STRING_INPUT,
115 null).setReadOnly(true));
116 folderSelectorInput.setActionInfo(UIFCCConstant.LOCATION_FORM_STRING_INPUT, new String[] {"SelectPath"}) ;
117 saveLocationField.addUIFormInput((UIFormInputSet)folderSelectorInput);
118
119 addChild(saveLocationField);
120 setSelectedTab(UIFCCConstant.SAVE_LOCATION_FIELD);
121
122 UIFormInputSetWithAction templateField = new UIFormInputSetWithAction(UIFCCConstant.TEMPLATE_FIELD);
123 templateField.addChild(new UIFormSelectBox(UIFCCConstant.TEMPLATE_FORM_SELECTBOX,
124 UIFCCConstant.TEMPLATE_FORM_SELECTBOX,
125 options));
126 templateField.addChild(new UIFormStringInput(UIFCCConstant.SAVE_FORM_STRING_INPUT,
127 UIFCCConstant.SAVE_FORM_STRING_INPUT,
128 null));
129 templateField.addChild(new UIFormTextAreaInput(UIFCCConstant.MESSAGE_FORM_TEXTAREA_INPUT,
130 UIFCCConstant.MESSAGE_FORM_TEXTAREA_INPUT,
131 null));
132 templateField.addChild(new UICheckBoxInput(UIFCCConstant.REDIRECT_FORM_CHECKBOX_INPUT,
133 UIFCCConstant.REDIRECT_FORM_CHECKBOX_INPUT,
134 false));
135 templateField.addChild(new UIFormStringInput(UIFCCConstant.REDIRECT_PATH_FORM_STRING_INPUT,
136 UIFCCConstant.REDIRECT_PATH_FORM_STRING_INPUT,
137 null));
138
139 addChild(templateField);
140 if (!BASIC_MODE.equals(preferenceMode)) {
141 UIFormInputSetWithAction actionField = new UIFormInputSetWithAction(UIFCCConstant.ACTION_FIELD);
142 UIFCCActionList fastContentCreatorActionList = actionField.addChild(UIFCCActionList.class, null, "UIFCCActionList");
143 fastContentCreatorActionList.init(preferenceMode);
144 Session session = WCMCoreUtils.getUserSessionProvider().getSession(preferenceWorkspace, WCMCoreUtils.getRepository());
145 fastContentCreatorActionList.updateGrid((Node) session.getItem(preferencePath),
146 fastContentCreatorActionList.getChild(UIGrid.class)
147 .getUIPageIterator()
148 .getCurrentPage());
149
150 addChild(actionField);
151 }
152
153 setActions(new String[] {"Save"}) ;
154 }
155
156
157
158
159
160
161 public void initEditMode() throws Exception {
162 PortletPreferences preferences = UIFCCUtils.getPortletPreferences();
163 String preferenceMode = preferences.getValue(UIFCCConstant.PREFERENCE_MODE, "");
164 String preferenceWorkspace = preferences.getValue(UIFCCConstant.PREFERENCE_WORKSPACE, "") ;
165 String preferencePath = preferences.getValue(UIFCCConstant.PREFERENCE_PATH, "") ;
166
167 boolean isDefaultWorkspace = false ;
168 if (!BASIC_MODE.equals(preferenceMode)) {
169 ManageableRepository repository = WCMCoreUtils.getRepository();
170 String[] workspaceNames = repository.getWorkspaceNames();
171 String systemWsName = repository.getConfiguration().getSystemWorkspaceName();
172 List<SelectItemOption<String>> workspace = new ArrayList<SelectItemOption<String>>();
173 for (String workspaceName : workspaceNames) {
174 if (!workspaceName.equals(systemWsName)) {
175 if (workspaceName.equals(preferenceWorkspace))
176 isDefaultWorkspace = true;
177 workspace.add(new SelectItemOption<String>(workspaceName));
178 }
179 }
180 UIFormSelectBox uiWorkspaceList = getUIFormSelectBox(UIFCCConstant.WORKSPACE_FORM_SELECTBOX);
181 uiWorkspaceList.setOptions(workspace);
182 if (isDefaultWorkspace) {
183 uiWorkspaceList.setValue(preferenceWorkspace);
184 } else if (workspace.size() > 0) {
185 uiWorkspaceList.setValue(workspace.get(0).getValue());
186 }
187 }
188 getUIStringInput(UIFCCConstant.LOCATION_FORM_STRING_INPUT).setValue(preferencePath) ;
189
190 setTemplateOptions(preferencePath, preferenceWorkspace) ;
191
192 getUIStringInput(UIFCCConstant.SAVE_FORM_STRING_INPUT).setValue(preferences.
193 getValue(UIFCCConstant.PREFERENCE_SAVE_BUTTON,
194 ""));
195 getUIFormTextAreaInput(UIFCCConstant.MESSAGE_FORM_TEXTAREA_INPUT).setValue(preferences.
196 getValue(UIFCCConstant.PREFERENCE_SAVE_MESSAGE,
197 ""));
198 getUICheckBoxInput(UIFCCConstant.REDIRECT_FORM_CHECKBOX_INPUT).
199 setChecked(Boolean.parseBoolean(preferences.getValue(UIFCCConstant.PREFERENCE_IS_REDIRECT, "")));
200 getUIStringInput(UIFCCConstant.REDIRECT_PATH_FORM_STRING_INPUT).setValue(preferences.
201 getValue(UIFCCConstant.PREFERENCE_REDIRECT_PATH,
202 ""));
203 }
204
205
206
207
208
209
210
211
212
213 private void setTemplateOptions(String nodePath, String workspaceName) throws Exception {
214 try {
215 Session session = WCMCoreUtils.getUserSessionProvider().getSession(workspaceName, WCMCoreUtils.getRepository());
216 Node currentNode = null ;
217 UIFormSelectBox uiSelectTemplate = getUIFormSelectBox(UIFCCConstant.TEMPLATE_FORM_SELECTBOX);
218 List<SelectItemOption<String>> options = new ArrayList<SelectItemOption<String>>();
219 boolean hasDefaultDoc = false ;
220 String defaultValue = UIFCCUtils.getPreferenceType();
221 try {
222 currentNode = (Node)session.getItem(nodePath) ;
223 setSavedLocationNode(currentNode);
224 } catch(PathNotFoundException ex) {
225 UIApplication uiApp = getAncestorOfType(UIApplication.class) ;
226 uiApp.addMessage(new ApplicationMessage("UIFCCConfig.msg.item-not-found",
227 null,
228 ApplicationMessage.WARNING));
229 return ;
230 }
231 NodeTypeManager ntManager = session.getWorkspace().getNodeTypeManager() ;
232 NodeType currentNodeType = currentNode.getPrimaryNodeType() ;
233 NodeDefinition[] childDefs = currentNodeType.getChildNodeDefinitions() ;
234 TemplateService templateService = getApplicationComponent(TemplateService.class) ;
235 List<String> templates = templateService.getDocumentTemplates() ;
236 List<String> labels = new ArrayList<String>() ;
237 try {
238 for(int i = 0; i < templates.size(); i ++){
239 String nodeTypeName = templates.get(i).toString() ;
240 NodeType nodeType = ntManager.getNodeType(nodeTypeName) ;
241 NodeType[] superTypes = nodeType.getSupertypes() ;
242 boolean isCanCreateDocument = false ;
243 for(NodeDefinition childDef : childDefs){
244 NodeType[] requiredChilds = childDef.getRequiredPrimaryTypes() ;
245 for(NodeType requiredChild : requiredChilds) {
246 if(nodeTypeName.equals(requiredChild.getName())){
247 isCanCreateDocument = true ;
248 break ;
249 }
250 }
251 if(nodeTypeName.equals(childDef.getName()) || isCanCreateDocument) {
252 if(!hasDefaultDoc && nodeTypeName.equals(defaultValue)) hasDefaultDoc = true ;
253 String label = templateService.getTemplateLabel(nodeTypeName) ;
254 if(!labels.contains(label)) {
255 options.add(new SelectItemOption<String>(label, nodeTypeName));
256 }
257 labels.add(label) ;
258 isCanCreateDocument = true ;
259 }
260 }
261 if(!isCanCreateDocument){
262 for(NodeType superType:superTypes) {
263 for(NodeDefinition childDef : childDefs){
264 for(NodeType requiredType : childDef.getRequiredPrimaryTypes()) {
265 if (superType.getName().equals(requiredType.getName())) {
266 if(!hasDefaultDoc && nodeTypeName.equals(defaultValue)) {
267 hasDefaultDoc = true ;
268 }
269 String label = templateService.getTemplateLabel(nodeTypeName) ;
270 if(!labels.contains(label)) {
271 options.add(new SelectItemOption<String>(label, nodeTypeName));
272 }
273 labels.add(label) ;
274 isCanCreateDocument = true ;
275 break;
276 }
277 }
278 if(isCanCreateDocument) break ;
279 }
280 if(isCanCreateDocument) break ;
281 }
282 }
283 }
284 uiSelectTemplate.setOptions(options) ;
285 if(hasDefaultDoc) {
286 uiSelectTemplate.setValue(defaultValue);
287 } else if(options.size() > 0) {
288 defaultValue = options.get(0).getValue() ;
289 uiSelectTemplate.setValue(defaultValue);
290 }
291 } catch(Exception e) {
292 Utils.createPopupMessage(this, "UIFCCConfig.msg.get-template", null, ApplicationMessage.ERROR);
293 }
294 } catch(Exception ex) {
295 Utils.createPopupMessage(this, "UIFCCConfig.msg.set-template-option", null, ApplicationMessage.ERROR);
296 }
297 }
298
299
300
301
302 public void doSelect(String selectField, Object value) {
303 getUIStringInput(selectField).setValue(value.toString()) ;
304 PortletPreferences preferences = UIFCCUtils.getPortletPreferences();
305 String preferenceMode = preferences.getValue(UIFCCConstant.PREFERENCE_MODE, "");
306 String preferenceWorkspace = preferences.getValue(UIFCCConstant.PREFERENCE_WORKSPACE, "") ;
307 if (!BASIC_MODE.equals(preferenceMode)) {
308 preferenceWorkspace = getUIFormSelectBox(UIFCCConstant.WORKSPACE_FORM_SELECTBOX).getValue() ;
309 }
310 String savedLocationPath = value.toString();
311 try {
312 setTemplateOptions(savedLocationPath, preferenceWorkspace) ;
313 } catch(Exception ex) {
314 Utils.createPopupMessage(this, "UIFCCConfig.msg.do-select", null, ApplicationMessage.ERROR);
315 }
316
317 try {
318 Session session = WCMCoreUtils.getUserSessionProvider().getSession(preferenceWorkspace, WCMCoreUtils.getRepository());
319 UIFCCActionList uiFCCActionList = ((UIFormFieldSet) getChildById("UIFCCActionField")).getChild(UIFCCActionList.class);
320 uiFCCActionList.updateGrid((Node) session.getItem(savedLocationPath),
321 uiFCCActionList.getChild(UIGrid.class)
322 .getUIPageIterator()
323 .getCurrentPage());
324 } catch (RepositoryConfigurationException e) {
325 if (LOG.isWarnEnabled()) {
326 LOG.warn(e.getMessage());
327 }
328 } catch (RepositoryException e) {
329 if (LOG.isWarnEnabled()) {
330 LOG.warn(e.getMessage());
331 }
332 } catch (Exception e) {
333 if (LOG.isWarnEnabled()) {
334 LOG.warn(e.getMessage());
335 }
336 }
337
338 Utils.closePopupWindow(this, UIFCCConstant.SELECTOR_POPUP_WINDOW);
339 }
340
341
342
343
344
345
346 public Node getSavedLocationNode() {
347 return NodeLocation.getNodeByLocation(savedLocationNode);
348 }
349
350
351
352
353
354
355 public void setSavedLocationNode(Node savedLocationNode) {
356 this.savedLocationNode = NodeLocation.getNodeLocationByNode(savedLocationNode);
357 }
358
359
360
361
362
363
364 public String getPreferenceMode() {
365 PortletPreferences portletPreferences = UIFCCUtils.getPortletPreferences();
366 return portletPreferences.getValue(UIFCCConstant.PREFERENCE_MODE, "");
367 }
368
369
370
371
372
373
374
375
376
377
378 static public class SelectPathActionListener extends EventListener<UIFCCConfig> {
379
380
381
382
383 public void execute(Event<UIFCCConfig> event) throws Exception {
384 UIFCCConfig fastContentCreatorConfig = event.getSource() ;
385 PortletPreferences preferences = UIFCCUtils.getPortletPreferences();
386 String preferenceMode = preferences.getValue(UIFCCConstant.PREFERENCE_MODE, "");
387 String preferenceRepository = WCMCoreUtils.getRepository().getConfiguration().getName();
388 String preferenceWorkspace = preferences.getValue(UIFCCConstant.PREFERENCE_WORKSPACE, "") ;
389 if (!BASIC_MODE.equals(preferenceMode)) {
390 preferenceWorkspace = fastContentCreatorConfig.getUIFormSelectBox(UIFCCConstant.WORKSPACE_FORM_SELECTBOX)
391 .getValue();
392 }
393 UIOneNodePathSelector uiOneNodePathSelector = fastContentCreatorConfig.createUIComponent(UIOneNodePathSelector.class,
394 null,
395 null);
396 uiOneNodePathSelector.setIsDisable(preferenceWorkspace, true) ;
397 uiOneNodePathSelector.setShowRootPathSelect(true) ;
398 uiOneNodePathSelector.setRootNodeLocation(preferenceRepository, preferenceWorkspace, "/");
399 uiOneNodePathSelector.init(WCMCoreUtils.getUserSessionProvider()) ;
400 uiOneNodePathSelector.setSourceComponent(fastContentCreatorConfig,
401 new String[] { UIFCCConstant.LOCATION_FORM_STRING_INPUT });
402 Utils.createPopupWindow(fastContentCreatorConfig, uiOneNodePathSelector, UIFCCConstant.SELECTOR_POPUP_WINDOW, 610);
403 fastContentCreatorConfig.setSelectedTab(UIFCCConstant.SAVE_LOCATION_FIELD);
404 }
405 }
406
407
408
409
410
411
412
413
414
415
416 static public class ChangeWorkspaceActionListener extends EventListener<UIFCCConfig> {
417
418
419
420
421 public void execute(Event<UIFCCConfig> event) throws Exception {
422 UIFCCConfig uiFCCConfig = event.getSource();
423 uiFCCConfig.getUIStringInput(UIFCCConstant.LOCATION_FORM_STRING_INPUT).setValue("/");
424 String wsName = uiFCCConfig.getUIFormSelectBox(UIFCCConstant.WORKSPACE_FORM_SELECTBOX).getValue();
425 uiFCCConfig.setTemplateOptions(uiFCCConfig.getUIStringInput(UIFCCConstant.LOCATION_FORM_STRING_INPUT)
426 .getValue(),
427 wsName);
428 event.getRequestContext().addUIComponentToUpdateByAjax(uiFCCConfig);
429 }
430 }
431
432
433
434
435
436
437
438
439
440
441 static public class SaveActionListener extends EventListener<UIFCCConfig> {
442
443
444
445
446 public void execute(Event<UIFCCConfig> event) throws Exception {
447 UIFCCConfig fastContentCreatorConfig = event.getSource() ;
448 UIApplication uiApp = fastContentCreatorConfig.getAncestorOfType(UIApplication.class) ;
449 PortletPreferences portletPreferences = UIFCCUtils.getPortletPreferences();
450 String preferenceMode = portletPreferences.getValue(UIFCCConstant.PREFERENCE_MODE, "");
451 boolean preferenceIsActionNeeded = Boolean.parseBoolean(portletPreferences.
452 getValue(UIFCCConstant.PREFERENCE_IS_ACTION_NEEDED,
453 "false"));
454 String type = fastContentCreatorConfig.getUIFormSelectBox(UIFCCConstant.TEMPLATE_FORM_SELECTBOX)
455 .getValue();
456 String path = fastContentCreatorConfig.getUIStringInput(UIFCCConstant.LOCATION_FORM_STRING_INPUT)
457 .getValue();
458 String saveButton = fastContentCreatorConfig.getUIStringInput(UIFCCConstant.SAVE_FORM_STRING_INPUT)
459 .getValue();
460 String saveMessage = fastContentCreatorConfig.getUIFormTextAreaInput(UIFCCConstant.MESSAGE_FORM_TEXTAREA_INPUT)
461 .getValue();
462 String isRedirect = String.valueOf(fastContentCreatorConfig.
463 getUICheckBoxInput(UIFCCConstant.REDIRECT_FORM_CHECKBOX_INPUT)
464 .isChecked());
465 String redirectPath = fastContentCreatorConfig.getUIStringInput(UIFCCConstant.REDIRECT_PATH_FORM_STRING_INPUT)
466 .getValue();
467 if (("false".equals(isRedirect) || redirectPath == null) && saveMessage == null) {
468 Utils.createPopupMessage(fastContentCreatorConfig,
469 "UIFCCConfig.msg.message-empty",
470 null,
471 ApplicationMessage.WARNING);
472 }
473
474 String workspaceName = null;
475 if (BASIC_MODE.equals(preferenceMode) && preferenceIsActionNeeded)
476 workspaceName = portletPreferences.getValue(UIFCCConstant.PREFERENCE_WORKSPACE, "");
477 else
478 workspaceName = fastContentCreatorConfig.getUIFormSelectBox(UIFCCConstant.WORKSPACE_FORM_SELECTBOX)
479 .getValue();
480 if (workspaceName == null || workspaceName.trim().length() == 0) {
481 uiApp.addMessage(new ApplicationMessage("UIFCCConfig.msg.ws-empty",
482 null,
483 ApplicationMessage.WARNING));
484
485 return;
486 }
487 if (type == null || type.trim().length() == 0) {
488 uiApp.addMessage(new ApplicationMessage("UIFCCConfig.msg.fileType-empty",
489 null,
490 ApplicationMessage.WARNING));
491
492 return;
493 }
494 portletPreferences.setValue(UIFCCConstant.PREFERENCE_WORKSPACE, workspaceName);
495
496 portletPreferences.setValue(UIFCCConstant.PREFERENCE_PATH, path);
497 portletPreferences.setValue(UIFCCConstant.PREFERENCE_TYPE, type);
498 portletPreferences.setValue(UIFCCConstant.PREFERENCE_SAVE_BUTTON, saveButton);
499 portletPreferences.setValue(UIFCCConstant.PREFERENCE_SAVE_MESSAGE, saveMessage);
500 portletPreferences.setValue(UIFCCConstant.PREFERENCE_IS_REDIRECT, isRedirect);
501 portletPreferences.setValue(UIFCCConstant.PREFERENCE_REDIRECT_PATH, redirectPath);
502 portletPreferences.store();
503 uiApp.addMessage(new ApplicationMessage("UIFCCConfig.msg.save-successfully", null));
504
505 }
506 }
507
508 }