-
Book Overview & Buying
-
Table Of Contents
Alfresco Developer Guide
Step-by-step examples for working with the various Alfresco APIs are provided throughout the book. But the examples are limited in scope to the specific requirement at hand. This section shows how to perform the same functional task (for example, "Create an object") using the three major APIs: Foundation, JavaScript, and Web Services.
These examples show how to create a new node in the repository.
ChildAssociationRef association = nodeService.createNode(companyHome, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX, name), ContentModel.TYPE_CONTENT, contentProps);
var newNode = space.createNode(nodeName, contentType);
// Construct CML statement to create content node
CMLCreate createDoc = new CMLCreate("ref1", docParent, null, null, null, Constants.createQNameString(SomeCoModel.NAMESPACE_SOMECO_CONTENT_MODEL, getContentType()), contentProps);
// Construct CML Block
CML cml = new CML();
cml.setCreate(new CMLCreate[] {createDoc});
// Execute CML Block
UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml); These examples show how to execute a Lucene search.
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"); ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, queryString);
var queryResults = search.luceneSearch(queryString);
Query query = new Query(Constants.QUERY_LANG_LUCENE, queryString ); QueryResult queryResult = getRepositoryService().query(getStoreRef(), query, false);
These examples show how to write content to a node.
ContentWriter writer = contentService.getWriter(content, ContentModel.PROP_CONTENT, true);
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.setEncoding("UTF-8");
String text = "The quick brown fox jumps over the lazy dog";
writer.putContent(text);
var newDoc = targetFolder.createFile(filename); newDoc.properties.content.write(content); newDoc.properties.content.mimetype = mimetype; newDoc.save();
ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8");
String docText = "This is a sample " + getContentType() + " document called " + getContentName();
Content docContentRef = contentService.write(docRef, Constants.PROP_CONTENT, docText.getBytes(), contentFormat);
These examples show how to add an aspect to a node.
nodeService.addAspect(nodeRef, aspectQName, props);
document.addAspect(aspectQName, props);
CMLAddAspect addWebableAspectToDoc = new CMLAddAspect(Constants.createQNameString(SomeCoModel.NAMESPACE_SOMECO_CONTENT_MODEL, SomeCoModel.ASPECT_SC_WEBABLE), null, null, "ref1");
// Construct CML Block
CML cml = new CML();
cml.setUpdate(new CMLUpdate[] {updateDoc});
cml.setAddAspect(new CMLAddAspect[] {addWebableAspectToDoc});
// Execute CML Block
UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);These examples set a single-value property on a node.
nodeService.setProperty(nodeRef, propertyQName, value);
document.properties["sc:published"] = new Date(); document.save();
// Create a reference to the doc to be updated
Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
Reference doc = new Reference(storeRef, this.targetUuid, null);
// Create an array of NamedValue objects with the props to set
NamedValue nameValue = Utils.createNamedValue(Constants.PROP_NAME, this.contentName);
NamedValue[] contentProps = new NamedValue[] {nameValue};
// Construct CML statement to update node
CMLUpdate updateDoc = new CMLUpdate(contentProps, new Predicate(new Reference[] {doc}, storeRef, null), null);
// Construct CML Block
CML cml = new CML();
cml.setUpdate(new CMLUpdate[] {updateDoc});
// Execute CML Block
UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);These examples grant the custom PortalPublisher permission created in Chapter 9 to the Publisher group for a specific node.
permissionService.setPermission(nodeRef, "GROUP_Publisher", "PortalPublisher", true);
document.setPermission("PortalPublisher", "GROUP_Publisher")
Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
Reference doc = new Reference(storeRef, this.targetUuid, null);
ACE ace = new ACE("GROUP_Publisher", "PortalPublisher", AccessStatus.acepted);
WebServiceFactory.getAccessControlService().addACEs(new Predicate(new Reference[] {doc}, storeRef, null), new ACE[] {ace});
These examples start the out of the box "adhoc" workflow, assign it to tuser1 and give it a description. In 2.2 Enterprise, there is no workflow API for JavaScript for Web Services. To work around that, the code simply executes the start-workflow action.
Map<QName, Serializable> workflowParameters = new HashMap<QName,Serializable>();
workflowParameters.put(QName.createQName("bpm", "assignee"), "tuser1");
workflowParameters.put(QName.createQName("bpm", "description"), "Started from Foundation");
workflowService.startWorkflow("jbpm$wf:adhoc", workflowParameters);
var workflow = actions.create("start-workflow");
workflow.parameters.workflowName = "jbpm$wf:adhoc";
workflow.parameters["bpm:workflowDescription"] = "Workflow from JavaScript";
workflow.parameters["bpm:assignee"] = "tuser1";
workflow.execute(document);
Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
Reference doc = new Reference(storeRef, this.targetUuid, null);
NamedValue workflowValue = Utils.createNamedValue("workflowName", this.workflow);
NamedValue descriptionValue = Utils.createNamedValue("bpm:workflowDescription", "Submitted from web service");
NamedValue assigneeValue = Utils.createNamedValue("bpm:assignee", this.assignee);
NamedValue[] actionArguments = new NamedValue[] {workflowValue, descriptionValue, assigneeValue};
Action startWorkflowAction = new Action(null, this.targetUuid, "start-workflow", null, null, actionArguments, null, null, null);
WebServiceFactory.getActionService().executeActions(new Predicate(new Reference[] {doc}, storeRef, null), new Action[] {startWorkflowAction});
Change the font size
Change margin width
Change background colour