ALF-2620: Fixed problem with the paths being used in rendition path template model. "cwd" is now a file/display path and "companyHome" is a TemplateNode.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@20037 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Brian Remmington
2010-04-28 15:58:18 +00:00
parent 8bf1c1042c
commit aa06314b72
6 changed files with 86 additions and 55 deletions

View File

@@ -597,7 +597,7 @@ public class RenditionServiceIntegrationTest extends BaseAlfrescoSpringTest
RenditionDefinition definition = makeReformatAction(ContentModel.TYPE_CONTENT, RenditionDefinition definition = makeReformatAction(ContentModel.TYPE_CONTENT,
MimetypeMap.MIMETYPE_TEXT_PLAIN); MimetypeMap.MIMETYPE_TEXT_PLAIN);
Serializable targetFolderName = nodeService.getProperty(testTargetFolder, ContentModel.PROP_NAME); Serializable targetFolderName = nodeService.getProperty(testTargetFolder, ContentModel.PROP_NAME);
String path = "${companyHome}/" + targetFolderName +"/test.txt"; String path = "${companyHome.name}/" + targetFolderName +"/test.txt";
definition.setParameterValue(RenditionService.PARAM_DESTINATION_PATH_TEMPLATE, path); definition.setParameterValue(RenditionService.PARAM_DESTINATION_PATH_TEMPLATE, path);
// Perform the action with an explicit destination folder // Perform the action with an explicit destination folder

View File

