diff --git a/src/main/java/org/springframework/samples/petclinic/rest/importcsv/ImportCSV.java b/src/main/java/org/springframework/samples/petclinic/rest/importcsv/ImportCSV.java index 4e174f4..c7bba0d 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/importcsv/ImportCSV.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/importcsv/ImportCSV.java @@ -1,5 +1,6 @@ package org.springframework.samples.petclinic.rest.importcsv; +import jdk.javadoc.internal.doclets.toolkit.taglets.UserTaglet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -26,6 +27,8 @@ public class ImportCSV { @Autowired private ClinicService clinicService; + private int currentPosition; + @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @RequestMapping(value = "importPets", method = RequestMethod.POST, @@ -33,107 +36,147 @@ public class ImportCSV { produces = "application/json") public ResponseEntity> importPets(@RequestBody String csv) { - int i = 0; + this.currentPosition = 0; + List pets = new LinkedList(); Pet pet; + boolean petInformationAvailable; do { pet = new Pet(); - String field = ""; - while (i < csv.length() && csv.charAt(i) != ';') { - field += csv.charAt(i++); - } - i++; + setPetName(pet, csv); - pet.setName(field); + ResponseEntity> invalidDate = setBirthDate(pet, csv); + if (invalidDate != null) return invalidDate; - field = ""; - while (i < csv.length() && csv.charAt(i) != ';') { - field += csv.charAt(i++); - } - i++; - - try { - pet.setBirthDate((new SimpleDateFormat("yyyy-MM-dd")).parse(field)); - } catch (ParseException e) { - HttpHeaders headers = new HttpHeaders(); - headers.add("errors", "date " + field + " not valid"); - return new ResponseEntity>(headers, HttpStatus.BAD_REQUEST); - } + setPetType(pet, csv); - field = ""; - while (i < csv.length() && csv.charAt(i) != ';') { - field += csv.charAt(i++); - } - i++; - - if (pet != null) { - ArrayList ts = (ArrayList) clinicService.findPetTypes(); - for (int j = 0; j < ts.size(); j++) { - if (ts.get(j).getName().toLowerCase().equals(field)) { - pet.setType(ts.get(j)); - break; - } - } - } + ResponseEntity> invalidOwner = setPetOwner(pet, csv); + if (invalidOwner != null) return invalidOwner; - field = ""; - while (i < csv.length() && (csv.charAt(i) != ';' && csv.charAt(i) != '\n')) { - field += csv.charAt(i++); - } + applyActionToPet(pet, csv); - if (pet != null) { - String owner = field; - List matchingOwners = clinicService.findAllOwners() - .stream() - .filter(o -> o.getLastName().equals(owner)) - .collect(Collectors.toList()); - - if (matchingOwners.size() == 0) { - HttpHeaders headers = new HttpHeaders(); - headers.add("errors", "Owner not found"); - return new ResponseEntity>(headers, HttpStatus.BAD_REQUEST); - } - if (matchingOwners.size() > 1) { - HttpHeaders headers = new HttpHeaders(); - headers.add("errors", "Owner not unique"); - return new ResponseEntity>(headers, HttpStatus.BAD_REQUEST); - } - pet.setOwner(matchingOwners.iterator().next()); - } + this.currentPosition++; - if (csv.charAt(i) == ';') { - i++; + pets.add(pet); - field = ""; - while (i < csv.length() && csv.charAt(i) != '\n') { - field += csv.charAt(i++); - } + petInformationAvailable = this.currentPosition < csv.length() && pet != null; + } while (petInformationAvailable); + + return new ResponseEntity>(pets, HttpStatus.OK); + } - if (field.toLowerCase().equals("add")) { - clinicService.savePet(pet); - } else { - for (Pet q : pet.getOwner().getPets()) { - if (q.getName().equals(pet.getName())) { - if (q.getType().getId().equals(pet.getType().getId())) { - if (pet.getBirthDate().equals(q.getBirthDate())) { - clinicService.deletePet(q); - } - } - } + private void removePetFromClinic(Pet pet) { + for (Pet q : pet.getOwner().getPets()) { + if (q.getName().equals(pet.getName())) { + if (q.getType().getId().equals(pet.getType().getId())) { + if (pet.getBirthDate().equals(q.getBirthDate())) { + clinicService.deletePet(q); } } + } + } + } - } else { + private void applyActionToPet(Pet pet, String csv) { + if (csv.charAt(this.currentPosition) == ';') { + this.currentPosition++; + + String method = readNextField(csv); + + if (method.toLowerCase().equals("add")) { clinicService.savePet(pet); + } else { + removePetFromClinic(pet); } - i++; - pets.add(pet); + } else { + clinicService.savePet(pet); + } + } - } while (i < csv.length() && pet != null); + private String readNextField(String csvString) { + String field = ""; + while (this.currentPosition < csvString.length() && (csvString.charAt(this.currentPosition) != ';' && csvString.charAt(this.currentPosition) != '\n')) { + field += csvString.charAt(this.currentPosition++); + } + return field; + } - return new ResponseEntity>(pets, HttpStatus.OK); + private ResponseEntity> generateErrorResponse(String errorMessage) { + HttpHeaders headers = new HttpHeaders(); + headers.add("errors", errorMessage); + return new ResponseEntity>(headers, HttpStatus.BAD_REQUEST); + } + + private ResponseEntity> setPetOwner(Pet pet, String csv) { + String petOwner = readNextField(csv); + + if (pet != null) { + String owner = petOwner; + List matchingOwners = getOwnerByLastName(owner); + + ResponseEntity> ownerNotFound = checkOwnerInDatabase(matchingOwners); + if (ownerNotFound != null) return ownerNotFound; + + ResponseEntity> ownerNotUnique = checkOwnerIsUnique(matchingOwners); + if (ownerNotUnique != null) return ownerNotUnique; + + pet.setOwner(matchingOwners.iterator().next()); + } + return null; + } + + private List getOwnerByLastName(String lastName) { + List matchingOwners = clinicService.findAllOwners() + .stream() + .filter(o -> o.getLastName().equals(lastName)) + .collect(Collectors.toList()); + return matchingOwners; + } + + private ResponseEntity> checkOwnerInDatabase(List owners) { + if (owners.size() == 0) { + return generateErrorResponse("Owner not found"); + } + return null; } + + private ResponseEntity> checkOwnerIsUnique(List owners) { + if (owners.size() > 1) { + return generateErrorResponse("Owner not unique"); + } + return null; + } + + private void setPetName(Pet pet, String csv) { + String petName = readNextField(csv); + pet.setName(petName); + } + + private ResponseEntity> setBirthDate(Pet pet, String csv) { + String birthDate = readNextField(csv); + try { + pet.setBirthDate((new SimpleDateFormat("yyyy-MM-dd")).parse(birthDate)); + } catch (ParseException e) { + return generateErrorResponse("date " + birthDate + " not valid"); + } + return null; + } + + private void setPetType(Pet pet, String csv){ + String petType = readNextField(csv); + + if (pet != null) { + ArrayList ts = (ArrayList) clinicService.findPetTypes(); + for (int j = 0; j < ts.size(); j++) { + if (ts.get(j).getName().toLowerCase().equals(petType)) { + pet.setType(ts.get(j)); + break; + } + } + } + } + }