Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

API - Fetch all DITA Map dependencies | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

API - Fetch all DITA Map dependencies by Techrevel Blog - Aanchal Sikka

Abstract

Sharing a code snippet that would help you retrieve all dependencies of a DITAMAP. This includes:
1. DITA Map itself
2. Associated Topics
3. Assets used in Topics like Images
In particular, following is the snippet fetching all dependencies

List mapDependents = MapUtilities.getAllDependencies(ditamapNode);

mapDependents.forEach(associatedAsset -> {
//associatedAsset is object representing DITA-Map/Topics/Images
});
Sharing a Servlet that retrieves all dependencies and returns info in json format.

package com.techrevel.dam.core.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.fmdita.api.maps.MapUtilities;
import com.day.cq.dam.api.DamConstants;
import com.google.gson.Gson;

@Component(service = Servlet.class, immediate = true, property = {
Constants.SERVICE_DESCRIPTION + "=Fetch all the referenced assets for a map",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=/bin/techrevel/fetchreferencedassets" })
public class FetchReferencedAssetsForMap2 extends SlingAllMethodsServlet {

private static final long serialVersionUID = 1L;

/** Default LOG. */
private static final Logger LOG = LoggerFactory.getLogger(FetchReferencedAssetsForMap2.class);
private static final String REQUEST_PARAM_PATH = "path";

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {

String requestedPath = request.getParameter(REQUEST_PARAM_PATH);
ResourceResolver resolver = request.getResourceResolver();

try {

Map<String, Object> resultMap = new HashMap<>();
Gson gson = new Gson();

Session session = resolver.adaptTo(Session.class);
Node ditamapNode = session.getNode(requestedPath);

List mapDependents = MapUtilities.getAllDependencies(ditamapNode);
List listOfDitaObject = new ArrayList<>();

mapDependents.forEach(associatedAsset -> {
listOfDitaObject.add(getAssetObject(resolver, associatedAsset));
});
resultMap.put("assets", listOfDitaObject);
String jsonFromMap = gson.toJson(resultMap);

response.getWriter().print(jsonFromMap);

} catch (IOException | RepositoryException e) {
LOG.error("Exception while fetching dependencies of DITA Map {} : {}", requestedPath, e);
} catch (Exception e) {
LOG.error("Exception while fetching dependencies of DITA Map {} : {}", requestedPath, e);
}
}

private AssetObject getAssetObject(ResourceResolver resolver, Node associatedAsset) {
AssetObject ditaObject = new AssetObject();
try {
Resource assetMetadataResource = resolver
.getResource(associatedAsset.getPath() + "/jcr:content/metadata");
if (null != assetMetadataResource) {
ValueMap valueMap = assetMetadataResource.getValueMap();

String title = valueMap.get(DamConstants.DC_TITLE, String.class);
if (title != null) {
ditaObject.setTitle(title);
} else {
// Fallback to filename if title is not set. Can happen with images…
ditaObject.setTitle(associatedAsset.getName());
}
}
ditaObject.setName(associatedAsset.getName());
ditaObject.setPath(associatedAsset.getPath());

} catch (RepositoryException e) {
LOG.error("Repository Exception while fetching asset details : {}", e.getMessage());
} catch (Exception e) {
LOG.error("Exception while fetching asset details : {}", e.getMessage());

}
return ditaObject;
}
}

class AssetObject {

String title;
String name;
String docState;
String path;
String checkedOutBy;

public String getCheckedOutBy() {
return checkedOutBy;
}
public void setCheckedOutBy(String checkedOutBy) {
this.checkedOutBy = checkedOutBy;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDocState() {
return docState;
}
public void setDocState(String docState) {
this.docState = docState;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}

Read Full Blog

API - Fetch all DITA Map dependencies

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies