diff --git a/budgeteer-ubw-importer/src/main/java/org/wickedsource/budgeteer/importer/ubw/UBWWorkRecordsImporter.java b/budgeteer-ubw-importer/src/main/java/org/wickedsource/budgeteer/importer/ubw/UBWWorkRecordsImporter.java index 7ddc48192..7099fc5c3 100644 --- a/budgeteer-ubw-importer/src/main/java/org/wickedsource/budgeteer/importer/ubw/UBWWorkRecordsImporter.java +++ b/budgeteer-ubw-importer/src/main/java/org/wickedsource/budgeteer/importer/ubw/UBWWorkRecordsImporter.java @@ -10,9 +10,13 @@ import java.io.IOException; import java.util.*; +import static java.nio.charset.StandardCharsets.UTF_8; + public class UBWWorkRecordsImporter implements WorkRecordsImporter { - private static final int SHEET_INDEX = 6; + private int sheetIndex = -1; + + private static final String SHEET_NAME = "Aufwände gesamt"; private static final int COLUMN_INVOICEABLE = 11; @@ -24,23 +28,23 @@ public class UBWWorkRecordsImporter implements WorkRecordsImporter { private static final int COLUMN_HOURS = 10; - private List> skippedRecords = new LinkedList>(); + private List> skippedRecords = new LinkedList<>(); @Override public List importFile(ImportFile file) throws ImportException, InvalidFileFormatException { try { - skippedRecords.add(new LinkedList()); + skippedRecords.add(new LinkedList<>()); //Adds the name of the imported file at the beginning of the list of skipped data sets.. - List fileName = new LinkedList(); + List fileName = new LinkedList<>(); fileName.add(file.getFilename()); skippedRecords.add(fileName); - List resultList = new ArrayList(); + List resultList = new ArrayList<>(); Workbook workbook = new XSSFWorkbook(file.getInputStream()); if (!checkValidity(workbook)) { throw new InvalidFileFormatException("Invalid file", file.getFilename()); } - Sheet sheet = workbook.getSheetAt(SHEET_INDEX); + Sheet sheet = workbook.getSheetAt(sheetIndex); int i = 3; Row row = sheet.getRow(i); while (row != null && row.getCell(0) != null && row.getCell(0).getStringCellValue() != null) { @@ -61,6 +65,10 @@ public List importFile(ImportFile file) throws ImportExcepti } } + private int findSheetIndex(Workbook workbook) { + return workbook.getSheetIndex(new String(SHEET_NAME.getBytes(), UTF_8)); + } + private boolean isCompletelyEmpty(Row row) { for (short i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) { Cell cell = row.getCell(i); @@ -82,7 +90,7 @@ public String getDisplayName() { @Override public List getSupportedFileExtensions() { - return Arrays.asList(".xlsx"); + return Collections.singletonList(".xlsx"); } @Override @@ -96,11 +104,12 @@ public ExampleFile getExampleFile() { Calendar maxCalendar = Calendar.getInstance(); Calendar maxineCalendar = Calendar.getInstance(); XSSFWorkbook workbook = new XSSFWorkbook(getClass().getResourceAsStream("/example_ubw_report.xlsx")); - XSSFSheet sheet = workbook.getSheetAt(SHEET_INDEX); + sheetIndex = findSheetIndex(workbook); + XSSFSheet sheet = workbook.getSheetAt(sheetIndex); XSSFRow row; XSSFCell cell; int col = 3; - int i = sheet.getLastRowNum()-2; + int i = sheet.getLastRowNum(); XSSFCellStyle style = workbook.createCellStyle(); style.setBorderBottom(BorderStyle.THIN); @@ -158,16 +167,16 @@ public ExampleFile getExampleFile() { public List> getSkippedRecords() { //if just an empty row at the beginning and the filename is in the List of skipped records, return an empty List if (skippedRecords != null && skippedRecords.size() == 2) { - skippedRecords = new LinkedList>(); + skippedRecords = new LinkedList<>(); } return skippedRecords; } - boolean checkValidity(Workbook workbook) { - boolean isValid = workbook.getNumberOfSheets() >= SHEET_INDEX; + sheetIndex = findSheetIndex(workbook); + boolean isValid = sheetIndex != -1 && workbook.getNumberOfSheets() >= sheetIndex; if (isValid) { - Sheet sheet = workbook.getSheetAt(SHEET_INDEX); + Sheet sheet = workbook.getSheetAt(sheetIndex); int headerRowIndex = 2; if (sheet.getRow(headerRowIndex) == null) { isValid = false; @@ -188,7 +197,7 @@ boolean checkValidity(Workbook workbook) { } private List getRowAsStrings(Row row, int index) { - List result = new LinkedList(); + List result = new LinkedList<>(); for (short i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) { Cell cell = row.getCell(i); if (cell == null) { diff --git a/budgeteer-ubw-importer/src/test/java/org/wickedsource/budgeteer/importer/ubw/UBWWorkRecordsImporterTest.java b/budgeteer-ubw-importer/src/test/java/org/wickedsource/budgeteer/importer/ubw/UBWWorkRecordsImporterTest.java index 2bcd46e31..9e99f739c 100644 --- a/budgeteer-ubw-importer/src/test/java/org/wickedsource/budgeteer/importer/ubw/UBWWorkRecordsImporterTest.java +++ b/budgeteer-ubw-importer/src/test/java/org/wickedsource/budgeteer/importer/ubw/UBWWorkRecordsImporterTest.java @@ -33,6 +33,18 @@ void testRead() throws Exception { assertEquals(format.parse("09.01.2017"), records.get(0).getDate()); } + @Test + void testReadOldFormat() throws Exception { + UBWWorkRecordsImporter importer = new UBWWorkRecordsImporter(); + InputStream in = getClass().getResourceAsStream("/demo_ubw_report_old.xlsx"); + List records = importer.importFile(new ImportFile("file.xslx", in)); + assertEquals(1225, records.size()); + assertEquals("Archie, Holmes", records.get(0).getPersonName()); + assertEquals("Collecting Requirements", records.get(0).getBudgetName()); + assertEquals(570d, records.get(0).getMinutesWorked(), 1d); + assertEquals(format.parse("09.01.2017"), records.get(0).getDate()); + } + @Test void testGetSkippedDataSets() throws Exception { UBWWorkRecordsImporter importer = new UBWWorkRecordsImporter(); diff --git a/budgeteer-ubw-importer/src/test/resources/demo_ubw_report_old.xlsx b/budgeteer-ubw-importer/src/test/resources/demo_ubw_report_old.xlsx new file mode 100644 index 000000000..708233815 Binary files /dev/null and b/budgeteer-ubw-importer/src/test/resources/demo_ubw_report_old.xlsx differ diff --git a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/persistence/template/TemplateEntity.java b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/persistence/template/TemplateEntity.java index 5f4b5a4c0..cbbbb2780 100644 --- a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/persistence/template/TemplateEntity.java +++ b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/persistence/template/TemplateEntity.java @@ -38,12 +38,12 @@ public class TemplateEntity implements Serializable { @Column(name="TYPE", length = 128) private ReportType type; - @Column(name="ISDEFAULT", nullable = true) + @Column(name="ISDEFAULT") private Boolean isDefault; //Transient because Hibernate cannot save the Workbook directly in the db @Transient - private XSSFWorkbook wbXSSF; + private transient XSSFWorkbook wbXSSF; //So we get the internal data and store it in a byte array. @Column(name="TEMPLATE", length = 2 * 1024 * 1024) @@ -57,13 +57,17 @@ public TemplateEntity( String name, String description, ReportType type, XSSFWor this.wbXSSF = workbook; this.projectId = projectID; this.isDefault = isDefault; - try{ - //Write the workbook to an OutputStream and then get the byteArray of it. - ByteArrayOutputStream out = new ByteArrayOutputStream(); - wbXSSF.write(out); - wbArr = out.toByteArray(); - }catch (IOException e){ - e.printStackTrace(); + if (this.wbXSSF == null) { + this.wbArr = null; + } else { + try { + //Write the workbook to an OutputStream and then get the byteArray of it. + ByteArrayOutputStream out = new ByteArrayOutputStream(); + wbXSSF.write(out); + wbArr = out.toByteArray(); + } catch (IOException e) { + e.printStackTrace(); + } } } @@ -75,13 +79,17 @@ public TemplateEntity(long id, String name, String description, ReportType type, this.wbXSSF = workbook; this.projectId = projectID; this.isDefault = isDefault; - try{ - //Write the workbook to an OutputStream and then get the byteArray of it. - ByteArrayOutputStream out = new ByteArrayOutputStream(); - wbXSSF.write(out); - wbArr = out.toByteArray(); - }catch (IOException e){ - e.printStackTrace(); + if (this.wbXSSF == null) { + this.wbArr = null; + } else { + try { + //Write the workbook to an OutputStream and then get the byteArray of it. + ByteArrayOutputStream out = new ByteArrayOutputStream(); + wbXSSF.write(out); + wbArr = out.toByteArray(); + } catch (IOException e) { + e.printStackTrace(); + } } } @@ -90,7 +98,7 @@ public TemplateEntity(long id, String name, String description, ReportType type, * @return A new Template from this TemplateEntity */ public Template getTemplate(){ - return new Template(id, name, description, type ,wbXSSF, isDefault, projectId); + return new Template(id, name, description, type ,getWb(), isDefault, projectId); } /** diff --git a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/service/template/TemplateService.java b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/service/template/TemplateService.java index 95e90e27f..2d27d1230 100644 --- a/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/service/template/TemplateService.java +++ b/budgeteer-web-interface/src/main/java/org/wickedsource/budgeteer/service/template/TemplateService.java @@ -68,10 +68,10 @@ public Template getDefault(ReportType type, long projectID){ */ public List