Skip to content
Snippets Groups Projects
Commit 20298f41 authored by gpawel's avatar gpawel
Browse files

LMS-2494 Flexible import/upload: xlsx support

SVN: 22759
parent 044143c8
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,7 @@ 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.xssf.usermodel.XSSFWorkbook;
import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
import ch.systemsx.cisd.common.exceptions.UserFailureException;
......@@ -76,12 +77,12 @@ public class ExcelFileSection
return end;
}
public static ExcelFileSection createFromInputStream(InputStream stream, String sectionName)
public static ExcelFileSection createFromInputStream(InputStream stream, String sectionName,
String fileName)
{
try
{
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream);
Workbook wb = new HSSFWorkbook(poifsFileSystem);
Workbook wb = getWorkBook(stream, fileName);
Sheet sheet = wb.getSheetAt(0);
return new ExcelFileSection(sheet, sectionName, 0, sheet.getLastRowNum());
} catch (IOException e)
......@@ -90,13 +91,26 @@ public class ExcelFileSection
}
}
public static List<ExcelFileSection> extractSections(InputStream stream, String excelSheetName)
private static final Workbook getWorkBook(InputStream stream, String fileName)
throws IOException
{
if (fileName.endsWith("xlsx"))
{
return new XSSFWorkbook(stream);
} else
{
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream);
return new HSSFWorkbook(poifsFileSystem);
}
}
public static List<ExcelFileSection> extractSections(InputStream stream, String excelSheetName,
String fileName)
{
List<ExcelFileSection> sections = new ArrayList<ExcelFileSection>();
try
{
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(stream);
Workbook wb = new HSSFWorkbook(poifsFileSystem);
Workbook wb = getWorkBook(stream, fileName);
Sheet sheet = null;
if (excelSheetName == null)
......
......@@ -191,17 +191,18 @@ public class SampleUploadSectionsParser
new ArrayList<BatchRegistrationResult>(uploadedFiles.size());
for (final NamedInputStream multipartFile : uploadedFiles)
{
if (multipartFile.getOriginalFilename().toLowerCase().endsWith("xls"))
final String fileName = multipartFile.getOriginalFilename().toLowerCase();
if (fileName.endsWith("xls") || fileName.endsWith("xlsx"))
{
List<ExcelFileSection> sampleSections = new ArrayList<ExcelFileSection>();
if (sampleType.isDefinedInFileEntityTypeCode())
{
sampleSections.addAll(ExcelFileSection.extractSections(
multipartFile.getInputStream(), excelSheetName));
multipartFile.getInputStream(), excelSheetName, fileName));
} else
{
sampleSections.add(ExcelFileSection.createFromInputStream(
multipartFile.getInputStream(), sampleType.getCode()));
multipartFile.getInputStream(), sampleType.getCode(), fileName));
}
int sampleCounter = 0;
Map<String, String> defaults = Collections.emptyMap();
......@@ -224,8 +225,7 @@ public class SampleUploadSectionsParser
+ fs.getSectionName() + ")";
final List<NewSample> loadedSamples =
excelFileLoader.load(fs.getSheet(), fs.getBegin(), fs.getEnd(),
multipartFile.getOriginalFilename() + sectionInFile,
defaults);
fileName + sectionInFile, defaults);
if (loadedSamples.size() > 0)
{
newSamples.add(new NewSamplesWithTypes(typeFromSection, loadedSamples));
......@@ -233,9 +233,9 @@ public class SampleUploadSectionsParser
}
}
}
results.add(new BatchRegistrationResult(multipartFile.getOriginalFilename(), String
.format("%s of %d sample(s) is complete.", operationKind.getDescription(),
sampleCounter)));
results.add(new BatchRegistrationResult(fileName, String.format(
"%s of %d sample(s) is complete.", operationKind.getDescription(),
sampleCounter)));
} else
{
List<FileSection> sampleSections = new ArrayList<FileSection>();
......@@ -269,9 +269,8 @@ public class SampleUploadSectionsParser
sampleSections.size() == 1 ? "" : " (section:"
+ fs.getSectionName() + ")";
final List<NewSample> loadedSamples =
tabFileLoader.load(
new DelegatedReader(reader, multipartFile
.getOriginalFilename() + sectionInFile), defaults);
tabFileLoader.load(new DelegatedReader(reader, fileName
+ sectionInFile), defaults);
if (loadedSamples.size() > 0)
{
newSamples.add(new NewSamplesWithTypes(typeFromSection, loadedSamples));
......@@ -279,9 +278,9 @@ public class SampleUploadSectionsParser
}
}
}
results.add(new BatchRegistrationResult(multipartFile.getOriginalFilename(), String
.format("%s of %d sample(s) is complete.", operationKind.getDescription(),
sampleCounter)));
results.add(new BatchRegistrationResult(fileName, String.format(
"%s of %d sample(s) is complete.", operationKind.getDescription(),
sampleCounter)));
}
}
return results;
......
......@@ -106,19 +106,21 @@ public class MaterialUploadSectionsParser
{
final List<BatchRegistrationResult> results =
new ArrayList<BatchRegistrationResult>(uploadedFiles.size());
for (final NamedInputStream multipartFile : uploadedFiles)
{
if (multipartFile.getOriginalFilename().toLowerCase().endsWith("xls"))
final String fileName = multipartFile.getOriginalFilename().toLowerCase();
if (fileName.endsWith("xls") || fileName.endsWith("xlsx"))
{
List<ExcelFileSection> materialSections = new ArrayList<ExcelFileSection>();
if (materialType.isDefinedInFileEntityTypeCode())
{
materialSections.addAll(ExcelFileSection.extractSections(
multipartFile.getInputStream(), excelSheetName));
multipartFile.getInputStream(), excelSheetName, fileName));
} else
{
materialSections.add(ExcelFileSection.createFromInputStream(
multipartFile.getInputStream(), materialType.getCode()));
multipartFile.getInputStream(), materialType.getCode(), fileName));
}
int materialCounter = 0;
Map<String, String> defaults = Collections.emptyMap();
......@@ -150,8 +152,7 @@ public class MaterialUploadSectionsParser
+ fs.getSectionName() + ")";
final List<NewMaterial> loadedMaterials =
excelFileLoader.load(fs.getSheet(), fs.getBegin(), fs.getEnd(),
multipartFile.getOriginalFilename() + sectionInFile,
defaults);
fileName + sectionInFile, defaults);
if (loadedMaterials.size() > 0)
{
newMaterials.add(new NewMaterialsWithTypes(typeFromSection,
......@@ -160,8 +161,8 @@ public class MaterialUploadSectionsParser
}
}
}
results.add(new BatchRegistrationResult(multipartFile.getOriginalFilename(), String
.format("Registration of %d material(s) is complete.", materialCounter)));
results.add(new BatchRegistrationResult(fileName, String.format(
"Registration of %d material(s) is complete.", materialCounter)));
} else
{
......@@ -205,9 +206,8 @@ public class MaterialUploadSectionsParser
materialSections.size() == 1 ? "" : " (section:"
+ fs.getSectionName() + ")";
final List<NewMaterial> loadedMaterials =
tabFileLoader.load(
new DelegatedReader(reader, multipartFile
.getOriginalFilename() + sectionInFile), defaults);
tabFileLoader.load(new DelegatedReader(reader, fileName
+ sectionInFile), defaults);
if (loadedMaterials.size() > 0)
{
newMaterials.add(new NewMaterialsWithTypes(typeFromSection,
......@@ -216,8 +216,8 @@ public class MaterialUploadSectionsParser
}
}
}
results.add(new BatchRegistrationResult(multipartFile.getOriginalFilename(), String
.format("Registration of %d material(s) is complete.", materialCounter)));
results.add(new BatchRegistrationResult(fileName, String.format(
"Registration of %d material(s) is complete.", materialCounter)));
}
}
return results;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment