Skip to content

Commit

Permalink
xslx support in bulk import
Browse files Browse the repository at this point in the history
  • Loading branch information
VSydor committed Jul 18, 2023
1 parent 27ac4ce commit 84fd361
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,7 @@ public Response bulkImport(
SecurityUtil.verifyApiKey(env);

// Important to do this outside of the new thread -- ensures the InputStream is still open.
CSVParser csvParser = CSVParser.parse(
inputStream,
Charset.defaultCharset(),
CSVFormat.DEFAULT
.withFirstRecordAsHeader()
.withIgnoreHeaderCase()
.withTrim()
);
List<Map<String, String>> data = new ArrayList<>();
for (CSVRecord csvRecord : csvParser) {
data.add(csvRecord.toMap());
}

List<Map<String, String>> data = toListOfMap(inputStream, fileDisposition);
List<CrmImportEvent> importEvents = CrmImportEvent.fromGeneric(data);

Runnable thread = () -> {
Expand Down Expand Up @@ -201,19 +189,7 @@ public Response bulkImportFBFundraisers(
SecurityUtil.verifyApiKey(env);

// Important to do this outside of the new thread -- ensures the InputStream is still open.
CSVParser csvParser = CSVParser.parse(
inputStream,
Charset.defaultCharset(),
CSVFormat.DEFAULT
.withFirstRecordAsHeader()
.withIgnoreHeaderCase()
.withTrim()
);
List<Map<String, String>> data = new ArrayList<>();
for (CSVRecord csvRecord : csvParser) {
data.add(csvRecord.toMap());
}

List<Map<String, String>> data = toListOfMap(inputStream, fileDisposition);
List<CrmImportEvent> importEvents = CrmImportEvent.fromFBFundraiser(data);

Runnable thread = () -> {
Expand Down Expand Up @@ -247,7 +223,7 @@ public Response bulkImportGreaterGiving(
// Important to do this outside of the new thread -- ensures the InputStream is still open.
// Excel is expected. Greater Giving has an Excel report export purpose built for SFDC.
// TODO: But, what if a different CRM is targeted?
List<Map<String, String>> data = Utils.getExcelData(inputStream);
List<Map<String, String>> data = toListOfMap(inputStream, fileDisposition);

List<CrmImportEvent> importEvents = CrmImportEvent.fromGreaterGiving(data);

Expand Down Expand Up @@ -436,4 +412,29 @@ public Response getContactFields(

return Response.status(200).entity(filteredList).build();
}

private static List<Map<String, String>> toListOfMap(InputStream inputStream, FormDataContentDisposition fileDisposition) throws Exception {
String fileExtension = Utils.getFileExtension(fileDisposition.getFileName());
List<Map<String, String>> data = new ArrayList<>();

if ("csv".equals(fileExtension)) {
CSVParser csvParser = CSVParser.parse(
inputStream,
Charset.defaultCharset(),
CSVFormat.DEFAULT
.withFirstRecordAsHeader()
.withIgnoreHeaderCase()
.withTrim()
);
data = new ArrayList<>();
for (CSVRecord csvRecord : csvParser) {
data.add(csvRecord.toMap());
}
} else if ("xlsx".equals(fileExtension)) {
data = Utils.getExcelData(inputStream, 0);
} else {
log.warn("Unsupported file extension '{}'!", fileExtension);
}
return data;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/impactupgrade/nucleus/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,16 @@ public static String formatDuration(Duration duration) {
(absSeconds % 3600) / 60);
return seconds < 0 ? "-" + positive : positive;
}

public static String getFileExtension(String fileName) {
if (Strings.isNullOrEmpty(fileName)) {
return null;
}
String extension = "";
int index = fileName.lastIndexOf('.');
if (index > 0) {
extension = fileName.substring(index + 1);
}
return extension;
}
}

0 comments on commit 84fd361

Please sign in to comment.