Merged searchapi (5.2.1) to 5.2.N (5.2.1)

129824 gjames: SEARCH-115: Implementing Sort


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@130184 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gethin James
2016-09-06 14:19:28 +00:00
parent d5c3d3c1ca
commit e6549ed9a0
6 changed files with 242 additions and 25 deletions

View File

@@ -28,6 +28,7 @@ package org.alfresco.rest.api.search.impl;
import org.alfresco.rest.api.search.model.Query;
import org.alfresco.rest.api.search.model.SearchQuery;
import org.alfresco.rest.api.search.model.SortDef;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.content.BasicContentInfo;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
@@ -38,6 +39,7 @@ import org.alfresco.service.cmr.search.LimitBy;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.rest.api.model.Node;
import org.alfresco.service.cmr.search.SearchParameters.SortDefinition;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.util.ParameterCheck;
import org.apache.commons.lang.NotImplementedException;
@@ -81,6 +83,7 @@ public class SearchMapper
fromQuery(sp, searchQuery.getQuery());
fromPaging(sp, searchQuery.getPaging());
fromSort(sp, searchQuery.getSort());
validateInclude(searchQuery.getInclude());
return sp;
@@ -123,7 +126,6 @@ public class SearchMapper
}
sp.setQuery(q.getQuery());
sp.setSearchTerm(q.getUserQuery());
}
public void fromPaging(SearchParameters sp, Paging paging)
@@ -136,6 +138,25 @@ public class SearchMapper
}
}
public void fromSort(SearchParameters sp, List<SortDef> sort)
{
if (sort != null && !sort.isEmpty())
{
for (SortDef sortDef:sort)
{
try
{
sp.addSort(sortDef.toDefinition());
}
catch (IllegalArgumentException e)
{
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_MESSAGE_ID, new Object[] { sortDef.getType() });
}
}
}
}
public void validateInclude(List<String> includes)
{
if (includes != null && !includes.isEmpty())

View File

@@ -42,17 +42,20 @@ public class SearchQuery
private final Query query;
private final Paging paging;
private final List<String> include;
private final List<SortDef> sort;
public static final SearchQuery EMPTY = new SearchQuery(null, null, null);
public static final SearchQuery EMPTY = new SearchQuery(null, null, null, null);
@JsonCreator
public SearchQuery(@JsonProperty("query") Query query,
@JsonProperty("paging") Paging paging,
@JsonProperty("include") List<String> include)
@JsonProperty("include") List<String> include,
@JsonProperty("sort") List<SortDef> sort)
{
this.query = query;
this.paging = paging;
this.include = include;
this.sort = sort;
}
public Query getQuery()
@@ -69,4 +72,8 @@ public class SearchQuery
{
return include;
}
public List<SortDef> getSort()
{
return sort;
}
}

View File

@@ -0,0 +1,73 @@
/*-
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.search.model;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.cmr.search.SearchParameters.SortDefinition;
import org.alfresco.service.cmr.search.SearchParameters.SortDefinition.SortType;
import org.codehaus.jackson.annotate.JsonCreator;
import org.codehaus.jackson.annotate.JsonProperty;
/**
* This is a copy of the SortDefinition class found in data-model. Due to the use of a very old version
* of Jackson it is necessary to duplicate this class rather than reuse it.
*
* @author Gethin James
*/
public class SortDef
{
String type;
String field;
boolean ascending;
@JsonCreator
public SortDef(@JsonProperty("type") String type,
@JsonProperty("field") String field,
@JsonProperty("ascending") boolean ascending)
{
this.type = type;
this.field = field;
this.ascending = ascending;
}
public String getType()
{
return type;
}
/**
* You are allowed to create an instance of this class that has an invalid sortType
* but this method will validate the definition is correct and return it.
*
* @return SortDefinition
*/
public SortDefinition toDefinition()
{
return new SortDefinition(SortType.valueOf(type), field, ascending);
}
}