Skip to content

Commit

Permalink
#22 add csv parser
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Feb 15, 2015
1 parent b572302 commit 81d6690
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
22 changes: 12 additions & 10 deletions src/main/java/alfio/controller/api/AdminApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import alfio.model.user.User;
import alfio.util.ValidationResult;
import alfio.util.Validator;
import com.opencsv.CSVReader;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
Expand All @@ -40,13 +41,11 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.security.Principal;
import java.util.*;
import java.util.regex.MatchResult;
import java.util.stream.Collectors;

import static alfio.model.system.ConfigurationKeys.MAPS_CLIENT_API_KEY;
Expand Down Expand Up @@ -197,17 +196,20 @@ public String deletePendingPayment(@PathVariable("eventName") String eventName,
}

@RequestMapping(value = "/events/{eventName}/pending-payments/bulk-confirmation", method = POST)
public List<Triple<Boolean, String, String>> bulkConfirmation(@PathVariable("eventName") String eventName, Principal principal, @RequestParam("file") MultipartFile file) throws IOException {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
public List<Triple<Boolean, String, String>> bulkConfirmation(@PathVariable("eventName") String eventName,
Principal principal,
@RequestParam("file") MultipartFile file) throws IOException {

try(InputStreamReader isr = new InputStreamReader(file.getInputStream())) {
CSVReader reader = new CSVReader(isr);
String username = principal.getName();
return reader.lines()
return reader.readAll().stream()
.map(line -> {
String reservationID = null;
try (Scanner scanner = new Scanner(line)) {
scanner.findInLine("([a-zA-Z0-9\\-]+);(\\d+(\\.\\d+)?)");
MatchResult match = scanner.match();
reservationID = match.group(1);
eventManager.confirmPayment(eventName, reservationID, new BigDecimal(match.group(2)), username);
try {
Validate.isTrue(line.length >= 2);
reservationID = line[0];
eventManager.confirmPayment(eventName, reservationID, new BigDecimal(line[1]), username);
return Triple.of(Boolean.TRUE, reservationID, "");
} catch (Exception e) {
return Triple.of(Boolean.FALSE, Optional.ofNullable(reservationID).orElse(""), e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,22 @@ <h3>Bulk confirmation</h3>
<small class="text-muted">Upload CSV file(s) with received payments</small>
</div>

<div class="drop-file-zone wMarginBottom well" data-accept="text/*" data-ng-file-change="uploadFiles($files)" data-ng-file-drop data-drag-over-class="drop-file-zone-hover">
<div>
<h2>File Specifications:</h2>
<h3>General</h3>
<span>Please create a CSV file without header, using commas (<strong>,</strong>) as separator, double quotes (<strong>"</strong>)as quote character and backslash (<strong>\</strong>) as escaping character</span>
<h3>Row specification</h3>
<pre>
<span class="text-info">abcd-efghi-jklm</span><strong>,</strong><span class="text-danger">10.00</span>

where:

<span class="text-info">abcd-efghi-jklm</span> is the partial or full reservation code
<span class="text-danger">10.00</span> is the amount of the payment
</pre>
</div>

<div class="drop-file-zone wMarginBottom well" data-accept="text/*" data-ng-file-change="uploadFiles($files)" data-ng-file-drop data-drag-over-class="'drop-file-zone-hover'">
Drop file(s) here
</div>

Expand Down

0 comments on commit 81d6690

Please sign in to comment.