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
136 public void get(ServiceContext ctx, String id, DocumentHandler handler)
137 throws DocumentNotFoundException, DocumentException {
139 throw new IllegalArgumentException(
140 "JpaStorageClient.get: ctx is missing");
142 if (handler == null) {
143 throw new IllegalArgumentException(
144 "JpaStorageClient.get: handler is missing");
146 DocumentFilter docFilter = handler.getDocumentFilter();
147 if (docFilter == null) {
148 docFilter = handler.createDocumentFilter(ctx);
150 EntityManagerFactory emf = null;
151 EntityManager em = null;
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";
162 queryStrBldr.append(" WHERE " + objectId + " = :objectId");
163 String where = docFilter.getWhereClause();
164 if ((null != where) && (where.length() > 0)) {
165 queryStrBldr.append(" AND " + where);
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);
173 List<PermissionRoleRel> prrl = new ArrayList<PermissionRoleRel>();
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();
183 String msg = "could not find entity with id=" + id;
184 logger.error(msg, nre);
185 throw new DocumentNotFoundException(msg, nre);
187 if (prrl.size() == 0) {
188 String msg = "could not find entity with id=" + id;
190 throw new DocumentNotFoundException(msg);
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) {
198 } catch (Exception e) {
199 if (logger.isDebugEnabled()) {
200 logger.debug("Caught exception ", e);
202 throw new DocumentException(e);
205 releaseEntityManagerFactory(emf);
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
214 * @param id of the object in the relationship
215 * @throws DocumentNotFoundException
216 * @throws DocumentException
219 public void delete(ServiceContext ctx, String id)
220 throws DocumentNotFoundException,
223 if (logger.isDebugEnabled()) {
224 logger.debug("deleting entity with id=" + id);
227 throw new IllegalArgumentException(
228 "PermissionRoleStorageClient.delete : ctx is missing");
230 EntityManagerFactory emf = null;
231 EntityManager em = null;
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";
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);
246 em.getTransaction().begin();
247 rcount = q.executeUpdate();
249 if (em != null && em.getTransaction().isActive()) {
250 em.getTransaction().rollback();
252 String msg = "could not find entity with id=" + id;
254 throw new DocumentNotFoundException(msg);
256 em.getTransaction().commit();
258 } catch (DocumentException de) {
259 if (em != null && em.getTransaction().isActive()) {
260 em.getTransaction().rollback();
263 } catch (Exception e) {
264 if (logger.isDebugEnabled()) {
265 logger.debug("Caught exception ", e);
267 if (em != null && em.getTransaction().isActive()) {
268 em.getTransaction().rollback();
270 throw new DocumentException(e);
273 releaseEntityManagerFactory(emf);