Refactored RenditionService JS API to use ScriptNodes instead of ChildAssociations.

Consistent with the existing JavaScript APIs.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@19207 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2010-03-10 20:16:41 +00:00
parent 36cdb669b1
commit a3a95cef81
2 changed files with 34 additions and 58 deletions

View File

@@ -18,16 +18,14 @@
*/ */
package org.alfresco.repo.rendition.script; package org.alfresco.repo.rendition.script;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.alfresco.repo.jscript.BaseScopableProcessorExtension; import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.jscript.ChildAssociation;
import org.alfresco.repo.jscript.ScriptNode; import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.repo.jscript.ValueConverter;
import org.alfresco.service.ServiceRegistry; import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.rendition.RenditionDefinition; import org.alfresco.service.cmr.rendition.RenditionDefinition;
import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@@ -44,8 +42,6 @@ public class ScriptRenditionService extends BaseScopableProcessorExtension
/** The Services registry */ /** The Services registry */
private ServiceRegistry serviceRegistry; private ServiceRegistry serviceRegistry;
private final ValueConverter valueConverter = new ValueConverter();
/** /**
* Set the service registry * Set the service registry
* *
@@ -68,10 +64,10 @@ public class ScriptRenditionService extends BaseScopableProcessorExtension
* rendition definition. * rendition definition.
* @param sourceNode the source node to be rendered. * @param sourceNode the source node to be rendered.
* @param renditionDefshortQName the rendition definition to be used e.g. cm:doclib * @param renditionDefshortQName the rendition definition to be used e.g. cm:doclib
* @return the parent association of the rendition node. * @return the rendition scriptnode.
* @see org.alfresco.service.cmr.rendition.RenditionService#render(org.alfresco.service.cmr.repository.NodeRef, RenditionDefinition) * @see org.alfresco.service.cmr.rendition.RenditionService#render(org.alfresco.service.cmr.repository.NodeRef, RenditionDefinition)
*/ */
public ChildAssociation render(ScriptNode sourceNode, String renditionDefshortQName) public ScriptNode render(ScriptNode sourceNode, String renditionDefshortQName)
{ {
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {
@@ -85,31 +81,36 @@ public class ScriptRenditionService extends BaseScopableProcessorExtension
RenditionDefinition rendDef = loadRenditionDefinitionImpl(renditionDefshortQName); RenditionDefinition rendDef = loadRenditionDefinitionImpl(renditionDefshortQName);
ChildAssociationRef result = this.serviceRegistry.getRenditionService().render(sourceNode.getNodeRef(), rendDef); ChildAssociationRef result = this.serviceRegistry.getRenditionService().render(sourceNode.getNodeRef(), rendDef);
NodeRef renditionNode = result.getChildRef();
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {
StringBuilder msg = new StringBuilder(); StringBuilder msg = new StringBuilder();
msg.append("Rendition: ").append(result); msg.append("Rendition: ").append(renditionNode);
logger.debug(msg.toString()); logger.debug(msg.toString());
} }
return (ChildAssociation)valueConverter.convertValueForScript(serviceRegistry, this.getScope(), null, result); return new ScriptNode(renditionNode, serviceRegistry);
} }
/** /**
* This method gets all the renditions of the specified node. * This method gets all the renditions of the specified node.
* *
* @param node the source node * @param node the source node
* @return a list of parent associations for the renditions. * @return an array of the renditions.
* @see org.alfresco.service.cmr.rendition.RenditionService#getRenditions(org.alfresco.service.cmr.repository.NodeRef) * @see org.alfresco.service.cmr.rendition.RenditionService#getRenditions(org.alfresco.service.cmr.repository.NodeRef)
*/ */
public ChildAssociation[] getRenditions(ScriptNode node) public ScriptNode[] getRenditions(ScriptNode node)
{ {
List<ChildAssociationRef> renditions = this.serviceRegistry.getRenditionService().getRenditions(node.getNodeRef()); List<ChildAssociationRef> renditions = this.serviceRegistry.getRenditionService().getRenditions(node.getNodeRef());
ChildAssociation[] result = convertChildAssRefs(renditions); ScriptNode[] results = new ScriptNode[renditions.size()];
for (int i = 0; i < renditions.size(); i++)
return result; {
results[i] = new ScriptNode(renditions.get(i).getChildRef(), serviceRegistry);
}
return results;
} }
/** /**
@@ -120,37 +121,20 @@ public class ScriptRenditionService extends BaseScopableProcessorExtension
* @param node the source node for the renditions * @param node the source node for the renditions
* @param mimeTypePrefix a prefix to check against the rendition MIME-types. * @param mimeTypePrefix a prefix to check against the rendition MIME-types.
* This must not be null and must not be an empty String * This must not be null and must not be an empty String
* @return a list of parent associations for the filtered renditions. * @return an array of the filtered renditions.
* @see org.alfresco.service.cmr.rendition.RenditionService#getRenditions(org.alfresco.service.cmr.repository.NodeRef) * @see org.alfresco.service.cmr.rendition.RenditionService#getRenditions(org.alfresco.service.cmr.repository.NodeRef)
*/ */
public ChildAssociation[] getRenditions(ScriptNode node, String mimeTypePrefix) public ScriptNode[] getRenditions(ScriptNode node, String mimeTypePrefix)
{ {
List<ChildAssociationRef> renditions = this.serviceRegistry.getRenditionService().getRenditions(node.getNodeRef(), mimeTypePrefix); List<ChildAssociationRef> renditions = this.serviceRegistry.getRenditionService().getRenditions(node.getNodeRef(), mimeTypePrefix);
ChildAssociation[] result = convertChildAssRefs(renditions); ScriptNode[] results = new ScriptNode[renditions.size()];
for (int i = 0; i < renditions.size(); i++)
return result;
}
/**
* This method converts a list of (Java-ish) ChildAssociationRefs to an array
* of (JavaScript-ish) ChildAssociations.
* @param renditions
* @return
*/
private ChildAssociation[] convertChildAssRefs(
List<ChildAssociationRef> renditions)
{
List<ChildAssociation> childAssocs = new ArrayList<ChildAssociation>(renditions.size());
for (ChildAssociationRef chAssRef : renditions)
{ {
ChildAssociation chAss = (ChildAssociation)valueConverter.convertValueForScript(serviceRegistry, getScope(), null, chAssRef); results[i] = new ScriptNode(renditions.get(i).getChildRef(), serviceRegistry);
childAssocs.add(chAss);
} }
ChildAssociation[] result = childAssocs.toArray(new ChildAssociation[0]); return results;
return result;
} }
/** /**
@@ -162,11 +146,11 @@ public class ScriptRenditionService extends BaseScopableProcessorExtension
* @return the parent association for the rendition or <code>null</code> if there is no such rendition. * @return the parent association for the rendition or <code>null</code> if there is no such rendition.
* @see org.alfresco.service.cmr.rendition.RenditionService#getRenditionByName(org.alfresco.service.cmr.repository.NodeRef, QName) * @see org.alfresco.service.cmr.rendition.RenditionService#getRenditionByName(org.alfresco.service.cmr.repository.NodeRef, QName)
*/ */
public ChildAssociation getRenditionByName(ScriptNode node, String renditionName) public ScriptNode getRenditionByName(ScriptNode node, String renditionName)
{ {
QName qname = QName.createQName(renditionName, serviceRegistry.getNamespaceService()); QName qname = QName.createQName(renditionName, serviceRegistry.getNamespaceService());
ChildAssociationRef result = this.serviceRegistry.getRenditionService().getRenditionByName(node.getNodeRef(), qname); ChildAssociationRef result = this.serviceRegistry.getRenditionService().getRenditionByName(node.getNodeRef(), qname);
return (ChildAssociation) valueConverter.convertValueForScript(serviceRegistry, getScope(), null, result); return result == null ? null : new ScriptNode(result.getChildRef(), serviceRegistry);
} }
} }

