KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > dialogs > UserRolesEditDialog


1 package info.magnolia.module.admininterface.dialogs;
2
3 import info.magnolia.cms.beans.config.ContentRepository;
4 import info.magnolia.cms.core.Content;
5 import info.magnolia.cms.core.ItemType;
6 import info.magnolia.cms.core.Path;
7 import info.magnolia.cms.gui.control.Save;
8 import info.magnolia.cms.gui.dialog.DialogBox;
9 import info.magnolia.cms.gui.dialog.DialogDialog;
10 import info.magnolia.cms.gui.dialog.DialogEdit;
11 import info.magnolia.cms.gui.dialog.DialogFactory;
12 import info.magnolia.cms.gui.dialog.DialogInclude;
13 import info.magnolia.cms.gui.dialog.DialogStatic;
14 import info.magnolia.cms.gui.dialog.DialogTab;
15
16 import javax.jcr.RepositoryException;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.log4j.Logger;
22
23
24 /**
25  * @author Fabrizio Giustina
26  * @version $Id: $
27  */

28 public class UserRolesEditDialog extends ConfiguredDialog {
29
30     protected static Logger log = Logger.getLogger("roles dialog"); //$NON-NLS-1$
31

32     /**
33      * Stable serialVersionUID.
34      */

35     private static final long serialVersionUID = 222L;
36
37     /**
38      * @param name
39      * @param request
40      * @param response
41      * @param configNode
42      */

43     public UserRolesEditDialog(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Content configNode) {
44         super(name, request, response, configNode);
45     }
46
47     public String JavaDoc getRepository() {
48         String JavaDoc repository = super.getRepository();
49         if (repository == null) {
50             repository = ContentRepository.USER_ROLES;
51         }
52         return repository;
53     }
54
55     /*
56      * (non-Javadoc)
57      * @see info.magnolia.module.admininterface.DialogMVCHandler#createDialog(info.magnolia.cms.core.Content,
58      * info.magnolia.cms.core.Content)
59      */

60     protected DialogDialog createDialog(Content configNode, Content storageNode) throws RepositoryException {
61
62         DialogDialog dialog = super.createDialog(configNode, storageNode);
63
64         dialog.setJavascriptSources(request.getContextPath() + "/admindocroot/js/dialogs/DynamicTable.js"); //$NON-NLS-1$
65
dialog.setJavascriptSources(request.getContextPath()
66             + "/admindocroot/js/dialogs/pages/userRolesEditDialogPage.js"); //$NON-NLS-1$
67
dialog.setCssSources(request.getContextPath() + "/admindocroot/css/dialogs/pages/userRolesEditDialogPage.css"); //$NON-NLS-1$
68
dialog.setConfig("height", 600); //$NON-NLS-1$
69

70         dialog.setLabel(msgs.get("roles.edit.edit")); //$NON-NLS-1$
71

72         DialogTab tab0 = dialog.addTab(msgs.get("roles.edit.properties")); //$NON-NLS-1$
73

74         DialogTab tab1 = dialog.addTab(msgs.get("roles.edit.accessControlList")); //$NON-NLS-1$
75

76         DialogStatic spacer = DialogFactory.getDialogStaticInstance(request, response, null, null);
77         spacer.setConfig("line", false); //$NON-NLS-1$
78

79         DialogStatic name = DialogFactory.getDialogStaticInstance(request, response, null, null);
80         // name.setConfig("line",false);
81
name.setLabel("<strong>" + msgs.get("roles.edit.rolename") + "</strong>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
82
name.setValue("<strong>" + storageNode.getName() + "</strong>"); //$NON-NLS-1$ //$NON-NLS-2$
83
tab0.addSub(name);
84
85         tab0.addSub(spacer);
86
87         DialogEdit title = DialogFactory.getDialogEditInstance(request, response, storageNode, null);
88         title.setName("title"); //$NON-NLS-1$
89
title.setLabel(msgs.get("roles.edit.fullname")); //$NON-NLS-1$
90
tab0.addSub(title);
91
92         tab0.addSub(spacer);
93
94         DialogEdit desc = DialogFactory.getDialogEditInstance(request, response, storageNode, null);
95         desc.setName("description"); //$NON-NLS-1$
96
desc.setLabel(msgs.get("roles.edit.description")); //$NON-NLS-1$
97
desc.setConfig("rows", 6); //$NON-NLS-1$
98
tab0.addSub(desc);
99
100         DialogInclude acl = DialogFactory.getDialogIncludeInstance(request, response, storageNode, null);
101         acl.setBoxType(DialogBox.BOXTYPE_1COL);
102         acl.setName("aclRolesRepository"); //$NON-NLS-1$
103
acl.setConfig("file", "/.magnolia/dialogpages/userRolesEditAclInclude.html"); //$NON-NLS-1$ //$NON-NLS-2$
104
tab1.addSub(acl);
105
106         dialog.setConfig("saveOnclick", "aclFormSubmit();"); //$NON-NLS-1$ //$NON-NLS-2$
107
return dialog;
108     }
109
110     /*
111      * (non-Javadoc)
112      * @see info.magnolia.module.admininterface.DialogMVCHandler#onPreSave()
113      */

114     protected Save onPreSave() {
115         Save control = super.onPreSave();
116         control.setPath(path);
117         return control;
118     }
119
120     protected void onPostSave(Save saveControl) {
121         Content role = this.getStorageNode();
122
123         // for each repository
124
for (int x = 0; x < ContentRepository.getAllRepositoryNames().length; x++) {
125             String JavaDoc repository = ContentRepository.getAllRepositoryNames()[x];
126
127             // ######################
128
// # acl
129
// ######################
130
// remove existing
131
try {
132                 role.delete("acl_" + repository); //$NON-NLS-1$
133
}
134             catch (RepositoryException re) {
135                 // ignore, not existing
136
}
137             // rewrite
138
try {
139                 Content acl = role.createContent("acl_" + repository, ItemType.CONTENTNODE); //$NON-NLS-1$
140
String JavaDoc aclValueStr = form.getParameter("acl" + repository + "List"); //$NON-NLS-1$ //$NON-NLS-2$
141
if (StringUtils.isNotEmpty(aclValueStr)) {
142                     String JavaDoc[] aclEntries = aclValueStr.split(";"); //$NON-NLS-1$
143
for (int i = 0; i < aclEntries.length; i++) {
144                         String JavaDoc path = StringUtils.EMPTY;
145                         long accessRight = 0;
146                         String JavaDoc accessType = StringUtils.EMPTY;
147
148                         String JavaDoc[] aclValuePairs = aclEntries[i].split(","); //$NON-NLS-1$
149
for (int j = 0; j < aclValuePairs.length; j++) {
150                             String JavaDoc[] aclValuePair = aclValuePairs[j].split(":"); //$NON-NLS-1$
151
String JavaDoc aclName = aclValuePair[0].trim();
152                             String JavaDoc aclValue = StringUtils.EMPTY;
153                             if (aclValuePair.length > 1) {
154                                 aclValue = aclValuePair[1].trim();
155                             }
156
157                             if (aclName.equals("path")) { //$NON-NLS-1$
158
path = aclValue;
159                             }
160                             else if (aclName.equals("accessType")) { //$NON-NLS-1$
161
accessType = aclValue;
162                             }
163                             else if (aclName.equals("accessRight")) { //$NON-NLS-1$
164
try{
165                                         accessRight = Long.parseLong(aclValue);
166                                     }
167                                     catch(NumberFormatException JavaDoc e){
168                                         accessRight = 0;
169                                     }
170                             }
171                         }
172
173                         if (StringUtils.isNotEmpty(path)) {
174                             if (path.equals("/")) { //$NON-NLS-1$
175
// needs only one entry: "/*"
176
accessType = "sub"; //$NON-NLS-1$
177
path = StringUtils.EMPTY;
178                             }
179
180                             if (accessType.equals("self")) { //$NON-NLS-1$
181
try {
182                                     String JavaDoc newLabel = Path.getUniqueLabel(hm, acl.getHandle(), "0"); //$NON-NLS-1$
183
Content r = acl.createContent(newLabel, ItemType.CONTENTNODE);
184                                     r.createNodeData("path").setValue(path); //$NON-NLS-1$
185
r.createNodeData("permissions").setValue(accessRight); //$NON-NLS-1$
186
}
187                                 catch (Exception JavaDoc e) {
188                                     log.error(e.getMessage(), e);
189                                 }
190                             }
191                             try {
192                                 String JavaDoc newLabel = Path.getUniqueLabel(hm, acl.getHandle(), "0"); //$NON-NLS-1$
193
Content r = acl.createContent(newLabel, ItemType.CONTENTNODE);
194                                 r.createNodeData("path").setValue(path + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
195
r.createNodeData("permissions").setValue(accessRight); //$NON-NLS-1$
196
}
197                             catch (Exception JavaDoc e) {
198                                 log.error(e.getMessage(), e);
199                             }
200                         }
201                     }
202                 }
203                 hm.save();
204             }
205             catch (RepositoryException re) {
206                 log.error(re.getMessage(), re);
207             }
208         }
209     }
210
211 }
Popular Tags