Copying a document to a new object

6
How can I get a physical document (.doc), which is linked to an existing object, copied and linked to a new object (using microflow)? If this is not possible with microflows, how can it be done it using Java?
asked
4 answers
6

Using a java action with Origin and Destination as FileDocument parameters:

InputStream in  = Core.getFileDocumentContent(getContext(), __Origin);
File dest = Core.getFileDocumentContentAsFile(getContext(), __Destination);
OutputStream out = new FileOutputStream(dest);
org.apache.commons.io.IOUtils.copy(in, out);
in.close();
out.close();
Destination.setHasContents(true);
answered
5

See an example of this in our Java API tutorial at https://world.mendix.com/display/howto25/Java+API+Tutorial

answered
1

You can use the Misc.duplicatieFileDocument function which is provided in the community commons module.

answered
0

I had similar issues last week, trying to copy the contents of old FileDocuments into new ones.

To copy the document and link it to a new object, create that new object, as well as a System.FileDocument or specialization of it for the copied document. Set the association between the two objects, and copy any 'regular' attributes (such as Name) to the new Filedocument in the microflow. The slightly tricky part of course is the copying of binary data, for which Michel provided me a Java action. (I'm going by the assumption here that the document you want to copy is a System.Filedocument or specialization) This Java action should have two parameters, both FileDocuments; the original FileDocument and the one you are copying to. Code for the Java action in the user code area:

        try {
            InputStream in  = Core.getFileDocumentContent(getContext(), __Origin);
            File dest = Core.getFileDocumentContentAsFile(getContext(), __Destination);
            OutputStream out = new FileOutputStream(dest);
            org.apache.commons.io.IOUtils.copy(in, out);
            in.close();
            out.close();
            Destination.setHasContents(true);
        }
        catch(Exception e) {
            Core.getLogger(this.toString()).warn("Error while copying ", e);
            throw e;
        }
        return true;

Names of the parameters here are Origin for the original document, and Destination for the new document you are copying to.

answered