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.PermissionRoleRel;
36 import org.collectionspace.services.authorization.perms.Permission;
37 import org.collectionspace.services.authorization.storage.RoleStorageConstants;
38 import org.collectionspace.services.common.document.JaxbUtils;
39 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * AuthorizationStore stores persistent entities during import
47 public class AuthorizationStore {
49 private static final Logger logger = LoggerFactory.getLogger(AuthorizationStore.class);
50 private final static String PERSISTENCE_UNIT = "org.collectionspace.services.authorization";
51 public final static String ENTITY_MANAGER_PROP_KEY = EntityManager.class.getCanonicalName();
53 static public Role getRoleByName(String roleName, String tenantId) {
57 theRole = (Role)JpaStorageUtils.getEnityByKey(Role.class.getName(),
58 RoleStorageConstants.ROLE_NAME, roleName, tenantId);
59 } catch (Throwable e) {
60 if (logger.isTraceEnabled() == true) {
61 logger.trace("Could not retrieve role with name =" + roleName, e);
68 static public Role getRoleByName(EntityManager em, String roleName, String tenantId) {
72 theRole = (Role)JpaStorageUtils.getEnityByKey(em, Role.class.getName(),
73 RoleStorageConstants.ROLE_NAME, roleName, tenantId);
74 } catch (Throwable e) {
75 if (logger.isTraceEnabled() == true) {
76 logger.trace("Could not retrieve role with name =" + roleName, e);
84 static public PermissionRoleRel getPermRoleRel(EntityManager em, String permId, String roleId) {
85 PermissionRoleRel permRoleRel = null;
88 permRoleRel = (PermissionRoleRel)JpaStorageUtils.getEntityByDualKeys(em,
89 PermissionRoleRel.class.getName(),
90 RoleStorageConstants.PERM_ROLE_REL_PERM_ID, permId,
91 RoleStorageConstants.PERM_ROLE_REL_ROLE_ID, roleId);
92 } catch (Throwable e) {
93 if (logger.isTraceEnabled()) {
94 logger.trace("Could not retrieve permissionRoleRel with permId =" + permId
95 +" and roleId="+roleId, e);
103 static public Permission getPermission(Permission permission) {
104 Permission result = null;
106 // We need to perform a DB lookup to see if this permission already exists. If so,
107 // we should return the existing permission.
115 * store the given entity
117 * @return csid of the entity
120 public String store(Object entity) throws Exception {
121 EntityManagerFactory emf = null;
122 EntityManager em = null;
124 emf = JpaStorageUtils.getEntityManagerFactory(PERSISTENCE_UNIT);
125 em = emf.createEntityManager();
126 //FIXME: more efficient would be to participate in transaction already started
128 em.getTransaction().begin();
129 if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
130 JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
133 em.getTransaction().commit();
136 id = (String) JaxbUtils.getValue(entity, "getCsid"); //NOTE: Not all entities have a CSID attribute
137 } catch(NoSuchMethodException nsme) {
138 //do nothing ok, relationship does not have csid
141 } catch (Exception e) {
142 if (em != null && em.getTransaction().isActive()) {
143 em.getTransaction().rollback();
145 if (logger.isDebugEnabled()) {
146 logger.debug("Caught exception ", e);
153 JpaStorageUtils.releaseEntityManagerFactory(emf);
158 private boolean exists(EntityManager em, Object entity) {
159 boolean result = false;
162 if(entity instanceof Role) {
163 // If find by name, exists
164 Role roleEntity = (Role)entity;
165 String roleName = roleEntity.getRoleName();
166 String tenantId = roleEntity.getTenantId();
167 if(getRoleByName(em, roleName, tenantId)!=null) {
169 logger.trace("Role {} already exists in tenant {}.", roleName, tenantId);
171 logger.trace("Role {} does not exist in tenant {}.", roleName, tenantId);
173 } else if(entity instanceof PermissionRoleRel) {
174 // If find by name, exists
175 PermissionRoleRel permRoleEntity = (PermissionRoleRel)entity;
176 String roleId = permRoleEntity.getRoleId();
177 String permId = permRoleEntity.getPermissionId();
178 if(getPermRoleRel(em, permId, roleId)!=null) {
180 logger.trace("PermRoleRel for {}, {} already exists.", permId, roleId);
182 logger.trace("PermRoleRel for {}, {} does not exist.", permId, roleId);
184 } else { // Default case; also best test for Permission
185 String csid = (String)JaxbUtils.getValue(entity, "getCsid");
186 Object existingEntity = em.find(entity.getClass(), csid);
187 if (existingEntity != null) {
189 logger.trace("Entity with csid {} already exists.", csid);
191 logger.trace("Entity with csid {} does not exist.", csid);
194 } catch (Exception e) {
195 //NOTE: Not all entities have a CSID attribute
201 * Use this method if you've already started a transaction with an EntityManager
203 public String store(EntityManager em, Object entity) throws Exception {
204 boolean entityExists = exists(em, entity);
206 * Logging moved to exists, for better detail
207 if (entityExists == true) {
208 logger.trace("Entity to persist already exists.");
211 if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
212 JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
215 if (entityExists == true) {
216 //em.merge(entity); FIXME: Leave commented out until we address CSPACE-5031
217 // PLS: Question: why merge? what might be new to change, and is this really a good idea?
218 // Shouldn't we define them once and leave them alone?
226 id = (String) JaxbUtils.getValue(entity, "getCsid"); //NOTE: Not all entities have a CSID attribute
227 } catch(NoSuchMethodException nsme) {
228 //do nothing ok, relationship does not have csid