1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.ecm.webui.component.admin.namespace;
18
19 import javax.jcr.NamespaceRegistry;
20
21 import org.exoplatform.ecm.webui.form.validator.ECMNameValidator;
22 import org.exoplatform.services.jcr.RepositoryService;
23 import org.exoplatform.web.application.ApplicationMessage;
24 import org.exoplatform.webui.config.annotation.ComponentConfig;
25 import org.exoplatform.webui.config.annotation.EventConfig;
26 import org.exoplatform.webui.core.UIApplication;
27 import org.exoplatform.webui.core.UIPopupWindow;
28 import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
29 import org.exoplatform.webui.event.Event;
30 import org.exoplatform.webui.event.EventListener;
31 import org.exoplatform.webui.event.Event.Phase;
32 import org.exoplatform.webui.form.UIForm;
33 import org.exoplatform.webui.form.UIFormStringInput;
34 import org.exoplatform.webui.form.validator.MandatoryValidator;
35
36
37
38
39
40
41
42 @ComponentConfig(
43 lifecycle = UIFormLifecycle.class,
44 template = "classpath:groovy/ecm/webui/form/UIForm.gtmpl",
45 events = {
46 @EventConfig(listeners = UINamespaceForm.SaveActionListener.class),
47 @EventConfig(phase=Phase.DECODE, listeners = UINamespaceForm.CancelActionListener.class)
48 }
49 )
50 public class UINamespaceForm extends UIForm {
51
52 final static public String FIELD_PREFIX = "namespace" ;
53 final static public String FIELD_URI = "uri" ;
54
55 public UINamespaceForm() throws Exception {
56 addUIFormInput(new UIFormStringInput(FIELD_PREFIX, FIELD_PREFIX, null).
57 addValidator(MandatoryValidator.class).
58 addValidator(ECMNameValidator.class)) ;
59 addUIFormInput(new UIFormStringInput(FIELD_URI, FIELD_URI, null).
60 addValidator(MandatoryValidator.class)) ;
61 }
62
63 static public class SaveActionListener extends EventListener<UINamespaceForm> {
64 public void execute(Event<UINamespaceForm> event) throws Exception {
65 UINamespaceForm uiForm = event.getSource() ;
66 String uri = uiForm.getUIStringInput(FIELD_URI).getValue() ;
67 UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class) ;
68 NamespaceRegistry namespaceRegistry = uiForm.getApplicationComponent(RepositoryService.class)
69 .getCurrentRepository().getNamespaceRegistry() ;
70 String prefix = uiForm.getUIStringInput(FIELD_PREFIX).getValue() ;
71 if(prefix == null || prefix.trim().length() == 0) {
72 uiApp.addMessage(new ApplicationMessage("UINamespaceForm.msg.prefix-null", null,
73 ApplicationMessage.WARNING)) ;
74 return ;
75 }
76 if(uri == null || uri.trim().length() == 0) {
77 uiApp.addMessage(new ApplicationMessage("UINamespaceForm.msg.uri-null", null,
78 ApplicationMessage.WARNING)) ;
79 return ;
80 }
81 UINamespaceManager uiManager = uiForm.getAncestorOfType(UINamespaceManager.class) ;
82 if (contains(namespaceRegistry.getPrefixes(), prefix) ||
83 contains(namespaceRegistry.getURIs(), uri)) {
84 uiApp.addMessage(new ApplicationMessage("UINamespaceForm.msg.register-unsuccessfull", null,
85 ApplicationMessage.WARNING)) ;
86 return ;
87 }
88 try {
89 namespaceRegistry.registerNamespace(prefix, uri) ;
90 uiManager.refresh() ;
91 uiManager.removeChild(UIPopupWindow.class) ;
92 } catch (Exception e) {
93 uiApp.addMessage(new ApplicationMessage("UINamespaceForm.msg.register-unsuccessfull", null,
94 ApplicationMessage.WARNING)) ;
95 return ;
96 }
97 event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
98 }
99
100 private boolean contains(String[] arr, String st) {
101 if (st == null) return false;
102 for (String value : arr)
103 if (st.equals(value))
104 return true;
105 return false;
106 }
107 }
108
109 static public class CancelActionListener extends EventListener<UINamespaceForm> {
110 public void execute(Event<UINamespaceForm> event) throws Exception {
111 UINamespaceForm uiForm = event.getSource() ;
112 UINamespaceManager uiManager = uiForm.getAncestorOfType(UINamespaceManager.class) ;
113 uiManager.removeChild(UIPopupWindow.class) ;
114 event.getRequestContext().addUIComponentToUpdateByAjax(uiManager) ;
115 }
116 }
117 }