From 07a9c684dedac372ce860dd76590ed6ca4e9957a Mon Sep 17 00:00:00 2001 From: Tom Page Date: Mon, 16 Jan 2017 13:41:49 +0000 Subject: [PATCH 1/2] RM-4262 Refactor common code for making paged queries. --- .../util/dao/QueryParams.java | 115 ++++++++++++++++++ .../util/dao/SortField.java | 40 ++++++ 2 files changed, 155 insertions(+) create mode 100644 rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryParams.java create mode 100644 rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/SortField.java diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryParams.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryParams.java new file mode 100644 index 0000000000..337864b5bc --- /dev/null +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryParams.java @@ -0,0 +1,115 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.module.org_alfresco_module_rm.util.dao; + +import java.util.Collections; +import java.util.List; + +import com.google.common.collect.ImmutableList; + +import org.alfresco.api.AlfrescoPublicApi; +import org.alfresco.util.Pair; + +/** + * Options when listing something. + * + * @author Tom Page + * @since 2.6 + */ +@AlfrescoPublicApi +public class QueryParams +{ + /** The ordered list of columns to sort on (and their sort direction). */ + private List> sortProps = Collections.emptyList(); + /** The number of items to skip before creating the list. */ + private int skipCount = 0; + /** The total number of items to return (assuming enough are available). */ + private int maxItems = 10; + + /** Sets the skip count required. */ + public QueryParams withSkipCount(final int skipCount) + { + this.skipCount = skipCount; + return this; + } + + /** Sets the max items count required. */ + public QueryParams withMaxItems(final int maxItems) + { + this.maxItems = maxItems; + return this; + } + + /** + * Sets the sort properties required. + * + * @param sortProps A list of pairs of properties and sort directions. + * @return The updated QueryParams object. + */ + public QueryParams withSortProps(List> sortProps) + { + this.setSortProps(sortProps); + return this; + } + + /** + * Sets the sort properties required and validates the list. + * + * @param sortProps A list of pairs of properties and sort directions. + */ + public void setSortProps(List> sortProps) + { + this.sortProps = ImmutableList.copyOf(sortProps); + + //validate the list + for(Pair sortPair : sortProps) + { + if(sortPair == null || sortPair.getFirst() == null || sortPair.getSecond() == null) + { + throw new IllegalArgumentException("Unexpected null or null containing element in list: " + sortProps); + } + } + } + + /** Get the ordered list of columns to sort on (and their sort direction). */ + public List> getSortProps() + { + return this.sortProps; + } + + /** Get the number of items to skip before creating the list. */ + public int getSkipCount() + { + return this.skipCount; + } + + /** Get the total number of items to return (assuming enough are available). */ + public int getMaxItems() + { + return this.maxItems; + } +} diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/SortField.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/SortField.java new file mode 100644 index 0000000000..5b066832ee --- /dev/null +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/SortField.java @@ -0,0 +1,40 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2017 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.module.org_alfresco_module_rm.util.dao; + +import org.alfresco.api.AlfrescoPublicApi; + +/** + * Interface for representations of fields that queries can be sorted by. + * + * @author Tom Page + * @since 2.6 + */ +@AlfrescoPublicApi +public interface SortField +{ +} From 64b0e89773ade5a6f2cf04a4d89609c22390f0b8 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Tue, 17 Jan 2017 09:17:58 +0000 Subject: [PATCH 2/2] RM-4262 Remove the ClassificationGuideQueryParams class. We can just use the generic QueryParams class instead. Also rename SortField to QueryField, as it might later be used for e.g. filtering too. --- .../util/dao/{SortField.java => QueryField.java} | 2 +- .../util/dao/QueryParams.java | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) rename rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/{SortField.java => QueryField.java} (97%) diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/SortField.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryField.java similarity index 97% rename from rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/SortField.java rename to rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryField.java index 5b066832ee..79a937bf29 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/SortField.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryField.java @@ -35,6 +35,6 @@ import org.alfresco.api.AlfrescoPublicApi; * @since 2.6 */ @AlfrescoPublicApi -public interface SortField +public interface QueryField { } diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryParams.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryParams.java index 337864b5bc..b11b71cea5 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryParams.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/util/dao/QueryParams.java @@ -26,7 +26,6 @@ */ package org.alfresco.module.org_alfresco_module_rm.util.dao; -import java.util.Collections; import java.util.List; import com.google.common.collect.ImmutableList; @@ -41,15 +40,25 @@ import org.alfresco.util.Pair; * @since 2.6 */ @AlfrescoPublicApi -public class QueryParams +public class QueryParams { /** The ordered list of columns to sort on (and their sort direction). */ - private List> sortProps = Collections.emptyList(); + private List> sortProps; /** The number of items to skip before creating the list. */ private int skipCount = 0; /** The total number of items to return (assuming enough are available). */ private int maxItems = 10; + /** + * Constructor that takes the sort order. + * + * @param sortProps A list of fields to sort on, and the direction to sort in. + */ + public QueryParams(List> sortProps) + { + setSortProps(sortProps); + } + /** Sets the skip count required. */ public QueryParams withSkipCount(final int skipCount) {