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