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;
35 import org.collectionspace.services.common.authorization_mgt.AuthorizationRoleRel;
36 import org.collectionspace.services.common.authorization_mgt.PermissionRoleUtil;
37 import org.collectionspace.services.common.document.DocumentFilter;
38 import org.collectionspace.services.common.document.DocumentWrapper;
39 import org.collectionspace.services.common.storage.jpa.JpaDocumentFilter;
40 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 // TODO: Auto-generated Javadoc
46 * Document handler for PermissionRole association.
50 public class PermissionRoleDocumentHandler
51 extends JpaDocumentHandler<PermissionRole, PermissionsRolesList, List<PermissionRoleRel>, List<PermissionRoleRel>> {
54 private final Logger logger = LoggerFactory.getLogger(PermissionRoleDocumentHandler.class);
56 /** The permission role. */
57 private PermissionRole permissionRole;
59 /** The permission roles list. */
60 private PermissionsRolesList permissionRolesList;
63 /** The permission role csid. */
64 private String permissionRoleCsid = null;
67 * Sets the permission role csid.
69 * @param thePermissionRoleCsid the new permission role csid
71 public void setPermissionRoleCsid(String thePermissionRoleCsid) {
72 this.permissionRoleCsid = thePermissionRoleCsid;
76 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
79 public void handleCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
80 fillCommonPart(getCommonPart(), wrapDoc);
84 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
87 public void completeCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
88 PermissionRole pr = getCommonPart();
89 AuthorizationDelegate.addPermissions(getServiceContext(), pr);
93 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
96 public void handleUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
97 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
101 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
104 public void completeUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
105 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
109 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
112 public void handleGet(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
113 setCommonPart(extractCommonPart(wrapDoc));
114 getServiceContext().setOutput(permissionRole);
118 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
121 public void handleGetAll(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
122 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
126 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
129 public void handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
130 fillCommonPart(getCommonPart(), wrapDoc, true);
134 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
137 public void completeDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
138 PermissionRole pr = getCommonPart();
139 AuthorizationDelegate.deletePermissions(getServiceContext(), pr);
143 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
146 public PermissionRole extractCommonPart(
147 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
149 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
150 PermissionRole pr = new PermissionRole();
151 SubjectType subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
152 if (prrl.size() == 0) {
155 PermissionRoleRel prr0 = prrl.get(0);
156 if (SubjectType.ROLE.equals(subject)) {
158 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
159 pr.setPermission(pvs);
160 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr0);
164 List<RoleValue> rvs = new ArrayList<RoleValue>();
166 for (PermissionRoleRel prr : prrl) {
167 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr);
170 } else if (SubjectType.PERMISSION.equals(subject)) {
172 List<RoleValue> rvs = new ArrayList<RoleValue>();
174 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr0);
178 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
179 pr.setPermission(pvs);
180 for (PermissionRoleRel prr : prrl) {
181 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr);
192 * @param wrapDoc the wrap doc
193 * @param handleDelete the handle delete
194 * @throws Exception the exception
196 public void fillCommonPart(PermissionRole pr,
197 DocumentWrapper<List<PermissionRoleRel>> wrapDoc,
198 boolean handleDelete)
200 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
201 SubjectType subject = pr.getSubject();
202 if (subject == null) {
203 //it is not required to give subject as URI determines the subject
204 subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
206 //subject mismatch should have been checked during validation
208 PermissionRoleUtil.buildPermissionRoleRel(pr, subject, prrl, handleDelete);
212 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
215 public void fillCommonPart(PermissionRole ar,
216 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
218 fillCommonPart(ar, wrapDoc, false);
222 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
225 public PermissionsRolesList extractCommonPartList(
226 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
229 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
233 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
236 public PermissionRole getCommonPart() {
237 return permissionRole;
241 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
244 public void setCommonPart(PermissionRole permissionRole) {
245 this.permissionRole = permissionRole;
249 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
252 public PermissionsRolesList getCommonPartList() {
253 return permissionRolesList;
257 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
260 public void setCommonPartList(PermissionsRolesList permissionRolesList) {
261 this.permissionRolesList = permissionRolesList;
265 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
268 public String getQProperty(
274 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
277 public DocumentFilter createDocumentFilter() {
278 return new JpaDocumentFilter(this.getServiceContext());