Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dreske committed Jan 17, 2023
1 parent f7e0a09 commit 7b3c073
Showing 1 changed file with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@Controller
Expand All @@ -50,6 +54,18 @@ public class JobsController {

private final AutowireCapableBeanFactory beanFactory;

private interface DateParser {
LocalDateTime parse(String value);
}

private final List<DateParser> DATA_PARSERS = Arrays.asList(
value -> LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss", Locale.GERMANY)),
value -> LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm", Locale.GERMANY)).withSecond(59),
value -> LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss", Locale.GERMANY)),
value -> LocalDateTime.parse(value, DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm", Locale.GERMANY)).withSecond(59),
value -> LocalDate.parse(value, DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.GERMANY)).atTime(23, 59, 59)
);

public JobsController(CancellationsService cancellationsService, JobRepository jobRepository, JobEntryRepository jobEntryRepository, JobSummaryRepository jobSummaryRepository, AutowireCapableBeanFactory beanFactory) {
this.cancellationsService = cancellationsService;
this.jobRepository = jobRepository;
Expand Down Expand Up @@ -166,15 +182,19 @@ public String uploadJob(@RequestParam("file") MultipartFile file,
entry.setPartnerId(tokens[0]);
entry.setReceiver(tokens[8]);
entry.setAttachmentFilename(tokens[0] + ".pdf");
try {
entry.setFinalDeletionRequest(LocalDate.parse(tokens[11], DateTimeFormatter.ofPattern("dd.MM.yyyy")).atTime(23, 59, 59));
} catch (DateTimeParseException e1) {

for (Iterator<DateParser> iterator = DATA_PARSERS.iterator(); iterator.hasNext(); ) {
DateParser dateParser = iterator.next();
try {
entry.setFinalDeletionRequest(LocalDateTime.parse(tokens[11], DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm")).withSecond(59));
} catch (Exception e2) {
entry.setFinalDeletionRequest(LocalDateTime.parse(tokens[11], DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")));
entry.setFinalDeletionRequest(dateParser.parse(tokens[11]));
break;
} catch (DateTimeParseException e) {
if (!iterator.hasNext()) {
throw e;
}
}
}

jobEntryRepository.save(entry);
}
} catch (Exception e) {
Expand All @@ -183,3 +203,4 @@ public String uploadJob(@RequestParam("file") MultipartFile file,
return "redirect:/cancellations";
}
}

0 comments on commit 7b3c073

Please sign in to comment.