]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3ff03ee6db34aed43787e40b44ad0737756cf1e9
[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.common.authorization_mgt;
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.common.authorization_mgt.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 (Throwable 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 Role getRoleByName(EntityManager em, String roleName, String tenantId) {
67         Role theRole = null;
68         
69         try {
70                 theRole = (Role)JpaStorageUtils.getEnityByKey(em, Role.class.getName(),
71                                 RoleStorageConstants.ROLE_NAME, roleName, tenantId);
72         } catch (Throwable e) {
73                 if (logger.isTraceEnabled() == true) {
74                         logger.trace("Could not retrieve role with name =" + roleName, e);
75                 }
76         }
77         
78         return theRole;
79     }
80     
81     
82     static public Permission getPermission(Permission permission) {
83         Permission result = null;
84         //
85         // We need to perform a DB lookup to see if this permission already exists.  If so,
86         // we should return the existing permission.
87         //
88         result = permission;
89         
90         return result;
91     }
92     
93     /**
94      * store the given entity
95      * @param entity
96      * @return csid of the entity
97      * @throws Exception
98      */
99     public String store(Object entity) throws Exception {
100         EntityManagerFactory emf = null;
101         EntityManager em = null;
102         try {
103             emf = JpaStorageUtils.getEntityManagerFactory(PERSISTENCE_UNIT);
104             em = emf.createEntityManager();
105             //FIXME: more efficient would be to participate in transaction already started
106             //by the caller
107             em.getTransaction().begin();
108             if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
109                 JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
110             }
111             em.persist(entity);
112             em.getTransaction().commit();
113             String id = null;
114             try{
115                 id = (String) JaxbUtils.getValue(entity, "getCsid"); //NOTE: Not all entities have a CSID attribute
116             } catch(NoSuchMethodException nsme) {
117                 //do nothing ok, relationship does not have csid
118             }
119             return id;
120         } catch (Exception e) {
121             if (em != null && em.getTransaction().isActive()) {
122                 em.getTransaction().rollback();
123             }
124             if (logger.isDebugEnabled()) {
125                 logger.debug("Caught exception ", e);
126             }
127             throw e;
128         } finally {
129             if (em != null) {
130                 em.clear();
131                 em.close();
132                 JpaStorageUtils.releaseEntityManagerFactory(emf);
133             }
134         }
135     }
136     
137     private boolean exists(EntityManager em, Object entity) {
138         boolean result = false;
139         
140         try {
141                 String csid = (String)JaxbUtils.getValue(entity, "getCsid");
142                 Object existingEntity = em.find(entity.getClass(), csid);
143                 if (existingEntity != null) {
144                         result = true;
145                 }
146         } catch (Exception e) {
147                 //NOTE: Not all entities have a CSID attribute
148         }
149         
150         return result;
151     }
152     /*
153      * Use this method if you've already started a transaction with an EntityManager
154      */
155     public String store(EntityManager em, Object entity) throws Exception {
156         boolean entityExists = exists(em, entity);
157         if (entityExists == true) {
158                 logger.debug("Entity to persist already exists.");
159         }
160         if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
161             JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
162         }
163         
164         if (entityExists == true) {
165                 //em.merge(entity); FIXME: Leave commented out until we address CSPACE-5031
166         } else {
167                 em.persist(entity);
168         }
169         
170         // look for a CSID
171         String id = null;
172         try{
173             id = (String) JaxbUtils.getValue(entity, "getCsid"); //NOTE: Not all entities have a CSID attribute
174         } catch(NoSuchMethodException nsme) {
175             //do nothing ok, relationship does not have csid
176         }
177         return id;
178     }
179 }