Obsolete Pages{{Obsolete}}
The official documentation is at: http://docs.alfresco.com
CookbookJava_APIDeveloper Guide
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();
String fileName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
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);
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);
ChildAssociationRef childAssociationRef = nodeService.getPrimaryParent(nodeRef);
NodeRef parent = childAssociationRef.getParentRef();
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);
QName CUSTOM_ASPECT_QNAME = QName.createQName('custom.model', 'MyAspect');
boolean hasAspect = nodeService.hasAspect(node, CUSTOM_ASPECT_QNAME);
List<ChildAssociationRef> children = nodeService.getChildAssocs(companyHome);
for (ChildAssociationRef childAssoc : children) {
NodeRef childNodeRef = childAssoc.getChildRef();
// Use childNodeRef here.
}
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);
QName PROP_QNAME_MY_ASSOCIATION = QName.createQName('custom.model', 'myAssociation');
nodeService.createAssociation(sourceNodeRef, targetNodeRef, PROP_QNAME_MY_ASSOCIATION);
QName PROP_QNAME_MY_TYPE = QName.createQName('custom.model', 'myType');
nodeService.setType(finalOriginal, MY_TYPE);
ContentData contentData = (ContentData) nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
String originalMimeType = contentData.getMimetype();
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);
}
List<NodeRef> categories = (List<NodeRef>) nodeService.getProperty(nodeRef, ContentModel.PROP_CATEGORIES);
nodeService.addAspect(nodeRef, ContentModel.ASPECT_TEMPORARY, null);
nodeService.deleteNode(nodeRef);
ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
String content = reader.getContentString();
ContentReader reader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
InputStream originalInputStream = reader.getContentInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final int BUF_SIZE = 1
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
writer.putContent(new ByteArrayInputStream(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);
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.
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.
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);
List<WorkflowInstance> workflowInstances = workflowService.getWorkflowsForContent(nodeRef, true);
permissionService.setPermission(nodeRef, 'NameOfUser...', PermissionService.COORDINATOR, true);
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);
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);
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();
}