Views
NodeRef cookbook
From alfrescowiki
The NodeRef object is a unique reference to a Node, the object that represents locations and resources within an Alfresco repository.
Custom Actions will provide you with a NodeRef of the resource or space that the action is acting on. Once you have it, though, what do you do with it?
This page is the opening index for a list of things that you can do with a NodeRef. If a question looks like "How do I use a NodeRef to..." then it probably belongs here. Hopefully some kindly developers will stop by and fill in some answers.
Some services are required, you can get them this way:
ServiceRegistry serviceRegistry = (ServiceRegistry) beanFactory.getBean(ServiceRegistry.SERVICE_REGISTRY);
NodeService nodeService = serviceRegistry.getNodeService();
ContentService contentService = serviceRegistry.getContentService();
FileFolderService fileFolderService = serviceRegistry.getFileFolderService();
Using NodeService
Getting a file name from a NodeRef
String fileName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
Reading a property of a node
The property may come from an aspect or not. You will probably want to cast to the appropriate type.
QName PROP_QNAME_MY_PROPERTY = QName.createQName("custom.model", "myProperty");
value = nodeService.getProperty(nodeRef, PROP_QNAME_MY_PROPERTY);
Updating a property of a node
The property may come from an aspect or not.
QName PROP_QNAME_MY_PROPERTY = QName.createQName("custom.model", "myProperty");
nodeService.setProperty(nodeRef, PROP_QNAME_MY_PROPERTY, value);
Getting the parent of a NodeRef
ChildAssociationRef childAssociationRef = nodeService.getPrimaryParent(nodeRef);
NodeRef parent = childAssociationRef.getParentRef();
Adding an aspect to a node
Supposing the "MyAspect" aspect defines a "myProperty" property in the "custom.model" namespace.
QName CUSTOM_ASPECT_QNAME = QName.createQName("custom.model", "MyAspect");
QName PROP_QNAME_MY_PROPERTY = QName.createQName("custom.model", "myProperty");
Map<QName,Serializable> aspectValues = new HashMap<QName,Serializable>();
aspectValues.put(PROP_QNAME_MY_PROPERTY, value);
nodeService.addAspect(nodeRef, CUSTOM_ASPECT_QNAME, aspectValues);
Checking whether a node has a given aspect
QName CUSTOM_ASPECT_QNAME = QName.createQName("custom.model", "MyAspect");
boolean hasAspect = nodeService.hasAspect(node, CUSTOM_ASPECT_QNAME);
Looping through children of a NodeRef
List<ChildAssociationRef> children = nodeService.getChildAssocs(companyHome);
for (ChildAssociationRef childAssoc : children) {
NodeRef childNodeRef = childAssoc.getChildRef();
// Use childNodeRef here.
}
Creating a child association between two existing NodeRef
QName PROP_QNAME_MY_CHILD_ASSOCIATION = QName.createQName("custom.model", "myChildAssociation");
nodeService.addChild(parentNodeRef, childNodeRef, PROP_QNAME_MY_CHILD_ASSOCIATION, PROP_QNAME_MY_CHILD_ASSOCIATION);
Creating an association between two NodeRef
QName PROP_QNAME_MY_ASSOCIATION = QName.createQName("custom.model", "myAssociation");
nodeService.createAssociation(sourceNodeRef, targetNodeRef, PROP_QNAME_MY_ASSOCIATION);
Setting the type of a node
QName PROP_QNAME_MY_TYPE = QName.createQName("custom.model", "myType");
nodeService.setType(finalOriginal, MY_TYPE);
Getting the MIME type of a node
QName PROP_QNAME_CONTENT = QName.createQName("http://www.alfresco.org/model/content/1.0", "content");
ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, PROP_QNAME_CONTENT);
String originalMimeType = contentData.getMimetype();
Adding a category to a node
ArrayList<NodeRef> categories = new ArrayList<NodeRef>(1);
categories.add(categoryNode);
if(!nodeService.hasAspect(targetNode, ContentModel.ASPECT_GEN_CLASSIFIABLE)
{
HashMap<QName, Serializable> props = new HashMap<QName, Serializable>();
props.put(ContentModel.PROP_CATEGORIES, categories);
nodeService.addAspect(targetNode, ContentModel.ASPECT_GEN_CLASSIFIABLE, props);
}
else
{
nodeService.setProperty(targetNode, ContentModel.PROP_CATEGORIES, categories);
}
Getting the categories of a node
List<NodeRef> categories = (List<NodeRef>) nodeService.getProperty(nodeRef, ContentModel.PROP_CATEGORIES);
Deleting a node for real (not recycle bin)
nodeService.addAspect(nodeRef, ContentModel.ASPECT_TEMPORARY, null);
nodeService.deleteNode(nodeRef);
Using ContentService
Reading the data content of a NodeRef (plain text)
ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
String content = reader.getContentString();
Reading the data content of a NodeRef (binary)
ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
InputStream originalInputStream = reader.getContentInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8; //1KiB buffer
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = originalInputStream.read(buffer)) > -1) {
outputStream.write(buffer, 0, bytesRead);
}
originalInputStream.close();
byte[] binaryData = outputStream.toByteArray();
Writing data to a node's content
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
writer.putContent(new ByteArrayInputStream(content));
Writing a file's data to a node's content
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
writer.setLocale(CONTENT_LOCALE);
File file = new File("c:/temp/images/BigCheese1.bmp");
writer.setMimetype("image/bmp");
writer.putContent(file);
Transforming a PPT to PDF
ContentReader pptReader = contentService.getReader(pptNodeRef, ContentModel.PROP_CONTENT);
ContentWriter pdfWriter = contentService.getWriter(pdfNodeRef, ContentModel.PROP_CONTENT, true);
ContentTransformer pptToPdfTransformer =
contentService.getTransformer(MimetypeMap.MIMETYPE_PPT, MimetypeMap.MIMETYPE_PDF);
pptToPdfTransformer.transform(pptReader, pdfWriter);
Note: It also works for many other file formats.
Using FileFolderService
Creating a PDF (or other document)
QName contentQName = QName.createQName("{http://www.alfresco.org/model/content/1.0}content");
FileInfo pdfInfo = fileFolderService.create(directory, filename, contentQName);
NodeRef pdf = pdfInfo.getNodeRef();
Note: The content is empty and has to be filled with the file's data, for instance like this.
Using WorkflowService
Starting a workflow instance for a node
NodeRef workflowPackage = workflowService.createPackage(null);
nodeService.addChild(workflowPackage, nodeRef, ContentModel.ASSOC_CONTAINS,
QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI,
QName.createValidLocalName(
(String)getNodeService().getProperty(nodeRef, ContentModel.PROP_NAME))));
Map<QName, Serializable> workflowProps = new HashMap<QName, Serializable>(16);
workflowProps.put(WorkflowModel.ASSOC_PACKAGE, workflowPackage);
WorkflowDefinition workflowDefinition = workflowService.getDefinitionByName("jbpm$myworkflow");
getWorkflowService().startWorkflow(workflowDefinition.getId(), workflowProps);
Getting the workflow instances running for a node
List<WorkflowInstance> workflowInstances = workflowService.getWorkflowsForContent(nodeRef, true);
Using PermissionService
Set permissions to a user on a node
permissionService.setPermission(nodeRef, "NameOfUser...", PermissionService.COORDINATOR, true);
Using RuleService and ActionService
Set Rule on a node
This sample add a rule on the nodeRef. This rule starts an action (which needs a parameter) but starts only if the new nodeRef (RuleType.INBOUND) is a content (Condition).
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.repo.action.evaluator.CompareMimeTypeEvaluator;
import org.alfresco.repo.action.evaluator.HasAspectEvaluator;
import org.alfresco.repo.action.evaluator.IsSubTypeEvaluator;
import org.alfresco.repo.action.executer.MoveActionExecuter;
import org.alfresco.repo.action.executer.SpecialiseTypeActionExecuter;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.action.CompositeAction;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.rule.Rule;
import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.service.cmr.rule.RuleType;
import org.alfresco.service.namespace.QName;
import org.alfresco.web.bean.actions.handlers.SpecialiseTypeHandler;
import org.alfresco.web.bean.actions.handlers.MoveHandler;
import org.apache.log4j.Logger;
...
// Add Rule
Rule rule = new Rule();
rule.setTitle("Rule...");
rule.setDescription("Make ....");
rule.applyToChildren(false);
rule.setExecuteAsynchronously(false);
rule.setRuleDisabled(false);
rule.setRuleType(RuleType.INBOUND);
CompositeAction compositeAction = actionService.createCompositeAction();
rule.setAction(compositeAction);
// Conditions for the Rule
Map<String, Serializable> actionMap = new HashMap<String, Serializable>();
actionMap.put(IsSubTypeEvaluator.PARAM_TYPE, ContentModel.TYPE_CONTENT);
compositeAction.addActionCondition(actionService.createActionCondition(IsSubTypeEvaluator.NAME, actionMap));
// Action
Action myAction= actionService.createAction("Name of my action...");
compositeAction.addAction(myAction);
// Parameters for the action
// See next sample...
// Save the rule
ruleService.saveRule(nodeRef, rule);
- Conditions for the Rule
- If you need other conditions than IsSubTypeEvaluator.NAME ("is-subtype") : look at org.alfresco.repo.action.evaluator.X
- Action
- If you need to use the Alfresco Actions : look at the source : org.alfresco.repo.action.executer.X
Set Rule on a node with action having parameters
This sample shows how to add rule with an action having parameters. An action having parameters has a "Handler" class. Next sample starts the Alfresco action "Move : Move item to a specific space".
// Add Rule
Rule rule = new Rule();
rule.setTitle("Rule...");
rule.setDescription("Make ....");
rule.applyToChildren(false);
rule.setExecuteAsynchronously(false);
rule.setRuleDisabled(false);
rule.setRuleType(RuleType.INBOUND);
CompositeAction compositeAction = actionService.createCompositeAction();
rule.setAction(compositeAction);
// Conditions for the Rule
Map<String, Serializable> actionMap = new HashMap<String, Serializable>();
actionMap.put(IsSubTypeEvaluator.PARAM_TYPE, ContentModel.TYPE_CONTENT);
compositeAction.addActionCondition(actionService.createActionCondition(IsSubTypeEvaluator.NAME, actionMap));
// Action
Action myAction= actionService.createAction(MoveActionExecuter.NAME);
compositeAction.addAction(myAction);
// Parameters for the action
Map<String, Serializable> actionProps = compositeAction.getParameterValues();
actionProps.put("destinationLocation", destNodeRef);
Map<String, Serializable> repoProps = compositeAction.getParameterValues();
(new MoveHandler()).prepareForSave(actionProps,repoProps);
action.setParameterValues(repoProps);
// Save the rule
ruleService.saveRule(nodeRef, rule);
- Parameters for the action
- the Alfresco parameters actions are stored at : org.alfresco.web.bean.actions.handlers.X. See here if you don't know the name of the param to use.
- If you use your own action (with parameters) you need to use your handler method : (new yourActionHandler).prepareForSave(...)
Obtaining a NodeRef
Getting a NodeRef from its path
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
ResultSet rs = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home/app:user_homes/sys:boris/cm:mypics\"");
NodeRef companyHomeNodeRef = null;
try
{
if (rs.length() == 0)
{
throw new AlfrescoRuntimeException("Didn't find Company Home");
}
companyHomeNodeRef = rs.getNodeRef(0);
}
finally
{
rs.close();
}