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,
67 throw new IllegalArgumentException(
68 "TenantStorageClient.create : ctx is missing");
70 if (handler == null) {
71 throw new IllegalArgumentException(
72 "TenantStorageClient.create: handler is missing");
74 EntityManagerFactory emf = null;
75 EntityManager em = null;
76 Tenant tenant = (Tenant) handler.getCommonPart();
78 handler.prepare(Action.CREATE);
79 DocumentWrapper<Tenant> wrapDoc =
80 new DocumentWrapperImpl<Tenant>(tenant);
81 handler.handle(Action.CREATE, wrapDoc);
82 emf = JpaStorageUtils.getEntityManagerFactory();
83 em = emf.createEntityManager();
84 em.getTransaction().begin();
85 tenant.setCreatedAtItem(new Date());
87 em.getTransaction().commit();
88 handler.complete(Action.CREATE, wrapDoc);
89 return (String) JaxbUtils.getValue(tenant, "getId");
90 } catch (BadRequestException bre) {
91 if (em != null && em.getTransaction().isActive()) {
92 em.getTransaction().rollback();
95 } catch (Exception e) {
96 if (logger.isDebugEnabled()) {
97 logger.debug("Caught exception ", e);
99 boolean uniqueConstraint = false;
101 if(em.find(Tenant.class, tenant.getId()) != null) {
102 //might be unique constraint violation
103 uniqueConstraint = true;
105 } catch(Exception ignored) {
106 //Ignore - we just care if exists
108 if (em != null && em.getTransaction().isActive()) {
109 em.getTransaction().rollback();
111 if (uniqueConstraint) {
112 String msg = "TenantId exists. Non unique tenantId=" + tenant.getId();
114 throw new BadRequestException(msg);
116 throw new DocumentException(e);
119 JpaStorageUtils.releaseEntityManagerFactory(emf);
125 public void get(ServiceContext ctx, String id, DocumentHandler handler)
126 throws DocumentNotFoundException, DocumentException {
128 throw new IllegalArgumentException(
129 "get: ctx is missing");
131 if (handler == null) {
132 throw new IllegalArgumentException(
133 "get: handler is missing");
135 DocumentFilter docFilter = handler.getDocumentFilter();
136 if (docFilter == null) {
137 docFilter = handler.createDocumentFilter();
139 EntityManagerFactory emf = null;
140 EntityManager em = null;
142 handler.prepare(Action.GET);
144 String whereClause = " where id = :id";
145 HashMap<String, Object> params = new HashMap<String, Object>();
146 params.put("id", id);
148 o = JpaStorageUtils.getEntity(
149 "org.collectionspace.services.account.Tenant", whereClause, params);
151 if (em != null && em.getTransaction().isActive()) {
152 em.getTransaction().rollback();
154 String msg = "could not find entity with id=" + id;
155 throw new DocumentNotFoundException(msg);
157 DocumentWrapper<Object> wrapDoc = new DocumentWrapperImpl<Object>(o);
158 handler.handle(Action.GET, wrapDoc);
159 handler.complete(Action.GET, wrapDoc);
160 } catch (DocumentException de) {
162 } catch (Exception e) {
163 if (logger.isDebugEnabled()) {
164 logger.debug("Caught exception ", e);
166 throw new DocumentException(e);
169 JpaStorageUtils.releaseEntityManagerFactory(emf);
175 public void update(ServiceContext ctx, String id, DocumentHandler handler)
176 throws BadRequestException, DocumentNotFoundException,
179 throw new IllegalArgumentException(
180 "TenantStorageClient.update : ctx is missing");
182 if (handler == null) {
183 throw new IllegalArgumentException(
184 "TenantStorageClient.update: handler is missing");
186 EntityManagerFactory emf = null;
187 EntityManager em = null;
189 handler.prepare(Action.UPDATE);
190 Tenant tenantReceived = (Tenant) handler.getCommonPart();
191 emf = JpaStorageUtils.getEntityManagerFactory();
192 em = emf.createEntityManager();
193 em.getTransaction().begin();
194 Tenant tenantFound = getTenant(em, id);
195 checkAllowedUpdates(tenantReceived, tenantFound);
196 DocumentWrapper<Tenant> wrapDoc =
197 new DocumentWrapperImpl<Tenant>(tenantFound);
198 handler.handle(Action.UPDATE, wrapDoc);
199 em.getTransaction().commit();
200 handler.complete(Action.UPDATE, wrapDoc);
201 } catch (BadRequestException bre) {
202 if (em != null && em.getTransaction().isActive()) {
203 em.getTransaction().rollback();
206 } catch (DocumentException de) {
207 if (em != null && em.getTransaction().isActive()) {
208 em.getTransaction().rollback();
211 } catch (Exception e) {
212 if (logger.isDebugEnabled()) {
213 logger.debug("Caught exception ", e);
215 throw new DocumentException(e);
218 JpaStorageUtils.releaseEntityManagerFactory(emf);
224 public void delete(ServiceContext ctx, String id)
225 throws DocumentNotFoundException,
228 if (logger.isDebugEnabled()) {
229 logger.debug("deleting entity with id=" + id);
232 throw new IllegalArgumentException(
233 "TenantStorageClient.delete : ctx is missing");
235 EntityManagerFactory emf = null;
236 EntityManager em = null;
238 emf = JpaStorageUtils.getEntityManagerFactory();
239 em = emf.createEntityManager();
241 Tenant tenantFound = getTenant(em, id);
242 em.getTransaction().begin();
243 em.remove(tenantFound);
244 em.getTransaction().commit();
246 } catch (DocumentException de) {
247 if (em != null && em.getTransaction().isActive()) {
248 em.getTransaction().rollback();
251 } catch (Exception e) {
252 if (logger.isDebugEnabled()) {
253 logger.debug("Caught exception ", e);
255 if (em != null && em.getTransaction().isActive()) {
256 em.getTransaction().rollback();
258 throw new DocumentException(e);
261 JpaStorageUtils.releaseEntityManagerFactory(emf);
266 private Tenant getTenant(EntityManager em, String id) throws DocumentNotFoundException {
267 Tenant tenantFound = em.find(Tenant.class, id);
268 if (tenantFound == null) {
269 if (em != null && em.getTransaction().isActive()) {
270 em.getTransaction().rollback();
272 String msg = "could not find account with id=" + id;
274 throw new DocumentNotFoundException(msg);
279 private boolean checkAllowedUpdates(Tenant toTenant, Tenant fromTenant) throws BadRequestException {
280 if (!fromTenant.getId().equals(toTenant.getId())) {
281 String msg = "Cannot change Tenant Id!";
283 throw new BadRequestException(msg);