Created
May 15, 2019 19:29
-
-
Save hemanth415/918bcfde876850b08252f2d18d8b9796 to your computer and use it in GitHub Desktop.
AEM Script to list out all page asset references [V2]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package custom; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.node.ArrayNode; | |
import com.fasterxml.jackson.databind.node.ObjectNode; | |
import com.fasterxml.jackson.databind.node.ValueNode; | |
import org.apache.commons.io.FileUtils; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.commons.lang3.StringUtils; | |
import org.apache.commons.lang3.math.NumberUtils; | |
import org.apache.poi.ss.usermodel.Row; | |
import org.apache.poi.ss.usermodel.Sheet; | |
import org.apache.poi.ss.usermodel.Workbook; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.nio.charset.StandardCharsets; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.TreeMap; | |
import java.util.TreeSet; | |
/** | |
* Created by hemanth on 2019-04-23 | |
*/ | |
public class AssetPageReferences { | |
public static void main(String[] args) throws Exception { | |
List<String> inputData = IOUtils.readLines(FileUtils.openInputStream(new File("/Users/hemanth/temp-253471.csv")), StandardCharsets.UTF_8.name()); | |
ProcessBuilder process; | |
Process p; | |
List<Map<String, String>> finalDataset = new ArrayList<>(); | |
Set<String> header = new TreeSet<>(); | |
Map<String, String> map; | |
for (String input : inputData) { | |
String[] inputSpilt = input.split(","); | |
process = new ProcessBuilder( | |
("curl -u admin:admin --insecure GET https://localhost:4502/bin/wcm/references.json?path=" + new URI(null, null, inputSpilt[1], null).getRawPath()).split(" ")); | |
try { | |
p = process.start(); | |
String jsonString = IOUtils.toString(p.getInputStream(), StandardCharsets.UTF_8.name()); | |
map = new TreeMap<>(); | |
map.put("status",inputSpilt[0]); | |
map.put("assetPath",inputSpilt[1]); | |
if (StringUtils.isNotBlank(jsonString)){ | |
ArrayNode arrayNode = (ArrayNode) new ObjectMapper().readTree(jsonString).get("pages"); | |
for (int i = 0; i < arrayNode.size(); i++) { | |
addKeys("pages", arrayNode.get(i), map); | |
} | |
if (map.size() > header.size()) { | |
header = map.keySet(); | |
} | |
} else { | |
//map.put(input, "ERROR"); | |
} | |
finalDataset.add(map); | |
//map.forEach((k, v) -> System.out.print(k + "," + v)); | |
//System.out.println(System.lineSeparator()); | |
//break; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
writeToXLSXFile("/Users/hemanthponnuru/Downloads/Asset_Pages_Extraction_", finalDataset, header); | |
} | |
private static void addKeys(String currentPath, JsonNode jsonNode, Map<String, String> map) { | |
if (jsonNode.isObject()) { | |
ObjectNode objectNode = (ObjectNode) jsonNode; | |
Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields(); | |
String pathPrefix = currentPath.isEmpty() ? "" : currentPath + "."; | |
while (iter.hasNext()) { | |
Map.Entry<String, JsonNode> entry = iter.next(); | |
addKeys(pathPrefix + entry.getKey(), entry.getValue(), map); | |
} | |
} else if (jsonNode.isArray()) { | |
ArrayNode arrayNode = (ArrayNode) jsonNode; | |
for (int i = 0; i < arrayNode.size(); i++) { | |
addKeys(currentPath , arrayNode.get(i), map); | |
} | |
} else if (jsonNode.isValueNode()) { | |
ValueNode valueNode = (ValueNode) jsonNode; | |
if (map.containsKey(currentPath)) { | |
String data = map.get(currentPath); | |
map.put(currentPath, data + System.getProperty("line.separator") + valueNode.asText()); | |
} else { | |
map.put(currentPath, valueNode.asText()); | |
} | |
} | |
} | |
public static void writeToXLSXFile(String outputFilePath, List<Map<String, String>> finalDataset, Set<String> header) { | |
try { | |
Workbook workbook = new XSSFWorkbook(); | |
Sheet sheet = workbook.createSheet("Report"); | |
int cellCount = NumberUtils.INTEGER_ZERO; | |
Row dataRow = sheet.createRow(NumberUtils.INTEGER_ZERO); | |
for(String temp: header){ | |
dataRow.createCell(cellCount++).setCellValue(temp); | |
} | |
loadDataWorksheetCell(sheet, finalDataset, header); | |
String filePath = outputFilePath + System.currentTimeMillis() + ".xlsx"; | |
FileOutputStream outputStream = new FileOutputStream(filePath); | |
System.out.println("File Path:" + filePath); | |
workbook.write(outputStream); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void loadDataWorksheetCell(Sheet sheet, List<Map<String, String>> finalDataset, Set<String> header) { | |
int rowCount = NumberUtils.INTEGER_ONE; | |
int cellCount; | |
Row dataRow; | |
for( Map<String, String> rowData: finalDataset){ | |
dataRow = sheet.createRow(rowCount++); | |
cellCount = NumberUtils.INTEGER_ZERO; | |
for (String key : header) { | |
if (rowData.containsKey(key)) { | |
dataRow.createCell(cellCount++).setCellValue(rowData.get(key)); | |
} else { | |
cellCount++; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment