]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-181: Adding paging info to Roles and Permissions resources. Also fixed 'totalIt...
authorremillet <remillet@yahoo.com>
Fri, 15 Dec 2017 23:12:01 +0000 (15:12 -0800)
committerremillet <remillet@yahoo.com>
Fri, 15 Dec 2017 23:12:01 +0000 (15:12 -0800)
services/authorization-mgt/jaxb/.gitignore [new file with mode: 0644]
services/authorization-mgt/jaxb/pom.xml [new file with mode: 0644]
services/authorization-mgt/jaxb/src/test/resources/log4j.properties [new file with mode: 0644]
services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/RoleDocumentHandler.java
services/authorization/jaxb/src/main/resources/roles_list.xsd
services/common/src/main/java/org/collectionspace/services/authorization/storage/PermissionDocumentHandler.java
services/common/src/main/java/org/collectionspace/services/common/document/DocumentFilter.java
services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaDocumentHandler.java
services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClientImpl.java
services/hyperjaxb/src/main/resources/permissions_list.xsd

diff --git a/services/authorization-mgt/jaxb/.gitignore b/services/authorization-mgt/jaxb/.gitignore
new file mode 100644 (file)
index 0000000..b83d222
--- /dev/null
@@ -0,0 +1 @@
+/target/
diff --git a/services/authorization-mgt/jaxb/pom.xml b/services/authorization-mgt/jaxb/pom.xml
new file mode 100644 (file)
index 0000000..45e66d0
--- /dev/null
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <parent>
+        <groupId>org.collectionspace.services</groupId>
+        <artifactId>org.collectionspace.services.authorization-mgt</artifactId>
+        <version>5.0-SNAPSHOT</version>
+    </parent>
+
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>org.collectionspace.services.authorization-mgt.jaxb</artifactId>
+    <name>services.authorization-mgt.jaxb</name>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>com.sun.xml.bind</groupId>
+            <artifactId>jaxb-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.jvnet.jaxb2-commons</groupId>
+            <artifactId>property-listener-injector</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>javax.persistence</groupId>
+            <artifactId>persistence-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.hibernate</groupId>
+            <artifactId>hibernate-entitymanager</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.jvnet.hyperjaxb3</groupId>
+            <artifactId>hyperjaxb3-ejb-runtime</artifactId>
+        </dependency>
+        <dependency>
+                       <groupId>org.collectionspace.services</groupId>
+                       <artifactId>org.collectionspace.services.jaxb</artifactId>
+                       <version>${project.version}</version>
+        </dependency>
+        <dependency>
+                       <groupId>org.collectionspace.services</groupId>
+                       <artifactId>org.collectionspace.services.hyperjaxb</artifactId>
+                       <version>${project.version}</version>
+        </dependency>        
+    </dependencies>
+
+    <build>
+        <finalName>collectionspace-services-authorization-mgt-jaxb</finalName>
+        <defaultGoal>install</defaultGoal>
+        <plugins>
+            <plugin>
+                <!-- maven-hyperjaxb3-plugin generates jaxb + jpa bindings -->
+                <groupId>org.jvnet.hyperjaxb3</groupId>
+                <artifactId>maven-hyperjaxb3-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
+
diff --git a/services/authorization-mgt/jaxb/src/test/resources/log4j.properties b/services/authorization-mgt/jaxb/src/test/resources/log4j.properties
new file mode 100644 (file)
index 0000000..53720a7
--- /dev/null
@@ -0,0 +1,25 @@
+log4j.rootLogger=debug, stdout, R
+
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+
+# Pattern to output the caller's file name and line number.
+log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n
+
+log4j.appender.R=org.apache.log4j.RollingFileAppender
+log4j.appender.R.File=target/test-client.log
+
+log4j.appender.R.MaxFileSize=100KB
+# Keep one backup file
+log4j.appender.R.MaxBackupIndex=1
+
+log4j.appender.R.layout=org.apache.log4j.PatternLayout
+log4j.appender.R.layout.ConversionPattern=%d %-5p [%t] [%c:%L] %m%n
+
+#packages
+log4j.logger.org.collectionspace=DEBUG
+log4j.logger.org.apache=INFO
+log4j.logger.httpclient=INFO
+log4j.logger.org.jboss.resteasy=INFO
+log4j.logger.org.jvnet.hyperjaxb3=DEBUG
+log4j.logger.org.hibernate=WARN
\ No newline at end of file
index 1661b78ff982e544ab7aae258c88a5b719059770..77b3ca387fd9075de6be5b321bba5e88d0b21617 100644 (file)
@@ -27,6 +27,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
+import org.collectionspace.services.account.AccountsCommonList;
 import org.collectionspace.services.authorization.PermissionRole;
 import org.collectionspace.services.authorization.PermissionRoleSubResource;
 import org.collectionspace.services.authorization.PermissionValue;
