01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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)
29: throws Exception {
30: Form form = (Form) widget;
31: User user = (User) jxPathContext.getValue(".");
32:
33: if (user.getDefaultRole() != null)
34: form.getChild("defaultRole").setValue(
35: new Long(user.getDefaultRole().getId()));
36:
37: Role[] roles = user.getAllRoles().getArray();
38: Long[] roleIds = new Long[roles.length];
39: for (int i = 0; i < roleIds.length; i++)
40: roleIds[i] = new Long(roles[i].getId());
41: form.getChild("roles").setValue(roleIds);
42: }
43:
44: protected void doSave(Widget widget, JXPathContext jxPathContext)
45: throws Exception {
46: Form form = (Form) widget;
47: User user = (User) jxPathContext.getValue(".");
48:
49: UserManager userManager = (UserManager) form
50: .getAttribute("UserManager");
51:
52: Role roles[] = user.getAllRoles().getArray();
53:
54: // sync role additions
55: MultiValueField rolesField = (MultiValueField) form
56: .getChild("roles");
57: Long[] roleIds = (Long[]) rolesField.getValue();
58: for (int i = 0; i < roleIds.length; i++) {
59: if (getRole(roles, roleIds[i].longValue()) == null)
60: user.addToRole(userManager.getRole(roleIds[i]
61: .longValue(), false));
62: }
63:
64: // sync role removals
65: for (int i = 0; i < roles.length; i++) {
66: if (!contains(roleIds, roles[i].getId()))
67: user.removeFromRole(roles[i]);
68: }
69:
70: if (form.getChild("defaultRole").getValue() != null) {
71: long defaultRoleId = ((Long) form.getChild("defaultRole")
72: .getValue()).longValue();
73: Role defaultRole = userManager
74: .getRole(defaultRoleId, false);
75: user.setDefaultRole(defaultRole);
76: } else {
77: user.setDefaultRole(null);
78: }
79: }
80:
81: private Role getRole(Role[] roles, long id) {
82: for (int i = 0; i < roles.length; i++) {
83: if (roles[i].getId() == id)
84: return roles[i];
85: }
86: return null;
87: }
88:
89: private boolean contains(Long[] ids, long id) {
90: for (int i = 0; i < ids.length; i++) {
91: if (ids[i].longValue() == id)
92: return true;
93: }
94: return false;
95: }
96: }
|