Skip to content

Commit

Permalink
Feature/generic excel writer (#259)
Browse files Browse the repository at this point in the history
Co-authored-by: ohecker <[email protected]>
  • Loading branch information
duph97 and ohecker authored Apr 25, 2024
1 parent 0d4c358 commit d786c03
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* SPDX-License-Identifier: Apache-2.0
*/

package com.devonfw.tools.solicitor.writer.genericxls;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.poi.EncryptedDocumentException;
// usermodel api for creating, reading and modifying xls files
import org.apache.poi.ss.usermodel.Cell;
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.ss.usermodel.WorkbookFactory;
import org.springframework.stereotype.Component;

import com.devonfw.tools.solicitor.common.IOHelper;
import com.devonfw.tools.solicitor.common.SolicitorRuntimeException;
import com.devonfw.tools.solicitor.writer.Writer;
import com.devonfw.tools.solicitor.writer.data.DataTable;
import com.devonfw.tools.solicitor.writer.data.DataTableField;
import com.devonfw.tools.solicitor.writer.data.DataTableRow;

/**
* A {@link Writer} which creates a generic XLS workbook with a separate sheet for each defined dataTable. Field data
* will be trimmed when exceeding more than 200 characters. This report is intended for debugging purposes.
*/
@Component
public class GenericExcelWriter implements Writer {

/**
* {@inheritDoc}
*
* Accepted type is "xls".
*/
@Override
public boolean accept(String type) {

return "genericxls".equals(type);
}

private String trimToReasonableLength(String original) {

String trimmed = original;
if (original.length() > 200) {
trimmed = "HASH: " + original.hashCode() + "----" + original.substring(0, 200);
}
return trimmed;

}

/**
* {@inheritDoc}
*
* This function will generate a generic report.
*/
@Override
public void writeReport(String templateSource, String target, Map<String, DataTable> dataTables) {

try {

Workbook wb = WorkbookFactory.create(true);

for (Entry<String, DataTable> tableEntry : dataTables.entrySet()) {
Sheet sh = wb.createSheet(limitSheetNameLength(tableEntry.getKey()));

DataTable dataTable = tableEntry.getValue();
int rowIndex = 0;
Row row = sh.createRow(rowIndex++);
int columnIndex = 0;
for (String columnName : dataTable.getHeadRow()) {
Cell cell = row.createCell(columnIndex++);
cell.setCellValue(columnName);
}
for (DataTableRow dataTableRow : dataTable) {
row = sh.createRow(rowIndex++);
for (int i = 0; i < dataTableRow.getSize(); i++) {
Cell cell = row.createCell(i);
DataTableField value = dataTableRow.getValueByIndex(i);
String textValue = value.toString() == null ? "" : value.toString();
cell.setCellValue(trimToReasonableLength(textValue));
}
}
}

// Write the output to a file
IOHelper.checkAndCreateLocation(target);
try (OutputStream fileOut = new FileOutputStream(target)) {
wb.write(fileOut);
}
} catch (IOException | EncryptedDocumentException e) {
throw new SolicitorRuntimeException("Processing of XLS report failed", e);
}

}

/**
* @param name
* @return
*/
private String limitSheetNameLength(String name) {

if (name.length() > 31) {
return (name.substring(0, 14) + "..." + name.substring(name.length() - 14));
} else {
return name;
}
}

}
22 changes: 22 additions & 0 deletions documentation/master-solicitor.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,26 @@ as follows when using the XLS templating:
* For rows that are "new" (so no corresponding old row available) an Excel note indicating that this row is new will be attached to the field that contained the `\#...#` placeholder.
* Fields in non-new rows that have changed their value will be marked with an Excel note indicating the old value.

==== Generic Excel Writer
The Generic Excel Writer exists purely for debugging purposes. This writer writes the contents of the `dataTables` defined in the writer configuration to an Excel file. Each `dataTable` will be available in a separate Excel sheet. To use this writer, an `additionalWriter` (see <<Writers and Reporting>> ) needs to be set in the `solicitor.cfg`. Example:
[listing]
"additionalWriters" : [ {
"type" : "genericxls",
"templateSource" : "", <1>
"target" : "${cfgdir}/output/GenericXLS.xlsx",
"description" : "Excel workbook with a separate sheet for each defined dataTable",
"dataTables" : {
"ENGAGEMENT" : "classpath:com/devonfw/tools/solicitor/sql/allden_engagements.sql",
"APPLICATIONCOMPONENT" : "classpath:com/devonfw/tools/solicitor/sql/allden_applicationcomponents.sql",
"LICENSE" : "classpath:com/devonfw/tools/solicitor/sql/allden_normalizedlicenses.sql",
"OSSLICENSES" : "classpath:com/devonfw/tools/solicitor/sql/ossapplicationcomponents.sql",
...
}
} ]

<1> This is unused and can be left empty.


== Resolving of License URLs

Resolving of the content of license texts which are referenced by the URLs given in `NormalizedLicense.effectiveNormalizedLicenseUrl` and `NormalizedLicense.licenseRefUrl` is done in the following way:
Expand Down Expand Up @@ -1356,6 +1376,7 @@ To use license guessing in a template, an `additionalWriter` (see <<Writers and
"ENGAGEMENT" : "classpath:com/devonfw/tools/solicitor/sql/allden_engagements.sql",
"OSSLICENSES" : "classpath:com/devonfw/tools/solicitor/sql/ossapplicationcomponents_guessedlicenses.sql",
"UNIQUELICENSES" : "classpath:com/devonfw/tools/solicitor/sql/uniqueguessedlicenses.sql"
}
} ]

== Feature Deprecation
Expand Down Expand Up @@ -1722,6 +1743,7 @@ Spring beans implementing this interface will be called at certain points in the
== Release Notes
Changes in 1.23.0::
* https://github.com/devonfw/solicitor/issues/255: Deprecate LicenseUrl guessing.
* https://github.com/devonfw/solicitor/issues/258: Add GenericExcelWriter for debugging of SQL scripts.

Changes in 1.22.0::
* https://github.com/devonfw/solicitor/pull/243: Make sure the MavenReader is protected against XXE threats.
Expand Down

0 comments on commit d786c03

Please sign in to comment.