Search code examples
springtemplatesalfrescoopencmis

How to find a template in Alfresco with OpenCMIS from Spring application?


I am working in a Spring 3.1 application and I need to find a String template document located in Alfresco's repository. I can already create a file in alfresco with OpenCMIS, but I couldn't figure out how to find a template, so if anyone knows how to do it or point me an example, please let me know, thanks in advance!


Solution

  • There are a number of options you can use. First of all, you need to have a criteria that uniquely identifies your document. Here below I'll show some, hopefully your case falls in one of them or they will inspire you towards a proper solution. The following uses pseudo code, please have a look to the OpenCMIS dev guide for working with the Java client API.

    BY ID

    Once you create a Document via CMIS, you get the unique ID of it that you can store in your application for later retrieval.

    Map<String, Object> templateProperties = createDocumentProperties();
    Folder folder = getTemplatesFolder();
    ObjectId templateId = createTemplateIn(folder);
    storeTemplateId(templateId.getId(), templateProperties); // persist the ID
    ...
    // later on
    ...
    String id = getTemplateId(); // retrieve the ID
    Session session = openCMISSession();
    Document template = (Document)session.getObject(id);
    

    BY PATH

    Similar to the previous example, you will have to take note of where you stored the document instead of its ID, or having a way to construct the path by hand.

    String path = getTemplatePath(); // either recover it from DB or construct a path
    Document template = (Document)session.getObjectByPath(path);
    

    BY PROPERTY VALUE

    Let's say you can use a specific metadata field on a template Document that allows you to easily retrieve it afterwards (e.g. you created some specific Alfresco metadata model for your use case).

    String meta = TemplateProperties.TEMPLATE_ID; // e.g. my:templateId
    String type = TemplateProperties.TEMPLATE_TYPE; // e.g. my:template
    String templateMeta = "TEMPLATE1";
    Map<String, Object> templateProperties = createDocumentProperties();
    templateProperties.put(meta, templateMeta);
    templateProperties.put(PropertyIds.OBJECT_TYPE_ID, type);
    createTemplate(templateProperties);
    ...
    // later on
    ...
    String type = TemplateProperties.TEMPLATE_TYPE; // e.g. my:template
    String meta = TemplateProperties.TEMPLATE_ID;
    String tplId = "TEMPLATE1";
    String query = String.format("SELECT * FROM % WHERE % = '%'", type, meta, tplId);
    ItemIterable<QueryResult> i = session.query(query, false);
    QueryResult qr = i.iterator().next(); // let's assume we have one single match
    String templateId = qr.getPropertyByQueryName("cmis:objectId").getFirstValue()
    Document template = (Document)session.getObject(templateId);
    

    BY QUERY

    The previous approach is not really tied to search by property name, and can be easily extended to use any kind of query that identifies your templates. Have a look at the Alfresco page on its CMIS query language implementation details to learn more ways of querying the repository.