Step-by-step guide
For each found result/node, we have to do the following:
For each found result/node, we have to do the following:
- Get all the top level properties (Node root level). These include `jcr:created`, `jcr:createdBy`, etc.
- Get all `jcr:content` level properties. These include `cq:name`, `cq:lastModified`, etc
- Get all `jcr:content\metadata` level properties. These include `dc:title`, any custom metadata etc.
- You can add all these to another new `ValueMap` that can hold all the properties of a particular Node/Asset.
AEMGetAllPropertiesOfAnAsset
Note
Resource resource;
ValueMap mainProperties;
ValueMap assetMetadataProperties;
Resource metadataResource;
ValueMap jcrProperties;
Resource jcrdataResource;
ValueMap allProperties;
for (Hit hit : result.getHits()) {
//LOGGER.info("nHit path="+hit.getPath()+", title="+hit.getTitle()); resource = hit.getResource();
if(null!=resource){ mainProperties = resource.getValueMap();
// Add JCR Properties
jcrdataResource = resource.getChild("jcr:content");
jcrProperties = ResourceUtil.getValueMap(jcrdataResource);
// Add Metadata properties
metadataResource = resource.getChild("jcr:content/metadata");
assetMetadataProperties = ResourceUtil.getValueMap(metadataResource);
// Adding all together
allProperties = new ValueMapDecorator(new HashMap());
allProperties.putAll(hit.getProperties());
allProperties.putAll(mainProperties); // Includes jcr:created createdDate etc.
allProperties.put("jcr:path",hit.getPath()); //Add Path
allProperties.putAll(jcrProperties);
allProperties.putAll(assetMetadataProperties);
//LOGGER.debug("All Asset Properties="+new Gson().toJson(allProperties));
}
}
Note
- `jcr:path` is not returned by any of the above. So I had to explicitly add it using `hit.getPath`
- The name of the node or Asset name can be pulled from `hit.getTitle()`. Of course, this is also returned as part of `cq:name`.
- There are other ways to get properties as well. One another way is to get the `Node` and retrieve the properties. `com.day.cq.search.result.Hit` has a method `getNode()` that returns a `java.jcr.Node` interface and you can use that get to fetch the properties.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.