Merged RETURN-OF-THE-API (5.2.0) to 5.2.N (5.2.1)

129010 jvonka: V1 REST API: 1st-cut of Sites Live Search
   - TODO api sanity test
   - open-question, re: orderBy
   REPO-232, REPO-882


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@129188 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Martin Muller
2016-08-05 13:49:19 +00:00
parent 83e9fdf964
commit f83b4b7654
4 changed files with 137 additions and 6 deletions

View File

@@ -552,6 +552,7 @@
<bean id="queries" class="org.alfresco.rest.api.impl.QueriesImpl"> <bean id="queries" class="org.alfresco.rest.api.impl.QueriesImpl">
<property name="nodes" ref="nodes"/> <property name="nodes" ref="nodes"/>
<property name="people" ref="people"/> <property name="people" ref="people"/>
<property name="sites" ref="sites"/>
<property name="serviceRegistry" ref="ServiceRegistry"/> <property name="serviceRegistry" ref="ServiceRegistry"/>
</bean> </bean>

View File

@@ -29,6 +29,7 @@ package org.alfresco.rest.api;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.rest.api.model.Node; import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.Person; import org.alfresco.rest.api.model.Person;
import org.alfresco.rest.api.model.Site;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Parameters; import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper; import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
@@ -61,6 +62,9 @@ public interface Queries
static String PARAM_LASTNAME = ContentModel.PROP_LASTNAME.getLocalName(); static String PARAM_LASTNAME = ContentModel.PROP_LASTNAME.getLocalName();
static int MIN_TERM_LENGTH_PEOPLE = 2; static int MIN_TERM_LENGTH_PEOPLE = 2;
// Sites query
static int MIN_TERM_LENGTH_SITES = 2;
/** /**
* Find Nodes * Find Nodes
* *
@@ -78,4 +82,13 @@ public interface Queries
* @return the search query results * @return the search query results
*/ */
CollectionWithPagingInfo<Person> findPeople(Parameters parameters); CollectionWithPagingInfo<Person> findPeople(Parameters parameters);
/**
* Find Sites
*
* @param parameters the {@link Parameters} object to get the parameters passed into the request
*
* @return the search query results
*/
CollectionWithPagingInfo<Site> findSites(Parameters parameters);
} }

View File

