Added File Reader
This commit is contained in:
parent
3dd8f0d82d
commit
7670401d6e
@ -45,10 +45,26 @@
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- dependency used for XLSX file format -->
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
|
||||
<!-- dependency used for XLS older file format
|
||||
(added for information) -->
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<pluginManagement>
|
||||
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<plugins>
|
||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||
<plugin>
|
||||
|
@ -0,0 +1,55 @@
|
||||
package com.plannaplan.configutils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import com.plannaplan.models.FileData;
|
||||
|
||||
public class FileReader {
|
||||
|
||||
private InputStream fileInputStream;
|
||||
|
||||
public FileReader(InputStream fileInputStream) {
|
||||
this.fileInputStream = fileInputStream;
|
||||
}
|
||||
|
||||
public FileData read() {
|
||||
|
||||
FileData result = null;
|
||||
|
||||
try {
|
||||
InputStream fis = this.fileInputStream;
|
||||
XSSFWorkbook workbook = new XSSFWorkbook(fis);
|
||||
XSSFSheet sheet = workbook.getSheetAt(0);
|
||||
|
||||
Iterator<Row> rowIt = sheet.iterator();
|
||||
|
||||
Row row = rowIt.next();
|
||||
Iterator<Cell> cellIt = row.cellIterator();
|
||||
|
||||
Hashtable<String, Integer> keys = new Hashtable<>();
|
||||
int index = 0;
|
||||
while (cellIt.hasNext()) {
|
||||
Cell c = cellIt.next();
|
||||
keys.put(c.toString(), index);
|
||||
}
|
||||
|
||||
rowIt.remove();
|
||||
result = new FileData(keys, rowIt);
|
||||
workbook.close();
|
||||
fis.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
package com.plannaplan.configutils;
|
||||
|
||||
import java.io.File;
|
||||
import com.plannaplan.models.FileData;
|
||||
|
||||
public class FileToDatabaseMigrator {
|
||||
public FileToDatabaseMigrator() {
|
||||
}
|
||||
|
||||
public void migrate(File file) {
|
||||
public void migrate(FileData data) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.plannaplan.models;
|
||||
|
||||
import java.util.Dictionary;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
|
||||
public class FileData {
|
||||
|
||||
private Dictionary<String, Integer> keys;
|
||||
private Iterator<Row> rows;
|
||||
|
||||
public FileData(Dictionary<String, Integer> keys, Iterator<Row> rows) {
|
||||
this.setKeys(keys);
|
||||
this.setRows(rows);
|
||||
}
|
||||
|
||||
public Iterator<Row> getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
public void setRows(Iterator<Row> rows) {
|
||||
this.rows = rows;
|
||||
}
|
||||
|
||||
public Dictionary<String, Integer> getKeys() {
|
||||
return keys;
|
||||
}
|
||||
|
||||
public void setKeys(Dictionary<String, Integer> keys) {
|
||||
this.keys = keys;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.plannaplan.configutils;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.plannaplan.models.FileData;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FileReaderTest {
|
||||
@Test
|
||||
public void shoulReturnNull() {
|
||||
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("Zajecia.xlsx");
|
||||
FileReader r = new FileReader(inputStream);
|
||||
FileData d = r.read();
|
||||
assertTrue(d.getRows().next().getCell(0).toString().equals("1.0"));
|
||||
assertTrue(d.getKeys().size() == 22);
|
||||
assertTrue(d != null);
|
||||
}
|
||||
}
|
BIN
buisnesslogic/src/test/resources/Zajecia.xlsx
Normal file
BIN
buisnesslogic/src/test/resources/Zajecia.xlsx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user