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 javax.persistence.NoResultException;
31 import org.collectionspace.services.authorization.PermissionRole;
32 import org.collectionspace.services.authorization.PermissionRoleRel;
33 import org.collectionspace.services.authorization.PermissionValue;
34 import org.collectionspace.services.authorization.PermissionsRolesList;
35 import org.collectionspace.services.authorization.RoleValue;
36 import org.collectionspace.services.authorization.SubjectType;
38 import org.collectionspace.services.common.authorization_mgt.AuthorizationRoleRel;
39 import org.collectionspace.services.common.authorization_mgt.PermissionRoleUtil;
40 import org.collectionspace.services.common.context.ServiceContext;
41 import org.collectionspace.services.common.document.DocumentFilter;
42 import org.collectionspace.services.common.document.DocumentWrapper;
43 import org.collectionspace.services.common.document.TransactionException;
44 import org.collectionspace.services.common.storage.jpa.JPATransactionContext;
45 import org.collectionspace.services.common.storage.jpa.JpaDocumentFilter;
46 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
47 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 // TODO: Auto-generated Javadoc
54 * Document handler for PermissionRole association.
58 public class PermissionRoleDocumentHandler
59 extends JpaDocumentHandler<PermissionRole, PermissionsRolesList, List<PermissionRoleRel>, List<PermissionRoleRel>> {
62 private final Logger logger = LoggerFactory.getLogger(PermissionRoleDocumentHandler.class);
64 /** The permission role. */
65 private PermissionRole permissionRole;
67 /** The permission roles list. */
68 private PermissionsRolesList permissionRolesList;
71 /** The permission role csid. */
72 private String permissionRoleCsid = null;
75 * Sets the permission role csid.
77 * @param thePermissionRoleCsid the new permission role csid
79 public void setPermissionRoleCsid(String thePermissionRoleCsid) {
80 this.permissionRoleCsid = thePermissionRoleCsid;
84 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
87 public void handleCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
88 fillCommonPart(getCommonPart(), wrapDoc);
89 filterOutExisting(wrapDoc);
92 private boolean permRoleRelExists(JPATransactionContext jpaTransactionContext, PermissionRoleRel permRoleRel) throws TransactionException {
93 boolean result = false;
95 PermissionRoleRel queryResult = null;
97 queryResult = (PermissionRoleRel) JpaStorageUtils.getEntityByDualKeys(jpaTransactionContext,
98 PermissionRoleRel.class.getName(),
99 PermissionStorageConstants.PERMREL_ROLE_ID, permRoleRel.getRoleId(),
100 PermissionStorageConstants.PERMREL_PERM_ID, permRoleRel.getPermissionId());
101 } catch (NoResultException e) {
102 // Ignore exception. Just means the permission hasn't been stored/persisted yet.
105 if (queryResult != null) {
113 * Find the existing (already persisted)
117 @SuppressWarnings("rawtypes")
118 private void filterOutExisting(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
119 List<PermissionRoleRel> permRoleRelList = wrapDoc.getWrappedObject();
121 ServiceContext ctx = getServiceContext();
122 JPATransactionContext jpaTransactionContext = (JPATransactionContext)ctx.openConnection();
124 jpaTransactionContext.beginTransaction();
126 for (PermissionRoleRel permRoleRel : permRoleRelList) {
127 if (permRoleRelExists(jpaTransactionContext, permRoleRel) == true) {
129 // Remove the item from the list since it already exists
131 permRoleRelList.remove(permRoleRel);
134 jpaTransactionContext.commitTransaction();
135 } catch (Exception e) {
136 jpaTransactionContext.markForRollback();
137 if (logger.isDebugEnabled()) {
138 logger.debug("Caught exception ", e);
142 ctx.closeConnection();
147 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
150 public void completeCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
151 PermissionRole pr = getCommonPart();
152 AuthorizationDelegate.addRelationships(getServiceContext(), pr);
156 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
159 public void handleUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
160 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
164 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
167 public void completeUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
168 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
172 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
174 @SuppressWarnings("unchecked")
176 public void handleGet(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
177 setCommonPart(extractCommonPart(wrapDoc));
178 getServiceContext().setOutput(permissionRole);
182 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
185 public void handleGetAll(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
186 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
190 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
193 public boolean handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
194 fillCommonPart(getCommonPart(), wrapDoc, true);
199 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
202 public void completeDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
203 PermissionRole pr = getCommonPart();
204 AuthorizationDelegate.deletePermissionsFromRoles(getServiceContext(), pr);
208 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
211 public PermissionRole extractCommonPart(
212 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
214 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
215 PermissionRole pr = new PermissionRole();
216 SubjectType subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
217 if (prrl.size() == 0) {
220 PermissionRoleRel prr0 = prrl.get(0);
221 if (SubjectType.ROLE.equals(subject)) {
223 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
224 pr.setPermission(pvs);
225 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr0);
229 List<RoleValue> rvs = new ArrayList<RoleValue>();
231 for (PermissionRoleRel prr : prrl) {
232 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr);
235 } else if (SubjectType.PERMISSION.equals(subject)) {
237 List<RoleValue> rvs = new ArrayList<RoleValue>();
239 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr0);
243 List<PermissionValue> pvs = new ArrayList<PermissionValue>();
244 pr.setPermission(pvs);
245 for (PermissionRoleRel prr : prrl) {
246 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr);
257 * @param wrapDoc the wrap doc
258 * @param handleDelete the handle delete
259 * @throws Exception the exception
261 @SuppressWarnings("rawtypes")
262 public void fillCommonPart(PermissionRole pr,
263 DocumentWrapper<List<PermissionRoleRel>> wrapDoc,
264 boolean handleDelete)
266 List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
267 SubjectType subject = pr.getSubject();
268 if (subject == null) {
269 //it is not required to give subject as URI determines the subject
270 subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
272 //subject mismatch should have been checked during validation
275 ServiceContext ctx = this.getServiceContext();
276 String tenantId = ctx.getTenantId();
277 PermissionRoleUtil.buildPermissionRoleRel(ctx, pr, subject, prrl, handleDelete, tenantId);
281 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
284 public void fillCommonPart(PermissionRole ar,
285 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
287 fillCommonPart(ar, wrapDoc, false);
291 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
294 public PermissionsRolesList extractCommonPartList(
295 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
298 throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
302 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
305 public PermissionRole getCommonPart() {
306 return permissionRole;
310 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
313 public void setCommonPart(PermissionRole permissionRole) {
314 this.permissionRole = permissionRole;
318 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
321 public PermissionsRolesList getCommonPartList() {
322 return permissionRolesList;
326 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
329 public void setCommonPartList(PermissionsRolesList permissionRolesList) {
330 this.permissionRolesList = permissionRolesList;
334 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
337 public String getQProperty(
343 * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
346 public DocumentFilter createDocumentFilter() {
347 return new JpaDocumentFilter(this.getServiceContext());