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.authorization_mgt.AuthorizationRoleRel;
37 import org.collectionspace.services.common.document.DocumentFilter;
38 import org.collectionspace.services.common.document.DocumentWrapper;
39 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 // TODO: Auto-generated Javadoc
45 * Document handler for PermissionRole association.
49 public class PermissionRoleDocumentHandler
50 extends JpaDocumentHandler<PermissionRole, PermissionsRolesList, List<PermissionRoleRel>, List<PermissionRoleRel>> {
53 private final Logger logger = LoggerFactory.getLogger(PermissionRoleDocumentHandler.class);
55 /** The permission role. */
56 private PermissionRole permissionRole;
58 /** The permission roles list. */
59 private PermissionsRolesList permissionRolesList;
62 /** The permission role csid. */
63 private String permissionRoleCsid = null;
66 * Sets the permission role csid.
68 * @param thePermissionRoleCsid the new permission role csid
70 public void setPermissionRoleCsid(String thePermissionRoleCsid) {
71 this.permissionRoleCsid = thePermissionRoleCsid;
75 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
78 public void handleCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
79 fillCommonPart(getCommonPart(), wrapDoc);
83 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
86 public void completeCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
87 PermissionRole pr = getCommonPart();
88 AuthorizationDelegate.addPermissions(getServiceContext(), pr);
92 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
95 public void handleUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
96 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
100 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
103 public void completeUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
104 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
108 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
111 public void handleGet(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
112 setCommonPart(extractCommonPart(wrapDoc));
113 getServiceContext().setOutput(permissionRole);
117 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
120 public void handleGetAll(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
121 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
125 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
128 public void handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
129 fillCommonPart(getCommonPart(), wrapDoc, true);
133 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
136 public void completeDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
137 PermissionRole pr = getCommonPart();
138 AuthorizationDelegate.deletePermissions(getServiceContext(), pr);
142 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
145 public PermissionRole extractCommonPart(
146 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
148 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
149 PermissionRole pr = new PermissionRole();
150 SubjectType subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
151 if (prrl.size() == 0) {
154 PermissionRoleRel prr0 = prrl.get(0);
155 if (SubjectType.ROLE.equals(subject)) {
157 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
158 pr.setPermission(pvs);
159 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr0);
163 List<RoleValue> rvs = new ArrayList<RoleValue>();
165 for (PermissionRoleRel prr : prrl) {
166 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr);
169 } else if (SubjectType.PERMISSION.equals(subject)) {
171 List<RoleValue> rvs = new ArrayList<RoleValue>();
173 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr0);
177 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
178 pr.setPermission(pvs);
179 for (PermissionRoleRel prr : prrl) {
180 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr);
191 * @param wrapDoc the wrap doc
192 * @param handleDelete the handle delete
193 * @throws Exception the exception
195 public void fillCommonPart(PermissionRole pr,
196 DocumentWrapper<List<PermissionRoleRel>> wrapDoc,
197 boolean handleDelete)
199 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
200 SubjectType subject = pr.getSubject();
201 if (subject == null) {
202 //it is not required to give subject as URI determines the subject
203 subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
205 //subject mismatch should have been checked during validation
207 PermissionRoleUtil.buildPermissionRoleRel(pr, subject, prrl, handleDelete);
211 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
214 public void fillCommonPart(PermissionRole ar,
215 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
217 fillCommonPart(ar, wrapDoc, false);
221 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
224 public PermissionsRolesList extractCommonPartList(
225 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
228 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
232 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
235 public PermissionRole getCommonPart() {
236 return permissionRole;
240 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
243 public void setCommonPart(PermissionRole permissionRole) {
244 this.permissionRole = permissionRole;
248 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
251 public PermissionsRolesList getCommonPartList() {
252 return permissionRolesList;
256 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
259 public void setCommonPartList(PermissionsRolesList permissionRolesList) {
260 this.permissionRolesList = permissionRolesList;
264 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
267 public String getQProperty(
273 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
276 public DocumentFilter createDocumentFilter() {
277 return new DocumentFilter(this.getServiceContext());