-
Notifications
You must be signed in to change notification settings - Fork 4
File support improvements #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6783fe1
File support improvements
krystian-panek-vmltech 725018b
Misused types
krystian-panek-vmltech 6f707f0
Validation
krystian-panek-vmltech f7a4d3d
XLS input read example
krystian-panek-vmltech 8a6f887
XLS/XLSX reading example
krystian-panek-vmltech 0e28e7f
Minor
krystian-panek-vmltech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -667,15 +667,11 @@ private void setFileContent( | |
| } | ||
|
|
||
| public InputStream readFileAsStream() { | ||
| Resource resource = require(); | ||
| Resource contentResource = resource.getChild(JcrConstants.JCR_CONTENT); | ||
| if (contentResource == null) { | ||
| throw new RepoException(String.format("Cannot read file at path '%s' as it does not have content!", path)); | ||
| } | ||
| InputStream inputStream = contentResource.adaptTo(InputStream.class); | ||
| RepoResource fileResource = requireFile(); | ||
| InputStream inputStream = fileResource.resolve().adaptTo(InputStream.class); | ||
| if (inputStream == null) { | ||
| throw new RepoException( | ||
| String.format("Cannot read file at path '%s' as it does not have content stream!", path)); | ||
| String.format("Cannot read file at path '%s' as it does not have binary data!", path)); | ||
| } | ||
| return inputStream; | ||
| } | ||
|
|
@@ -688,8 +684,40 @@ public String readFileAsString() { | |
| } | ||
| } | ||
|
|
||
| public RepoResource resolveFile() { | ||
| return file().orElse(null); | ||
| } | ||
|
|
||
| public RepoResource requireFile() { | ||
| return file().orElseThrow(() -> | ||
| new RepoException(String.format("Resource at path '%s' does not have file content!", path))); | ||
| } | ||
|
|
||
| private Optional<RepoResource> file() { | ||
|
krystian-panek-vmltech marked this conversation as resolved.
|
||
| Resource resource = resolve(); | ||
| if (resource == null) { | ||
| return Optional.empty(); | ||
| } | ||
| Resource contentResource = resource.getChild(JcrConstants.JCR_CONTENT); | ||
| if (contentResource == null) { | ||
| return Optional.empty(); | ||
| } | ||
| if (path.startsWith(AemConstants.DAM_ROOT + "/")) { | ||
| String damOriginalPath = AemConstants.DAM_RENDITION_FOLDER + "/" + AemConstants.DAM_RENDITION_ORIGINAL + "/" | ||
| + JcrConstants.JCR_CONTENT; | ||
| Resource damContent = contentResource.getChild(damOriginalPath); | ||
| if (damContent != null && JcrUtils.hasBinaryData(damContent.adaptTo(Node.class))) { | ||
| return Optional.of(new RepoResource(repo, damContent.getPath())); | ||
| } | ||
| } | ||
| if (JcrUtils.hasBinaryData(contentResource.adaptTo(Node.class))) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this simply behaves like Sling adapter for InputStream ; but unfortunately the original method is internal to Sling to copied-adapted to ACM to have 1-to-1 compatibility |
||
| return Optional.of(new RepoResource(repo, contentResource.getPath())); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| public boolean isFile() { | ||
| return isType(JcrConstants.NT_FILE); | ||
| return file().isPresent(); | ||
| } | ||
|
|
||
| public String type() { | ||
|
|
||
14 changes: 14 additions & 0 deletions
14
core/src/main/java/dev/vml/es/acm/core/util/AemConstants.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package dev.vml.es.acm.core.util; | ||
|
|
||
| public final class AemConstants { | ||
|
|
||
| public static final String DAM_ROOT = "/content/dam"; | ||
|
|
||
| public static final String DAM_RENDITION_FOLDER = "renditions"; | ||
|
|
||
| public static final String DAM_RENDITION_ORIGINAL = "original"; | ||
|
|
||
| private AemConstants() { | ||
| // constants only | ||
| } | ||
| } |
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
107 changes: 107 additions & 0 deletions
107
...c/main/content/jcr_root/conf/acm/settings/script/manual/example/ACME-204_input-xls.groovy
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import org.apache.poi.ss.usermodel.* | ||
| import org.apache.poi.xssf.usermodel.XSSFWorkbook | ||
| import java.time.LocalDate | ||
| import java.time.ZoneId | ||
|
|
||
| /* | ||
| --- | ||
| author: <john.doe@acme.com> | ||
| --- | ||
| Reads an XLSX file (e.g. generated by ACME-203) and outputs summary statistics. | ||
| Demonstrates how to use file input for uploading and parsing spreadsheet files. | ||
|
|
||
| ```mermaid | ||
| graph TD | ||
| A[Upload XLS File] --> B[Open Workbook] | ||
| B --> C[Read Sheet] | ||
| C --> D[Parse Rows] | ||
| D --> E[Calculate Statistics] | ||
| E --> F[Output Summary] | ||
| ``` | ||
| */ | ||
|
|
||
| void describeRun() { | ||
| inputs.file("xlsFile") { | ||
| label = "XLS/XLSX File" | ||
| description = "Upload a spreadsheet file to analyze (e.g. report.xlsx from ACME-203)" | ||
| mimeTypes = [ | ||
| "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | ||
| "application/vnd.ms-excel" | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| boolean canRun() { | ||
| return conditions.always() | ||
| } | ||
|
|
||
| void doRun() { | ||
| out.info "Reading XLS file..." | ||
|
|
||
| def xlsFile = repo.get(inputs.value("xlsFile")) | ||
|
|
||
| try { | ||
| Workbook workbook = new XSSFWorkbook(xlsFile.readFileAsStream()) | ||
| Sheet sheet = workbook.getSheetAt(0) | ||
|
|
||
| // Build column name -> index map from header row | ||
| Row headerRow = sheet.getRow(0) | ||
| def columns = [:] | ||
| for (Cell cell : headerRow) { | ||
| columns[cell.getStringCellValue()] = cell.getColumnIndex() | ||
| } | ||
| out.info "Found columns: ${columns.keySet().join(', ')}" | ||
|
|
||
| def rowCount = 0 | ||
| def names = [] as Set | ||
| def surnames = [] as Set | ||
| def oldestDate = null | ||
| def youngestDate = null | ||
|
|
||
| for (Row row : sheet) { | ||
| if (row.getRowNum() == 0) continue // skip header | ||
|
|
||
| context.checkAborted() | ||
| rowCount++ | ||
|
|
||
| def nameCell = row.getCell(columns["Name"]) | ||
| def surnameCell = row.getCell(columns["Surname"]) | ||
| def dateCell = row.getCell(columns["Birth Date"]) | ||
|
|
||
| if (nameCell) names.add(nameCell.getStringCellValue()) | ||
| if (surnameCell) surnames.add(surnameCell.getStringCellValue()) | ||
|
|
||
| if (dateCell) { | ||
| def date = null | ||
| if (DateUtil.isCellDateFormatted(dateCell)) { | ||
| date = dateCell.getDateCellValue().toInstant().atZone(ZoneId.systemDefault()).toLocalDate() | ||
| } else if (dateCell.getCellType() == CellType.STRING) { | ||
| date = LocalDate.parse(dateCell.getStringCellValue()) | ||
| } | ||
| if (date != null) { | ||
| if (oldestDate == null || date.isBefore(oldestDate)) oldestDate = date | ||
| if (youngestDate == null || date.isAfter(youngestDate)) youngestDate = date | ||
| } | ||
| } | ||
|
|
||
| if (rowCount % 1000 == 0) out.info("Processed ${rowCount} rows...") | ||
| } | ||
|
|
||
| workbook.close() | ||
|
|
||
| outputs.text("summary") { | ||
| label = "Summary" | ||
| value = lines([ | ||
| "Rows processed": rowCount, | ||
| "Unique names": names.size(), | ||
| "Unique surnames": surnames.size(), | ||
| "Oldest birth date": oldestDate ?: 'N/A', | ||
| "Youngest birth date": youngestDate ?: 'N/A' | ||
| ]) | ||
| } | ||
|
|
||
| out.success "XLS file analysis completed" | ||
| } finally { | ||
| xlsFile.delete() | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.