From f8303fe122055f5567a319715a0410db64a3d792 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Thu, 19 Aug 2010 15:53:12 +0000 Subject: [PATCH] CSPACE-1432: Odd that my SVN client shows these added and checked-in, but apparently not. Original post: Apply "List - pagination (return total items, current page, total pages) " functionality to Account --- .../common/document/DocumentListWrapper.java | 40 ++++ .../document/DocumentListWrapperImpl.java | 55 ++++++ .../services/common/query/QueryContext.java | 134 ++++++++++++++ .../storage/jpa/JpaDocumentHandler.java | 38 ++++ .../hyperjaxb/AbstractCommonList.java | 174 ++++++++++++++++++ 5 files changed, 441 insertions(+) create mode 100644 services/common/src/main/java/org/collectionspace/services/common/document/DocumentListWrapper.java create mode 100644 services/common/src/main/java/org/collectionspace/services/common/document/DocumentListWrapperImpl.java create mode 100644 services/common/src/main/java/org/collectionspace/services/common/query/QueryContext.java create mode 100644 services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaDocumentHandler.java create mode 100644 services/hyperjaxb/target/generated-sources/xjc/org/collectionspace/services/hyperjaxb/AbstractCommonList.java diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentListWrapper.java b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentListWrapper.java new file mode 100644 index 000000000..44562af17 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentListWrapper.java @@ -0,0 +1,40 @@ +/** + * 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.common.document; + +import java.util.List; +/** + * + * DocumentWrapper wraps DocumentModel (java) or Representation (REST) + * + */ +public interface DocumentListWrapper { + + /** + * getWrappedObject + * @return wrapped object + */ + public List getWrappedObject(); +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentListWrapperImpl.java b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentListWrapperImpl.java new file mode 100644 index 000000000..0ab43ea43 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentListWrapperImpl.java @@ -0,0 +1,55 @@ +/** + * 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.common.document; + +import java.util.List; + +// TODO: Auto-generated Javadoc +/** + * The Class DocumentListWrapperImpl. + * + * @param the generic type + */ +public class DocumentListWrapperImpl implements DocumentListWrapper { + + /** The document list wrapper. */ + private List documentListWrapper; + + /** + * Instantiates a new document list wrapper impl. + * + * @param theDocumentListWrapper the the document list wrapper + */ + public DocumentListWrapperImpl(List theDocumentListWrapper) { + documentListWrapper = theDocumentListWrapper; + } + + /* (non-Javadoc) + * @see org.collectionspace.services.common.document.DocumentListWrapper#getWrappedObject() + */ + public List getWrappedObject() { + return documentListWrapper; + } +} \ No newline at end of file diff --git a/services/common/src/main/java/org/collectionspace/services/common/query/QueryContext.java b/services/common/src/main/java/org/collectionspace/services/common/query/QueryContext.java new file mode 100644 index 000000000..b70a0e185 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/query/QueryContext.java @@ -0,0 +1,134 @@ +package org.collectionspace.services.common.query; + +import org.collectionspace.services.common.context.ServiceContext; +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.DocumentNotFoundException; + +import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; +import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; + +// TODO: Auto-generated Javadoc +/** + * The Class QueryContext. + */ +public class QueryContext { + + /** The doc type. */ + String docType; + /** The doc filter. */ + DocumentFilter docFilter; + /** The where clause. */ + String whereClause; + /** The order by clause. */ + String orderByClause; + /** The domain. */ + String domain; + /** The tenant id. */ + String tenantId; + + /** + * Instantiates a new query context. + * + * @param ctx the ctx + * @throws DocumentNotFoundException the document not found exception + * @throws DocumentException the document exception + */ + QueryContext(ServiceContext ctx) throws DocumentNotFoundException, DocumentException { + docType = ctx.getDocumentType(); + if (docType == null) { + throw new DocumentNotFoundException( + "Unable to find DocumentType for service " + ctx.getServiceName()); + } + domain = ctx.getRepositoryDomainName(); + tenantId = ctx.getTenantId(); + if (tenantId == null) { + throw new IllegalArgumentException( + "Service context has no Tenant ID specified."); + } + } + + /** + * Instantiates a new query context. + * + * @param ctx the ctx + * @param theWhereClause the where clause + * @throws DocumentNotFoundException the document not found exception + * @throws DocumentException the document exception + */ + public QueryContext(ServiceContext ctx, + String theWhereClause) throws DocumentNotFoundException, DocumentException { + this(ctx); + whereClause = theWhereClause; + } + + /** + * Instantiates a new query context. + * + * @param ctx the ctx + * @param handler the handler + * @throws DocumentNotFoundException the document not found exception + * @throws DocumentException the document exception + */ + public QueryContext(ServiceContext ctx, + DocumentHandler handler) throws DocumentNotFoundException, DocumentException { + this(ctx); + if (handler == null) { + throw new IllegalArgumentException( + "Document handler is missing."); + } + docFilter = handler.getDocumentFilter(); + if (docFilter == null) { + throw new IllegalArgumentException( + "Document handler has no Filter specified."); + } + whereClause = docFilter.getWhereClause(); + orderByClause = docFilter.getOrderByClause(); + } + + /** + * Gets the doc filter. + * + * @return the doc filter + */ + public DocumentFilter getDocFilter() { + return docFilter; + } + + /** + * Gets the where clause. + * + * @return the where clause + */ + public String getWhereClause() { + return whereClause; + } + + /** + * Gets the tenant id. + * + * @return the tenant id + */ + public String getTenantId() { + return this.tenantId; + } + + /** + * Gets the order by clause. + * + * @return the order by clause + */ + public String getOrderByClause() { + return this.orderByClause; + } + + /** + * Gets the doc type. + * + * @return the doc type + */ + public String getDocType() { + return this.docType; + } +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaDocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaDocumentHandler.java new file mode 100644 index 000000000..b421640d8 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaDocumentHandler.java @@ -0,0 +1,38 @@ +package org.collectionspace.services.common.storage.jpa; + +import java.util.List; + +import org.collectionspace.services.common.document.AbstractDocumentHandlerImpl; +import org.collectionspace.services.common.document.DocumentFilter; +import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.hyperjaxb.AbstractCommonList; + +public abstract class JpaDocumentHandler + extends AbstractDocumentHandlerImpl{ + + /** + * Extract paging info. + * + * @param commonsList the commons list + * @return the tL + * @throws Exception the exception + */ + public TL extractPagingInfo(TL theCommonList, DocumentWrapper wrapDoc) + throws Exception { + AbstractCommonList commonList = (AbstractCommonList) theCommonList; + + DocumentFilter docFilter = this.getDocumentFilter(); + long pageSize = docFilter.getPageSize(); + long pageNum = pageSize != 0 ? docFilter.getOffset() / pageSize : pageSize; + // set the page size and page number + commonList.setPageNum(pageNum); + commonList.setPageSize(pageSize); + List docList = (List)wrapDoc.getWrappedObject(); + // 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()); + + return (TL) commonList; + } +} diff --git a/services/hyperjaxb/target/generated-sources/xjc/org/collectionspace/services/hyperjaxb/AbstractCommonList.java b/services/hyperjaxb/target/generated-sources/xjc/org/collectionspace/services/hyperjaxb/AbstractCommonList.java new file mode 100644 index 000000000..e507fb3ea --- /dev/null +++ b/services/hyperjaxb/target/generated-sources/xjc/org/collectionspace/services/hyperjaxb/AbstractCommonList.java @@ -0,0 +1,174 @@ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v3.0-03/04/2009 09:20 AM(valikov)-fcs +// See http://java.sun.com/xml/jaxb +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2010.08.19 at 08:39:19 AM PDT +// + + +package org.collectionspace.services.hyperjaxb; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlSchemaType; +import javax.xml.bind.annotation.XmlType; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import org.jvnet.jaxb2_commons.lang.Equals; +import org.jvnet.jaxb2_commons.lang.HashCode; +import org.jvnet.jaxb2_commons.lang.builder.JAXBEqualsBuilder; +import org.jvnet.jaxb2_commons.lang.builder.JAXBHashCodeBuilder; + + +/** + * + * + * + *

Java class for anonymous complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType>
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="pageNum" type="{http://www.w3.org/2001/XMLSchema}unsignedInt"/>
+ *         <element name="pageSize" type="{http://www.w3.org/2001/XMLSchema}unsignedInt"/>
+ *         <element name="itemsInPage" type="{http://www.w3.org/2001/XMLSchema}unsignedInt"/>
+ *         <element name="totalItems" type="{http://www.w3.org/2001/XMLSchema}unsignedInt"/>
+ *       </sequence>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "", propOrder = { + "pageNum", + "pageSize", + "itemsInPage", + "totalItems" +}) +@XmlRootElement(name = "abstract-common-list") +public class AbstractCommonList + implements Equals, HashCode +{ + + @XmlSchemaType(name = "unsignedInt") + protected long pageNum; + @XmlSchemaType(name = "unsignedInt") + protected long pageSize; + @XmlSchemaType(name = "unsignedInt") + protected long itemsInPage; + @XmlSchemaType(name = "unsignedInt") + protected long totalItems; + + /** + * Gets the value of the pageNum property. + * + */ + public long getPageNum() { + return pageNum; + } + + /** + * Sets the value of the pageNum property. + * + */ + public void setPageNum(long value) { + this.pageNum = value; + } + + /** + * Gets the value of the pageSize property. + * + */ + public long getPageSize() { + return pageSize; + } + + /** + * Sets the value of the pageSize property. + * + */ + public void setPageSize(long value) { + this.pageSize = value; + } + + /** + * Gets the value of the itemsInPage property. + * + */ + public long getItemsInPage() { + return itemsInPage; + } + + /** + * Sets the value of the itemsInPage property. + * + */ + public void setItemsInPage(long value) { + this.itemsInPage = value; + } + + /** + * Gets the value of the totalItems property. + * + */ + public long getTotalItems() { + return totalItems; + } + + /** + * Sets the value of the totalItems property. + * + */ + public void setTotalItems(long value) { + this.totalItems = value; + } + + public void equals(Object object, EqualsBuilder equalsBuilder) { + if (!(object instanceof AbstractCommonList)) { + equalsBuilder.appendSuper(false); + return ; + } + if (this == object) { + return ; + } + final AbstractCommonList that = ((AbstractCommonList) object); + equalsBuilder.append(this.getPageNum(), that.getPageNum()); + equalsBuilder.append(this.getPageSize(), that.getPageSize()); + equalsBuilder.append(this.getItemsInPage(), that.getItemsInPage()); + equalsBuilder.append(this.getTotalItems(), that.getTotalItems()); + } + + public boolean equals(Object object) { + if (!(object instanceof AbstractCommonList)) { + return false; + } + if (this == object) { + return true; + } + final EqualsBuilder equalsBuilder = new JAXBEqualsBuilder(); + equals(object, equalsBuilder); + return equalsBuilder.isEquals(); + } + + public void hashCode(HashCodeBuilder hashCodeBuilder) { + hashCodeBuilder.append(this.getPageNum()); + hashCodeBuilder.append(this.getPageSize()); + hashCodeBuilder.append(this.getItemsInPage()); + hashCodeBuilder.append(this.getTotalItems()); + } + + public int hashCode() { + final HashCodeBuilder hashCodeBuilder = new JAXBHashCodeBuilder(); + hashCode(hashCodeBuilder); + return hashCodeBuilder.toHashCode(); + } + +} -- 2.47.3