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

126455 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)
      122167 gjames: RA-823 adding "source" into the collection output


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126800 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ancuta Morarasu
2016-05-11 11:23:20 +00:00
parent c90ab5e034
commit 6b821ac9c9
6 changed files with 92 additions and 16 deletions

View File

@@ -61,11 +61,21 @@ public class SerializerOfCollectionWithPaging extends SerializerBase<Serializabl
jgen.writeStartObject();
serializePagination(pagedCol, jgen);
jgen.writeObjectField("entries", pagedCol.getCollection());
serializeIncludedSource(pagedCol, jgen);
jgen.writeEndObject();
jgen.writeEndObject();
}
}
private void serializeIncludedSource(SerializablePagedCollection pagedCol, JsonGenerator jgen) throws IOException,
JsonProcessingException
{
if (pagedCol.getSourceEntity() != null)
{
jgen.writeObjectField("source",pagedCol.getSourceEntity());
}
}
private void serializePagination(SerializablePagedCollection pagedCol, JsonGenerator jgen) throws IOException,
JsonProcessingException
{

View File

@@ -30,6 +30,11 @@ public interface SerializablePagedCollection<T>
*/
Integer getTotalItems();
/**
* The parent/source entity responsible for the collection
*/
Object getSourceEntity();
/**
* The requested paging parameters set by the client
*/

View File

@@ -49,6 +49,7 @@ public class CollectionWithPagingInfo<T> implements SerializablePagedCollection
private final boolean hasMoreItems;
private final Integer totalItems;
private final Paging paging;
private final Object sourceEntity;
/**
* Constructs a new CollectionWithPagingInfo.
@@ -57,7 +58,7 @@ public class CollectionWithPagingInfo<T> implements SerializablePagedCollection
* @param hasMoreItems - Are there more items after this Collection?
* @param totalItems - The total number of items available.
*/
protected CollectionWithPagingInfo(Collection<T> collection, Paging paging, boolean hasMoreItems, Integer totalItems)
protected CollectionWithPagingInfo(Collection<T> collection, Paging paging, boolean hasMoreItems, Integer totalItems, Object sourceEntity)
{
super();
this.hasMoreItems = hasMoreItems;
@@ -73,6 +74,7 @@ public class CollectionWithPagingInfo<T> implements SerializablePagedCollection
this.collection = collection;
this.totalItems = totalItems;
}
this.sourceEntity = sourceEntity;
}
/**
@@ -87,7 +89,7 @@ public class CollectionWithPagingInfo<T> implements SerializablePagedCollection
public static <T> CollectionWithPagingInfo<T> asPaged(Paging paging, Collection<T> aCollection)
{
int collectionSize = aCollection==null?0:aCollection.size();
return new CollectionWithPagingInfo<T>(aCollection, paging, false, collectionSize);
return new CollectionWithPagingInfo<T>(aCollection, paging, false, collectionSize, null);
}
/**
@@ -101,7 +103,7 @@ public class CollectionWithPagingInfo<T> implements SerializablePagedCollection
public static <T> CollectionWithPagingInfo<T> asPagedCollection(T ...entity)
{
Collection<T> aNewCollection = Arrays.asList(entity);
return new CollectionWithPagingInfo<T>(aNewCollection, Paging.DEFAULT, false, aNewCollection.size());
return new CollectionWithPagingInfo<T>(aNewCollection, Paging.DEFAULT, false, aNewCollection.size(), null);
}
/**
@@ -115,7 +117,22 @@ public class CollectionWithPagingInfo<T> implements SerializablePagedCollection
*/
public static <T> CollectionWithPagingInfo<T> asPaged(Paging paging, Collection<T> aCollection, boolean hasMoreItems, Integer totalItems)
{
return new CollectionWithPagingInfo<T>(aCollection, paging, hasMoreItems, totalItems);
return new CollectionWithPagingInfo<T>(aCollection, paging, hasMoreItems, totalItems, null);
}
/**
* Constructs a new CollectionWithPagingInfo. Not for public use.
*
* @param paging - Paging request info
* @param aCollection - the collection that needs to be paged.
* @param hasMoreItems - Are there more items after this Collection?
* @param totalItems - The total number of items available.
* @param sourceEntity - The parent/source entity responsible for the collection
* @return CollectionWithPagingInfo
*/
public static <T> CollectionWithPagingInfo<T> asPaged(Paging paging, Collection<T> aCollection, boolean hasMoreItems, Integer totalItems, Object sourceEntity)
{
return new CollectionWithPagingInfo<T>(aCollection, paging, hasMoreItems, totalItems, sourceEntity);
}
/**
@@ -149,6 +166,15 @@ public class CollectionWithPagingInfo<T> implements SerializablePagedCollection
return this.totalItems;
}
/**
* The parent/source entity responsible for the collection
*/
@Override
public Object getSourceEntity()
{
return sourceEntity;
}
/**
* The requested paging parameters set by the client
*/

