Programatically working with the DAM | Community
Skip to main content
October 16, 2015
Solved

Programatically working with the DAM

  • October 16, 2015
  • 8 replies
  • 10264 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Lokesh_Shivalingaiah

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>);

8 replies

vikramca06
October 16, 2015

Hi,

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

Thanks.

Vikram

October 16, 2015

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

smacdonald2008
October 16, 2015

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

October 16, 2015

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

Lokesh_Shivalingaiah
Lokesh_ShivalingaiahAccepted solution
October 16, 2015

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>);

October 16, 2015

Thank you

smacdonald2008
October 16, 2015

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

October 4, 2019