0

I have created one Web Application using Servlets and JSP. Through that I have connected to alfresco repository. I am also able be to upload document in Alfresco and view document in external web application.

Now my requirement is, I have to give checkin and checkout option to those documents.

I found below rest apis for this purpuse. But I am not getting how to use these apis in servlets to full-fill my requirment.

POST /alfresco/service/slingshot/doclib/action/cancel-checkout/site/{site}/{container}/{path}


POST /alfresco/service/slingshot/doclib/action/cancel-checkout/node/{store_type}/{store_id}/{id}

Can anyone please provide the simple steps or some piece of code to do this task?

Thanks in advance.

Jeff Potts
  • 10,468
  • 17
  • 40
Deepak Talape
  • 997
  • 1
  • 9
  • 35
  • 1
    How are you connecting to Alfresco? – Gagravarr Aug 29 '16 at 11:47
  • 1
    did you test your rest API ?!? it always good to do it before starting coding – Yagami Light Aug 29 '16 at 12:15
  • Hi Gagravarr, I am connecting to alfresco using Java CMIS – Deepak Talape Aug 29 '16 at 12:20
  • Hi Yagami, here alfresco itself providing rest API for checkout and checkin. But i am not getting how and where should i use this api. Thats the reason Asked this question. – Deepak Talape Aug 29 '16 at 12:22
  • 2
    Ok i understand your problem, we will work with steps first step did you download the Chimestry openCMIS library and includ it in your project – Yagami Light Aug 29 '16 at 12:24
  • Yes, i have downloaded it. And also i am able to upload document repository and able to change document properties. I am also able to create folder in repository. And i also have Document node ref, now i just want to edit that document, and after editing i have to upload rendition copy of that document to repository. – Deepak Talape Aug 29 '16 at 12:26
  • 1
    you can work with an other version that mean it's the same document but the content is a little bit different you can see it in the ID of the document (wich version you work if it's 1.0 it's the first if it's 1.1 it's the second) – Yagami Light Aug 29 '16 at 12:33
  • 1
    please use @ notation to send me a notification if you comment @deeps – Yagami Light Aug 29 '16 at 12:34

1 Answers1

4

Please do not use the internal slingshot URLs for this. Instead, use OpenCMIS from Apache Chemistry. It will save you a lot of time and headaches and it is more portable to other repositories besides Alfresco.

The example below grabs an existing document by path, performs a checkout, then checks in a new major version of the plain text document.

package com.someco.cmis.examples;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class CheckoutCheckinExample {
    private String serviceUrl = "http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom"; // Uncomment for Atom Pub binding
    private Session session = null;

    public static void main(String[] args) {
        CheckoutCheckinExample cce = new CheckoutCheckinExample();
        cce.doExample();
    }

    public void doExample() {
        Document doc = (Document) getSession().getObjectByPath("/test/test-plain-1.txt");
        String fileName = doc.getName();
        ObjectId pwcId = doc.checkOut(); // Checkout the document
        Document pwc = (Document) getSession().getObject(pwcId); // Get the working copy

        // Set up an updated content stream
        String docText = "This is a new major version.";
        byte[] content = docText.getBytes();
        InputStream stream = new ByteArrayInputStream(content);
        ContentStream contentStream = session.getObjectFactory().createContentStream(fileName, Long.valueOf(content.length), "text/plain", stream);

        // Check in the working copy as a major version with a comment
        ObjectId updatedId = pwc.checkIn(true, null, contentStream, "My new version comment");
        doc = (Document) getSession().getObject(updatedId);
        System.out.println("Doc is now version: " + doc.getProperty("cmis:versionLabel").getValueAsString());
    }

    public Session getSession() {

        if (session == null) {
            // default factory implementation
            SessionFactory factory = SessionFactoryImpl.newInstance();
            Map<String, String> parameter = new HashMap<String, String>();

            // user credentials
            parameter.put(SessionParameter.USER, "admin"); // <-- Replace
            parameter.put(SessionParameter.PASSWORD, "admin"); // <-- Replace

            // connection settings
            parameter.put(SessionParameter.ATOMPUB_URL, this.serviceUrl); // Uncomment for Atom Pub binding
            parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value()); // Uncomment for Atom Pub binding

            List<Repository> repositories = factory.getRepositories(parameter);

            this.session = repositories.get(0).createSession();
        }
        return this.session;
    }
}

Note that on the version of Alfresco I tested with (5.1.e) the document must already have the versionable aspect applied for the version label to get incremented, otherwise the checkin will simply override the original.

Jeff Potts
  • 10,468
  • 17
  • 40
  • I used this code, But here you are replacing previous document content with new string. ObjectId updatedId = pwc.checkIn(true, null, contentStream, "My new version comment"); But in my requirement i want to upload new document copy from custom web application. so how can i use file object or input stream instead of String. – Deepak Talape Aug 30 '16 at 07:49
  • Hi all, above solution is very good, but i am getting problem checkIn() method. Actually i am editing the file offline and uploading new file. so i have direct file object or i can use input stream. But in checkIn() method last parameter is String. So how can i do. please reply – Deepak Talape Aug 30 '16 at 09:53
  • 2
    The String that says "My new version comment" is the checkin comment, not the content. The updated content is in the previous block where the docText is declared and then converted to an InputStream. So if you have a new file to check in, just get its InputStream like you would in any other Java code, then use it to create the ContentStream as shown. – Jeff Potts Aug 30 '16 at 14:41
  • Ok, Got it. Thank you so much – Deepak Talape Aug 30 '16 at 15:50
  • Hi, Here In versioning I am automatically 2.0 after 1.0 But I am also looking for minor version like 1.1 after 1.0 How can i achieve this with above code. Please help – Deepak Talape Sep 07 '16 at 10:29
  • 1
    That first boolean argument to the checkIn method tells the server to create a major version. Use false instead to create a minor version. – Jeff Potts Sep 07 '16 at 16:09