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 permissions and
22 * limitations under the License.
24 package org.collectionspace.services.authorization.storage;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.UUID;
31 import org.collectionspace.services.authorization.Permission;
32 import org.collectionspace.services.authorization.PermissionAction;
33 import org.collectionspace.services.authorization.PermissionsList;
35 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
36 import org.collectionspace.services.common.document.DocumentFilter;
37 import org.collectionspace.services.common.document.DocumentWrapper;
38 import org.collectionspace.services.common.document.JaxbUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Document handler for Permission
46 public class PermissionDocumentHandler
47 extends AbstractDocumentHandlerImpl<Permission, PermissionsList, Permission, List> {
49 private final Logger logger = LoggerFactory.getLogger(PermissionDocumentHandler.class);
50 private Permission permission;
51 private PermissionsList permissionsList;
54 public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
55 String id = UUID.randomUUID().toString();
56 Permission permission = wrapDoc.getWrappedObject();
57 permission.setCsid(id);
58 //FIXME: if admin updating the permission is a CS admin rather than
59 //the tenant admin, tenant id should be retrieved from the request
60 permission.setTenantId(getServiceContext().getTenantId());
64 public void completeCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
68 public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
69 Permission permissionFound = wrapDoc.getWrappedObject();
70 Permission permissionReceived = getCommonPart();
71 merge(permissionReceived, permissionFound);
75 * merge manually merges the from from to the to permission
76 * -this method is created due to inefficiency of JPA EM merge
79 * @return merged permission
81 private Permission merge(Permission from, Permission to) {
82 Date now = new Date();
83 to.setUpdatedAtItem(now);
84 if (from.getResourceName() != null) {
85 to.setResourceName(from.getResourceName());
87 if (from.getAttributeName() != null) {
88 to.setAttributeName(from.getAttributeName());
90 if (from.getDescription() != null) {
91 to.setDescription(from.getDescription());
93 if (from.getEffect() != null) {
94 to.setEffect(from.getEffect());
96 List<PermissionAction> fromActions = from.getActions();
97 if (!fromActions.isEmpty()) {
98 //override the whole list, no reconcilliation by design
99 to.setActions(fromActions);
102 if (logger.isDebugEnabled()) {
103 logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
110 public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
111 Permission upAcc = wrapDoc.getWrappedObject();
112 getServiceContext().setOutput(permission);
117 public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
118 setCommonPart(extractCommonPart(wrapDoc));
119 sanitize(getCommonPart());
120 getServiceContext().setOutput(permission);
124 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
125 PermissionsList permissionsList = extractCommonPartList(wrapDoc);
126 setCommonPartList(permissionsList);
127 getServiceContext().setOutput(getCommonPartList());
131 public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
135 public Permission extractCommonPart(
136 DocumentWrapper<Permission> wrapDoc)
138 return wrapDoc.getWrappedObject();
142 public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
144 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
148 public PermissionsList extractCommonPartList(
149 DocumentWrapper<List> wrapDoc)
152 PermissionsList permissionsList = new PermissionsList();
153 List<Permission> list = new ArrayList<Permission>();
154 permissionsList.setPermissions(list);
155 for (Object obj : wrapDoc.getWrappedObject()) {
156 Permission permission = (Permission) obj;
157 sanitize(permission);
158 list.add(permission);
160 return permissionsList;
164 public Permission getCommonPart() {
169 public void setCommonPart(Permission permission) {
170 this.permission = permission;
174 public PermissionsList getCommonPartList() {
175 return permissionsList;
179 public void setCommonPartList(PermissionsList permissionsList) {
180 this.permissionsList = permissionsList;
184 public String getQProperty(
190 public DocumentFilter createDocumentFilter() {
191 DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
196 * sanitize removes data not needed to be sent to the consumer
199 private void sanitize(Permission permission) {
200 permission.setTenantId(null);