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.common.authorization_mgt;
30 import java.util.Date;
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
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;
43 * AuthorizationStore stores persistent entities during import
46 public class AuthorizationStore {
48 private static final Logger logger = LoggerFactory.getLogger(AuthorizationStore.class);
49 private final static String PERSISTENCE_UNIT = "org.collectionspace.services.authorization";
51 static public Role getRoleByName(String roleName, String tenantId) {
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);
66 static public Role getRoleByName(EntityManager em, String roleName, String tenantId) {
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);
82 static public Permission getPermission(Permission permission) {
83 Permission result = null;
85 // We need to perform a DB lookup to see if this permission already exists. If so,
86 // we should return the existing permission.
94 * store the given entity
96 * @return csid of the entity
99 public String store(Object entity) throws Exception {
100 EntityManagerFactory emf = null;
101 EntityManager em = null;
103 emf = JpaStorageUtils.getEntityManagerFactory(PERSISTENCE_UNIT);
104 em = emf.createEntityManager();
105 //FIXME: more efficient would be to participate in transaction already started
107 em.getTransaction().begin();
108 if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
109 JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
112 em.getTransaction().commit();
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
120 } catch (Exception e) {
121 if (em != null && em.getTransaction().isActive()) {
122 em.getTransaction().rollback();
124 if (logger.isDebugEnabled()) {
125 logger.debug("Caught exception ", e);
132 JpaStorageUtils.releaseEntityManagerFactory(emf);
137 private boolean exists(EntityManager em, Object entity) {
138 boolean result = false;
141 String csid = (String)JaxbUtils.getValue(entity, "getCsid");
142 Object existingEntity = em.find(entity.getClass(), csid);
143 if (existingEntity != null) {
146 } catch (Exception e) {
147 //NOTE: Not all entities have a CSID attribute
153 * Use this method if you've already started a transaction with an EntityManager
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.");
160 if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
161 JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
164 if (entityExists == true) {
165 //em.merge(entity); FIXME: Leave commented out until we address CSPACE-5031
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