Expand my Community achievements bar.

SOLVED

Programatically working with the DAM

Avatar

Level 6

Hi I have 2  questions around programatically.

 

1) I need to save files to the DAM.

 

I can save jpg files using the  code

 

public  String ToDam(InputStream is, String fileName, javax.jcr.Session session, String damLocation, String fileType,String addOrRemove) {
        try {
            javax.jcr.Node node = session.getNode(damLocation);
            javax.jcr.ValueFactory valueFactory = session.getValueFactory();
            javax.jcr.Binary contentValue = valueFactory.createBinary(is);
            javax.jcr.Node fileNode = node.addNode(fileName, "nt:file");
            fileNode.addMixin("mix:referenceable");
            javax.jcr.Node resNode = fileNode.addNode("jcr:content",
                    "nt:resource");
            if (addOrRemove.equals("add")) {
                resNode.setProperty("jcr:mimeType","image/jpeg");
                resNode.setProperty("jcr:data", contentValue);
            } else {
             //TODO
            }
            Calendar lastModified = Calendar.getInstance();
            lastModified.setTimeInMillis(lastModified.getTimeInMillis());
            resNode.setProperty("jcr:lastModified", lastModified);

            // Return the path to the document that was stored in CQ.
            System.out.println(fileNode.getPath());
            return fileNode.getPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

 

I have tried to save XML files using the above code with one change

 resNode.setProperty("jcr:mimeType","XML");

 

The code runs, but i cannot locate the xml file in the CQ5 DAM http://localhost:4502/damadmin#/content/dam.

 

2) how does one delete files from a DAM node in Java.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Clive,

Use the AssetManager API to work with DAM instead of directly dealing with the JCR Nodes.

1. To create a DAM resource

    com.day.cq.dam.api.AssetManager assetMgr = resourceResolver.adaptTo(com.day.cq.dam.api.AssetManager.class);

    assetMgr.createAsset(<attachmentPath>, <input stream>,<contentType>, true);

    for XML files, contentype = 'application/xml'

 

2. To delete the DAM resource

    com.adobe.granite.asset.api.AssetManager assetApiMgr = resourceResolver.adaptTo(com.adobe.granite.asset.api.AssetManager.class);

    assetApiMgr.removeAsset(<asset path>);

View solution in original post

8 Replies

Avatar

Level 4

Hi,

Add session.save() next to  resNode.setProperty("jcr:lastModified", lastModified); line and to delete fileNode.remove().

Thanks.

Vikram

Avatar

Level 6

Pls mention proper mimetype:  resNode.setProperty("jcr:mimeType","application/xml"); 

Avatar

Level 10

Here is a community article that talks about How To develop a Java client app that can upload multiple files to the AEM DAM. The use of the AssetManager is also in this article. The client developed is:

[img]MultipleSwingClient.png[/img]

Notice that each file that is uploaded is displayed in the client. 

AEM applies different renditions to the digital assets:

[img]DAMRen.png[/img]

All assets are stored in the DAM:

[img]DamFiles.png[/img]

Here is the article:

http://helpx.adobe.com/experience-manager/using/multiple-digital-assets.html

Avatar

Level 6

I think I would preffer to use the Asset class to do a lot of this stuff instead of manipulating the different nodes.

Or even the AssetHanlder http://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/dam/api/handler/AssetHandler.html

/Ove

Avatar

Correct answer by
Level 10

Hi Clive,

Use the AssetManager API to work with DAM instead of directly dealing with the JCR Nodes.

1. To create a DAM resource

    com.day.cq.dam.api.AssetManager assetMgr = resourceResolver.adaptTo(com.day.cq.dam.api.AssetManager.class);

    assetMgr.createAsset(<attachmentPath>, <input stream>,<contentType>, true);

    for XML files, contentype = 'application/xml'

 

2. To delete the DAM resource

    com.adobe.granite.asset.api.AssetManager assetApiMgr = resourceResolver.adaptTo(com.adobe.granite.asset.api.AssetManager.class);

    assetApiMgr.removeAsset(<asset path>);

Avatar

Level 10

For anyone reading this thread and wanting to perform this use case, see this AEM community article that talks about how to use the AssetManager API to programmatically upload files to the AEM DAM:

Uploading files to Adobe Experience Manager DAM using AssetManager API