@@ -45,7 +46,7 @@ import org.collectionspace.services.common.document.DocumentWrapper;
 import org.collectionspace.services.common.document.JaxbUtils;
 import org.collectionspace.services.common.security.SecurityUtils;
 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
-
+import org.collectionspace.services.jaxb.AbstractCommonList;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -54,7 +55,7 @@ import org.slf4j.LoggerFactory;
  * @author 
  */
 public class RoleDocumentHandler
-               extends JpaDocumentHandler<Role, RolesList, Role, List> {
+               extends JpaDocumentHandler<Role, RolesList, Role, List<Role>> {
     private final Logger logger = LoggerFactory.getLogger(RoleDocumentHandler.class);
     private Role role;
     private RolesList rolesList;
@@ -170,9 +171,9 @@ public class RoleDocumentHandler
 
     @Override
     public void completeUpdate(DocumentWrapper<Role> wrapDoc) throws Exception {
-        Role upAcc = wrapDoc.getWrappedObject();
-        getServiceContext().setOutput(upAcc);
-        sanitize(upAcc);
+        Role updatedRole = wrapDoc.getWrappedObject();
+        getServiceContext().setOutput(updatedRole);
+        sanitize(updatedRole);
     }
 
     @Override
@@ -183,7 +184,7 @@ public class RoleDocumentHandler
     }
 
     @Override
-    public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
+    public void handleGetAll(DocumentWrapper<List<Role>> wrapDoc) throws Exception {
         RolesList rolesList = extractCommonPartList(wrapDoc);
         setCommonPartList(rolesList);
         getServiceContext().setOutput(getCommonPartList());
@@ -213,19 +214,44 @@ public class RoleDocumentHandler
         throw new UnsupportedOperationException("operation not relevant for AccountDocumentHandler");
     }
 
+    /*
+     * See https://issues.collectionspace.org/browse/DRYD-181
+     * 
+     * For backward compatibility, we could not change the role list to be a child class of AbstractCommonList.  This
+     * would have change the result payload and would break existing API clients.  So the best we can do, it treat
+     * the role list payload as a special case and return the paging information.
+     * 
+     */
+       protected RolesList extractPagingInfoForRoles(RolesList roleList, DocumentWrapper<List<Role>> wrapDoc)
+            throws Exception {
+
+        DocumentFilter docFilter = this.getDocumentFilter();
+        long pageSize = docFilter.getPageSize();
+        long pageNum = pageSize != 0 ? docFilter.getOffset() / pageSize : pageSize;
+        // set the page size and page number
+        roleList.setPageNum(pageNum);
+        roleList.setPageSize(pageSize);
+        List<Role> docList = wrapDoc.getWrappedObject();
+        // Set num of items in list. this is useful to our testing framework.
+        roleList.setItemsInPage(docList.size());
+        // set the total result size
+        roleList.setTotalItems(docFilter.getTotalItemsResult());
+
+        return roleList;
+    }
+       
     @Override
     public RolesList extractCommonPartList(
-            DocumentWrapper<List> wrapDoc)
-            throws Exception {
+            DocumentWrapper<List<Role>> wrapDoc) throws Exception {
 
-        RolesList rolesList = new RolesList();
+        RolesList rolesList = extractPagingInfoForRoles(new RolesList(), wrapDoc);        
         List<Role> list = new ArrayList<Role>();
         rolesList.setRole(list);
-        for (Object obj : wrapDoc.getWrappedObject()) {
-            Role role = (Role) obj;
+        for (Role role : wrapDoc.getWrappedObject()) {
             sanitize(role);
             list.add(role);
         }
+        
         return rolesList;
     }
 
index 0e3fc1db13985d04eacf4ca92325db80bef361d1..b83b3dcb988b5c5cada5fdb6130f09c8405e0e90 100644 (file)
             <xs:annotation>
                 <xs:documentation>role list</xs:documentation>
             </xs:annotation>
+                       <xs:element name="pageNum" type="xs:unsignedInt"/>
+                       <xs:element name="pageSize" type="xs:unsignedInt"/>
+                       <!-- "itemsInPage" is useful to our testing framework's unit tests -->
+                       <xs:element name="itemsInPage" type="xs:unsignedInt"/>
+                       <xs:element name="totalItems" type="xs:unsignedInt"/>
             <xs:element name="role" type="ns:role" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
     </xs:complexType>
index 5aed20f46bfbcf479ad84666e766dd0eae636eda..156ad27f6e592d188523fb34ab2b2b2af057361f 100644 (file)
@@ -29,11 +29,11 @@ import java.util.UUID;
 
 import javax.persistence.EntityExistsException;
 import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
 import javax.persistence.NoResultException;
 
 import org.collectionspace.services.authorization.perms.ActionType;
 import org.collectionspace.services.authorization.CSpaceAction;
+import org.collectionspace.services.authorization.RolesList;
 import org.collectionspace.services.authorization.perms.Permission;
 import org.collectionspace.services.authorization.perms.PermissionAction;
 import org.collectionspace.services.authorization.perms.PermissionsList;
@@ -49,6 +49,7 @@ import org.collectionspace.services.common.document.JaxbUtils;
 import org.collectionspace.services.common.security.SecurityUtils;
 import org.collectionspace.services.common.storage.jpa.JpaDocumentHandler;
 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -57,7 +58,7 @@ import org.slf4j.LoggerFactory;
  * @author 
  */
 public class PermissionDocumentHandler
-               extends JpaDocumentHandler<Permission, PermissionsList, Permission, List> {
+               extends JpaDocumentHandler<Permission, PermissionsList, Permission, List<Permission>> {
 
     private final Logger logger = LoggerFactory.getLogger(PermissionDocumentHandler.class);
     private Permission permission;
@@ -241,7 +242,7 @@ public class PermissionDocumentHandler
     }
 
     @Override
-    public void handleGetAll(DocumentWrapper<List> wrapDoc) throws Exception {
+    public void handleGetAll(DocumentWrapper<List<Permission>> wrapDoc) throws Exception {
         PermissionsList permissionsList = extractCommonPartList(wrapDoc);
         setCommonPartList(permissionsList);
         getServiceContext().setOutput(getCommonPartList());
@@ -251,6 +252,32 @@ public class PermissionDocumentHandler
     public void completeDelete(DocumentWrapper<Permission> wrapDoc) throws Exception {
     }
 
+    /*
+     * See https://issues.collectionspace.org/browse/DRYD-181
+     * 
+     * For backward compatibility, we could not change the permission list to be a child class of AbstractCommonList.  This
+     * would have change the result payload and would break existing API clients.  So the best we can do, it treat
+     * the role list payload as a special case and return the paging information.
+     * 
+     */
+       protected PermissionsList extractPagingInfoForPerms(PermissionsList permList, DocumentWrapper<List<Permission>> wrapDoc)
+            throws Exception {
+
+        DocumentFilter docFilter = this.getDocumentFilter();
+        long pageSize = docFilter.getPageSize();
+        long pageNum = pageSize != 0 ? docFilter.getOffset() / pageSize : pageSize;
+        // set the page size and page number
+        permList.setPageNum(pageNum);
+        permList.setPageSize(pageSize);
+        List<Permission> docList = wrapDoc.getWrappedObject();
+        // Set num of items in list. this is useful to our testing framework.
+        permList.setItemsInPage(docList.size());
+        // set the total result size
+        permList.setTotalItems(docFilter.getTotalItemsResult());
+
+        return permList;
+    }
+       
     @Override
     public Permission extractCommonPart(
             DocumentWrapper<Permission> wrapDoc)
@@ -266,10 +293,10 @@ public class PermissionDocumentHandler
 
     @Override
     public PermissionsList extractCommonPartList(
-            DocumentWrapper<List> wrapDoc)
+            DocumentWrapper<List<Permission>> wrapDoc)
             throws Exception {
 
-        PermissionsList permissionsList = new PermissionsList();
+       PermissionsList permissionsList = extractPagingInfoForPerms(new PermissionsList(), wrapDoc);
         List<Permission> list = new ArrayList<Permission>();
         permissionsList.setPermission(list);
         for (Object obj : wrapDoc.getWrappedObject()) {
@@ -277,6 +304,7 @@ public class PermissionDocumentHandler
             sanitize(permission);
             list.add(permission);
         }
+        
         return permissionsList;
     }
 
index b2c2150ad70eeb84f2b7eda1eddcb78bf45515e5..cc885f10bd4e2aff3d166e86d94c573de524046a 100644 (file)
@@ -61,6 +61,11 @@ public abstract class DocumentFilter {
     /** The page size. */
     protected int pageSize;                    // Pagination limit for list results
     
+    /** The total number of items for a query result -independent of paging restrictions.
+     * 
+     */
+    protected long totalItemsResult = -1;
+    
     //queryParams is not initialized as it would require a multi-valued map implementation
     //unless it is used from opensource lib...this variable holds ref to
     //implementation available in JBoss RESTeasy
@@ -515,4 +520,18 @@ public abstract class DocumentFilter {
     protected String getTenantId() {
         return AuthN.get().getCurrentTenantId();
     }
+
+    /*
+     * Used to set the total number of items matching a query -independent of the paging restrictions.
+     */
+       public void setTotalItemsResult(long totalItems) {
+               totalItemsResult = totalItems;
+       }
+       
+    /*
+     * Used to get the total number of items matching the last query made -independent of the paging restrictions.
+     */        
+       public long getTotalItemsResult() {
+               return totalItemsResult;
+       }       
 }
index c2d77bd06665dbcd826af5a629a73a2276705356..152e12a494b43dba7425a0ce16ba8e0e9e49a7c8 100644 (file)
@@ -50,7 +50,7 @@ public abstract class JpaDocumentHandler<T, TL, WT, WLT>
         // Set num of items in list. this is useful to our testing framework.
         commonList.setItemsInPage(docList.size());
         // set the total result size
-        commonList.setTotalItems(docList.size());
+        commonList.setTotalItems(docFilter.getTotalItemsResult());
 
         return (TL) commonList;
     }
index 2e25788becea69538d9e296e84aa5fd753d552fd..b5349389b275b0f2b60465563d1d68c95fdf03b8 100644 (file)
@@ -272,7 +272,11 @@ public class JpaStorageClientImpl implements StorageClient {
             //FIXME is transaction required for get?
             em.getTransaction().begin();
             List list = q.getResultList();
+            long totalItems = getTotalItems(em, ctx, handler); // Find out how many items our query would find independent of the paging restrictions
             em.getTransaction().commit();
+            
+            docFilter.setTotalItemsResult(totalItems); // Save the items total in the doc filter for later reporting
+
             DocumentWrapper<List> wrapDoc = new DocumentWrapperImpl<List>(list);
             handler.handle(Action.GET_ALL, wrapDoc);
             handler.complete(Action.GET_ALL, wrapDoc);
@@ -290,7 +294,31 @@ public class JpaStorageClientImpl implements StorageClient {
         }
     }
 
-    /* (non-Javadoc)
+    /*
+     * Return the COUNT for a query to find the total number of matches independent of the paging restrictions.
+     */
+    private long getTotalItems(EntityManager em, ServiceContext ctx, DocumentHandler handler) {
+       long result = -1;
+       
+        DocumentFilter docFilter = handler.getDocumentFilter();
+        StringBuilder queryStrBldr = new StringBuilder("SELECT COUNT(*) FROM ");
+        queryStrBldr.append(getEntityName(ctx));
+        queryStrBldr.append(" a");
+        
+        List<DocumentFilter.ParamBinding> params = docFilter.buildWhereForSearch(queryStrBldr);
+        String queryStr = queryStrBldr.toString();
+        Query q = em.createQuery(queryStr);
+        //bind parameters
+        for (DocumentFilter.ParamBinding p : params) {
+            q.setParameter(p.getName(), p.getValue());
+        }
+
+        result = (long) q.getSingleResult();
+
+        return result;
+    }
+
+       /* (non-Javadoc)
      * @see org.collectionspace.services.common.storage.StorageClient#update(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler)
      */
     @Override
index 2427a84df1bb41d94c84ded8b57cfbd004b6d1c6..f45fe2c535a9357ae78a536aef80fa55d227ed42 100644 (file)
                    <xs:annotation>
                        <xs:documentation>permission configuration list</xs:documentation>
                    </xs:annotation>
+                               <xs:element name="pageNum" type="xs:unsignedInt"/>
+                               <xs:element name="pageSize" type="xs:unsignedInt"/>
+                               <!-- "itemsInPage" is useful to our testing framework's unit tests -->
+                               <xs:element name="itemsInPage" type="xs:unsignedInt"/>
+                               <xs:element name="totalItems" type="xs:unsignedInt"/>               
                    <xs:element name="permission" type="ns:permission" minOccurs="1" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:complexType>