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.
// 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 write content to a node.
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.
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.
// 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.
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);
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});