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.account.storage;
26 import java.util.Date;
27 import java.util.HashMap;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import org.collectionspace.services.account.Tenant;
31 import org.collectionspace.services.common.context.ServiceContext;
32 import org.collectionspace.services.common.document.BadRequestException;
33 import org.collectionspace.services.common.document.DocumentException;
34 import org.collectionspace.services.common.document.DocumentFilter;
35 import org.collectionspace.services.common.document.DocumentHandler;
36 import org.collectionspace.services.common.document.DocumentHandler.Action;
37 import org.collectionspace.services.common.document.DocumentNotFoundException;
38 import org.collectionspace.services.common.document.DocumentWrapper;
39 import org.collectionspace.services.common.document.DocumentWrapperImpl;
40 import org.collectionspace.services.common.document.JaxbUtils;
41 import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl;
42 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * TenantStorageClient deals with both Account and CSIdP's
49 * state in persistent storage. The rationale behind creating this class is that
50 * this class manages persistence for both account and CSIP's user. Transactions
51 * are used where possible to perform the persistence operations atomically.
54 public class TenantStorageClient extends JpaStorageClientImpl {
56 private final Logger logger = LoggerFactory.getLogger(TenantStorageClient.class);
58 public TenantStorageClient() {
62 public String create(ServiceContext ctx,
63 DocumentHandler handler) throws BadRequestException,
68 throw new IllegalArgumentException(
69 "TenantStorageClient.create : ctx is missing");
72 if (handler == null) {
73 throw new IllegalArgumentException(
74 "TenantStorageClient.create: handler is missing");
76 EntityManagerFactory emf = null;
77 EntityManager em = null;
78 Tenant tenant = (Tenant) handler.getCommonPart();
80 handler.prepare(Action.CREATE);
81 DocumentWrapper<Tenant> wrapDoc =
82 new DocumentWrapperImpl<Tenant>(tenant);
83 handler.handle(Action.CREATE, wrapDoc);
84 emf = JpaStorageUtils.getEntityManagerFactory();
85 em = emf.createEntityManager();
86 em.getTransaction().begin();
87 tenant.setCreatedAtItem(new Date());
89 em.getTransaction().commit();
90 handler.complete(Action.CREATE, wrapDoc);
91 return (String) JaxbUtils.getValue(tenant, "getId");
92 } catch (BadRequestException bre) {
93 if (em != null && em.getTransaction().isActive()) {
94 em.getTransaction().rollback();
97 } catch (Exception e) {
98 if (logger.isDebugEnabled()) {
99 logger.debug("Caught exception ", e);
101 boolean uniqueConstraint = false;
103 if(em.find(Tenant.class, tenant.getId()) != null) {
104 //might be unique constraint violation
105 uniqueConstraint = true;
107 } catch(Exception ignored) {
108 //Ignore - we just care if exists
110 if (em != null && em.getTransaction().isActive()) {
111 em.getTransaction().rollback();
113 if (uniqueConstraint) {
114 String msg = "TenantId exists. Non unique tenantId=" + tenant.getId();
116 throw new BadRequestException(msg);
118 throw new DocumentException(e);
121 JpaStorageUtils.releaseEntityManagerFactory(emf);
127 public void get(ServiceContext ctx, String id, DocumentHandler handler)
128 throws DocumentNotFoundException, DocumentException {
131 throw new IllegalArgumentException(
132 "get: ctx is missing");
135 if (handler == null) {
136 throw new IllegalArgumentException(
137 "get: handler is missing");
139 DocumentFilter docFilter = handler.getDocumentFilter();
140 if (docFilter == null) {
141 docFilter = handler.createDocumentFilter();
143 EntityManagerFactory emf = null;
144 EntityManager em = null;
146 handler.prepare(Action.GET);
148 String whereClause = " where id = :id";
149 HashMap<String, Object> params = new HashMap<String, Object>();
150 params.put("id", id);
152 o = JpaStorageUtils.getEntity(
153 "org.collectionspace.services.account.Tenant", whereClause, params);
155 if (em != null && em.getTransaction().isActive()) {
156 em.getTransaction().rollback();
158 String msg = "could not find entity with id=" + id;
159 throw new DocumentNotFoundException(msg);
161 DocumentWrapper<Object> wrapDoc = new DocumentWrapperImpl<Object>(o);
162 handler.handle(Action.GET, wrapDoc);
163 handler.complete(Action.GET, wrapDoc);
164 } catch (DocumentException de) {
166 } catch (Exception e) {
167 if (logger.isDebugEnabled()) {
168 logger.debug("Caught exception ", e);
170 throw new DocumentException(e);
173 JpaStorageUtils.releaseEntityManagerFactory(emf);
179 public void update(ServiceContext ctx, String id, DocumentHandler handler)
180 throws BadRequestException, DocumentNotFoundException,
184 throw new IllegalArgumentException(
185 "TenantStorageClient.update : ctx is missing");
188 if (handler == null) {
189 throw new IllegalArgumentException(
190 "TenantStorageClient.update: handler is missing");
192 EntityManagerFactory emf = null;
193 EntityManager em = null;
195 handler.prepare(Action.UPDATE);
196 Tenant tenantReceived = (Tenant) handler.getCommonPart();
197 emf = JpaStorageUtils.getEntityManagerFactory();
198 em = emf.createEntityManager();
199 em.getTransaction().begin();
200 Tenant tenantFound = getTenant(em, id);
201 checkAllowedUpdates(tenantReceived, tenantFound);
202 DocumentWrapper<Tenant> wrapDoc =
203 new DocumentWrapperImpl<Tenant>(tenantFound);
204 handler.handle(Action.UPDATE, wrapDoc);
205 em.getTransaction().commit();
206 handler.complete(Action.UPDATE, wrapDoc);
207 } catch (BadRequestException bre) {
208 if (em != null && em.getTransaction().isActive()) {
209 em.getTransaction().rollback();
212 } catch (DocumentException de) {
213 if (em != null && em.getTransaction().isActive()) {
214 em.getTransaction().rollback();
217 } catch (Exception e) {
218 if (logger.isDebugEnabled()) {
219 logger.debug("Caught exception ", e);
221 throw new DocumentException(e);
224 JpaStorageUtils.releaseEntityManagerFactory(emf);
230 public void delete(ServiceContext ctx, String id)
231 throws DocumentNotFoundException,
234 if (logger.isDebugEnabled()) {
235 logger.debug("deleting entity with id=" + id);
239 throw new IllegalArgumentException(
240 "TenantStorageClient.delete : ctx is missing");
243 EntityManagerFactory emf = null;
244 EntityManager em = null;
246 emf = JpaStorageUtils.getEntityManagerFactory();
247 em = emf.createEntityManager();
249 Tenant tenantFound = getTenant(em, id);
250 em.getTransaction().begin();
251 em.remove(tenantFound);
252 em.getTransaction().commit();
254 } catch (DocumentException de) {
255 if (em != null && em.getTransaction().isActive()) {
256 em.getTransaction().rollback();
259 } catch (Exception e) {
260 if (logger.isDebugEnabled()) {
261 logger.debug("Caught exception ", e);
263 if (em != null && em.getTransaction().isActive()) {
264 em.getTransaction().rollback();
266 throw new DocumentException(e);
269 JpaStorageUtils.releaseEntityManagerFactory(emf);
274 private Tenant getTenant(EntityManager em, String id) throws DocumentNotFoundException {
275 Tenant tenantFound = em.find(Tenant.class, id);
276 if (tenantFound == null) {
277 if (em != null && em.getTransaction().isActive()) {
278 em.getTransaction().rollback();
280 String msg = "could not find account with id=" + id;
282 throw new DocumentNotFoundException(msg);
287 private boolean checkAllowedUpdates(Tenant toTenant, Tenant fromTenant) throws BadRequestException {
288 if (!fromTenant.getId().equals(toTenant.getId())) {
289 String msg = "Cannot change Tenant Id!";
291 throw new BadRequestException(msg);