KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > dialogpages > UserRolesEditIncludeAclDialogPage


1 package info.magnolia.module.admininterface.dialogpages;
2
3 import info.magnolia.cms.beans.config.ContentRepository;
4 import info.magnolia.cms.core.Content;
5 import info.magnolia.cms.gui.control.Button;
6 import info.magnolia.cms.gui.control.Edit;
7 import info.magnolia.cms.gui.control.Hidden;
8 import info.magnolia.cms.gui.control.Select;
9 import info.magnolia.cms.gui.dialog.DialogBox;
10 import info.magnolia.cms.gui.dialog.DialogButton;
11 import info.magnolia.cms.gui.dialog.DialogFactory;
12 import info.magnolia.cms.gui.dialog.DialogSuper;
13 import info.magnolia.cms.gui.misc.CssConstants;
14 import info.magnolia.cms.i18n.Messages;
15 import info.magnolia.cms.i18n.MessagesManager;
16 import info.magnolia.cms.security.Permission;
17 import info.magnolia.module.admininterface.DialogPageMVCHandler;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.jcr.RepositoryException;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.commons.lang.StringUtils;
28
29
30 /**
31  * @author Fabrizio Giustina
32  * @version $Id: $
33  */

34 public class UserRolesEditIncludeAclDialogPage extends DialogPageMVCHandler {
35
36     public UserRolesEditIncludeAclDialogPage(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
37         super(name, request, response);
38     }
39
40     /**
41      * Stable serialVersionUID.
42      */

43     private static final long serialVersionUID = 222L;
44
45     // todo: permission global available somewhere
46
private static final long PERMISSION_ALL = Permission.ALL;
47
48     private static final long PERMISSION_READ = Permission.READ;
49
50     private static final long PERMISSION_NO = 0;
51
52     private static final String JavaDoc CSS_ACL_DIV = "aclDynamicTable"; //$NON-NLS-1$
53

54     private static String JavaDoc getHtmlRowInner(HttpServletRequest JavaDoc request, String JavaDoc dynamicTable, String JavaDoc repository) {
55         boolean small = true;
56         Messages msgs = MessagesManager.getMessages(request);
57
58         Select accessRight = new Select();
59         accessRight.setSaveInfo(false);
60         accessRight.setName("'+prefix+'AccessRight"); //$NON-NLS-1$
61
accessRight.setCssClass("mgnlDialogControlSelect"); //$NON-NLS-1$
62
accessRight.setOptions(msgs.get("roles.permission.readWrite"), Long.toString(PERMISSION_ALL)); //$NON-NLS-1$
63
accessRight.setOptions(msgs.get("roles.permission.readOnly"), Long.toString(PERMISSION_READ)); //$NON-NLS-1$
64
accessRight.setOptions(msgs.get("roles.permission.deny"), Long.toString(PERMISSION_NO)); //$NON-NLS-1$
65
accessRight.setValue("' + object.accessRight + '"); //$NON-NLS-1$
66

67         Select accessType = new Select();
68         accessType.setSaveInfo(false);
69         accessType.setName("'+prefix+'AccessType"); //$NON-NLS-1$
70
accessType.setCssClass("mgnlDialogControlSelect"); //$NON-NLS-1$
71
if (repository.equals(ContentRepository.WEBSITE)) {
72             accessType.setOptions(msgs.get("roles.edit.thisAndSubPages"), "self"); //$NON-NLS-1$ //$NON-NLS-2$
73
accessType.setOptions(msgs.get("roles.edit.subPages"), "sub"); //$NON-NLS-1$ //$NON-NLS-2$
74
}
75         else {
76             accessType.setOptions(msgs.get("roles.edit.thisAndSubNodes"), "self"); //$NON-NLS-1$ //$NON-NLS-2$
77
accessType.setOptions(msgs.get("roles.edit.subNodes"), "sub"); //$NON-NLS-1$ //$NON-NLS-2$
78
}
79         accessType.setValue("' + object.accessType + '"); //$NON-NLS-1$
80

81         Edit path = new Edit();
82         path.setSaveInfo(false);
83         path.setName("'+prefix+'Path"); //$NON-NLS-1$
84
path.setValue("'+object.path+'"); //$NON-NLS-1$
85
path.setCssClass(CssConstants.CSSCLASS_EDIT);
86         path.setCssStyles("width", "100%"); //$NON-NLS-1$ //$NON-NLS-2$
87

88         Button choose = new Button();
89         choose.setLabel(msgs.get("buttons.choose")); //$NON-NLS-1$
90
choose.setOnclick("aclChoose(\\''+prefix+'\\',\\'" + repository + "\\');"); //$NON-NLS-1$ //$NON-NLS-2$
91
choose.setSmall(small);
92
93         Button delete = new Button();
94         delete.setLabel(msgs.get("buttons.delete")); //$NON-NLS-1$
95
delete.setOnclick(dynamicTable + ".del('+index+');"); //$NON-NLS-1$
96
delete.setSmall(small);
97
98         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
99         // set as table since ie/win does not support setting of innerHTML of a
100
// tr
101
html.append("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr>"); //$NON-NLS-1$
102
html.append("<td width=\"1\" class=\"" //$NON-NLS-1$
103
+ CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" //$NON-NLS-1$
104
+ accessRight.getHtml() + "</td>"); //$NON-NLS-1$
105
html.append("<td width=\"1\" class=\"mgnlDialogBoxInput\"></td>"); //$NON-NLS-1$
106

107         if (!repository.equals(ContentRepository.USERS) && !repository.equals(ContentRepository.USER_ROLES)) {
108             html.append("<td width=\"1\" class=\"" //$NON-NLS-1$
109
+ CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" //$NON-NLS-1$
110
+ accessType.getHtml() + "</td>"); //$NON-NLS-1$
111
html.append("<td width=\"1\"></td>"); //$NON-NLS-1$
112
}
113         else {
114             html
115                 .append("<input type=\"hidden\" id=\"' + prefix + 'AccessType\" name=\"' + prefix + 'AccessType\" value=\"sub\"/>"); //$NON-NLS-1$
116
}
117
118         html.append("<td width=\"100%\"class=\"" //$NON-NLS-1$
119
+ CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" //$NON-NLS-1$
120
+ path.getHtml() + "</td>"); //$NON-NLS-1$
121
html.append("<td width=\"1\"></td>"); //$NON-NLS-1$
122
html.append("<td width=\"1\" class=\"" //$NON-NLS-1$
123
+ CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" //$NON-NLS-1$
124
+ choose.getHtml() + "</td>"); //$NON-NLS-1$
125
html.append("<td width=\"1\"></td>"); //$NON-NLS-1$
126
html.append("<td width=\"1\" class=\"" //$NON-NLS-1$
127
+ CssConstants.CSSCLASS_EDITWITHBUTTON + "\">" //$NON-NLS-1$
128
+ delete.getHtml() + "</td>"); //$NON-NLS-1$
129
html.append("</tr></table>"); //$NON-NLS-1$
130

131         return html.toString();
132     }
133
134     /**
135      * @throws Exception
136      * @see info.magnolia.cms.servlets.BasePageServlet#draw(HttpServletRequest, HttpServletResponse)
137      */

138     protected void draw(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
139         PrintWriter JavaDoc out = response.getWriter();
140         Messages msgs = MessagesManager.getMessages(request);
141
142         DialogSuper dialogControl = (DialogSuper) request.getAttribute("dialogObject"); //$NON-NLS-1$
143
Content role = dialogControl.getWebsiteNode();
144
145         // select the repository
146
Select repositorySelect = getReposiotySelect(request);
147
148         out.print(repositorySelect.getHtml());
149         out.print("<p><p/>"); //$NON-NLS-1$
150
for (int i = 0; i < ContentRepository.getAllRepositoryNames().length; i++) {
151             writeRepositoryTable(request, response, msgs, out, role, ContentRepository.getAllRepositoryNames()[i]);
152         }
153         // out.print("<p>&nbsp;<p>&nbsp;<p>&nbsp;<input type=\"button\" onclick=\"aclChangeRepository('website')\">");
154
out.println("<script>aclChangeRepository('website');</script>"); //$NON-NLS-1$ //$NON-NLS-2$
155

156     }
157
158     /**
159      * @param request
160      * @param out
161      * @param role
162      * @throws RepositoryException
163      * @throws IOException
164      */

165     private void writeRepositoryTable(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Messages msgs,
166         PrintWriter JavaDoc out, Content role, String JavaDoc repository) throws RepositoryException, IOException JavaDoc {
167         String JavaDoc tableName = "acl" + repository + "Table"; //$NON-NLS-1$ //$NON-NLS-2$
168
String JavaDoc dynamicTableName = "acl" + repository + "DynamicTable"; //$NON-NLS-1$ //$NON-NLS-2$
169
String JavaDoc hiddenFieldName = "acl" + repository + "List"; //$NON-NLS-1$ //$NON-NLS-2$
170

171         out.println("<div id=\"acl" + repository + "Div\" class=\"" + CSS_ACL_DIV + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
172
out.println(new Hidden(hiddenFieldName, StringUtils.EMPTY, false).getHtml());
173
174         // the table
175
out.println("<table id=\"" //$NON-NLS-1$
176
+ tableName
177             + "\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr><td></td></tr></table>"); //$NON-NLS-1$
178

179         // add button
180

181         out.println("<table width=\"100%\">"); //$NON-NLS-1$
182
DialogButton add = DialogFactory.getDialogButtonInstance(request, response, null, null);
183         add.setBoxType(DialogBox.BOXTYPE_1COL);
184         add.setConfig("buttonLabel", msgs.get("buttons.add")); //$NON-NLS-1$ //$NON-NLS-2$
185
add.setConfig("onclick", dynamicTableName + ".addNew();"); //$NON-NLS-1$ //$NON-NLS-2$
186
add.drawHtml(out);
187         out.println("</table>"); //$NON-NLS-1$
188

189         out.println("</div>"); //$NON-NLS-1$
190

191         out.println("<script type=\"text/javascript\">"); //$NON-NLS-1$
192
// register the repository
193
out.println("aclRepositories[aclRepositories.length]= '" + repository + "';"); //$NON-NLS-1$ //$NON-NLS-2$
194

195         // make renderer function
196
out.println("function acl" + repository + "RenderFunction(cell, prefix, index, object)"); //$NON-NLS-1$ //$NON-NLS-2$
197
out.println("{"); //$NON-NLS-1$
198

199         // get some debug informations
200
out.println("mgnlDebug('acl" + repository + "RenderFunction: prefix = ' + prefix, 'acl', object)");
201         out.println("cell.innerHTML= '" + getHtmlRowInner(request, dynamicTableName, repository) + "';\n"); //$NON-NLS-1$ //$NON-NLS-2$
202
out.println("document.getElementById(prefix + 'AccessType').value = object.accessType;\n"); //$NON-NLS-1$
203

204         out.println("document.getElementById(prefix + 'AccessRight').value = object.accessRight;\n"); //$NON-NLS-1$
205

206         out.println("}"); //$NON-NLS-1$
207

208         // create the dynamicTable
209
out.println(dynamicTableName + " = new MgnlDynamicTable('" //$NON-NLS-1$
210
+ tableName + "',document.mgnlFormMain." //$NON-NLS-1$
211
+ hiddenFieldName + ", aclGetNewPermissionObject, aclGetPermissionObject, acl" //$NON-NLS-1$
212
+ repository + "RenderFunction, null);"); //$NON-NLS-1$
213

214         // add existing acls to table (by js, so the same mechanism as at
215
// adding rows can be used)
216
addExistingAclToTable(out, role, dynamicTableName, repository);
217
218         out.println("</script>"); //$NON-NLS-1$
219
}
220
221     /**
222      * @param out
223      * @param role
224      */

225     private void addExistingAclToTable(PrintWriter JavaDoc out, Content role, String JavaDoc dynamicTableName, String JavaDoc repository) {
226         boolean noAcl = false;
227         try {
228             Content acl = role.getContent("acl_" + repository); //$NON-NLS-1$
229
if (acl.getChildren().size() == 0) {
230                 noAcl = true;
231             }
232             Iterator JavaDoc it = acl.getChildren().iterator();
233             boolean skipNext = false;
234             while (it.hasNext()) {
235                 Content c = (Content) it.next();
236
237                 if (skipNext) {
238                     skipNext = false;
239                 }
240                 else {
241                     String JavaDoc path = c.getNodeData("path").getString(); //$NON-NLS-1$
242
String JavaDoc accessRight = c.getNodeData("permissions").getString(); //$NON-NLS-1$
243
String JavaDoc accessType;
244
245                     if (!StringUtils.contains(path, "/*")) { //$NON-NLS-1$
246
// access to self and subs, skip next (which is same with /*)
247
skipNext = true;
248                         accessType = "self"; //$NON-NLS-1$
249
}
250                     else {
251                         if (path.equals("/*")) { //$NON-NLS-1$
252
path = "/"; //$NON-NLS-1$
253
accessType = "self"; //$NON-NLS-1$
254
}
255                         else {
256                             path = StringUtils.substringBeforeLast(path, "/*"); //$NON-NLS-1$
257
accessType = "sub"; //$NON-NLS-1$
258
}
259                     }
260
261                     out.println(dynamicTableName + ".add({accessRight:" //$NON-NLS-1$
262
+ accessRight + ",accessType:'" //$NON-NLS-1$
263
+ accessType + "',path:'" //$NON-NLS-1$
264
+ path + "'});"); //$NON-NLS-1$
265
}
266             }
267         }
268         catch (Exception JavaDoc e) {
269             noAcl = true;
270         }
271         if (noAcl) {
272             out.println(dynamicTableName + ".addNew();"); //$NON-NLS-1$
273
}
274     }
275
276     /**
277      * @param request
278      * @return
279      */

280     private Select getReposiotySelect(HttpServletRequest JavaDoc request) {
281         Select repositorySelect = new Select();
282         repositorySelect.setName("aclRepository"); //$NON-NLS-1$
283
repositorySelect.setCssClass("mgnlDialogControlSelect"); //$NON-NLS-1$
284
repositorySelect.setEvent("onchange", "aclChangeRepository(this.value)"); //$NON-NLS-1$ //$NON-NLS-2$
285
repositorySelect.setSaveInfo(false);
286         repositorySelect.setValue(ContentRepository.WEBSITE);
287
288         // loop through the repositories
289
for (int i = 0; i < ContentRepository.getAllRepositoryNames().length; i++) {
290             String JavaDoc name = ContentRepository.getAllRepositoryNames()[i];
291             String JavaDoc label = MessagesManager.get(request, "repository." + name); //$NON-NLS-1$
292
repositorySelect.setOptions(label, name);
293         }
294         return repositorySelect;
295     }
296 }
Popular Tags