View File

@@ -3,35 +3,27 @@ function testRenderNodeUsingRenditionDefinitionNames()
// Produce two different renditions of the same source node // Produce two different renditions of the same source node
var renditionDefName1 = "cm:doclib"; var renditionDefName1 = "cm:doclib";
var renditionDefName2 = "cm:imgpreview"; var renditionDefName2 = "cm:imgpreview";
var childAssoc1 = renditionService.render(testSourceNode, renditionDefName1); var rendition1 = renditionService.render(testSourceNode, renditionDefName1);
var childAssoc2 = renditionService.render(testSourceNode, renditionDefName2); var rendition2 = renditionService.render(testSourceNode, renditionDefName2);
// Assert that the returned ChildAssociation objects have the correct name and type. test.assertNotNull(rendition1, "rendition1 was null.");
test.assertNotNull(childAssoc1, "Rendition ChildAssoc1 was null."); // Renditions created under the source node will be 'hiddenRenditions'.
test.assertEquals(testSourceNode.id, childAssoc1.parent.id, "Rendition 1's parent was not source node"); test.assertTrue(rendition1.hasAspect("rn:hiddenRendition"));
test.assertEquals("{http://www.alfresco.org/model/rendition/1.0}rendition", childAssoc1.type);
test.assertEquals("{http://www.alfresco.org/model/content/1.0}doclib", childAssoc1.name);
test.assertNotNull(childAssoc2, "Rendition ChildAssoc2 was null."); test.assertNotNull(rendition2, "rendition2 was null.");
test.assertEquals(testSourceNode.id, childAssoc2.parent.id, "Rendition 2's parent was not source node"); test.assertTrue(rendition2.hasAspect("rn:hiddenRendition"));
test.assertEquals("{http://www.alfresco.org/model/rendition/1.0}rendition", childAssoc2.type);
test.assertEquals("{http://www.alfresco.org/model/content/1.0}imgpreview", childAssoc2.name);
} }
function testGetRenditions() function testGetRenditions()
{ {
// Get all renditions // Get all renditions
var allRenditionAssocs = renditionService.getRenditions(testSourceNode); var allRenditions = renditionService.getRenditions(testSourceNode);
test.assertNotNull(allRenditionAssocs, "allRenditions returned null."); test.assertNotNull(allRenditions, "allRenditions returned null.");
test.assertEquals(2, allRenditionAssocs.length); test.assertEquals(2, allRenditions.length);
// We've already checked the types and names, so there's no point rechecking.
// Get named renditions // Get named renditions
var doclibRendition = renditionService.getRenditionByName(testSourceNode, "cm:doclib"); var doclibRendition = renditionService.getRenditionByName(testSourceNode, "cm:doclib");
test.assertNotNull(doclibRendition, "doclibRendition returned null."); test.assertNotNull(doclibRendition, "doclibRendition returned null.");
test.assertEquals("{http://www.alfresco.org/model/content/1.0}doclib", doclibRendition.name);
var noSuchRendition = renditionService.getRenditionByName(testSourceNode, "cm:nonsense"); var noSuchRendition = renditionService.getRenditionByName(testSourceNode, "cm:nonsense");
test.assertNull(noSuchRendition, "noSuchRendition should have been null."); test.assertNull(noSuchRendition, "noSuchRendition should have been null.");