@@ -37,6 +37,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -45,8 +46,10 @@ import org.alfresco.query.PagingRequest;
import org.alfresco.rest.api.Nodes; import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.People; import org.alfresco.rest.api.People;
import org.alfresco.rest.api.Queries; import org.alfresco.rest.api.Queries;
import org.alfresco.rest.api.Sites;
import org.alfresco.rest.api.model.Node; import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.Person; import org.alfresco.rest.api.model.Person;
import org.alfresco.rest.api.model.Site;
import org.alfresco.rest.api.model.UserInfo; import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException; import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
@@ -65,6 +68,8 @@ import org.alfresco.service.cmr.search.PermissionEvaluationMode;
import org.alfresco.service.cmr.search.ResultSet; import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchParameters; import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.cmr.search.SearchService; import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.util.ISO9075; import org.alfresco.util.ISO9075;
@@ -117,9 +122,11 @@ public class QueriesImpl implements Queries, InitializingBean
private NodeService nodeService; private NodeService nodeService;
private NamespaceService namespaceService; private NamespaceService namespaceService;
private DictionaryService dictionaryService; private DictionaryService dictionaryService;
private SiteService siteService;
private Nodes nodes; private Nodes nodes;
private People people; private People people;
private Sites sites;
public void setServiceRegistry(ServiceRegistry sr) public void setServiceRegistry(ServiceRegistry sr)
{ {
@@ -136,17 +143,24 @@ public class QueriesImpl implements Queries, InitializingBean
this.people = people; this.people = people;
} }
public void setSites(Sites sites)
{
this.sites = sites;
}
@Override @Override
public void afterPropertiesSet() public void afterPropertiesSet()
{ {
ParameterCheck.mandatory("sr", this.sr); ParameterCheck.mandatory("sr", this.sr);
ParameterCheck.mandatory("nodes", this.nodes); ParameterCheck.mandatory("nodes", this.nodes);
ParameterCheck.mandatory("people", this.people); ParameterCheck.mandatory("people", this.people);
ParameterCheck.mandatory("sites", this.sites);
this.searchService = sr.getSearchService(); this.searchService = sr.getSearchService();
this.nodeService = sr.getNodeService(); this.nodeService = sr.getNodeService();
this.namespaceService = sr.getNamespaceService(); this.namespaceService = sr.getNamespaceService();
this.dictionaryService = sr.getDictionaryService(); this.dictionaryService = sr.getDictionaryService();
this.siteService = sr.getSiteService();
} }
@Override @Override
@@ -290,6 +304,70 @@ public class QueriesImpl implements Queries, InitializingBean
new SortColumn(PARAM_FIRSTNAME, true), new SortColumn(PARAM_LASTNAME, true)); new SortColumn(PARAM_FIRSTNAME, true), new SortColumn(PARAM_LASTNAME, true));
} }
@Override
public CollectionWithPagingInfo<Site> findSites(Parameters parameters)
{
// TODO Alternatively do we want to implement our own find rather than rely on SiteService ?!
return new AbstractQuery<Site>(nodeService, searchService)
{
@Override
protected void buildQuery(StringBuilder query, String term, SearchParameters sp, String queryTemplateName)
{
// not used
}
@Override
protected List<Site> newList(int capacity)
{
return new ArrayList<>(capacity);
}
@Override
protected Site convert(NodeRef nodeRef, List<String> includeParam)
{
// not used
return null;
}
@Override
public CollectionWithPagingInfo<Site> find(Parameters parameters, String termName, int minTermLength,
String queryTemplateName,
Sort sort, Map<String, QName> sortParamsToQNames, SortColumn... defaultSort)
{
Paging paging = parameters.getPaging();
String term = getTerm(parameters, termName, minTermLength);
// TODO optimise paging
// TODO implement sorting (see open-question)
List<SiteInfo> siteInfos = siteService.findSites(term, Integer.MAX_VALUE);
List<Site> collection = newList(siteInfos.size());
for (SiteInfo siteInfo : siteInfos)
{
// convert
Site s = getSite(siteInfo, true);
collection.add(s);
}
return listPage(collection, paging);
}
}.find(parameters, PARAM_TERM, MIN_TERM_LENGTH_SITES, null, null, null, null);
}
// note: see also Sites.getSite
private Site getSite(SiteInfo siteInfo, boolean includeRole)
{
// set the site id to the short name (to deal with case sensitivity issues with using the siteId from the url)
String siteId = siteInfo.getShortName();
String role = null;
if(includeRole)
{
role = sites.getSiteRole(siteId);
}
return new Site(siteInfo, role);
}
public abstract static class AbstractQuery<T> public abstract static class AbstractQuery<T>
{ {
public enum Sort public enum Sort
@@ -332,7 +410,7 @@ public class QueriesImpl implements Queries, InitializingBean
sp.setMaxItems(pagingRequest.getMaxItems()); sp.setMaxItems(pagingRequest.getMaxItems());
ResultSet queryResults = null; ResultSet queryResults = null;
List<T> colection = null; List<T> collection = null;
try try
{ {
queryResults = searchService.query(sp); queryResults = searchService.query(sp);
@@ -344,13 +422,13 @@ public class QueriesImpl implements Queries, InitializingBean
nodeRefs = postQuerySort(parameters, sortParamsToQNames, defaultSortCols, nodeRefs); nodeRefs = postQuerySort(parameters, sortParamsToQNames, defaultSortCols, nodeRefs);
} }
colection = newList(nodeRefs.size()); collection = newList(nodeRefs.size());
List<String> includeParam = parameters.getInclude(); List<String> includeParam = parameters.getInclude();
for (NodeRef nodeRef : nodeRefs) for (NodeRef nodeRef : nodeRefs)
{ {
T t = convert(nodeRef, includeParam); T t = convert(nodeRef, includeParam);
colection.add(t); collection.add(t);
} }
} }
finally finally
@@ -361,7 +439,7 @@ public class QueriesImpl implements Queries, InitializingBean
} }
} }
return CollectionWithPagingInfo.asPaged(paging, colection, queryResults.hasMore(), new Long(queryResults.getNumberFound()).intValue()); return CollectionWithPagingInfo.asPaged(paging, collection, queryResults.hasMore(), new Long(queryResults.getNumberFound()).intValue());
} }
/** /**
@@ -389,7 +467,7 @@ public class QueriesImpl implements Queries, InitializingBean
*/ */
protected abstract T convert(NodeRef nodeRef, List<String> includeParam); protected abstract T convert(NodeRef nodeRef, List<String> includeParam);
private String getTerm(Parameters parameters, String termName, int minTermLength) protected String getTerm(Parameters parameters, String termName, int minTermLength)
{ {
String term = parameters.getParameter(termName); String term = parameters.getParameter(termName);
if (term == null) if (term == null)
@@ -504,4 +582,39 @@ public class QueriesImpl implements Queries, InitializingBean
return nodeRefs; return nodeRefs;
} }
} }
// note: see also AbstractNodeRelation
protected CollectionWithPagingInfo listPage(List result, Paging paging)
{
// return 'page' of results (based on full result set)
int skipCount = paging.getSkipCount();
int pageSize = paging.getMaxItems();
int pageEnd = skipCount + pageSize;
final List page = new ArrayList<>(pageSize);
if (result == null)
{
result = Collections.emptyList();
}
Iterator it = result.iterator();
for (int counter = 0; counter < pageEnd && it.hasNext(); counter++)
{
Object element = it.next();
if (counter < skipCount)
{
continue;
}
if (counter > pageEnd - 1)
{
break;
}
page.add(element);
}
int totalCount = result.size();
boolean hasMoreItems = ((skipCount + page.size()) < totalCount);
return CollectionWithPagingInfo.asPaged(paging, page, hasMoreItems, totalCount);
}
} }

