]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c394a0eee6d90beb23c3001e5e3ff9105e7a309d
[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 permissionRoles and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.authorization.storage;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.collectionspace.services.authorization.PermissionRole;
30 import org.collectionspace.services.authorization.PermissionRoleRel;
31 import org.collectionspace.services.authorization.PermissionValue;
32 import org.collectionspace.services.authorization.PermissionsRolesList;
33 import org.collectionspace.services.authorization.RoleValue;
34 import org.collectionspace.services.authorization.SubjectType;
35
36 import org.collectionspace.services.common.authorization_mgt.AuthorizationRoleRel;
37 import org.collectionspace.services.common.authorization_mgt.PermissionRoleUtil;
38 import org.collectionspace.services.common.document.DocumentFilter;
39 import org.collectionspace.services.common.document.DocumentWrapper;
40 import org.collectionspace.services.common.storage.jpa.JpaDocumentFilter;
41 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 // TODO: Auto-generated Javadoc
46 /**
47  * Document handler for PermissionRole association.
48  *
49  * @author
50  */
51 public class PermissionRoleDocumentHandler
52                 extends JpaDocumentHandler<PermissionRole, PermissionsRolesList, List<PermissionRoleRel>, List<PermissionRoleRel>> {
53
54     /** The logger. */
55     private final Logger logger = LoggerFactory.getLogger(PermissionRoleDocumentHandler.class);
56     
57     /** The permission role. */
58     private PermissionRole permissionRole;
59     
60     /** The permission roles list. */
61     private PermissionsRolesList permissionRolesList;
62
63     //
64     /** The permission role csid. */
65     private String permissionRoleCsid = null;
66
67     /**
68      * Sets the permission role csid.
69      *
70      * @param thePermissionRoleCsid the new permission role csid
71      */
72     public void setPermissionRoleCsid(String thePermissionRoleCsid) {
73         this.permissionRoleCsid = thePermissionRoleCsid;
74     }
75     
76     /* (non-Javadoc)
77      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
78      */
79     @Override
80     public void handleCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
81         fillCommonPart(getCommonPart(), wrapDoc);
82     }
83
84     /* (non-Javadoc)
85      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
86      */
87     @Override
88     public void completeCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
89         PermissionRole pr = getCommonPart();
90         AuthorizationDelegate.addPermissions(getServiceContext(), pr);
91     }
92
93     /* (non-Javadoc)
94      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
95      */
96     @Override
97     public void handleUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
98         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
99     }
100
101     /* (non-Javadoc)
102      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
103      */
104     @Override
105     public void completeUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
106         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
107     }
108
109     /* (non-Javadoc)
110      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
111      */
112     @Override
113     public void handleGet(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
114         setCommonPart(extractCommonPart(wrapDoc));
115         getServiceContext().setOutput(permissionRole);
116     }
117
118     /* (non-Javadoc)
119      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
120      */
121     @Override
122     public void handleGetAll(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
123         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
124     }
125
126     /* (non-Javadoc)
127      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
128      */
129     @Override
130     public boolean handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
131         fillCommonPart(getCommonPart(), wrapDoc, true);
132         return true;
133     }
134
135     /* (non-Javadoc)
136      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
137      */
138     @Override
139     public void completeDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
140         PermissionRole pr = getCommonPart();
141         AuthorizationDelegate.deletePermissions(getServiceContext(), pr);
142     }
143
144     /* (non-Javadoc)
145      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
146      */
147     @Override
148     public PermissionRole extractCommonPart(
149             DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
150             throws Exception {
151         List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
152         PermissionRole pr = new PermissionRole();
153         SubjectType subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
154         if (prrl.size() == 0) {
155             return pr;
156         }
157         PermissionRoleRel prr0 = prrl.get(0);
158         if (SubjectType.ROLE.equals(subject)) {
159
160             List<PermissionValue> pvs = new ArrayList<PermissionValue>();
161             pr.setPermission(pvs);
162             PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr0);
163             pvs.add(pv);
164
165             //add roles
166             List<RoleValue> rvs = new ArrayList<RoleValue>();
167             pr.setRole(rvs);
168             for (PermissionRoleRel prr : prrl) {
169                 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr);
170                 rvs.add(rv);
171             }
172         } else if (SubjectType.PERMISSION.equals(subject)) {
173
174             List<RoleValue> rvs = new ArrayList<RoleValue>();
175             pr.setRole(rvs);
176             RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr0);
177             rvs.add(rv);
178
179             //add permssions
180             List<PermissionValue> pvs = new ArrayList<PermissionValue>();
181             pr.setPermission(pvs);
182             for (PermissionRoleRel prr : prrl) {
183                 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr);
184                 pvs.add(pv);
185             }
186         }
187         return pr;
188     }
189
190     /**
191      * Fill common part.
192      *
193      * @param pr the pr
194      * @param wrapDoc the wrap doc
195      * @param handleDelete the handle delete
196      * @throws Exception the exception
197      */
198     public void fillCommonPart(PermissionRole pr,
199                         DocumentWrapper<List<PermissionRoleRel>> wrapDoc,
200                         boolean handleDelete)
201             throws Exception {
202         List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
203         SubjectType subject = pr.getSubject();
204         if (subject == null) {
205             //it is not required to give subject as URI determines the subject
206             subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
207         } else {
208             //subject mismatch should have been checked during validation
209         }
210         
211         String tenantId = this.getServiceContext().getTenantId();
212         PermissionRoleUtil.buildPermissionRoleRel(pr, subject, prrl, handleDelete, tenantId);
213     }
214     
215     /* (non-Javadoc)
216      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
217      */
218     @Override
219     public void fillCommonPart(PermissionRole ar,
220                 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
221                         throws Exception {
222         fillCommonPart(ar, wrapDoc, false);
223     }    
224
225     /* (non-Javadoc)
226      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
227      */
228     @Override
229     public PermissionsRolesList extractCommonPartList(
230             DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
231             throws Exception {
232
233         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
234     }
235
236     /* (non-Javadoc)
237      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
238      */
239     @Override
240     public PermissionRole getCommonPart() {
241         return permissionRole;
242     }
243
244     /* (non-Javadoc)
245      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
246      */
247     @Override
248     public void setCommonPart(PermissionRole permissionRole) {
249         this.permissionRole = permissionRole;
250     }
251
252     /* (non-Javadoc)
253      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
254      */
255     @Override
256     public PermissionsRolesList getCommonPartList() {
257         return permissionRolesList;
258     }
259
260     /* (non-Javadoc)
261      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
262      */
263     @Override
264     public void setCommonPartList(PermissionsRolesList permissionRolesList) {
265         this.permissionRolesList = permissionRolesList;
266     }
267
268     /* (non-Javadoc)
269      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
270      */
271     @Override
272     public String getQProperty(
273             String prop) {
274         return null;
275     }
276
277     /* (non-Javadoc)
278      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
279      */
280     @Override
281     public DocumentFilter createDocumentFilter() {
282         return new JpaDocumentFilter(this.getServiceContext());
283     }
284 }