DEV Community

jerinraj5555
jerinraj5555

Posted on • Edited on

Replication in AEM(Adobe Experience Manager)-Part 2

In my previous post, I have explained Replication in detail. In this post, I will explain how to activate/deactivate the content(asset and web page) by reading the path from an excel file.

  • Once I get the path of the excel file from the servlet then need to check if the path exists in the system or not.
  • If the file exists in the asset folder then convert it as a Workbook and read the row and column.
  • If the data in the cell is valid and the path exists then the page and assets paths are replicated one by one.
       String path = "/content/dam/sample.xls"
    Session session = resolver.adaptTo(Session.class);
    if (session.itemExists(path)) {
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(asset.getOriginal().getStream());
// Getting the Sheet at index
Sheet sheet = workbook.getSheetAt(sheetNo);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
for (Row row : sheet) {
    Cell cell =row.getCell(columnId);
    String cellValue = dataFormatter.formatCellValue(cell);                  
      if(StringUtils.isNotBlank(cellValue) && 
               (cellValue.startsWith("/content")&& 
                  session.itemExists(cellValue))) {
        replicateContent(cellValue,session,isReplicate);
    activatePageAssets(cellValue,session,isReplicate);
}
}
}


Enter fullscreen mode Exit fullscreen mode
  • The function replicateContent will accept session instance, web page link, and status for identifying which operation to be performed.
private void replicateContent( String path,Session session,boolean isReplicate) 
     {

    try {
            if (isReplicate) {
                replicator.replicate(session, ReplicationActionType.ACTIVATE, path);
            } else {
                replicator.replicate(session, ReplicationActionType.DEACTIVATE, path);
            }

    } catch (ReplicationException e) {
            e.printStackTrace();
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Next, we want to publish the assets as well right? so we need to find the path of the assets associated with the page.
  • The following code snippet will return the path of the assets associated with each page.
AssetReferenceSearch search = new AssetReferenceSearch 
    (jcrNode, DAM_ROOT, request.getResourceResolver()); 
Map<String,Asset> result = search.search();
result.keySet()
Enter fullscreen mode Exit fullscreen mode
  • Once we get the path of the asset (result.keySet()) then we will iterate one by one and replicate the asset by calling replicateContent( String path,Session session,boolean isReplicate) .

If you think there is a more efficient way to achieve the same, please leave a note in the comments. Happy coding๐Ÿ˜Š!

Top comments (0)