Skip to content

Commit

Permalink
Merge pull request #379 from adessoAG/template-and-importer-fix
Browse files Browse the repository at this point in the history
Fixed the issues with the templates and adjusted the importer to dynamically find the correct sheet in the UBW-Export
  • Loading branch information
maximAtanasov committed Nov 5, 2019
2 parents 156ec54 + 6b1232a commit 67f0323
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,23 +28,23 @@ public class UBWWorkRecordsImporter implements WorkRecordsImporter {

private static final int COLUMN_HOURS = 10;

private List<List<String>> skippedRecords = new LinkedList<List<String>>();
private List<List<String>> skippedRecords = new LinkedList<>();

@Override
public List<ImportedWorkRecord> importFile(ImportFile file) throws ImportException, InvalidFileFormatException {
try {
skippedRecords.add(new LinkedList<String>());
skippedRecords.add(new LinkedList<>());
//Adds the name of the imported file at the beginning of the list of skipped data sets..
List<String> fileName = new LinkedList<String>();
List<String> fileName = new LinkedList<>();
fileName.add(file.getFilename());
skippedRecords.add(fileName);

List<ImportedWorkRecord> resultList = new ArrayList<ImportedWorkRecord>();
List<ImportedWorkRecord> 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) {
Expand All @@ -61,6 +65,10 @@ public List<ImportedWorkRecord> 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);
Expand All @@ -82,7 +90,7 @@ public String getDisplayName() {

@Override
public List<String> getSupportedFileExtensions() {
return Arrays.asList(".xlsx");
return Collections.singletonList(".xlsx");
}

@Override
Expand All @@ -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);
Expand Down Expand Up @@ -158,16 +167,16 @@ public ExampleFile getExampleFile() {
public List<List<String>> 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<List<String>>();
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;
Expand All @@ -188,7 +197,7 @@ boolean checkValidity(Workbook workbook) {
}

private List<String> getRowAsStrings(Row row, int index) {
List<String> result = new LinkedList<String>();
List<String> result = new LinkedList<>();
for (short i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
if (cell == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImportedWorkRecord> 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();
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
}
}
}

Expand All @@ -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();
}
}
}

Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public Template getDefault(ReportType type, long projectID){
*/
public List<Template> getFilteredTemplatesInProject(@NotNull TemplateFilter filter){
List<Template> result = new ArrayList<>();
for(TemplateEntity E : templateRepository.findByProjectId(filter.getProjectId())){
for(TemplateEntity e : templateRepository.findByProjectId(filter.getProjectId())){
for(ReportType type : filter.getTypesList()){
if(type == E.getType()){
result.add(new Template(E.getId(), E.getName(), E.getDescription(), E.getType(), E.getWb(), E.isDefault(), E.getProjectId()));
if(type == e.getType()){
result.add(new Template(e.getId(), e.getName(), e.getDescription(), e.getType(), e.getWb(), e.isDefault(), e.getProjectId()));
}
}
}
Expand All @@ -84,11 +84,11 @@ public List<Template> getFilteredTemplatesInProject(@NotNull TemplateFilter filt
* @return A new Template object.
*/
public Template getById(long templateID){
TemplateEntity templateEntity = templateRepository.findOne(templateID);
if(templateEntity == null){
TemplateEntity e = templateRepository.findOne(templateID);
if(e == null){
return null;
} else {
return templateEntity.getTemplate();
return new Template(e.getId(), e.getName(), e.getDescription(), e.getType(), e.getWb(), e.isDefault(), e.getProjectId());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public EditTemplateForm(String id, IModel<TemplateFormInputDto> formModel, long

add(fileUpload);
add(createCancelButton("backlink2"));
add(DeleteTemplateButton("deleteButton"));
add(DownloadFileButton("downloadFileButton"));
add(deleteTemplateButton("deleteButton"));
add(downloadFileButton("downloadFileButton"));

add(new AjaxButton("save") {
@Override
Expand Down Expand Up @@ -169,7 +169,7 @@ public String getModelValue (){
/**
* Creates a button to download the template that is being edited.
*/
private Link DownloadFileButton(String wicketId) {
private Link downloadFileButton(String wicketId) {
return new Link<Void>(wicketId) {
@Override
public void onClick() {
Expand Down Expand Up @@ -204,7 +204,7 @@ public void onClick() {
/**
* Creates a button to delete the template that is being edited.
*/
private Link DeleteTemplateButton(String wicketId) {
private Link deleteTemplateButton(String wicketId) {
return new Link<Void>(wicketId) {
@Override
public void onClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ spring.jpa.properties.hibernate.id.new_generator_mappings=false

# FLYWAY (FlywayProperties)
flyway.baseline-on-migrate=true
flyway.baseline-version=1_1_0
flyway.baseline-version=1_1_1
flyway.check-location=true
flyway.enabled=true
flyway.locations=classpath:db/migration
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ file('gradle.d').listFiles().sort().each {
}

allprojects {
version = '1.1.1.BETA'
version = '1.1.2.BETA'
}

buildscript {
Expand Down

0 comments on commit 67f0323

Please sign in to comment.