Search code examples
javaalfrescoopencmisapache-chemistry

Create a new version of a document if document already exists in Alfresco using Chemistry CMIS


I am trying to create a document using Chemistry CMIS as shown below

final Map<String, Object> reportProps = new HashMap<String, Object>();
        reportProps.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
        reportProps.put(PropertyIds.NAME,file.getName());

session.getFolder().createDocument(reportProps, contentStream, VersioningState.MAJOR);

If a document with the same name already exists it will throw CmisContentAlreadyExistsException.

I want to create a new version of the document if this throws this exception.

Or is there a way where i can check whether the document with the given name already exists in Alfresco repository using Chemistry CMIS so that i can get the document in anyway and checkin the document with a new version.

Any other approach is welcome.


Solution

  • I usually check whether document already exists, and if it does I go for the update. I do not go for the check out/check in process, since I setup Alfresco to create versions for each update (but I guess both approaches would work).

    I'm not too experienced with CMIS, but I do remember this article talking about your use case.

    http://ecmarchitect.com/archives/2013/08/26/3528

    Document document = null;
    try {
      document = parentFolder.createDocument(props, contentStream, null);
      System.out.println("Created new document: " + document.getId());
    } catch (CmisContentAlreadyExistsException ccaee) {
      document = (Document) cmisSession.getObjectByPath(parentFolder.getPath() + "/" + fileName);
      System.out.println("Document already exists: " + fileName);
    }
    return document;