View File

@@ -55,6 +55,7 @@ import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException;
import org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter;
import org.alfresco.rest.framework.jacksonextensions.ExecutionResult;
import org.alfresco.rest.framework.jacksonextensions.JacksonHelper;
import org.alfresco.rest.framework.resource.SerializablePagedCollection;
import org.alfresco.rest.framework.resource.actions.ActionExecutor;
import org.alfresco.rest.framework.resource.actions.ActionExecutor.ExecutionCallback;
import org.alfresco.rest.framework.resource.content.ContentInfo;
@@ -482,22 +483,19 @@ public class ResourceWebScriptHelper
{
PropertyCheck.mandatory(this, null, params);
if (objectToWrap == null ) return null;
if (objectToWrap instanceof CollectionWithPagingInfo<?>)
if (objectToWrap instanceof SerializablePagedCollection<?>)
{
CollectionWithPagingInfo<?> collectionToWrap = (CollectionWithPagingInfo<?>) objectToWrap;
SerializablePagedCollection<?> collectionToWrap = (SerializablePagedCollection<?>) objectToWrap;
Object sourceEntity = executeIncludedSource(api,entityCollectionName,params.getEntityId(),params.includeSource());
Collection<Object> resultCollection = new ArrayList(collectionToWrap.getCollection().size());
if (!collectionToWrap.getCollection().isEmpty())
{
Collection<Object> resultCollection = new ArrayList(collectionToWrap.getCollection().size());
for (Object obj : collectionToWrap.getCollection())
{
resultCollection.add(processAdditionsToTheResponse(api,entityCollectionName,params,obj));
}
return CollectionWithPagingInfo.asPaged(collectionToWrap.getPaging(), resultCollection, collectionToWrap.hasMoreItems(), collectionToWrap.getTotalItems());
}
else
{ //It is empty so just return it for rendering.
return objectToWrap;
}
return CollectionWithPagingInfo.asPaged(collectionToWrap.getPaging(), resultCollection, collectionToWrap.hasMoreItems(), collectionToWrap.getTotalItems(), sourceEntity);
}
else
{
@@ -529,6 +527,20 @@ public class ResourceWebScriptHelper
}
}
private Object executeIncludedSource(Api api, String entityCollectionName, String uniqueEntityId, boolean includeSource)
{
if (includeSource)
{
ResourceWithMetadata res = locator.locateEntityResource(api, entityCollectionName, HttpMethod.GET);
if (res != null)
{
Object result = executeRelatedResource(api, null, uniqueEntityId, null, res);
if (result!=null && result instanceof ExecutionResult) return ((ExecutionResult) result).getRoot();
}
}
return null;
}
/**
* Loops through the embedded Resources and executes them. The results are added to list of embedded results used by
* the ExecutionResult object.

View File

@@ -50,6 +50,11 @@ public class ParamsExtender extends Params
return new ParamsExtender(entityId, null, null, null, null, new Params.RecognizedParams(null, null, null, rFilter, null, null, null, false));
}
public static Params valueOf(boolean includeSource, String entityId)
{
return new ParamsExtender(entityId, null, null, null, null, new Params.RecognizedParams(null, null, null, null, null, null, null, includeSource));
}
public static Params valueOf(Paging paging, String entityId)
{
return new ParamsExtender(entityId, null, null, null, null, new Params.RecognizedParams(null, paging, null, null, null, null, null, false));

View File

@@ -271,6 +271,24 @@ public class SerializeTests
assertTrue("There must be json output", StringUtils.isNotBlank(out));
}
@Test
public void testIncludeSource() throws IOException
{
ExecutionResult exec1 = new ExecutionResult(new Farmer("180"),null);
ExecutionResult exec2 = new ExecutionResult(new Farmer("456"), null);
CollectionWithPagingInfo<ExecutionResult> coll = CollectionWithPagingInfo.asPaged(null, Arrays.asList(exec1, exec2));
Object resultCollection = helper.processAdditionsToTheResponse(api,"sheep",ParamsExtender.valueOf(true,"1"),coll);
assertNotNull(resultCollection);
String out = writeResponse(resultCollection);
assertTrue("There must 'source' json output", StringUtils.contains(out, "\"source\":{\"name\":\"Dolly\",\"age\":3,\"sheepGuid\":\"1\"}"));
resultCollection = helper.processAdditionsToTheResponse(api,"sheep",ParamsExtender.valueOf(false,"1"),coll);
assertNotNull(resultCollection);
out = writeResponse(resultCollection);
assertFalse("There must not 'source' json output", StringUtils.contains(out, "\"source\":{\"name\":\"Dolly\",\"age\":3,\"sheepGuid\":\"1\"}"));
}
@Test
public void testExpandRecursiveRelations() throws IOException
{