View File

@@ -39,6 +39,7 @@ import org.springframework.beans.factory.InitializingBean;
* An implementation of an Entity Resource for Queries. * An implementation of an Entity Resource for Queries.
* *
* @author janv * @author janv
* @author Alan Davis
*/ */
@EntityResource(name="queries", title = "Queries") @EntityResource(name="queries", title = "Queries")
public class QueriesEntityResource implements public class QueriesEntityResource implements
@@ -47,6 +48,7 @@ public class QueriesEntityResource implements
{ {
private final static String QUERY_LIVE_SEARCH_NODES = "live-search-nodes"; private final static String QUERY_LIVE_SEARCH_NODES = "live-search-nodes";
private final static String QUERY_LIVE_SEARCH_PEOPLE = "live-search-people"; private final static String QUERY_LIVE_SEARCH_PEOPLE = "live-search-people";
private final static String QUERY_LIVE_SEARCH_SITES = "live-search-sites";
private Queries queries; private Queries queries;
@@ -72,6 +74,8 @@ public class QueriesEntityResource implements
return queries.findNodes(parameters); return queries.findNodes(parameters);
case QUERY_LIVE_SEARCH_PEOPLE: case QUERY_LIVE_SEARCH_PEOPLE:
return queries.findPeople(parameters); return queries.findPeople(parameters);
case QUERY_LIVE_SEARCH_SITES:
return queries.findSites(parameters);
default: default:
throw new NotFoundException(queryId); throw new NotFoundException(queryId);
} }