KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > admin > RolesBinding


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend.admin;
17
18 import org.apache.cocoon.forms.binding.AbstractCustomBinding;
19 import org.apache.cocoon.forms.formmodel.Widget;
20 import org.apache.cocoon.forms.formmodel.Form;
21 import org.apache.cocoon.forms.formmodel.MultiValueField;
22 import org.apache.commons.jxpath.JXPathContext;
23 import org.outerj.daisy.repository.user.User;
24 import org.outerj.daisy.repository.user.Role;
25 import org.outerj.daisy.repository.user.UserManager;
26
27 public class RolesBinding extends AbstractCustomBinding {
28     protected void doLoad(Widget widget, JXPathContext jxPathContext) throws Exception JavaDoc {
29         Form form = (Form)widget;
30         User user = (User)jxPathContext.getValue(".");
31
32         if (user.getDefaultRole() != null)
33             form.getChild("defaultRole").setValue(new Long JavaDoc(user.getDefaultRole().getId()));
34
35         Role[] roles = user.getAllRoles().getArray();
36         Long JavaDoc[] roleIds = new Long JavaDoc[roles.length];
37         for (int i = 0; i < roleIds.length; i++)
38             roleIds[i] = new Long JavaDoc(roles[i].getId());
39         form.getChild("roles").setValue(roleIds);
40     }
41
42     protected void doSave(Widget widget, JXPathContext jxPathContext) throws Exception JavaDoc {
43         Form form = (Form)widget;
44         User user = (User)jxPathContext.getValue(".");
45
46         UserManager userManager = (UserManager)form.getAttribute("UserManager");
47
48         Role roles[] = user.getAllRoles().getArray();
49
50         // sync role additions
51
MultiValueField rolesField = (MultiValueField)form.getChild("roles");
52         Long JavaDoc[] roleIds = (Long JavaDoc[])rolesField.getValue();
53         for (int i = 0; i < roleIds.length; i++) {
54             if (getRole(roles, roleIds[i].longValue()) == null)
55                 user.addToRole(userManager.getRole(roleIds[i].longValue(), false));
56         }
57
58         // sync role removals
59
for (int i = 0; i < roles.length; i++) {
60             if (!contains(roleIds, roles[i].getId()))
61                 user.removeFromRole(roles[i]);
62         }
63
64         if (form.getChild("defaultRole").getValue() != null) {
65             long defaultRoleId = ((Long JavaDoc)form.getChild("defaultRole").getValue()).longValue();
66             Role defaultRole = userManager.getRole(defaultRoleId, false);
67             user.setDefaultRole(defaultRole);
68         } else {
69             user.setDefaultRole(null);
70         }
71     }
72
73     private Role getRole(Role[] roles, long id) {
74         for (int i = 0; i < roles.length; i++) {
75             if (roles[i].getId() == id)
76                 return roles[i];
77         }
78         return null;
79     }
80
81     private boolean contains(Long JavaDoc[] ids, long id) {
82         for (int i = 0; i < ids.length; i++) {
83             if (ids[i].longValue() == id)
84                 return true;
85         }
86         return false;
87     }
88 }
89
Popular Tags