From 6ec99dfcd3c11db2f8fe6866a34951f4bbc6adc5 Mon Sep 17 00:00:00 2001 From: Sanjay Dalal Date: Wed, 21 Apr 2010 18:01:19 +0000 Subject: [PATCH] NOJIRA PermissionRoleSubResource now uses JpaRelationshipStorageClient (also used by AccountRoleSubResource) test: authorization-mgt D authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/PermissionRoleStorageClient.java M authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/PermissionRoleSubResource.java --- .../PermissionRoleSubResource.java | 26 +- .../storage/PermissionRoleStorageClient.java | 278 ------------------ 2 files changed, 11 insertions(+), 293 deletions(-) delete mode 100644 services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/PermissionRoleStorageClient.java diff --git a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/PermissionRoleSubResource.java b/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/PermissionRoleSubResource.java index df254b76d..7ac8f1957 100644 --- a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/PermissionRoleSubResource.java +++ b/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/PermissionRoleSubResource.java @@ -23,16 +23,13 @@ */ package org.collectionspace.services.authorization; -import org.collectionspace.services.authorization.storage.PermissionRoleStorageClient; - import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl; import org.collectionspace.services.common.context.RemoteServiceContextFactory; -import org.collectionspace.services.common.context.RemoteServiceContextImpl; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.context.ServiceContextFactory; -import org.collectionspace.services.common.document.DocumentFilter; import org.collectionspace.services.common.document.DocumentHandler; import org.collectionspace.services.common.storage.StorageClient; +import org.collectionspace.services.common.storage.jpa.JpaRelationshipStorageClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,12 +44,10 @@ public class PermissionRoleSubResource //service name to identify binding /** The service name. */ final private String serviceName = "authorization/permroles"; - /** The logger. */ final Logger logger = LoggerFactory.getLogger(PermissionRoleSubResource.class); - /** The storage client. */ - final StorageClient storageClient = new PermissionRoleStorageClient(); + final StorageClient storageClient = new JpaRelationshipStorageClient(); /* (non-Javadoc) * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString() @@ -71,22 +66,22 @@ public class PermissionRoleSubResource public String getServiceName() { return serviceName; } - + /* (non-Javadoc) * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass() */ @Override public Class getCommonPartClass() { - return PermissionRole.class; + return PermissionRole.class; } - + /* (non-Javadoc) * @see org.collectionspace.services.common.CollectionSpaceResource#getServiceContextFactory() */ @Override public ServiceContextFactory getServiceContextFactory() { - return RemoteServiceContextFactory.get(); - } + return RemoteServiceContextFactory.get(); + } /** * Creates the service context. @@ -99,14 +94,16 @@ public class PermissionRoleSubResource * @throws Exception the exception */ private ServiceContext createServiceContext(PermissionRole input, - SubjectType subject) throws Exception { - ServiceContext ctx = createServiceContext(input); + SubjectType subject) throws Exception { + ServiceContext ctx = createServiceContext(input); // ServiceContext ctx = new RemoteServiceContextImpl(getServiceName()); // ctx.setInput(input); ctx.setDocumentType(PermissionRole.class.getPackage().getName()); //persistence unit ctx.setProperty("entity-name", PermissionRoleRel.class.getName()); //subject name is necessary to indicate if role or permission is a subject ctx.setProperty("subject", subject); + //set context for the relationship query + ctx.setProperty("objectId", "permission_id"); return ctx; } @@ -125,7 +122,6 @@ public class PermissionRoleSubResource // docHandler.setCommonPart(ctx.getInput()); // return docHandler; // } - /** * createPermissionRole creates one or more permission-role relationships * between object (permission/role) and subject (role/permission) diff --git a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/PermissionRoleStorageClient.java b/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/PermissionRoleStorageClient.java deleted file mode 100644 index 8aa209676..000000000 --- a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/PermissionRoleStorageClient.java +++ /dev/null @@ -1,278 +0,0 @@ -/** - * This document is a part of the source code and related artifacts - * for CollectionSpace, an open source collections management system - * for museums and related institutions: - - * http://www.collectionspace.org - * http://wiki.collectionspace.org - - * Copyright 2009 University of California at Berkeley - - * Licensed under the Educational Community License (ECL), Version 2.0. - * You may not use this file except in compliance with this License. - - * You may obtain a copy of the ECL 2.0 License at - - * https://source.collectionspace.org/collection-space/LICENSE.txt - - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.collectionspace.services.authorization.storage; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.UUID; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.NoResultException; -import javax.persistence.Query; -import org.collectionspace.services.authorization.PermissionRoleRel; -import org.collectionspace.services.authorization.SubjectType; -import org.collectionspace.services.common.context.ServiceContext; -import org.collectionspace.services.common.document.BadRequestException; -import org.collectionspace.services.common.document.DocumentException; -import org.collectionspace.services.common.document.DocumentFilter; -import org.collectionspace.services.common.document.DocumentHandler; -import org.collectionspace.services.common.document.DocumentHandler.Action; -import org.collectionspace.services.common.document.DocumentNotFoundException; -import org.collectionspace.services.common.document.DocumentWrapper; -import org.collectionspace.services.common.document.DocumentWrapperImpl; -import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * PermissionRoleStorageClient deals with PermissionRole relationship - * in persistent storage. This storage client deals with bulk operations, i.e. - * create/post inserts multiple tuples between the given object and subjects - * get retrieves all subjects for the given object in relationship - * delete deletes all subjects for the given object in relationship - * @author - */ -public class PermissionRoleStorageClient extends JpaStorageClientImpl { - - private final Logger logger = LoggerFactory.getLogger(PermissionRoleStorageClient.class); - - public PermissionRoleStorageClient() { - } - - /** - * create of PermissionRole relationship creates one or more relationships between - * permission and role - * the object and subjects of the relationship is chosen (by doc handler) from - * the payload - * @param ctx - * @param handler - * @return - * @throws BadRequestException - * @throws DocumentException - */ - @Override - public String create(ServiceContext ctx, - DocumentHandler handler) throws BadRequestException, - DocumentException { - - if (ctx == null) { - throw new IllegalArgumentException( - "PermissionRoleStorageClient.create : ctx is missing"); - } - if (handler == null) { - throw new IllegalArgumentException( - "PermissionRoleStorageClient.create: handler is missing"); - } - EntityManagerFactory emf = null; - EntityManager em = null; - try { - handler.prepare(Action.CREATE); - List prrl = new ArrayList(); - DocumentWrapper> wrapDoc = - new DocumentWrapperImpl>(prrl); - handler.handle(Action.CREATE, wrapDoc); - emf = getEntityManagerFactory(); - em = emf.createEntityManager(); - em.getTransaction().begin(); - for (PermissionRoleRel prr : prrl) { - prr.setCreatedAtItem(new Date()); - em.persist(prr); - } - em.getTransaction().commit(); - handler.complete(Action.CREATE, wrapDoc); - return UUID.randomUUID().toString(); //filler, not useful - } catch (BadRequestException bre) { - if (em != null && em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - throw bre; - } catch (Exception e) { - if (em != null && em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - if (logger.isDebugEnabled()) { - logger.debug("Caught exception ", e); - } - throw new DocumentException(e); - } finally { - if (em != null) { - releaseEntityManagerFactory(emf); - } - } - } - - /** - * get retrieves all relationships for the object in the relationship - * identified by the id. the object could be a permission or a role - * @param ctx - * @param id of the object in the relationship - * @param handler - * @throws DocumentNotFoundException - * @throws DocumentException - */ - @Override - public void get(ServiceContext ctx, String id, DocumentHandler handler) - throws DocumentNotFoundException, DocumentException { - if (ctx == null) { - throw new IllegalArgumentException( - "PermissionRoleStorageClient.get: ctx is missing"); - } - if (handler == null) { - throw new IllegalArgumentException( - "PermissionRoleStorageClient.get: handler is missing"); - } - DocumentFilter docFilter = handler.getDocumentFilter(); - if (docFilter == null) { - docFilter = handler.createDocumentFilter(); - } - EntityManagerFactory emf = null; - EntityManager em = null; - try { - handler.prepare(Action.GET); - StringBuilder queryStrBldr = new StringBuilder("SELECT a FROM "); - queryStrBldr.append(getEntityName(ctx)); - queryStrBldr.append(" a"); - SubjectType subject = PermissionRoleUtil.getSubject(ctx); - String objectId = "permission_id"; - if (SubjectType.PERMISSION.equals(subject)) { - objectId = "role_id"; - } - queryStrBldr.append(" WHERE " + objectId + " = :objectId"); - String where = docFilter.getWhereClause(); - if ((null != where) && (where.length() > 0)) { - queryStrBldr.append(" AND " + where); - } - emf = getEntityManagerFactory(); - em = emf.createEntityManager(); - String queryStr = queryStrBldr.toString(); //for debugging - Query q = em.createQuery(queryStr); - q.setParameter("objectId", id); - - List prrl = new ArrayList(); - try { - //require transaction for get? - em.getTransaction().begin(); - prrl = q.getResultList(); - em.getTransaction().commit(); - } catch (NoResultException nre) { - if (em != null && em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - String msg = "could not find entity with id=" + id; - logger.error(msg, nre); - throw new DocumentNotFoundException(msg, nre); - } - if (prrl.size() == 0) { - String msg = "could not find entity with id=" + id; - logger.error(msg); - throw new DocumentNotFoundException(msg); - } - DocumentWrapper> wrapDoc = - new DocumentWrapperImpl>(prrl); - handler.handle(Action.GET, wrapDoc); - handler.complete(Action.GET, wrapDoc); - } catch (DocumentException de) { - throw de; - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Caught exception ", e); - } - throw new DocumentException(e); - } finally { - if (emf != null) { - releaseEntityManagerFactory(emf); - } - } - } - - /** - * delete removes all the relationships for the object in the relationship - * identified by the id. the object could be a permission or a role - * @param ctx - * @param id of the object in the relationship - * @throws DocumentNotFoundException - * @throws DocumentException - */ - @Override - public void delete(ServiceContext ctx, String id) - throws DocumentNotFoundException, - DocumentException { - - if (logger.isDebugEnabled()) { - logger.debug("deleting entity with id=" + id); - } - if (ctx == null) { - throw new IllegalArgumentException( - "PermissionRoleStorageClient.delete : ctx is missing"); - } - EntityManagerFactory emf = null; - EntityManager em = null; - try { - StringBuilder deleteStr = new StringBuilder("DELETE FROM "); - deleteStr.append(getEntityName(ctx)); - SubjectType subject = PermissionRoleUtil.getSubject(ctx); - String objectId = "permission_id"; - if (SubjectType.PERMISSION.equals(subject)) { - objectId = "role_id"; - } - deleteStr.append(" WHERE " + objectId + " = :objectId"); - emf = getEntityManagerFactory(); - em = emf.createEntityManager(); - Query q = em.createQuery(deleteStr.toString()); - q.setParameter("objectId", id); - int rcount = 0; - em.getTransaction().begin(); - rcount = q.executeUpdate(); - if (rcount == 0) { - if (em != null && em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - String msg = "could not find entity with id=" + id; - logger.error(msg); - throw new DocumentNotFoundException(msg); - } - em.getTransaction().commit(); - - } catch (DocumentException de) { - if (em != null && em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - throw de; - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Caught exception ", e); - } - if (em != null && em.getTransaction().isActive()) { - em.getTransaction().rollback(); - } - throw new DocumentException(e); - } finally { - if (emf != null) { - releaseEntityManagerFactory(emf); - } - } - } -} -- 2.47.3