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;
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;
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 (Exception e) {
58 if (logger.isTraceEnabled() == true) {
59 logger.trace("Could not retrieve role with name =" + roleName, e);
66 static public Permission getPermission(Permission permission) {
67 Permission result = null;
69 // We need to perform a DB lookup to see if this permission already exists. If so,
70 // we should return the existing permission.
78 * store the given entity
80 * @return csid of the entity
83 public String store(Object entity) throws Exception {
84 EntityManagerFactory emf = null;
85 EntityManager em = null;
87 emf = JpaStorageUtils.getEntityManagerFactory(PERSISTENCE_UNIT);
88 em = emf.createEntityManager();
89 //FIXME: more efficient would be to participate in transaction already started
91 em.getTransaction().begin();
92 if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
93 JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
96 em.getTransaction().commit();
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
104 } catch (Exception e) {
105 if (em != null && em.getTransaction().isActive()) {
106 em.getTransaction().rollback();
108 if (logger.isDebugEnabled()) {
109 logger.debug("Caught exception ", e);
114 JpaStorageUtils.releaseEntityManagerFactory(emf);