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