]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a9b3dc2b3807549d97815339592088fbd6fd5c48
[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
34 import org.collectionspace.services.authorization.Role;
35 import org.collectionspace.services.authorization.perms.Permission;
36 import org.collectionspace.services.authorization.storage.RoleStorageConstants;
37 import org.collectionspace.services.common.document.JaxbUtils;
38 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * AuthorizationStore stores persistent entities during import
44  * @author
45  */
46 public class AuthorizationStore {
47
48     private static final Logger logger = LoggerFactory.getLogger(AuthorizationStore.class);
49     private final static String PERSISTENCE_UNIT = "org.collectionspace.services.authorization";
50
51     static public Role getRoleByName(String roleName, String tenantId) {
52         Role theRole = null;
53         
54         try {
55                 theRole = (Role)JpaStorageUtils.getEnityByKey(Role.class.getName(),
56                                 RoleStorageConstants.ROLE_NAME, roleName, tenantId);
57         } catch (Exception e) {
58                 if (logger.isTraceEnabled() == true) {
59                         logger.trace("Could not retrieve role with name =" + roleName, e);
60                 }
61         }
62         
63         return theRole;
64     }
65     
66     static public Permission getPermission(Permission permission) {
67         Permission result = null;
68         //
69         // We need to perform a DB lookup to see if this permission already exists.  If so,
70         // we should return the existing permission.
71         //
72         result = permission;
73         
74         return result;
75     }
76     
77     /**
78      * store the given entity
79      * @param entity
80      * @return csid of the entity
81      * @throws Exception
82      */
83     public String store(Object entity) throws Exception {
84         EntityManagerFactory emf = null;
85         EntityManager em = null;
86         try {
87             emf = JpaStorageUtils.getEntityManagerFactory(PERSISTENCE_UNIT);
88             em = emf.createEntityManager();
89             //FIXME: more efficient would be to participate in transaction already started
90             //by the caller
91             em.getTransaction().begin();
92             if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
93                 JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
94             }
95             em.persist(entity);
96             em.getTransaction().commit();
97             String id = null;
98             try{
99                 id = (String) JaxbUtils.getValue(entity, "getCsid"); //NOTE: Not all entities have a CSID attribute
100             } catch(NoSuchMethodException nsme) {
101                 //do nothing ok, relationship does not have csid
102             }
103             return id;
104         } catch (Exception e) {
105             if (em != null && em.getTransaction().isActive()) {
106                 em.getTransaction().rollback();
107             }
108             if (logger.isDebugEnabled()) {
109                 logger.debug("Caught exception ", e);
110             }
111             throw e;
112         } finally {
113             if (em != null) {
114                 JpaStorageUtils.releaseEntityManagerFactory(emf);
115             }
116         }
117     }
118 }