@@ -35,19 +35,17 @@ import org.alfresco.repo.template.TemplateNode;
import org.alfresco.service.ServiceRegistry; import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.model.FileFolderService; import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo; import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.model.FileNotFoundException;
import org.alfresco.service.cmr.rendition.RenditionDefinition; import org.alfresco.service.cmr.rendition.RenditionDefinition;
import org.alfresco.service.cmr.rendition.RenditionService; import org.alfresco.service.cmr.rendition.RenditionService;
import org.alfresco.service.cmr.rendition.RenditionServiceException; import org.alfresco.service.cmr.rendition.RenditionServiceException;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.Path;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.repository.TemplateException; import org.alfresco.service.cmr.repository.TemplateException;
import org.alfresco.service.cmr.search.ResultSet; import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService; import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.util.FreeMarkerUtil; import org.alfresco.util.FreeMarkerUtil;
import org.alfresco.util.XMLUtil; import org.alfresco.util.XMLUtil;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@@ -100,9 +98,7 @@ public class StandardRenditionLocationResolverImpl implements RenditionLocationR
{ {
NodeRef companyHome = getCompanyHomeNode(sourceNode.getStoreRef()); NodeRef companyHome = getCompanyHomeNode(sourceNode.getStoreRef());
NodeService nodeService = serviceRegistry.getNodeService(); String path = renderPathTemplate(pathTemplate, sourceNode, tempRenditionLocation, companyHome);
Serializable companyHomeName = nodeService.getProperty(companyHome, ContentModel.PROP_NAME);
String path = renderPathTemplate(pathTemplate, sourceNode, tempRenditionLocation, companyHomeName);
if(path!=null) if(path!=null)
{ {
return findOrCreateTemplatedPath(sourceNode, path, companyHome); return findOrCreateTemplatedPath(sourceNode, path, companyHome);
@@ -157,14 +153,39 @@ public class StandardRenditionLocationResolverImpl implements RenditionLocationR
return new RenditionLocationImpl(parent, child, fileName); return new RenditionLocationImpl(parent, child, fileName);
} }
private String renderPathTemplate(String pathTemplate, NodeRef sourceNode, NodeRef tempRenditionLocation, Serializable companyHomeName) private String renderPathTemplate(String pathTemplate, NodeRef sourceNode, NodeRef tempRenditionLocation, NodeRef companyHome)
{ {
NodeService nodeService = serviceRegistry.getNodeService(); NodeService nodeService = serviceRegistry.getNodeService();
NamespaceService namespaceService = serviceRegistry.getNamespaceService(); FileFolderService fileFolderService = serviceRegistry.getFileFolderService();
final Map<String, Object> root = new HashMap<String, Object>(); final Map<String, Object> root = new HashMap<String, Object>();
ChildAssociationRef sourceAssoc = nodeService.getPrimaryParent(sourceNode);
String fullSourceName = sourceAssoc.getQName().getLocalName(); List<FileInfo> sourcePathInfo;
String fullSourceName;
String cwd;
try
{
//Since the root of the store is typically not a folder, we use company home as the root of this tree
sourcePathInfo = fileFolderService.getNamePath(companyHome, sourceNode);
//Remove the last element (the actual source file name)
FileInfo sourceFileInfo = sourcePathInfo.remove(sourcePathInfo.size() - 1);
fullSourceName = sourceFileInfo.getName();
StringBuilder cwdBuilder = new StringBuilder("/");
for (FileInfo file : sourcePathInfo)
{
cwdBuilder.append(file.getName());
cwdBuilder.append('/');
}
cwd = cwdBuilder.toString();
}
catch (FileNotFoundException e)
{
log.warn("Failed to resolve path to source node: " + sourceNode + ". Default to Company Home");
fullSourceName = nodeService.getPrimaryParent(sourceNode).getQName().getLocalName();
cwd = "/";
}
String trimmedSourceName = fullSourceName; String trimmedSourceName = fullSourceName;
String sourceExtension = ""; String sourceExtension = "";
int extensionIndex = fullSourceName.lastIndexOf('.'); int extensionIndex = fullSourceName.lastIndexOf('.');
@@ -175,16 +196,11 @@ public class StandardRenditionLocationResolverImpl implements RenditionLocationR
.substring(extensionIndex + 1); .substring(extensionIndex + 1);
} }
Path sourcePath = nodeService.getPath(sourceNode);
StoreRef store = sourceNode.getStoreRef();
getCompanyHomeNode( store);
root.put("name", trimmedSourceName); root.put("name", trimmedSourceName);
root.put("extension", sourceExtension); root.put("extension", sourceExtension);
root.put("date", new SimpleDate(new Date(), SimpleDate.DATETIME)); root.put("date", new SimpleDate(new Date(), SimpleDate.DATETIME));
root.put("cwd", sourcePath.toPrefixString(namespaceService)); root.put("cwd", cwd);
root.put("companyHome", companyHomeName); root.put("companyHome", new TemplateNode(companyHome, serviceRegistry, null));
root.put("sourceNode", new TemplateNode(sourceNode, serviceRegistry, null)); root.put("sourceNode", new TemplateNode(sourceNode, serviceRegistry, null));
root.put("sourceContentType", nodeService.getType(sourceNode).getLocalName()); root.put("sourceContentType", nodeService.getType(sourceNode).getLocalName());
root.put("renditionContentType", nodeService.getType(tempRenditionLocation).getLocalName()); root.put("renditionContentType", nodeService.getType(tempRenditionLocation).getLocalName());
@@ -214,7 +230,7 @@ public class StandardRenditionLocationResolverImpl implements RenditionLocationR
{ {
if (log.isDebugEnabled()) if (log.isDebugEnabled())
{ {
log.debug("Processing " + pathTemplate + " using source node " + sourcePath); log.debug("Processing " + pathTemplate + " using source node " + cwd + fullSourceName);
} }
result = serviceRegistry.getTemplateService().processTemplateString("freemarker", pathTemplate, result = serviceRegistry.getTemplateService().processTemplateString("freemarker", pathTemplate,
new SimpleHash(root)); new SimpleHash(root));

View File

@@ -145,13 +145,14 @@ public class StandardRenditionLocationResolverTest extends BaseAlfrescoSpringTes
assertTrue("Folder " + fooPath + " should not exist!", childAssocs.isEmpty()); assertTrue("Folder " + fooPath + " should not exist!", childAssocs.isEmpty());
QName renditionKind = QName.createQName(NamespaceService.APP_MODEL_1_0_URI, "test"); QName renditionKind = QName.createQName(NamespaceService.APP_MODEL_1_0_URI, "test");
NodeRef sourceNode = makeNode(companyHome, ContentModel.TYPE_CONTENT); NodeRef sourceFolder = makeNode(companyHome, ContentModel.TYPE_FOLDER);
NodeRef sourceNode = makeNode(sourceFolder, ContentModel.TYPE_CONTENT);
NodeRef tempRenditionNode = makeNode(companyHome, ContentModel.TYPE_CONTENT); NodeRef tempRenditionNode = makeNode(companyHome, ContentModel.TYPE_CONTENT);
RenditionDefinition renditionDef = renditionService.createRenditionDefinition(renditionKind, RenditionDefinition renditionDef = renditionService.createRenditionDefinition(renditionKind,
"nicks_test_engine"); "nicks_test_engine");
String pathTemplate = barPath + "/nick.xml"; String pathTemplate = barPath + "${cwd}nick.xml";
renditionDef.setParameterValue(RenditionService.PARAM_DESTINATION_PATH_TEMPLATE, pathTemplate); renditionDef.setParameterValue(RenditionService.PARAM_DESTINATION_PATH_TEMPLATE, pathTemplate);
RenditionLocation location = RenditionLocation location =
@@ -159,8 +160,9 @@ public class StandardRenditionLocationResolverTest extends BaseAlfrescoSpringTes
NodeRef fooNode = checkFolder(fooName, companyHome, "Foo"); NodeRef fooNode = checkFolder(fooName, companyHome, "Foo");
NodeRef barNode = checkFolder(barName, fooNode, "Bar"); NodeRef barNode = checkFolder(barName, fooNode, "Bar");
NodeRef finalFolderNode = checkFolder(nodeService.getPrimaryParent(sourceFolder).getQName(), barNode, "Final Folder");
assertEquals("Bar is not the rendition parent!", barNode, location.getParentRef()); assertEquals("Final folder is not the rendition parent!", finalFolderNode, location.getParentRef());
assertEquals("nick.xml", location.getChildName()); assertEquals("nick.xml", location.getChildName());
} }
@@ -202,7 +204,7 @@ public class StandardRenditionLocationResolverTest extends BaseAlfrescoSpringTes
Map<QName, Serializable> props = new HashMap<QName, Serializable>(); Map<QName, Serializable> props = new HashMap<QName, Serializable>();
props.put(ContentModel.PROP_NAME, uuid); props.put(ContentModel.PROP_NAME, uuid);
ChildAssociationRef assoc = nodeService.createNode(parent, ContentModel.ASSOC_CONTAINS, QName.createQName( ChildAssociationRef assoc = nodeService.createNode(parent, ContentModel.ASSOC_CONTAINS, QName.createQName(
NamespaceService.APP_MODEL_1_0_URI, uuid), nodeType, props); NamespaceService.CONTENT_MODEL_1_0_URI, uuid), nodeType, props);
return assoc.getChildRef(); return assoc.getChildRef();
} }

