]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c6c79bf6ff10bb6670f56c6380b8ef0d4405eeea
[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 2010 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 /*
25  * To change this template, choose Tools | Templates
26  * and open the template in the editor.
27  */
28 package org.collectionspace.services.authorization.importer;
29
30 import java.util.Date;
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import org.collectionspace.services.common.document.JaxbUtils;
34 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * AuthorizationStore stores persistent entities during import
40  * @author
41  */
42 public class AuthorizationStore {
43
44     private final Logger logger = LoggerFactory.getLogger(AuthorizationStore.class);
45     private final static String PERSISTENCE_UNIT = "org.collectionspace.services.authorization";
46
47     /**
48      * store the given entity
49      * @param entity
50      * @return csid of the entity
51      * @throws Exception
52      */
53     public String store(Object entity) throws Exception {
54         EntityManagerFactory emf = null;
55         EntityManager em = null;
56         try {
57             emf = JpaStorageUtils.getEntityManagerFactory(PERSISTENCE_UNIT);
58             em = emf.createEntityManager();
59             //FIXME: more efficient would be to participate in transaction already started
60             //by the caller
61             em.getTransaction().begin();
62             if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
63                 JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
64             }
65             em.persist(entity);
66             em.getTransaction().commit();
67             String id = null;
68             try{
69                 id = (String) JaxbUtils.getValue(entity, "getCsid");
70             } catch(NoSuchMethodException nsme) {
71                 //do nothing ok, relationship does not have csid
72             }
73             return id;
74         } catch (Exception e) {
75             if (em != null && em.getTransaction().isActive()) {
76                 em.getTransaction().rollback();
77             }
78             if (logger.isDebugEnabled()) {
79                 logger.debug("Caught exception ", e);
80             }
81             throw e;
82         } finally {
83             if (em != null) {
84                 JpaStorageUtils.releaseEntityManagerFactory(emf);
85             }
86         }
87     }
88 }