Modify DuplicateFileDocument java to copy image?

3
The Community Commons module includes java that can be used to duplicate FileDocument objects. Great, but unfortunately, it doesn't work with images since they also have a thumbnail. Although the image itself gets copied if I use the DuplicateFileDocument java, the thumbnail does not. I am not very well versed in java, but I suspect it shouldn't be too hard to modify the java that comes with the Community Commons module to include the thumbnail as well. Could anyone help me do this?
asked
2 answers
5

Ok the complete solution

    public static Boolean duplicateImage(IContext context, IMendixObject toClone, IMendixObject target) throws Exception
{
    if (toClone == null || target == null)
        throw new Exception("No file to clone or to clone into provided");

    MendixBoolean hasContents = (MendixBoolean) toClone.getMember(context, FileDocument.MemberNames.HasContents.toString());
    if (!hasContents.getValue(context))
        return false;

    InputStream inputStream = Core.getImage(context, toClone, false); 

    Core.storeImageDocumentContent(context, target, inputStream, 200, 200);

    return true;
}

Invoke from java action

    // BEGIN USER CODE
    return Misc.duplicateImage(this.getContext(), __fileToClone, __cloneTarget);
    // END USER CODE
answered
4

Easily done using this piece of code:

Core.storeImageDocumentContent(context, imageDocument, inputStream, thumbnailWidth, thumbnailHeight);

You can open the CommunityUtils.Misc, copy the duplicateFileDocument and replace line 62 with the above (make sure to fill in the actual parameters) and create a new Java action like the filedocument one, only it receives an Image and calls on your new 'duplicateImageDocument'.

Edit: Oh btw, you also might want to switch the inputstream to the image variety, but this should be easy to find using Eclipse with Core.get + <ctrl+space> and using the autosuggestions.

answered