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 2010 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.
25 * To change this template, choose Tools | Templates
26 * and open the template in the editor.
28 package org.collectionspace.services.authorization.importer;
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;
39 * AuthorizationStore stores persistent entities during import
42 public class AuthorizationStore {
44 private final Logger logger = LoggerFactory.getLogger(AuthorizationStore.class);
45 private final static String PERSISTENCE_UNIT = "org.collectionspace.services.authorization";
48 * store the given entity
50 * @return csid of the entity
53 public String store(Object entity) throws Exception {
54 EntityManagerFactory emf = null;
55 EntityManager em = null;
57 emf = JpaStorageUtils.getEntityManagerFactory(PERSISTENCE_UNIT);
58 em = emf.createEntityManager();
59 //FIXME: more efficient would be to participate in transaction already started
61 em.getTransaction().begin();
62 if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
63 JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
66 em.getTransaction().commit();
69 id = (String) JaxbUtils.getValue(entity, "getCsid");
70 } catch(NoSuchMethodException nsme) {
71 //do nothing ok, relationship does not have csid
74 } catch (Exception e) {
75 if (em != null && em.getTransaction().isActive()) {
76 em.getTransaction().rollback();
78 if (logger.isDebugEnabled()) {
79 logger.debug("Caught exception ", e);
84 JpaStorageUtils.releaseEntityManagerFactory(emf);