|
Project Information
Members
Featured
Downloads
Wiki pages
Links
|
Alfresco OpenCMIS ExtensionIn Alfresco, aspects are a fundamental concept related to content modeling. Aspects allow the addition of behaviors to existing content types. The CMIS specification does not include aspects in its scope, but it does provide extension points that allow additional functionality. CMIS extensions are XML fragments placed in different parts of a CMIS object. The Alfresco aspect fragments are documented on the Alfresco Wiki. So, they are available to all CMIS clients out there including OpenCMIS. However, programming with CMIS extensions can be complex, and can require quite a lot of code. OpenCMIS does all the XML parsing for you but since it knows nothing about aspects, it can not provide simple, elegant interfaces. That's where the "Alfresco OpenCMIS Extension" steps in. It seamlessly merges the Alfresco aspect properties with the CMIS object properties and provides interfaces to get, add, and remove aspects. It does this by replacing the OpenCMIS internal object factory with an object factory that is aspect-aware. It processes and adds Alfresco aspect fragments for you, behind the scenes, eliminating complexity in your code. Using the extensionThe Alfresco OpenCMIS Extension depends on the Apache Chemistry OpenCMIS libraries. You can download the latest OpenCMIS client libraries from the Apache Chemistry website. Then download the latest Alfresco OpenCMIS extension package and add the jars to your classpath. If you are using Maven, follow the instructions on the Maven Wiki page. You do not need to be modify OpenCMIS in order to use a different object factory. You just set an additional session parameter to change the object factory class, as shown in the following code fragment:- Map<String, String> parameter = new HashMap<String, String>(); // Set the user credentials parameter.put(SessionParameter.USER, "admin"); parameter.put(SessionParameter.PASSWORD, "admin"); // Specify the connection settings parameter.put(SessionParameter.ATOMPUB_URL, "http://localhost:8080/alfresco/service/cmis"); parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value()); // Set the alfresco object factory parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl"); // Create a session SessionFactory factory = SessionFactoryImpl.newInstance(); Session session = factory.getRepositories(parameter).get(0).createSession(); Now your code can access and update all aspect properties through the standard OpenCMIS interfaces. Creating a document or folder with aspectsAspects can be specified when creating a document or a folder. In order to create an object, the property "cmis:objectTypeId" must be set to a valid CMIS object type id. With the Alfresco OpenCMIS Extension installed, the OpenCMIS library accepts a comma-separated list of type ids. The first type id in that list must be the object type id. The following type ids must be aspect type ids. Aspect properties can be set for all aspects in the list. The following code fragment shows an example of creating a document with one aspect:- Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.NAME, "doc1");
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document,P:cm:titled");
properties.put("cm:description", "My document");
Document doc = session.getRootFolder().createDocument(properties, null, null);This creates a document without content in the root folder, with the titled aspect applied and the description property set. Adding, removing and discovering aspectsTo add and remove aspects to an existing object, you must cast OpenCMIS Document objects to AlfrescoDocument objects and Folder objects to AlfrescoFolder objects. These two classes provide the following additional methods: Methods for checking if an aspect is applied:- boolean hasAspect(String id); boolean hasAspect(ObjectType type); A method to retrieve the currently applied aspects:- Collection<ObjectType> getAspects(); A method to find the aspect type for a given property id ObjectType findAspect(String propertyId); Methods to add and remove aspects void addAspect(String... id); void addAspect(ObjectType... type); void removeAspect(String... id); void removeAspect(ObjectType... type); The following code fragment adds an aspect to an existing object, checks if the object has a second aspect, and updates the description property if it does:- Document doc = (Document) session.getObject(...);
AlfrescoDocument alfDoc = (AlfrescoDocument) doc;
alfDoc.addAspect("P:cm:taggable");
if(alfDoc.hasAspect("P:cm:titled")) {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("cm:description", "My taggable document");
alfDoc.updateProperties(properties);
}Note the format of the aspect type "P:cm:titled". In Alfresco, all aspect types visible through CMIS are prefixed with "P:", document types are prefixed with "D:" and folder types are prefixed with "F:". |