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.authorization_mgt.PermissionRoleUtil;
39 import org.collectionspace.services.common.document.DocumentFilter;
40 import org.collectionspace.services.common.document.DocumentWrapper;
41 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 // TODO: Auto-generated Javadoc
47 * Document handler for PermissionRole association.
51 public class PermissionRoleDocumentHandler
52 extends JpaDocumentHandler<PermissionRole, PermissionsRolesList, List<PermissionRoleRel>, List<PermissionRoleRel>> {
55 private final Logger logger = LoggerFactory.getLogger(PermissionRoleDocumentHandler.class);
57 /** The permission role. */
58 private PermissionRole permissionRole;
60 /** The permission roles list. */
61 private PermissionsRolesList permissionRolesList;
64 /** The permission role csid. */
65 private String permissionRoleCsid = null;
68 * Sets the permission role csid.
70 * @param thePermissionRoleCsid the new permission role csid
72 public void setPermissionRoleCsid(String thePermissionRoleCsid) {
73 this.permissionRoleCsid = thePermissionRoleCsid;
77 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
80 public void handleCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
81 fillCommonPart(getCommonPart(), wrapDoc);
85 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
88 public void completeCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
89 PermissionRole pr = getCommonPart();
90 AuthorizationDelegate.addPermissions(getServiceContext(), pr);
94 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
97 public void handleUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
98 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
102 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
105 public void completeUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
106 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
110 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
113 public void handleGet(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
114 setCommonPart(extractCommonPart(wrapDoc));
115 getServiceContext().setOutput(permissionRole);
119 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
122 public void handleGetAll(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
123 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
127 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
130 public void handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
131 fillCommonPart(getCommonPart(), wrapDoc, true);
135 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
138 public void completeDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
139 PermissionRole pr = getCommonPart();
140 AuthorizationDelegate.deletePermissions(getServiceContext(), pr);
144 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
147 public PermissionRole extractCommonPart(
148 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
150 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
151 PermissionRole pr = new PermissionRole();
152 SubjectType subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
153 if (prrl.size() == 0) {
156 PermissionRoleRel prr0 = prrl.get(0);
157 if (SubjectType.ROLE.equals(subject)) {
159 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
160 pr.setPermission(pvs);
161 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr0);
165 List<RoleValue> rvs = new ArrayList<RoleValue>();
167 for (PermissionRoleRel prr : prrl) {
168 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr);
171 } else if (SubjectType.PERMISSION.equals(subject)) {
173 List<RoleValue> rvs = new ArrayList<RoleValue>();
175 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr0);
179 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
180 pr.setPermission(pvs);
181 for (PermissionRoleRel prr : prrl) {
182 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr);
193 * @param wrapDoc the wrap doc
194 * @param handleDelete the handle delete
195 * @throws Exception the exception
197 public void fillCommonPart(PermissionRole pr,
198 DocumentWrapper<List<PermissionRoleRel>> wrapDoc,
199 boolean handleDelete)
201 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
202 SubjectType subject = pr.getSubject();
203 if (subject == null) {
204 //it is not required to give subject as URI determines the subject
205 subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
207 //subject mismatch should have been checked during validation
209 PermissionRoleUtil.buildPermissionRoleRel(pr, subject, prrl, handleDelete);
213 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
216 public void fillCommonPart(PermissionRole ar,
217 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
219 fillCommonPart(ar, wrapDoc, false);
223 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
226 public PermissionsRolesList extractCommonPartList(
227 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
230 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
234 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
237 public PermissionRole getCommonPart() {
238 return permissionRole;
242 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
245 public void setCommonPart(PermissionRole permissionRole) {
246 this.permissionRole = permissionRole;
250 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
253 public PermissionsRolesList getCommonPartList() {
254 return permissionRolesList;
258 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
261 public void setCommonPartList(PermissionsRolesList permissionRolesList) {
262 this.permissionRolesList = permissionRolesList;
266 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
269 public String getQProperty(
275 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
278 public DocumentFilter createDocumentFilter() {
279 return new DocumentFilter(this.getServiceContext());