Automation Client - create new document version

Hi, i'm using Nuxeo Automation Client to upload documents. Java platform (1.6 update 35), only text files at the moment.

  1. I want to check if a document already exists
  2. If the doc doesn't exist i want to create the first version for it
  3. If the document already exists i want to “update” it (i mean: create a new version and upload the new content).

To check for the document on the repository i use the following code:

doc = (Document) session.newRequest(DocumentService.FetchDocument).setHeader(Constants.HEADER_NX_SCHEMAS, "*").set("value", workingPath + "/" + fileName).execute();

It throws an exception if the doc doesn't exist, so in my catch block i create it this way:

session.newRequest(DocumentService.CreateDocument).setInput(customFolder).set("type", "File").set("name", fileName).set("properties", props).execute();
session.newRequest(DocumentService.SetBlob).setHeader(Constants.HEADER_NX_VOIDOP, "true").setInput(fb).set("document", workingPath + "/" + fileName).execute();

where “fb” is my FileBlob containing the file content. It works for new files (already tested it), but feel free to give me advice if it could be improved some way… :)

The problem i have is to create the new version and upload the new content on the repository. The check in the first code block doesn't throw any exception if the doc exist, so i know when i should create a new version of the doc BUT i'm having a hard time figuring out how to do that… :D

Can anyone pls help me?

( Sorry in advance for my poor english :-) )

0 votes

3 answers

3695 views

ANSWER



I'll put this here for future reference: feel free to check it and give any suggestion, tnx in advance. :-)

Just a note: one of the problems i had was i couldn't create versions… checking the source code i discovered why. In the docs about the CreateVersion operation, it says possible values for “increment” paramenter are “none”, “minor” and “major” BUT in the source code i found

if ("Minor".equals(snapshot)) {
        vo = VersioningOption.MINOR;
    } else if ("Major".equals(snapshot)) {
        vo = VersioningOption.MAJOR;
    } else {
        vo = null;
    }

so, the correct param values are “Minor” and “Major”… a small “issue” but it could be more effective to say it (VersionIncrement.MAJOR, VersionIncrement.MINOR) in the docs… pls don't kill me, it's just a suggestion! :-D

    package my.test.nuxeo;

public class TestWriteFile {

    private static Logger logger = Logger.getLogger(TestWriteFile.class);

    /**
     * @param args
     */
    public static void main(String[] args) {
        FileInputStream in = null;
        try {
            logger.debug("TestWriteFile START");

            HttpAutomationClient client = new HttpAutomationClient(NuxeoCfg.URL_HTTP_AUTOMATION_CLIENT);
            Session session = client.getSession(NuxeoCfg.NUXEO_USERNAME, NuxeoCfg.NUXEO_PASSWORD);

            String fileName = "test_2013.04.13_2.txt";
            String fileDescription = "This is a Nuxeo Automation Client test.";

            String filePath = "C:/" + fileName;
            FileInputStream fis = new FileInputStream(filePath);
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();

            // Get the nuxeo-SIB workspace
            Document root = (Document) session.newRequest(DocumentService.FetchDocument).set("value", NuxeoCfg.NUXEO_DIR_PATH).execute();
            String customFolderName = "testWithNuxeoAutomationClient";
            String workingPath = NuxeoCfg.NUXEO_DIR_PATH + "/" + customFolderName;
            Document customFolder;
            try {
                customFolder = (Document) session.newRequest(DocumentService.FetchDocument).set("value", workingPath).execute();
                // customerFolder exists
            } catch (Exception e) {
                // customerFolder doesn't exist -> Create a Folder on nuxeo
                session.newRequest(DocumentService.CreateDocument).setInput(root).set("type", "Folder").set("name", customFolderName).set("properties", "dc:title=" + customFolderName).execute();
                customFolder = (Document) session.newRequest(DocumentService.FetchDocument).set("value", workingPath).execute();
            }

            // Create temp file
            File file = new File(fileName);
            // Write file content
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bytes);
            fos.close();

            FileBlob fb = new FileBlob(file);
            fb.setMimeType(FileBlob.getMimeTypeFromExtension(fileName));

            Document doc;
            // Check if doc exists, create it if it doesn't (-> catch Exception)
            try {

                doc = (Document) session.newRequest(DocumentService.FetchDocument).setHeader(Constants.HEADER_NX_SCHEMAS, "*").set("value", workingPath + "/" + fileName).execute();

                logger.warn("WARNING: file already exists, file ID: " + doc.getProperties().getMap("file:content").getString("data") + " -> Update it...");

                PropertyMap props = new PropertyMap();
                props.set("dc:title", fileName);
                props.set("dc:description", fileDescription);
                session.newRequest(DocumentService.SetBlob).setHeader(Constants.HEADER_NX_VOIDOP, "true").setInput(fb).set("document", doc.getId()).execute();
                session.newRequest(DocumentService.CreateVersion).setInput(doc).set("increment", VersionIncrement.MAJOR).execute();

            } catch (Exception e) {
                logger.warn(e.getMessage(), e);

                // doc doesn't exist -> create it

                PropertyMap props = new PropertyMap();
                props.set("dc:title", fileName);
                props.set("dc:description", fileDescription);

                // Create a File on nuxeo
                doc = (Document) session.newRequest(DocumentService.CreateDocument).setInput(customFolder).set("type", "File").set("name", fileName).set("properties", props).execute();
                // Uploading a file will return null since we used HEADER_NX_VOIDOP
                session.newRequest(DocumentService.SetBlob).setHeader(Constants.HEADER_NX_VOIDOP, "true").setInput(fb).set("document", workingPath + "/" + fileName).execute();
                // Create new version
                session.newRequest(DocumentService.CreateVersion).setInput(doc).set("increment", VersionIncrement.MAJOR).execute();
            }


            doc = (Document) session.newRequest(DocumentService.FetchDocument).setHeader(Constants.HEADER_NX_SCHEMAS, "*").set("value", workingPath + "/" + fileName).execute();                

            // get the file content property
            PropertyMap map = doc.getProperties().getMap("file:content");
            // get the data URL
            String fileID = map.getString("data");
            logger.debug("Upload complete, file ID: " + fileID);

            client.shutdown();

            logger.debug("TestWriteFile END");
        } catch (Exception e) {
            logger.error("TestWriteFile ERROR: " + e.getMessage(), e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {}
            }
        }
    }

}

Hope it could help someone with my same problem! :-)

0 votes



Thanks for your feedback (we're going to ignore case;) - https://jira.nuxeo.com/browse/NXP-11449 )
04/22/2013


I'm a damn newbye… :-)

I'm really sorry but i don't understand how the CreateVersion works. I mean, i suppose i've to upload the file into a document, but how i say to the DMS that this document is another version of an already existing document? Maybe i'm drowning in an inch of water… :-(

What's the automation chain to add a new version of a document on the DMS?

( Tnx @ Fred :-) )

0 votes



Hi!

It seems you are looking for the CreateVersion operation ;)

Document.CreateVersion

Fred

0 votes