]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8aa20967613d3513864e374b60ad622aae930857
[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 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.NoResultException;
33 import javax.persistence.Query;
34 import org.collectionspace.services.authorization.PermissionRoleRel;
35 import org.collectionspace.services.authorization.SubjectType;
36 import org.collectionspace.services.common.context.ServiceContext;
37 import org.collectionspace.services.common.document.BadRequestException;
38 import org.collectionspace.services.common.document.DocumentException;
39 import org.collectionspace.services.common.document.DocumentFilter;
40 import org.collectionspace.services.common.document.DocumentHandler;
41 import org.collectionspace.services.common.document.DocumentHandler.Action;
42 import org.collectionspace.services.common.document.DocumentNotFoundException;
43 import org.collectionspace.services.common.document.DocumentWrapper;
44 import org.collectionspace.services.common.document.DocumentWrapperImpl;
45 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * PermissionRoleStorageClient deals with PermissionRole relationship
52  * in persistent storage. This storage client deals with bulk operations, i.e.
53  * create/post inserts multiple tuples between the given object and subjects
54  * get retrieves all subjects for the given object in relationship
55  * delete deletes all subjects for the given object in relationship
56  * @author 
57  */
58 public class PermissionRoleStorageClient extends JpaStorageClientImpl {
59
60     private final Logger logger = LoggerFactory.getLogger(PermissionRoleStorageClient.class);
61
62     public PermissionRoleStorageClient() {
63     }
64
65     /**
66      * create of PermissionRole relationship creates one or more relationships between
67      * permission and role
68      * the object and subjects of the relationship is chosen (by doc handler) from
69      * the payload
70      * @param ctx
71      * @param handler
72      * @return
73      * @throws BadRequestException
74      * @throws DocumentException
75      */
76     @Override
77     public String create(ServiceContext ctx,
78             DocumentHandler handler) throws BadRequestException,
79             DocumentException {
80
81         if (ctx == null) {
82             throw new IllegalArgumentException(
83                     "PermissionRoleStorageClient.create : ctx is missing");
84         }
85         if (handler == null) {
86             throw new IllegalArgumentException(
87                     "PermissionRoleStorageClient.create: handler is missing");
88         }
89         EntityManagerFactory emf = null;
90         EntityManager em = null;
91         try {
92             handler.prepare(Action.CREATE);
93             List<PermissionRoleRel> prrl = new ArrayList<PermissionRoleRel>();
94             DocumentWrapper<List<PermissionRoleRel>> wrapDoc =
95                     new DocumentWrapperImpl<List<PermissionRoleRel>>(prrl);
96             handler.handle(Action.CREATE, wrapDoc);
97             emf = getEntityManagerFactory();
98             em = emf.createEntityManager();
99             em.getTransaction().begin();
100             for (PermissionRoleRel prr : prrl) {
101                 prr.setCreatedAtItem(new Date());
102                 em.persist(prr);
103             }
104             em.getTransaction().commit();
105             handler.complete(Action.CREATE, wrapDoc);
106             return UUID.randomUUID().toString(); //filler, not useful
107         } catch (BadRequestException bre) {
108             if (em != null && em.getTransaction().isActive()) {
109                 em.getTransaction().rollback();
110             }
111             throw bre;
112         } catch (Exception e) {
113             if (em != null && em.getTransaction().isActive()) {
114                 em.getTransaction().rollback();
115             }
116             if (logger.isDebugEnabled()) {
117                 logger.debug("Caught exception ", e);
118             }
119             throw new DocumentException(e);
120         } finally {
121             if (em != null) {
122                 releaseEntityManagerFactory(emf);
123             }
124         }
125     }
126
127     /**
128      * get retrieves all relationships for the object in the relationship
129      * identified by the id. the object could be a permission or a role
130      * @param ctx
131      * @param id of the object in the relationship
132      * @param handler
133      * @throws DocumentNotFoundException
134      * @throws DocumentException
135      */
136     @Override
137     public void get(ServiceContext ctx, String id, DocumentHandler handler)
138             throws DocumentNotFoundException, DocumentException {
139         if (ctx == null) {
140             throw new IllegalArgumentException(
141                     "PermissionRoleStorageClient.get: ctx is missing");
142         }
143         if (handler == null) {
144             throw new IllegalArgumentException(
145                     "PermissionRoleStorageClient.get: handler is missing");
146         }
147         DocumentFilter docFilter = handler.getDocumentFilter();
148         if (docFilter == null) {
149             docFilter = handler.createDocumentFilter();
150         }
151         EntityManagerFactory emf = null;
152         EntityManager em = null;
153         try {
154             handler.prepare(Action.GET);
155             StringBuilder queryStrBldr = new StringBuilder("SELECT a FROM ");
156             queryStrBldr.append(getEntityName(ctx));
157             queryStrBldr.append(" a");
158             SubjectType subject = PermissionRoleUtil.getSubject(ctx);
159             String objectId = "permission_id";
160             if (SubjectType.PERMISSION.equals(subject)) {
161                 objectId = "role_id";
162             }
163             queryStrBldr.append(" WHERE " + objectId + " = :objectId");
164             String where = docFilter.getWhereClause();
165             if ((null != where) && (where.length() > 0)) {
166                 queryStrBldr.append(" AND " + where);
167             }
168             emf = getEntityManagerFactory();
169             em = emf.createEntityManager();
170             String queryStr = queryStrBldr.toString(); //for debugging
171             Query q = em.createQuery(queryStr);
172             q.setParameter("objectId", id);
173
174             List<PermissionRoleRel> prrl = new ArrayList<PermissionRoleRel>();
175             try {
176                 //require transaction for get?
177                 em.getTransaction().begin();
178                 prrl = q.getResultList();
179                 em.getTransaction().commit();
180             } catch (NoResultException nre) {
181                 if (em != null && em.getTransaction().isActive()) {
182                     em.getTransaction().rollback();
183                 }
184                 String msg = "could not find entity with id=" + id;
185                 logger.error(msg, nre);
186                 throw new DocumentNotFoundException(msg, nre);
187             }
188             if (prrl.size() == 0) {
189                 String msg = "could not find entity with id=" + id;
190                 logger.error(msg);
191                 throw new DocumentNotFoundException(msg);
192             }
193             DocumentWrapper<List<PermissionRoleRel>> wrapDoc =
194                     new DocumentWrapperImpl<List<PermissionRoleRel>>(prrl);
195             handler.handle(Action.GET, wrapDoc);
196             handler.complete(Action.GET, wrapDoc);
197         } catch (DocumentException de) {
198             throw de;
199         } catch (Exception e) {
200             if (logger.isDebugEnabled()) {
201                 logger.debug("Caught exception ", e);
202             }
203             throw new DocumentException(e);
204         } finally {
205             if (emf != null) {
206                 releaseEntityManagerFactory(emf);
207             }
208         }
209     }
210
211     /**
212      * delete removes all the relationships for the object in the relationship
213      * identified by the id. the object could be a permission or a role
214      * @param ctx
215      * @param id of the object in the relationship
216      * @throws DocumentNotFoundException
217      * @throws DocumentException
218      */
219     @Override
220     public void delete(ServiceContext ctx, String id)
221             throws DocumentNotFoundException,
222             DocumentException {
223
224         if (logger.isDebugEnabled()) {
225             logger.debug("deleting entity with id=" + id);
226         }
227         if (ctx == null) {
228             throw new IllegalArgumentException(
229                     "PermissionRoleStorageClient.delete : ctx is missing");
230         }
231         EntityManagerFactory emf = null;
232         EntityManager em = null;
233         try {
234             StringBuilder deleteStr = new StringBuilder("DELETE FROM ");
235             deleteStr.append(getEntityName(ctx));
236             SubjectType subject = PermissionRoleUtil.getSubject(ctx);
237             String objectId = "permission_id";
238             if (SubjectType.PERMISSION.equals(subject)) {
239                 objectId = "role_id";
240             }
241             deleteStr.append(" WHERE " + objectId + " = :objectId");
242             emf = getEntityManagerFactory();
243             em = emf.createEntityManager();
244             Query q = em.createQuery(deleteStr.toString());
245             q.setParameter("objectId", id);
246             int rcount = 0;
247             em.getTransaction().begin();
248             rcount = q.executeUpdate();
249             if (rcount == 0) {
250                 if (em != null && em.getTransaction().isActive()) {
251                     em.getTransaction().rollback();
252                 }
253                 String msg = "could not find entity with id=" + id;
254                 logger.error(msg);
255                 throw new DocumentNotFoundException(msg);
256             }
257             em.getTransaction().commit();
258
259         } catch (DocumentException de) {
260             if (em != null && em.getTransaction().isActive()) {
261                 em.getTransaction().rollback();
262             }
263             throw de;
264         } catch (Exception e) {
265             if (logger.isDebugEnabled()) {
266                 logger.debug("Caught exception ", e);
267             }
268             if (em != null && em.getTransaction().isActive()) {
269                 em.getTransaction().rollback();
270             }
271             throw new DocumentException(e);
272         } finally {
273             if (emf != null) {
274                 releaseEntityManagerFactory(emf);
275             }
276         }
277     }
278 }