]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a3614ef1bdf2663869fe56f3e35898bcafefc088
[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 javax.persistence.NoResultException;
30
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;
37
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;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 // TODO: Auto-generated Javadoc
53 /**
54  * Document handler for PermissionRole association.
55  *
56  * @author
57  */
58 public class PermissionRoleDocumentHandler
59                 extends JpaDocumentHandler<PermissionRole, PermissionsRolesList, List<PermissionRoleRel>, List<PermissionRoleRel>> {
60
61     /** The logger. */
62     private final Logger logger = LoggerFactory.getLogger(PermissionRoleDocumentHandler.class);
63     
64     /** The permission role. */
65     private PermissionRole permissionRole;
66     
67     /** The permission roles list. */
68     private PermissionsRolesList permissionRolesList;
69
70     //
71     /** The permission role csid. */
72     private String permissionRoleCsid = null;
73
74     /**
75      * Sets the permission role csid.
76      *
77      * @param thePermissionRoleCsid the new permission role csid
78      */
79     public void setPermissionRoleCsid(String thePermissionRoleCsid) {
80         this.permissionRoleCsid = thePermissionRoleCsid;
81     }
82     
83     /* (non-Javadoc)
84      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
85      */
86     @Override
87     public void handleCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
88         fillCommonPart(getCommonPart(), wrapDoc);
89         filterOutExisting(wrapDoc);
90     }
91     
92     private boolean permRoleRelExists(JPATransactionContext jpaTransactionContext, PermissionRoleRel permRoleRel) throws TransactionException {
93         boolean result = false;
94         
95         PermissionRoleRel queryResult = null;
96         try {
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.
103         }
104         
105         if (queryResult != null) {
106                 result = true;
107         }
108         
109         return result;
110     }
111     
112     /**
113      * Find the existing (already persisted) 
114      * @param wrapDoc
115      * @throws Exception
116      */
117     @SuppressWarnings("rawtypes")
118         private void filterOutExisting(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
119         List<PermissionRoleRel> permRoleRelList = wrapDoc.getWrappedObject();
120         
121         ServiceContext ctx = getServiceContext();
122         JPATransactionContext jpaTransactionContext = (JPATransactionContext)ctx.openConnection();
123         try {
124                 jpaTransactionContext.beginTransaction();
125             
126             for (PermissionRoleRel permRoleRel : permRoleRelList) {
127                 if (permRoleRelExists(jpaTransactionContext, permRoleRel) == true) {
128                         //
129                         // Remove the item from the list since it already exists
130                         //
131                         permRoleRelList.remove(permRoleRel);
132                 }
133             }
134             jpaTransactionContext.commitTransaction();
135         } catch (Exception e) {
136                 jpaTransactionContext.markForRollback();
137             if (logger.isDebugEnabled()) {
138                 logger.debug("Caught exception ", e);
139             }
140             throw e;
141         } finally {
142             ctx.closeConnection();
143         }
144     }
145
146     /* (non-Javadoc)
147      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeCreate(org.collectionspace.services.common.document.DocumentWrapper)
148      */
149     @Override
150     public void completeCreate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
151         PermissionRole pr = getCommonPart();
152         AuthorizationDelegate.addRelationships(getServiceContext(), pr);
153     }
154
155     /* (non-Javadoc)
156      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
157      */
158     @Override
159     public void handleUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
160         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
161     }
162
163     /* (non-Javadoc)
164      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
165      */
166     @Override
167     public void completeUpdate(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
168         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
169     }
170
171     /* (non-Javadoc)
172      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGet(org.collectionspace.services.common.document.DocumentWrapper)
173      */
174     @SuppressWarnings("unchecked")
175         @Override
176     public void handleGet(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
177         setCommonPart(extractCommonPart(wrapDoc));
178         getServiceContext().setOutput(permissionRole);
179     }
180
181     /* (non-Javadoc)
182      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleGetAll(org.collectionspace.services.common.document.DocumentWrapper)
183      */
184     @Override
185     public void handleGetAll(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
186         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
187     }
188
189     /* (non-Javadoc)
190      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
191      */
192     @Override
193     public boolean handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
194         fillCommonPart(getCommonPart(), wrapDoc, true);
195         return true;
196     }
197
198     /* (non-Javadoc)
199      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#completeDelete(org.collectionspace.services.common.document.DocumentWrapper)
200      */
201     @Override
202     public void completeDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
203         PermissionRole pr = getCommonPart();
204         AuthorizationDelegate.deletePermissionsFromRoles(getServiceContext(), pr);
205     }
206
207     /* (non-Javadoc)
208      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPart(org.collectionspace.services.common.document.DocumentWrapper)
209      */
210     @Override
211     public PermissionRole extractCommonPart(
212             DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
213             throws Exception {
214         List<PermissionRoleRel> prrl = wrapDoc.getWrappedObject();
215         PermissionRole pr = new PermissionRole();
216         SubjectType subject = PermissionRoleUtil.getRelationSubject(getServiceContext());
217         if (prrl.size() == 0) {
218             return pr;
219         }
220         PermissionRoleRel prr0 = prrl.get(0);
221         if (SubjectType.ROLE.equals(subject)) {
222
223             List<PermissionValue> pvs = new ArrayList<PermissionValue>();
224             pr.setPermission(pvs);
225             PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr0);
226             pvs.add(pv);
227
228             //add roles
229             List<RoleValue> rvs = new ArrayList<RoleValue>();
230             pr.setRole(rvs);
231             for (PermissionRoleRel prr : prrl) {
232                 RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr);
233                 rvs.add(rv);
234             }
235         } else if (SubjectType.PERMISSION.equals(subject)) {
236
237             List<RoleValue> rvs = new ArrayList<RoleValue>();
238             pr.setRole(rvs);
239             RoleValue rv = AuthorizationRoleRel.buildRoleValue(prr0);
240             rvs.add(rv);
241
242             //add permssions
243             List<PermissionValue> pvs = new ArrayList<PermissionValue>();
244             pr.setPermission(pvs);
245             for (PermissionRoleRel prr : prrl) {
246                 PermissionValue pv = AuthorizationRoleRel.buildPermissionValue(prr);
247                 pvs.add(pv);
248             }
249         }
250         return pr;
251     }
252
253     /**
254      * Fill common part.
255      *
256      * @param pr the pr
257      * @param wrapDoc the wrap doc
258      * @param handleDelete the handle delete
259      * @throws Exception the exception
260      */
261     @SuppressWarnings("rawtypes")
262         public void fillCommonPart(PermissionRole pr,
263                         DocumentWrapper<List<PermissionRoleRel>> wrapDoc,
264                         boolean handleDelete)
265             throws Exception {
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());
271         } else {
272             //subject mismatch should have been checked during validation
273         }
274         
275         ServiceContext ctx = this.getServiceContext();
276         String tenantId = ctx.getTenantId();
277         PermissionRoleUtil.buildPermissionRoleRel(ctx, pr, subject, prrl, handleDelete, tenantId);
278     }
279     
280     /* (non-Javadoc)
281      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#fillCommonPart(java.lang.Object, org.collectionspace.services.common.document.DocumentWrapper)
282      */
283     @Override
284     public void fillCommonPart(PermissionRole ar,
285                 DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
286                         throws Exception {
287         fillCommonPart(ar, wrapDoc, false);
288     }    
289
290     /* (non-Javadoc)
291      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#extractCommonPartList(org.collectionspace.services.common.document.DocumentWrapper)
292      */
293     @Override
294     public PermissionsRolesList extractCommonPartList(
295             DocumentWrapper<List<PermissionRoleRel>> wrapDoc)
296             throws Exception {
297
298         throw new UnsupportedOperationException("operation not relevant for PermissionRoleDocumentHandler");
299     }
300
301     /* (non-Javadoc)
302      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPart()
303      */
304     @Override
305     public PermissionRole getCommonPart() {
306         return permissionRole;
307     }
308
309     /* (non-Javadoc)
310      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPart(java.lang.Object)
311      */
312     @Override
313     public void setCommonPart(PermissionRole permissionRole) {
314         this.permissionRole = permissionRole;
315     }
316
317     /* (non-Javadoc)
318      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getCommonPartList()
319      */
320     @Override
321     public PermissionsRolesList getCommonPartList() {
322         return permissionRolesList;
323     }
324
325     /* (non-Javadoc)
326      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#setCommonPartList(java.lang.Object)
327      */
328     @Override
329     public void setCommonPartList(PermissionsRolesList permissionRolesList) {
330         this.permissionRolesList = permissionRolesList;
331     }
332
333     /* (non-Javadoc)
334      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#getQProperty(java.lang.String)
335      */
336     @Override
337     public String getQProperty(
338             String prop) {
339         return null;
340     }
341
342     /* (non-Javadoc)
343      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#createDocumentFilter()
344      */
345     @Override
346     public DocumentFilter createDocumentFilter() {
347         return new JpaDocumentFilter(this.getServiceContext());
348     }
349 }