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.PermissionsList;
34 import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl;
35 import org.collectionspace.services.common.document.DocumentFilter;
36 import org.collectionspace.services.common.document.DocumentWrapper;
37 import org.collectionspace.services.common.document.JaxbUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Document handler for Permission
45 public class PermissionDocumentHandler
46 extends AbstractDocumentHandlerImpl<Permission, PermissionsList, Permission, List> {
48 private final Logger logger = LoggerFactory.getLogger(PermissionDocumentHandler.class);
49 private Permission permission;
50 private PermissionsList permissionsList;
53 public void handleCreate(DocumentWrapper<Permission> wrapDoc) throws Exception {
54 String id = UUID.randomUUID().toString();
55 Permission permission = wrapDoc.getWrappedObject();
56 permission.setCsid(id);
57 //FIXME: if admin updating the permission is a CS admin rather than
58 //the tenant admin, tenant id should be retrieved from the request
59 permission.setTenantId(getServiceContext().getTenantId());
63 public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
64 Permission permissionFound = wrapDoc.getWrappedObject();
65 Permission permissionReceived = getCommonPart();
66 merge(permissionReceived, permissionFound);
70 * merge manually merges the from from to the to permission
71 * -this method is created due to inefficiency of JPA EM merge
74 * @return merged permission
76 private Permission merge(Permission from, Permission to) {
77 Date now = new Date();
78 to.setUpdatedAtItem(now);
79 if (from.getResourceName() != null) {
80 to.setResourceName(from.getResourceName());
82 if (from.getAttributeName() != null) {
83 to.setAttributeName(from.getAttributeName());
85 if (from.getDescription() != null) {
86 to.setDescription(from.getDescription());
88 if (from.getEffect() != null) {
89 to.setEffect(from.getEffect());
92 //fixme update on actions
94 if (logger.isDebugEnabled()) {
95 logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
102 public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
103 Permission upAcc = wrapDoc.getWrappedObject();
104 getServiceContext().setOutput(permission);
109 public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
110 setCommonPart(extractCommonPart(wrapDoc));
111 sanitize(getCommonPart());
112 getServiceContext().setOutput(permission);
116 public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
117 PermissionsList permissionsList = extractCommonPartList(wrapDoc);
118 setCommonPartList(permissionsList);
119 getServiceContext().setOutput(getCommonPartList());
123 public Permission extractCommonPart(
124 DocumentWrapper<Permission> wrapDoc)
126 return wrapDoc.getWrappedObject();
130 public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
132 throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
136 public PermissionsList extractCommonPartList(
137 DocumentWrapper<List> wrapDoc)
140 PermissionsList permissionsList = new PermissionsList();
141 List<Permission> list = new ArrayList<Permission>();
142 permissionsList.setPermissions(list);
143 for (Object obj : wrapDoc.getWrappedObject()) {
144 Permission permission = (Permission) obj;
145 sanitize(permission);
146 list.add(permission);
148 return permissionsList;
152 public Permission getCommonPart() {
157 public void setCommonPart(Permission permission) {
158 this.permission = permission;
162 public PermissionsList getCommonPartList() {
163 return permissionsList;
167 public void setCommonPartList(PermissionsList permissionsList) {
168 this.permissionsList = permissionsList;
172 public String getQProperty(
178 public DocumentFilter createDocumentFilter() {
179 DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
184 * sanitize removes data not needed to be sent to the consumer
187 private void sanitize(Permission permission) {
188 permission.setTenantId(null);