]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6822527f60b7cd3896a939d5e6a107b842e10317
[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.account.storage;
25
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;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
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.
52  * @author 
53  */
54 public class TenantStorageClient extends JpaStorageClientImpl {
55
56     private final Logger logger = LoggerFactory.getLogger(TenantStorageClient.class);
57
58     public TenantStorageClient() {
59     }
60
61     @Override
62     public String create(ServiceContext ctx,
63             DocumentHandler handler) throws BadRequestException,
64             DocumentException {
65
66         /*
67         if (ctx == null) {
68             throw new IllegalArgumentException(
69                     "TenantStorageClient.create : ctx is missing");
70         }
71         */
72         if (handler == null) {
73             throw new IllegalArgumentException(
74                     "TenantStorageClient.create: handler is missing");
75         }
76         EntityManagerFactory emf = null;
77         EntityManager em = null;
78         Tenant tenant = (Tenant) handler.getCommonPart();
79         try {
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());
88             em.persist(tenant);
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();
95             }
96             throw bre;
97         } catch (Exception e) {
98             if (logger.isDebugEnabled()) {
99                 logger.debug("Caught exception ", e);
100             }
101             boolean uniqueConstraint = false;
102             try {
103                 if(em.find(Tenant.class, tenant.getId()) != null) {
104                         //might be unique constraint violation
105                         uniqueConstraint = true;
106                 } 
107             } catch(Exception ignored) {
108                 //Ignore - we just care if exists
109             }
110             if (em != null && em.getTransaction().isActive()) {
111                 em.getTransaction().rollback();
112             }
113             if (uniqueConstraint) {
114                 String msg = "TenantId exists. Non unique tenantId=" + tenant.getId();
115                 logger.error(msg);
116                 throw new BadRequestException(msg);
117             }
118             throw new DocumentException(e);
119         } finally {
120             if (em != null) {
121                 JpaStorageUtils.releaseEntityManagerFactory(emf);
122             }
123         }
124     }
125
126         @Override
127     public void get(ServiceContext ctx, String id, DocumentHandler handler)
128             throws DocumentNotFoundException, DocumentException {
129         /*
130         if (ctx == null) {
131             throw new IllegalArgumentException(
132                     "get: ctx is missing");
133         } 
134         */
135         if (handler == null) {
136             throw new IllegalArgumentException(
137                     "get: handler is missing");
138         }
139         DocumentFilter docFilter = handler.getDocumentFilter();
140         if (docFilter == null) {
141             docFilter = handler.createDocumentFilter();
142         }
143         EntityManagerFactory emf = null;
144         EntityManager em = null;
145         try {
146             handler.prepare(Action.GET);
147             Object o = null;
148             String whereClause = " where id = :id";
149             HashMap<String, Object> params = new HashMap<String, Object>();
150             params.put("id", id);
151
152             o = JpaStorageUtils.getEntity(
153                     "org.collectionspace.services.account.Tenant", whereClause, params);
154             if (null == o) {
155                 if (em != null && em.getTransaction().isActive()) {
156                     em.getTransaction().rollback();
157                 }
158                 String msg = "could not find entity with id=" + id;
159                 throw new DocumentNotFoundException(msg);
160             }
161             DocumentWrapper<Object> wrapDoc = new DocumentWrapperImpl<Object>(o);
162             handler.handle(Action.GET, wrapDoc);
163             handler.complete(Action.GET, wrapDoc);
164         } catch (DocumentException de) {
165             throw de;
166         } catch (Exception e) {
167             if (logger.isDebugEnabled()) {
168                 logger.debug("Caught exception ", e);
169             }
170             throw new DocumentException(e);
171         } finally {
172             if (emf != null) {
173                 JpaStorageUtils.releaseEntityManagerFactory(emf);
174             }
175         }
176     }
177
178     @Override
179     public void update(ServiceContext ctx, String id, DocumentHandler handler)
180             throws BadRequestException, DocumentNotFoundException,
181             DocumentException {
182         /*
183         if (ctx == null) {
184             throw new IllegalArgumentException(
185                     "TenantStorageClient.update : ctx is missing");
186         }
187          */
188         if (handler == null) {
189             throw new IllegalArgumentException(
190                     "TenantStorageClient.update: handler is missing");
191         }
192         EntityManagerFactory emf = null;
193         EntityManager em = null;
194         try {
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();
210             }
211             throw bre;
212         } catch (DocumentException de) {
213             if (em != null && em.getTransaction().isActive()) {
214                 em.getTransaction().rollback();
215             }
216             throw de;
217         } catch (Exception e) {
218             if (logger.isDebugEnabled()) {
219                 logger.debug("Caught exception ", e);
220             }
221             throw new DocumentException(e);
222         } finally {
223             if (emf != null) {
224                 JpaStorageUtils.releaseEntityManagerFactory(emf);
225             }
226         }
227     }
228
229     @Override
230     public void delete(ServiceContext ctx, String id)
231             throws DocumentNotFoundException,
232             DocumentException {
233
234         if (logger.isDebugEnabled()) {
235             logger.debug("deleting entity with id=" + id);
236         }
237         /*
238         if (ctx == null) {
239             throw new IllegalArgumentException(
240                     "TenantStorageClient.delete : ctx is missing");
241         }
242         */
243         EntityManagerFactory emf = null;
244         EntityManager em = null;
245         try {
246             emf = JpaStorageUtils.getEntityManagerFactory();
247             em = emf.createEntityManager();
248
249             Tenant tenantFound = getTenant(em, id);
250             em.getTransaction().begin();
251             em.remove(tenantFound);
252             em.getTransaction().commit();
253
254         } catch (DocumentException de) {
255             if (em != null && em.getTransaction().isActive()) {
256                 em.getTransaction().rollback();
257             }
258             throw de;
259         } catch (Exception e) {
260             if (logger.isDebugEnabled()) {
261                 logger.debug("Caught exception ", e);
262             }
263             if (em != null && em.getTransaction().isActive()) {
264                 em.getTransaction().rollback();
265             }
266             throw new DocumentException(e);
267         } finally {
268             if (emf != null) {
269                 JpaStorageUtils.releaseEntityManagerFactory(emf);
270             }
271         }
272     }
273
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();
279             }
280             String msg = "could not find account with id=" + id;
281             logger.error(msg);
282             throw new DocumentNotFoundException(msg);
283         }
284         return tenantFound;
285     }
286
287     private boolean checkAllowedUpdates(Tenant toTenant, Tenant fromTenant) throws BadRequestException {
288         if (!fromTenant.getId().equals(toTenant.getId())) {
289             String msg = "Cannot change Tenant Id!";
290             logger.error(msg);
291             throw new BadRequestException(msg);
292         }
293         return true;
294     }
295
296 }