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.AccountRole;
30 import org.collectionspace.services.authorization.AccountRoleRel;
31 import org.collectionspace.services.authorization.Permission;
32 import org.collectionspace.services.authorization.PermissionRole;
33 import org.collectionspace.services.authorization.PermissionRoleRel;
34 import org.collectionspace.services.authorization.PermissionValue;
35 import org.collectionspace.services.authorization.PermissionsList;
36 import org.collectionspace.services.authorization.PermissionsRolesList;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.authorization.SubjectType;
40 import org.collectionspace.services.common.authorization_mgt.AuthorizationRoleRel;
41 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
42 import org.collectionspace.services.common.document.DocumentFilter;
43 import org.collectionspace.services.common.document.DocumentWrapper;
44 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 // TODO: Auto-generated Javadoc
50 * Document handler for PermissionRole association.
54 public class PermissionRoleDocumentHandler
55 extends JpaDocumentHandler<PermissionRole, PermissionsRolesList, List<PermissionRoleRel>, List<PermissionRoleRel>> {
58 private final Logger logger = LoggerFactory.getLogger(PermissionRoleDocumentHandler.class);
60 /** The permission role. */
61 private PermissionRole permissionRole;
63 /** The permission roles list. */
64 private PermissionsRolesList permissionRolesList;
67 /** The permission role csid. */
68 private String permissionRoleCsid = null;
71 * Sets the permission role csid.
73 * @param thePermissionRoleCsid the new permission role csid
75 public void setPermissionRoleCsid(String thePermissionRoleCsid) {
76 this.permissionRoleCsid = thePermissionRoleCsid;
80 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
83 public void handleCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
84 fillCommonPart(getCommonPart(), wrapDoc);
88 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
91 public void completeCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
92 PermissionRole pr = getCommonPart();
93 AuthorizationDelegate.addPermissions(getServiceContext(), pr);
97 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
100 public void handleUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
101 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
105 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
108 public void completeUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
109 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
113 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
116 public void handleGet(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
117 setCommonPart(extractCommonPart(wrapDoc));
118 getServiceContext().setOutput(permissionRole);
122 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
125 public void handleGetAll(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
126 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
130 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
133 public void handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
134 fillCommonPart(getCommonPart(), wrapDoc, true);
138 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
141 public void completeDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
142 PermissionRole pr = getCommonPart();
143 AuthorizationDelegate.deletePermissions(getServiceContext(), pr);
147 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
150 public PermissionRole extractCommonPart(
151 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
153 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
154 PermissionRole pr = new PermissionRole();
155 SubjectType subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
156 if (prrl.size() == 0) {
159 PermissionRoleRel prr0 = prrl.get(0);
160 if (SubjectType.ROLE.equals(subject)) {
162 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
163 pr.setPermissions(pvs);
164 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr0);
168 List<RoleValue> rvs = new ArrayList<RoleValue>();
170 for (PermissionRoleRel prr : prrl) {
171 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr);
174 } else if (SubjectType.PERMISSION.equals(subject)) {
176 List<RoleValue> rvs = new ArrayList<RoleValue>();
178 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr0);
182 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
183 pr.setPermissions(pvs);
184 for (PermissionRoleRel prr : prrl) {
185 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr);
196 * @param wrapDoc the wrap doc
197 * @param handleDelete the handle delete
198 * @throws Exception the exception
200 public void fillCommonPart(PermissionRole pr,
201 DocumentWrapper<List<PermissionRoleRel>> wrapDoc,
202 boolean handleDelete)
204 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
205 SubjectType subject = pr.getSubject();
206 if (subject == null) {
207 //it is not required to give subject as URI determines the subject
208 subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
210 //subject mismatch should have been checked during validation
212 PermissionRoleUtil.buildPermissionRoleRel(pr, subject, prrl, handleDelete);
216 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
219 public void fillCommonPart(PermissionRole ar,
220 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
222 fillCommonPart(ar, wrapDoc, false);
226 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
229 public PermissionsRolesList extractCommonPartList(
230 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
233 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
237 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
240 public PermissionRole getCommonPart() {
241 return permissionRole;
245 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
248 public void setCommonPart(PermissionRole permissionRole) {
249 this.permissionRole = permissionRole;
253 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
256 public PermissionsRolesList getCommonPartList() {
257 return permissionRolesList;
261 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
264 public void setCommonPartList(PermissionsRolesList permissionRolesList) {
265 this.permissionRolesList = permissionRolesList;
269 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
272 public String getQProperty(
278 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
281 public DocumentFilter createDocumentFilter() {
282 return new DocumentFilter(this.getServiceContext());