2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissionRoles and
22 * limitations under the License.
24 package org.collectionspace.services.authorization.storage;
26 import java.util.ArrayList;
27 import java.util.List;
29 import org.collectionspace.services.authorization.PermissionRole;
30 import org.collectionspace.services.authorization.PermissionRoleRel;
31 import org.collectionspace.services.authorization.PermissionValue;
32 import org.collectionspace.services.authorization.PermissionsRolesList;
33 import org.collectionspace.services.authorization.RoleValue;
34 import org.collectionspace.services.authorization.SubjectType;
36 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
37 import org.collectionspace.services.common.document.DocumentFilter;
38 import org.collectionspace.services.common.document.DocumentWrapper;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Document handler for PermissionRole association
46 public class PermissionRoleDocumentHandler
47 extends AbstractDocumentHandlerImpl<PermissionRole, PermissionsRolesList, List<PermissionRoleRel>, List<PermissionRoleRel>> {
49 private final Logger logger = LoggerFactory.getLogger(PermissionRoleDocumentHandler.class);
50 private PermissionRole permissionRole;
51 private PermissionsRolesList permissionRolesList;
54 public void handleCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
55 fillCommonPart(getCommonPart(), wrapDoc);
59 public void completeCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
60 PermissionRole pr = getCommonPart();
61 AuthorizationDelegate.addPermissions(getServiceContext(), pr);
65 public void handleUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
66 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
70 public void completeUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
71 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
75 public void handleGet(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
76 setCommonPart(extractCommonPart(wrapDoc));
77 getServiceContext().setOutput(permissionRole);
81 public void handleGetAll(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
82 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
86 public void completeDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
91 public PermissionRole extractCommonPart(
92 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
94 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
95 PermissionRole pr = new PermissionRole();
96 SubjectType subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
97 if (prrl.size() == 0) {
100 PermissionRoleRel prr0 = prrl.get(0);
101 if (SubjectType.ROLE.equals(subject)) {
103 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
104 pr.setPermissions(pvs);
105 PermissionValue pv = buildPermissionValue(prr0);
109 List<RoleValue> rvs = new ArrayList<RoleValue>();
111 for (PermissionRoleRel prr : prrl) {
112 RoleValue rv = buildRoleValue(prr);
115 } else if (SubjectType.PERMISSION.equals(subject)) {
117 List<RoleValue> rvs = new ArrayList<RoleValue>();
119 RoleValue rv = buildRoleValue(prr0);
123 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
124 pr.setPermissions(pvs);
125 for (PermissionRoleRel prr : prrl) {
126 PermissionValue pv = buildPermissionValue(prr);
134 public void fillCommonPart(PermissionRole pr, DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
136 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
137 SubjectType subject = pr.getSubject();
138 if (subject == null) {
139 //it is not required to give subject as URI determines the subject
140 subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
142 //subject mismatch should have been checked during validation
144 if (subject.equals(SubjectType.ROLE)) {
145 //FIXME: potential index out of bounds exception...negative test needed
146 PermissionValue pv = pr.getPermissions().get(0);
147 for (RoleValue rv : pr.getRoles()) {
148 PermissionRoleRel prr = buildPermissonRoleRel(pv, rv);
151 } else if (SubjectType.PERMISSION.equals(subject)) {
152 //FIXME: potential index out of bounds exception...negative test needed
153 RoleValue rv = pr.getRoles().get(0);
154 for (PermissionValue pv : pr.getPermissions()) {
155 PermissionRoleRel prr = buildPermissonRoleRel(pv, rv);
162 public PermissionsRolesList extractCommonPartList(
163 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
166 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
170 public PermissionRole getCommonPart() {
171 return permissionRole;
175 public void setCommonPart(PermissionRole permissionRole) {
176 this.permissionRole = permissionRole;
180 public PermissionsRolesList getCommonPartList() {
181 return permissionRolesList;
185 public void setCommonPartList(PermissionsRolesList permissionRolesList) {
186 this.permissionRolesList = permissionRolesList;
190 public String getQProperty(
196 public DocumentFilter createDocumentFilter() {
197 return new DocumentFilter(this.getServiceContext());
200 private PermissionValue buildPermissionValue(PermissionRoleRel prr) {
201 PermissionValue pv = new PermissionValue();
202 pv.setPermissionId(prr.getPermissionId());
203 pv.setResourceName(prr.getPermissionResource());
207 private RoleValue buildRoleValue(PermissionRoleRel prr) {
208 RoleValue rv = new RoleValue();
209 rv.setRoleId(prr.getRoleId());
210 rv.setRoleName(prr.getRoleName());
214 private PermissionRoleRel buildPermissonRoleRel(PermissionValue pv, RoleValue rv) {
215 PermissionRoleRel prr = new PermissionRoleRel();
216 prr.setPermissionId(pv.getPermissionId());
217 prr.setPermissionResource(pv.getResourceName());
218 prr.setRoleId(rv.getRoleId());
219 prr.setRoleName(rv.getRoleName());