View File

@@ -32,6 +32,7 @@ import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.action.executer.ActionExecuterAbstractBase; import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.rendition.RenderingEngineDefinitionImpl; import org.alfresco.repo.rendition.RenderingEngineDefinitionImpl;
import org.alfresco.repo.rendition.RenditionDefinitionImpl;
import org.alfresco.service.cmr.action.Action; import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionDefinition; import org.alfresco.service.cmr.action.ActionDefinition;
import org.alfresco.service.cmr.action.ParameterDefinition; import org.alfresco.service.cmr.action.ParameterDefinition;
@@ -297,9 +298,8 @@ public abstract class AbstractRenderingEngine extends ActionExecuterAbstractBase
protected void executeImpl(Action action, NodeRef sourceNode) protected void executeImpl(Action action, NodeRef sourceNode)
{ {
checkParameterValues(action); checkParameterValues(action);
checkActionIsRenditionDefinition(action); RenditionDefinition renditionDefinition = checkActionIsRenditionDefinition(action);
checkSourceNodeExists(sourceNode); checkSourceNodeExists(sourceNode);
RenditionDefinition renditionDefinition = (RenditionDefinition) action;
ChildAssociationRef renditionAssoc = createRenditionNodeAssoc(sourceNode, renditionDefinition); ChildAssociationRef renditionAssoc = createRenditionNodeAssoc(sourceNode, renditionDefinition);
QName targetContentProp = getRenditionContentProperty(renditionDefinition); QName targetContentProp = getRenditionContentProperty(renditionDefinition);
@@ -352,13 +352,15 @@ public abstract class AbstractRenderingEngine extends ActionExecuterAbstractBase
/** /**
* @param action * @param action
*/ */
protected void checkActionIsRenditionDefinition(Action action) protected RenditionDefinition checkActionIsRenditionDefinition(Action action)
{ {
if (action instanceof RenditionDefinition == false) if (action instanceof RenditionDefinition)
{ {
String msg = "Cannot execute action as it is not a RenditionDefinition: " + action; return (RenditionDefinition)action;
logger.warn(msg); }
throw new RenditionServiceException(msg); else
{
return new RenditionDefinitionImpl(action);
} }
} }

View File

@@ -31,7 +31,6 @@ import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ActionImpl;
import org.alfresco.repo.action.executer.ActionExecuter; import org.alfresco.repo.action.executer.ActionExecuter;
import org.alfresco.repo.rendition.RenditionDefinitionImpl; import org.alfresco.repo.rendition.RenditionDefinitionImpl;
import org.alfresco.repo.rendition.executer.AbstractRenderingEngine.RenderingContext; import org.alfresco.repo.rendition.executer.AbstractRenderingEngine.RenderingContext;
@@ -122,20 +121,6 @@ public class AbstractRenderingEngineTest extends TestCase
assertEquals(contentPropName, props.get(ContentModel.PROP_CONTENT_PROPERTY_NAME)); assertEquals(contentPropName, props.get(ContentModel.PROP_CONTENT_PROPERTY_NAME));
} }
public void testCheckActionIsRenditionDefinition()
{
ActionImpl action = new ActionImpl(null, "actionId", TestRenderingEngine.NAME);
try
{
engine.executeImpl(action, source);
fail("Should have thrown an exception here!");
} catch(RenditionServiceException e)
{
String expMsg = "02160000 Cannot execute action as it is not a RenditionDefinition: Action[ id=actionId, node=null ]";
assertEquals(expMsg, e.getMessage());
}
}
public void testCheckSourceNodeExists() public void testCheckSourceNodeExists()
{ {
when(nodeService.exists(any(NodeRef.class))).thenReturn(false); when(nodeService.exists(any(NodeRef.class))).thenReturn(false);
@@ -146,8 +131,7 @@ public class AbstractRenderingEngineTest extends TestCase
fail("Should have thrown an exception here!"); fail("Should have thrown an exception here!");
} catch(RenditionServiceException e) } catch(RenditionServiceException e)
{ {
String expMsg = "02160001 Cannot execute action as node does not exist: http://test/sourceId"; assertTrue(e.getMessage().endsWith("Cannot execute action as node does not exist: http://test/sourceId"));
assertEquals(expMsg, e.getMessage());
} }
} }
@@ -201,8 +185,7 @@ public class AbstractRenderingEngineTest extends TestCase
fail("Should throw an Exception if default value is null!"); fail("Should throw an Exception if default value is null!");
} catch(RenditionServiceException e) } catch(RenditionServiceException e)
{ {
String msg = "02160002 The defaultValue cannot be null!"; assertTrue(e.getMessage().endsWith("The defaultValue cannot be null!"));
assertEquals(msg, e.getMessage());
} }
// Check wrong type of default value throws exception. // Check wrong type of default value throws exception.
@@ -212,8 +195,7 @@ public class AbstractRenderingEngineTest extends TestCase
fail("Should throw an exception if default value is of incoorect type!"); fail("Should throw an exception if default value is of incoorect type!");
} catch (RenditionServiceException e) } catch (RenditionServiceException e)
{ {
String msg = "02160003 The parameter: Some-param must be of type: java.lang.Booleanbut was of type: java.lang.String"; assertTrue(e.getMessage().endsWith("The parameter: Some-param must be of type: java.lang.Booleanbut was of type: java.lang.String"));
assertEquals(msg, e.getMessage());
} }
} }
@@ -249,8 +231,7 @@ public class AbstractRenderingEngineTest extends TestCase
fail("Should throw an exception if type is wrong!"); fail("Should throw an exception if type is wrong!");
} catch(RenditionServiceException e) } catch(RenditionServiceException e)
{ {
String msg = "02160004 The parameter: Some param must be of type: java.lang.Booleanbut was of type: java.lang.String"; assertTrue(e.getMessage().endsWith("The parameter: Some param must be of type: java.lang.Booleanbut was of type: java.lang.String"));
assertEquals(msg, e.getMessage());
} }
// Check throws an exception if value is of wrong type. // Check throws an exception if value is of wrong type.
@@ -260,8 +241,7 @@ public class AbstractRenderingEngineTest extends TestCase
fail("Should throw an exception if type is wrong!"); fail("Should throw an exception if type is wrong!");
} catch(RenditionServiceException e) } catch(RenditionServiceException e)
{ {
String msg = "02160005 The class must not be null!"; assertTrue(e.getMessage().endsWith("The class must not be null!"));
assertEquals(msg, e.getMessage());
} }
} }

