December 2, 2021
Estimated Post Reading Time ~

Groovy Script for Updating countries nodes

import java.text.SimpleDateFormat;
import com.day.cq.dam.api.AssetManager;
import com.day.cq.dam.api.Asset;
import java.util.Iterator;
import com.day.cq.dam.api.Rendition;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

def filePath = "/content/dam/cleanup/input/country-drop-down_nl_NL.xlsx";  //***** Input file the Path *****/

def nodePath = "/etc/acs-commons/lists/countries/jcr:content/list" //***Country Node Path ****/

def sourceProp = "jcr:title.en";
def targetProp = "jcr:title.nl_NL";
def assetorPageNode = "jcr:content";
def countryNodeUpdateNum = 0;
def notFoundCount = 0;

assetManager = resourceResolver.adaptTo(AssetManager)
Resource res = resourceResolver.getResource(filePath);

Asset asset = res.adaptTo(Asset.class);

Rendition rendition = asset.getOriginal();

InputStream inputStream = rendition.adaptTo(InputStream.class);

XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet worksheet = workbook.getSheetAt(0);

Iterator<Row> itr = worksheet.iterator();

while (itr.hasNext()) {
def colCount = 0;

Row row = itr.next();

Iterator<Cell> cellIterator = row.cellIterator();
XSSFCell enTitleCell = row.getCell((short) 0);

def enTitleCellStr = ""
def noTitleCellStr = ""

//if (enTitleCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
enTitleCellStr = enTitleCell.getStringCellValue();
//}

XSSFCell noTitleCell = row.getCell((short) 1);

//if(noTitleCell.getCellType() == XSSFCell.CELL_TYPE_STRING){
noTitleCellStr = noTitleCell.getStringCellValue();;
// }

def nofoundNode = true;

if(noTitleCellStr.length() > 0){
getNode(nodePath).recurse { countryNode ->

if(countryNode.get(sourceProp)){

def sourcePropertyVal = countryNode.getProperty(sourceProp);

if(!sourcePropertyVal.isMultiple()){

sourcePropertyValStr = sourcePropertyVal.getString();

//println "sourcePropertyVal : " + sourcePropertyVal;

if(sourcePropertyValStr == enTitleCellStr){

//println sourcePropertyValStr.toString();

println "Found : " + enTitleCellStr;
countryNode.set(targetProp,noTitleCellStr);
session.save();

countryNodeUpdateNum++;
nofoundNode = false;
return true;
}
}
}
}

if(nofoundNode){
notFoundCount++;
println "Not Found : " + enTitleCellStr;

}
}
}

println "Total number of country nodes updated: "+countryNodeUpdateNum;
println "Not found countries: "+notFoundCount;



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.