Skip to content

Commit

Permalink
feature: inventory serialization improved to include ScanCode Ids, Li…
Browse files Browse the repository at this point in the history
…censeData sheet styling for category (C-) and marker (M-) columns
  • Loading branch information
karsten-klein committed Nov 5, 2024
1 parent d113254 commit 302f37e
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public enum Attribute implements AbstractModelBase.Attribute {
CANONICAL_NAME("Canonical Name"),
ID("Id"),
SPDX_ID("SPDX Id"),
SCANCODE_IDS("ScanCode Ids"),
OSI_APPROVED("OSI Approved"),
COPYLEFT_TYPE("Copyleft Type"),
COMMERCIAL("Commercial"),
Expand All @@ -65,6 +66,7 @@ public String getKey() {
CORE_ATTRIBUTES.add(Attribute.CANONICAL_NAME.getKey());
CORE_ATTRIBUTES.add(Attribute.ID.getKey());
CORE_ATTRIBUTES.add(Attribute.SPDX_ID.getKey());
CORE_ATTRIBUTES.add(Attribute.SCANCODE_IDS.getKey());
CORE_ATTRIBUTES.add(Attribute.REPRESENTED_AS.getKey());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

public class XlsInventoryWriter extends AbstractXlsInventoryWriter {

private final static Logger LOG = LoggerFactory.getLogger(XlsInventoryWriter.class);

public void writeInventory(Inventory inventory, File file) throws IOException {
final HSSFWorkbook workbook = new HSSFWorkbook();
final XlsHSSFInventorySheetCellStylers stylers = new XlsHSSFInventorySheetCellStylers(workbook);
Expand Down Expand Up @@ -298,12 +296,15 @@ private void writeLicenseData(Inventory inventory, HSSFWorkbook workbook, XlsHSS

final InventorySheetCellStyler[] headerCellStylers = new InventorySheetCellStyler[]{
stylers.headerStyleColumnNameAssetId,
stylers.headerStyleColumnNameMarker,
stylers.headerStyleColumnNameClassification,
stylers.createLicensesHeaderCellStyler(serializationContext),
stylers.headerStyleDefault,
};

final InventorySheetCellStyler[] dataCellStylers = new InventorySheetCellStyler[]{
stylers.contentStyleColumnNameAssetId,
stylers.contentStyleColumnNameMarkerCentered,
stylers.createLicensesCellStyler(serializationContext)
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

public class XlsxInventoryWriter extends AbstractXlsxInventoryWriter {

private final static Logger LOG = LoggerFactory.getLogger(XlsxInventoryWriter.class);

/**
* Defines a default order.
* <p>
Expand Down Expand Up @@ -274,8 +272,8 @@ private void writeAdvisoryMetaData(Inventory inventory, SXSSFWorkbook workbook,

// create columns for key / value map content
final Set<String> attributes = new HashSet<>();
for (AdvisoryMetaData cm : inventory.getAdvisoryMetaData()) {
attributes.addAll(cm.getAttributes());
for (AdvisoryMetaData am : inventory.getAdvisoryMetaData()) {
attributes.addAll(am.getAttributes());
}

AdvisoryMetaData.CORE_ATTRIBUTES.forEach(attributes::remove);
Expand Down Expand Up @@ -377,12 +375,15 @@ private void writeLicenseData(Inventory inventory, SXSSFWorkbook workbook, XlsxX

final InventorySheetCellStyler[] headerCellStylers = new InventorySheetCellStyler[]{
stylers.headerStyleColumnNameAssetId,
stylers.headerStyleColumnNameMarker,
stylers.headerStyleColumnNameClassification,
stylers.createLicensesHeaderCellStyler(serializationContext),
stylers.headerStyleDefault,
};

final InventorySheetCellStyler[] dataCellStylers = new InventorySheetCellStyler[]{
stylers.contentStyleColumnNameAssetId,
stylers.contentStyleColumnNameMarkerCentered,
stylers.createLicensesCellStyler(serializationContext)
};

Expand Down Expand Up @@ -410,13 +411,13 @@ private void writeAssetMetaData(Inventory inventory, SXSSFWorkbook workbook, Xls
// remove core attributes
final List<String> finalOrder = deriveOrder(attributes, AssetMetaData.CORE_ATTRIBUTES);

final InventorySheetCellStyler[] headerCellStylers = new InventorySheetCellStyler[]{
final InventorySheetCellStyler[] headerCellStylers = new InventorySheetCellStyler[] {
stylers.headerStyleColumnNameSrcAssetSource,
stylers.headerStyleColumnNameAssetConfig,
stylers.headerStyleDefault,
};

final InventorySheetCellStyler[] dataCellStylers = new InventorySheetCellStyler[]{
final InventorySheetCellStyler[] dataCellStylers = new InventorySheetCellStyler[] {
stylers.contentStyleColumnNameSrcCentered,
};

Expand Down Expand Up @@ -480,5 +481,4 @@ private static List<String> deriveOrder(Set<String> attributes, ArrayList<String
finalOrder.addAll(ordered);
return finalOrder;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ public class XlsHSSFInventorySheetCellStylers {
public final InventorySheetCellStyler headerStyleDefault;
public final InventorySheetCellStyler headerStyleColumnNameSrcAssetSource;
public final InventorySheetCellStyler headerStyleColumnNameAssetConfig;
public final InventorySheetCellStyler headerStyleColumnNameMarker;
public final InventorySheetCellStyler headerStyleColumnNameClassification;

// DATA CELLS
public final InventorySheetCellStyler contentStyleColumnNameAssetId;
public final InventorySheetCellStyler contentStyleColumnNameIncompleteMatch;
public final InventorySheetCellStyler contentStyleCvssScoresDoubleValue;
public final InventorySheetCellStyler contentStyleUrlValue;
public final InventorySheetCellStyler contentStyleColumnNameSrcCentered;
public final InventorySheetCellStyler contentStyleColumnNameMarkerCentered;

public XlsHSSFInventorySheetCellStylers(HSSFWorkbook workbook) {
final HSSFCellStyle defaultHeaderStyle = createDefaultHeaderStyle(workbook);
Expand All @@ -64,6 +67,9 @@ public XlsHSSFInventorySheetCellStylers(HSSFWorkbook workbook) {
final HSSFCellStyle warnHeaderStyle = createWarnHeaderStyle(workbook);
final HSSFCellStyle errorHeaderStyle = createErrorHeaderStyle(workbook);

final HSSFCellStyle termsMarkerHeaderStyle = createMarkerHeaderStyle(workbook);
final HSSFCellStyle termsClassificationStyle = createClassificationHeaderStyle(workbook);

// HEADER CELLS
this.headerStyleColumnNameAssetId = InventorySheetCellStyler.createStyler(
context -> isAssetId(context.getFullColumnHeader()),
Expand Down Expand Up @@ -117,6 +123,26 @@ public XlsHSSFInventorySheetCellStylers(HSSFWorkbook workbook) {
context.getSheet().setColumnWidth(context.getColumnIndex(), 40 * 42);
});

this.headerStyleColumnNameMarker = InventorySheetCellStyler.createStyler(
context -> {
return context.getFullColumnHeader().startsWith("M-");
}, context -> {
context.getCell().setCellStyle(termsMarkerHeaderStyle);
// number of pixel times magic number
context.getSheet().setColumnWidth(context.getColumnIndex(), 20 * 42);
context.getRow().setHeight((short) (170 * 20));
});

this.headerStyleColumnNameClassification = InventorySheetCellStyler.createStyler(
context -> {
return context.getFullColumnHeader().startsWith("C-");
}, context -> {
context.getCell().setCellStyle(termsClassificationStyle);
// number of pixel times magic number
context.getSheet().setColumnWidth(context.getColumnIndex(), 20 * 42);
context.getRow().setHeight((short) (170 * 20));
});

// DATA CELLS
this.contentStyleColumnNameAssetId = InventorySheetCellStyler.createStyler(
context -> {
Expand Down Expand Up @@ -172,6 +198,14 @@ public XlsHSSFInventorySheetCellStylers(HSSFWorkbook workbook) {
}, context -> {
context.getCell().setCellStyle(centeredStyle);
});

this.contentStyleColumnNameMarkerCentered = InventorySheetCellStyler.createStyler(
context -> {
return context.getFullColumnHeader().startsWith("M-") ||
context.getFullColumnHeader().startsWith("C-") ;
}, context -> {
context.getCell().setCellStyle(centeredStyle);
});
}

private boolean isAssetId(String key) {
Expand Down Expand Up @@ -234,6 +268,16 @@ private HSSFCellStyle createWarnHeaderStyle(HSSFWorkbook workbook) {
return createRotatedCellStyle(workbook, headerColor);
}

protected HSSFCellStyle createMarkerHeaderStyle(HSSFWorkbook workbook) {
final HSSFColor headerColor = resolveColor(workbook, "250,234,173");
return createRotatedCellStyle(workbook, headerColor);
}

protected HSSFCellStyle createClassificationHeaderStyle(HSSFWorkbook workbook) {
final HSSFColor headerColor = resolveColor(workbook, "248,214,128");
return createRotatedCellStyle(workbook, headerColor);
}

private HSSFCellStyle createErrorHeaderStyle(HSSFWorkbook workbook) {
final HSSFColor headerColor = resolveColor(workbook, "244,176,132");
final HSSFCellStyle cellStyle = createDefaultHeaderStyle(workbook);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ public class XlsxXSSFInventorySheetCellStylers {
public final InventorySheetCellStyler headerStyleDefault;
public final InventorySheetCellStyler headerStyleColumnNameSrcAssetSource;
public final InventorySheetCellStyler headerStyleColumnNameAssetConfig;
public final InventorySheetCellStyler headerStyleColumnNameMarker;
public final InventorySheetCellStyler headerStyleColumnNameClassification;

// DATA CELLS
public final InventorySheetCellStyler contentStyleColumnNameAssetId;
public final InventorySheetCellStyler contentStyleColumnNameIncompleteMatch;
public final InventorySheetCellStyler contentStyleCvssScoresDoubleValue;
public final InventorySheetCellStyler contentStyleUrlValue;
public final InventorySheetCellStyler contentStyleColumnNameSrcCentered;
public final InventorySheetCellStyler contentStyleColumnNameMarkerCentered;

public XlsxXSSFInventorySheetCellStylers(SXSSFWorkbook workbook) {
final CellStyle defaultHeaderStyle = createDefaultHeaderStyle(workbook);
Expand All @@ -65,6 +68,9 @@ public XlsxXSSFInventorySheetCellStylers(SXSSFWorkbook workbook) {
final CellStyle warnHeaderStyle = createWarnHeaderStyle(workbook);
final CellStyle errorHeaderStyle = createErrorHeaderStyle(workbook);

final CellStyle termsMarkerHeaderStyle = createMarkerHeaderStyle(workbook);
final CellStyle termsClassificationStyle = createClassificationHeaderStyle(workbook);

// HEADER CELLS
this.headerStyleColumnNameAssetId = InventorySheetCellStyler.createStyler(
context -> isAssetId(context.getFullColumnHeader()),
Expand Down Expand Up @@ -108,13 +114,36 @@ public XlsxXSSFInventorySheetCellStylers(SXSSFWorkbook workbook) {

this.headerStyleColumnNameAssetConfig = InventorySheetCellStyler.createStyler(
context -> {
return context.getFullColumnHeader().startsWith("config_");
final String fullColumnHeader = context.getFullColumnHeader();
return fullColumnHeader.startsWith("config_") ||
fullColumnHeader.startsWith("prop_") ||
fullColumnHeader.startsWith("meta_");
}, context -> {
context.getCell().setCellStyle(assetConfigHeaderStyle);
// number of pixel times magic number
context.getSheet().setColumnWidth(context.getColumnIndex(), 40 * 42);
});

this.headerStyleColumnNameMarker = InventorySheetCellStyler.createStyler(
context -> {
return context.getFullColumnHeader().startsWith("M-");
}, context -> {
context.getCell().setCellStyle(termsMarkerHeaderStyle);
// number of pixel times magic number
context.getSheet().setColumnWidth(context.getColumnIndex(), 20 * 42);
context.getRow().setHeight((short) (170 * 20));
});

this.headerStyleColumnNameClassification = InventorySheetCellStyler.createStyler(
context -> {
return context.getFullColumnHeader().startsWith("C-");
}, context -> {
context.getCell().setCellStyle(termsClassificationStyle);
// number of pixel times magic number
context.getSheet().setColumnWidth(context.getColumnIndex(), 20 * 42);
context.getRow().setHeight((short) (170 * 20));
});

// DATA CELLS
this.contentStyleColumnNameAssetId = InventorySheetCellStyler.createStyler(
context -> {
Expand Down Expand Up @@ -170,6 +199,14 @@ public XlsxXSSFInventorySheetCellStylers(SXSSFWorkbook workbook) {
}, context -> {
context.getCell().setCellStyle(centeredStyle);
});

this.contentStyleColumnNameMarkerCentered = InventorySheetCellStyler.createStyler(
context -> {
return context.getFullColumnHeader().startsWith("M-") ||
context.getFullColumnHeader().startsWith("C-") ;
}, context -> {
context.getCell().setCellStyle(centeredStyle);
});
}

private boolean isAssetId(String key) {
Expand Down Expand Up @@ -232,6 +269,16 @@ protected CellStyle createWarnHeaderStyle(SXSSFWorkbook workbook) {
return createRotatedCellStyle(workbook, headerColor);
}

protected CellStyle createMarkerHeaderStyle(SXSSFWorkbook workbook) {
final XSSFColor headerColor = resolveColor(workbook, "250,234,173");
return createRotatedCellStyle(workbook, headerColor);
}

protected CellStyle createClassificationHeaderStyle(SXSSFWorkbook workbook) {
final XSSFColor headerColor = resolveColor(workbook, "248,214,128");
return createRotatedCellStyle(workbook, headerColor);
}

protected CellStyle createErrorHeaderStyle(SXSSFWorkbook workbook) {
final XSSFColor headerColor = resolveColor(workbook, "244,176,132");
final CellStyle cellStyle = createDefaultHeaderStyle(workbook);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.metaeffekt.core.inventory.processor.writer;

import org.junit.Ignore;
import org.junit.Test;
import org.metaeffekt.core.inventory.processor.model.Inventory;
import org.metaeffekt.core.inventory.processor.reader.InventoryReader;
import org.metaeffekt.core.util.FileUtils;

import java.io.File;
import java.io.IOException;

public class InventoryWriterTest {

@Test
public void testWrite_Empty() throws IOException {
final File testOutputDir = new File("target/inventory-writer-test");
FileUtils.forceMkdir(testOutputDir);
new InventoryWriter().writeInventory(new Inventory(), new File(testOutputDir, "emtpy.xls"));
new InventoryWriter().writeInventory(new Inventory(), new File(testOutputDir, "emtpy.xlsx"));
new InventoryWriter().writeInventory(new Inventory(), new File(testOutputDir, "emtpy.ser"));
}

@Ignore
@Test
public void testReadWrite() throws IOException {
final File inputInventoryFile = new File("<path-to-input-inventory>");
final File outputInventoryFile = new File("<path-to-output-inventory>");

final Inventory inventory = new InventoryReader().readInventory(inputInventoryFile);
new InventoryWriter().writeInventory(inventory, outputInventoryFile);
}

}

0 comments on commit 302f37e

Please sign in to comment.