View File

@@ -112,6 +112,37 @@ public class XSLTRenderingEngineTest extends BaseAlfrescoSpringTest
} }
public void testSimplestTemplateWithTargetPath() throws Exception
{
try
{
FileInfo file = createXmlFile(companyHome);
FileInfo xslFile = createXmlFile(companyHome, verySimpleXSLT);
RenditionDefinition def = renditionService.createRenditionDefinition(QName.createQName("Test"), XSLTRenderingEngine.NAME);
def.setParameterValue(XSLTRenderingEngine.PARAM_TEMPLATE_NODE, xslFile.getNodeRef());
def.setParameterValue(RenditionService.PARAM_DESTINATION_PATH_TEMPLATE, "output/path/for/rendition/output.txt");
ChildAssociationRef rendition = renditionService.render(file.getNodeRef(), def);
assertNotNull(rendition);
assertEquals(2, nodeService.getParentAssocs(rendition.getChildRef()).size());
ContentReader reader = contentService.getReader(rendition.getChildRef(), ContentModel.PROP_CONTENT);
assertNotNull(reader);
String output = reader.getContentString();
log.debug("XSLT Processor output: " + output);
assertEquals("Avocado DipBagels, New York StyleBeef Frankfurter, Quarter PoundChicken Pot PieCole SlawEggsHazelnut SpreadPotato ChipsSoy Patties, GrilledTruffles, Dark Chocolate", output);
}
catch (Exception ex)
{
log.error("Error!", ex);
fail();
}
}
public void testParseXMLDocument() throws Exception public void testParseXMLDocument() throws Exception
{ {
try try