Automation in Java - Following Blob Upload Example

Hello, I was trying to follow the upload example listed here (http://doc.nuxeo.com/display/NXDOC/Using+Nuxeo+Automation+Client) and for the life of me I can not get the file to attach. I can get the document created, but when I browse to the created document, there's no attached files.

I've basically copied and pasted the code with a few modifications. I create the file using newFile method instead of the getFileToUpload method.

The newFile method, I believe I grabbed from unit test and was modified as follows:

static File newFile(String content) throws IOException {
                File file = File.createTempFile("automation-test-", ".xml");
                file.deleteOnExit();
                FileUtils.writeFile(file, content);
                return file;
            }

        Document root = (Document) session.newRequest("Document.Fetch").set(
                "value", "/").execute();
        session.newRequest("Document.Create").setInput(root).set("type", "File").set(
                "name", "myfile").set("properties", "dc:title=My File").execute();

        File file = newFile("<doc>mydoc</doc>");
        FileBlob fb = new FileBlob(file);
        fb.setMimeType("text/xml");
        session.newRequest("Blob.Attach").setHeader(
                Constants.HEADER_NX_VOIDOP, "true").setInput(fb)
                .set("document", "/myfile").execute();


        // Get the file document where blob was attached
        Document doc = (Document) session.newRequest(
                "Document.Fetch").setHeader(
                Constants.HEADER_NX_SCHEMAS, "*").set("value", "/myfile").execute();
        // get the file content property
        PropertyMap map = doc.getProperties().getMap("file:content");
        // get the data URL
        String path = map.getString("data");
        System.out.println(path);
        client.shutdown();

The output of the path does returns: files/84980d28-07e1-41e6-845f-6e72128307e0?path=%2Fcontent

Since I am able to retrieve file properties, does that mean the file is there? If so, how do I view it and why is not attached to the document? Am I missing something? Thanks!

0 votes

1 answers

3136 views

ANSWER

Update: I did see it the first time I ever ran the code but not subsequent executions. It appears that when you create a document with the same name as an existing one…it won't include the attachment. Even though I deleted the original document and cleared my list, future documents with the same name will not receive the attachments…but it will still create a document. That is confusing. Will the document name have to be randomized on the backend?
04/09/2012



By default, when you delete a document into the Nuxeo interface, you change the lifecycle state to delete => the document is not deleted. Into a Folderish document you can just create one document with a given name (I don't talk about title but the name the one you give at the creation time).

To delete definitively the document, you can do it through the UI you go to nearest workspace with the administrator account and you go to management tab > trash and remove it definitively.

Names are used to generate the path of your document (ex: /default-domain/workspaces/….).

  • default-domain is the name of the Default Domain document
  • workspaces is the name of the Workspaces document
  • etc.

If you create 2 documents with the same name nuxeo will automatically generate a different name during the creation time based of the wish name.

So in your case if there is already a document with a name “myfile”, nuxeo will create your document with a name “myfile.28374627346”. Like that your document will have an unique name. In your case the best way would be to catch the document returned by this line:

 Document doc = (Document) session.newRequest("Document.Create").setInput(root).set("type", "File").set(
            "name", "myfile").set("properties", "dc:title=My File").execute();

and get the name for the rest of your code or simply get the reference.

Hope this will answer to you.

0 votes