]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
20c1b820f13a2b6253b88255368e97eda4b04e87
[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     public void get(ServiceContext ctx, String id, DocumentHandler handler)
137             throws DocumentNotFoundException, DocumentException {
138         if (ctx == null) {
139             throw new IllegalArgumentException(
140                     "JpaStorageClient.get: ctx is missing");
141         }
142         if (handler == null) {
143             throw new IllegalArgumentException(
144                     "JpaStorageClient.get: handler is missing");
145         }
146         DocumentFilter docFilter = handler.getDocumentFilter();
147         if (docFilter == null) {
148             docFilter = handler.createDocumentFilter(ctx);
149         }
150         EntityManagerFactory emf = null;
151         EntityManager em = null;
152         try {
153             handler.prepare(Action.GET);
154             StringBuilder queryStrBldr = new StringBuilder("SELECT a FROM ");
155             queryStrBldr.append(getEntityName(ctx));
156             queryStrBldr.append(" a");
157             SubjectType subject = PermissionRoleUtil.getSubject(ctx);
158             String objectId = "permission_id";
159             if (SubjectType.PERMISSION.equals(subject)) {
160                 objectId = "role_id";
161             }
162             queryStrBldr.append(" WHERE " + objectId + " = :objectId");
163             String where = docFilter.getWhereClause();
164             if ((null != where) && (where.length() > 0)) {
165                 queryStrBldr.append(" AND " + where);
166             }
167             emf = getEntityManagerFactory();
168             em = emf.createEntityManager();
169             String queryStr = queryStrBldr.toString(); //for debugging
170             Query q = em.createQuery(queryStr);
171             q.setParameter("objectId", id);
172
173             List<PermissionRoleRel> prrl = new ArrayList<PermissionRoleRel>();
174             try {
175                 //require transaction for get?
176                 em.getTransaction().begin();
177                 prrl = q.getResultList();
178                 em.getTransaction().commit();
179             } catch (NoResultException nre) {
180                 if (em != null && em.getTransaction().isActive()) {
181                     em.getTransaction().rollback();
182                 }
183                 String msg = "could not find entity with id=" + id;
184                 logger.error(msg, nre);
185                 throw new DocumentNotFoundException(msg, nre);
186             }
187             if (prrl.size() == 0) {
188                 String msg = "could not find entity with id=" + id;
189                 logger.error(msg);
190                 throw new DocumentNotFoundException(msg);
191             }
192             DocumentWrapper<List<PermissionRoleRel>> wrapDoc =
193                     new DocumentWrapperImpl<List<PermissionRoleRel>>(prrl);
194             handler.handle(Action.GET, wrapDoc);
195             handler.complete(Action.GET, wrapDoc);
196         } catch (DocumentException de) {
197             throw de;
198         } catch (Exception e) {
199             if (logger.isDebugEnabled()) {
200                 logger.debug("Caught exception ", e);
201             }
202             throw new DocumentException(e);
203         } finally {
204             if (emf != null) {
205                 releaseEntityManagerFactory(emf);
206             }
207         }
208     }
209
210     /**
211      * delete removes all the relationships for the object in the relationship
212      * identified by the id. the object could be a permission or a role
213      * @param ctx
214      * @param id of the object in the relationship
215      * @throws DocumentNotFoundException
216      * @throws DocumentException
217      */
218     @Override
219     public void delete(ServiceContext ctx, String id)
220             throws DocumentNotFoundException,
221             DocumentException {
222
223         if (logger.isDebugEnabled()) {
224             logger.debug("deleting entity with id=" + id);
225         }
226         if (ctx == null) {
227             throw new IllegalArgumentException(
228                     "PermissionRoleStorageClient.delete : ctx is missing");
229         }
230         EntityManagerFactory emf = null;
231         EntityManager em = null;
232         try {
233             StringBuilder deleteStr = new StringBuilder("DELETE FROM ");
234             deleteStr.append(getEntityName(ctx));
235             SubjectType subject = PermissionRoleUtil.getSubject(ctx);
236             String objectId = "permission_id";
237             if (SubjectType.PERMISSION.equals(subject)) {
238                 objectId = "role_id";
239             }
240             deleteStr.append(" WHERE " + objectId + " = :objectId");
241             emf = getEntityManagerFactory();
242             em = emf.createEntityManager();
243             Query q = em.createQuery(deleteStr.toString());
244             q.setParameter("objectId", id);
245             int rcount = 0;
246             em.getTransaction().begin();
247             rcount = q.executeUpdate();
248             if (rcount == 0) {
249                 if (em != null && em.getTransaction().isActive()) {
250                     em.getTransaction().rollback();
251                 }
252                 String msg = "could not find entity with id=" + id;
253                 logger.error(msg);
254                 throw new DocumentNotFoundException(msg);
255             }
256             em.getTransaction().commit();
257
258         } catch (DocumentException de) {
259             if (em != null && em.getTransaction().isActive()) {
260                 em.getTransaction().rollback();
261             }
262             throw de;
263         } catch (Exception e) {
264             if (logger.isDebugEnabled()) {
265                 logger.debug("Caught exception ", e);
266             }
267             if (em != null && em.getTransaction().isActive()) {
268                 em.getTransaction().rollback();
269             }
270             throw new DocumentException(e);
271         } finally {
272             if (emf != null) {
273                 releaseEntityManagerFactory(emf);
274             }
275         }
276     }
277 }