]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
328205a9f76700266221669ce4857c0180d0add7
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.authorization.storage;
25
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.UUID;
30
31 import org.collectionspace.services.authorization.Permission;
32 import org.collectionspace.services.authorization.PermissionsList;
33
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;
40
41 /**
42  * Document handler for Permission
43  * @author 
44  */
45 public class PermissionDocumentHandler
46         extends AbstractDocumentHandlerImpl<Permission, PermissionsList, Permission, List> {
47
48     private final Logger logger = LoggerFactory.getLogger(PermissionDocumentHandler.class);
49     private Permission permission;
50     private PermissionsList permissionsList;
51
52     @Override
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());
60     }
61
62     @Override
63     public void handleUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
64         Permission permissionFound = wrapDoc.getWrappedObject();
65         Permission permissionReceived = getCommonPart();
66         merge(permissionReceived, permissionFound);
67     }
68
69     /**
70      * merge manually merges the from from to the to permission
71      * -this method is created due to inefficiency of JPA EM merge
72      * @param from
73      * @param to
74      * @return merged permission
75      */
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());
81         }
82         if (from.getAttributeName() != null) {
83             to.setAttributeName(from.getAttributeName());
84         }
85         if (from.getDescription() != null) {
86             to.setDescription(from.getDescription());
87         }
88         if (from.getEffect() != null) {
89             to.setEffect(from.getEffect());
90         }
91
92         //fixme update on actions
93
94         if (logger.isDebugEnabled()) {
95             logger.debug("merged permission=" + JaxbUtils.toString(to, Permission.class));
96         }
97
98         return to;
99     }
100
101     @Override
102     public void completeUpdate(DocumentWrapper<Permission> wrapDoc) throws Exception {
103         Permission upAcc = wrapDoc.getWrappedObject();
104         getServiceContext().setOutput(permission);
105         sanitize(upAcc);
106     }
107
108     @Override
109     public void handleGet(DocumentWrapper<Permission> wrapDoc) throws Exception {
110         setCommonPart(extractCommonPart(wrapDoc));
111         sanitize(getCommonPart());
112         getServiceContext().setOutput(permission);
113     }
114
115     @Override
116     public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
117         PermissionsList permissionsList = extractCommonPartList(wrapDoc);
118         setCommonPartList(permissionsList);
119         getServiceContext().setOutput(getCommonPartList());
120     }
121
122     @Override
123     public Permission extractCommonPart(
124             DocumentWrapper<Permission> wrapDoc)
125             throws Exception {
126         return wrapDoc.getWrappedObject();
127     }
128
129     @Override
130     public void fillCommonPart(Permission obj, DocumentWrapper<Permission> wrapDoc)
131             throws Exception {
132         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
133     }
134
135     @Override
136     public PermissionsList extractCommonPartList(
137             DocumentWrapper<List> wrapDoc)
138             throws Exception {
139
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);
147         }
148         return permissionsList;
149     }
150
151     @Override
152     public Permission getCommonPart() {
153         return permission;
154     }
155
156     @Override
157     public void setCommonPart(Permission permission) {
158         this.permission = permission;
159     }
160
161     @Override
162     public PermissionsList getCommonPartList() {
163         return permissionsList;
164     }
165
166     @Override
167     public void setCommonPartList(PermissionsList permissionsList) {
168         this.permissionsList = permissionsList;
169     }
170
171     @Override
172     public String getQProperty(
173             String prop) {
174         return null;
175     }
176
177     @Override
178     public DocumentFilter createDocumentFilter() {
179         DocumentFilter filter = new PermissionJpaFilter(this.getServiceContext());
180         return filter;
181     }
182
183     /**
184      * sanitize removes data not needed to be sent to the consumer
185      * @param permission
186      */
187     private void sanitize(Permission permission) {
188         permission.setTenantId(null);
189     }
190 }