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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.authorization.storage;
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;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
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
58 public class PermissionRoleStorageClient extends JpaStorageClientImpl {
60 private final Logger logger = LoggerFactory.getLogger(PermissionRoleStorageClient.class);
62 public PermissionRoleStorageClient() {
66 * create of PermissionRole relationship creates one or more relationships between
68 * the object and subjects of the relationship is chosen (by doc handler) from
73 * @throws BadRequestException
74 * @throws DocumentException
77 public String create(ServiceContext ctx,
78 DocumentHandler handler) throws BadRequestException,
82 throw new IllegalArgumentException(
83 "PermissionRoleStorageClient.create : ctx is missing");
85 if (handler == null) {
86 throw new IllegalArgumentException(
87 "PermissionRoleStorageClient.create: handler is missing");
89 EntityManagerFactory emf = null;
90 EntityManager em = null;
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());
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();
112 } catch (Exception e) {
113 if (em != null && em.getTransaction().isActive()) {
114 em.getTransaction().rollback();
116 if (logger.isDebugEnabled()) {
117 logger.debug("Caught exception ", e);
119 throw new DocumentException(e);
122 releaseEntityManagerFactory(emf);
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
131 * @param id of the object in the relationship
133 * @throws DocumentNotFoundException
134 * @throws DocumentException
137 public void get(ServiceContext ctx, String id, DocumentHandler handler)
138 throws DocumentNotFoundException, DocumentException {
140 throw new IllegalArgumentException(
141 "PermissionRoleStorageClient.get: ctx is missing");
143 if (handler == null) {
144 throw new IllegalArgumentException(
145 "PermissionRoleStorageClient.get: handler is missing");
147 DocumentFilter docFilter = handler.getDocumentFilter();
148 if (docFilter == null) {
149 docFilter = handler.createDocumentFilter();
151 EntityManagerFactory emf = null;
152 EntityManager em = null;
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";
163 queryStrBldr.append(" WHERE " + objectId + " = :objectId");
164 String where = docFilter.getWhereClause();
165 if ((null != where) && (where.length() > 0)) {
166 queryStrBldr.append(" AND " + where);
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);
174 List<PermissionRoleRel> prrl = new ArrayList<PermissionRoleRel>();
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();
184 String msg = "could not find entity with id=" + id;
185 logger.error(msg, nre);
186 throw new DocumentNotFoundException(msg, nre);
188 if (prrl.size() == 0) {
189 String msg = "could not find entity with id=" + id;
191 throw new DocumentNotFoundException(msg);
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) {
199 } catch (Exception e) {
200 if (logger.isDebugEnabled()) {
201 logger.debug("Caught exception ", e);
203 throw new DocumentException(e);
206 releaseEntityManagerFactory(emf);
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
215 * @param id of the object in the relationship
216 * @throws DocumentNotFoundException
217 * @throws DocumentException
220 public void delete(ServiceContext ctx, String id)
221 throws DocumentNotFoundException,
224 if (logger.isDebugEnabled()) {
225 logger.debug("deleting entity with id=" + id);
228 throw new IllegalArgumentException(
229 "PermissionRoleStorageClient.delete : ctx is missing");
231 EntityManagerFactory emf = null;
232 EntityManager em = null;
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";
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);
247 em.getTransaction().begin();
248 rcount = q.executeUpdate();
250 if (em != null && em.getTransaction().isActive()) {
251 em.getTransaction().rollback();
253 String msg = "could not find entity with id=" + id;
255 throw new DocumentNotFoundException(msg);
257 em.getTransaction().commit();
259 } catch (DocumentException de) {
260 if (em != null && em.getTransaction().isActive()) {
261 em.getTransaction().rollback();
264 } catch (Exception e) {
265 if (logger.isDebugEnabled()) {
266 logger.debug("Caught exception ", e);
268 if (em != null && em.getTransaction().isActive()) {
269 em.getTransaction().rollback();
271 throw new DocumentException(e);
274 releaseEntityManagerFactory(emf);