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..08078eb 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 @@ -13,11 +13,17 @@ import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; + +//removed wildcard import +import java.util.*; + + import java.util.stream.Collectors; +/* This class implements the Petclionic Service in the backend. +* The input is a CSV format with semi-colons (;) as separator. +*/ + @RestController @CrossOrigin(exposedHeaders = "errors, content-type") @RequestMapping("api/import") @@ -25,6 +31,7 @@ public class ImportCSV { @Autowired private ClinicService clinicService; + private HttpHeaders headers = new HttpHeaders(); @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @RequestMapping(value = "importPets", @@ -33,107 +40,153 @@ public class ImportCSV { produces = "application/json") public ResponseEntity> importPets(@RequestBody String csv) { - int i = 0; List pets = new LinkedList(); Pet pet; - do { - pet = new Pet(); + List splitCSV = segment_line_to_entries(csv); + pet = convert_string_list_to_pet( splitCSV ); + // DON'T FORGET TO ADD ERROR HANDLING FOR pet.setOwner: + // return new ResponseEntity>(headers, HttpStatus.BAD_REQUEST); - String field = ""; - while (i < csv.length() && csv.charAt(i) != ';') { - field += csv.charAt(i++); - } - i++; + if (splitCSV.get(4).toLowerCase().equals("add")) { + clinicService.savePet(pet); + } else { + removePetFromOwner(pet); + } - pet.setName(field); + pets.add(pet); - 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); - } + return new ResponseEntity>(pets, HttpStatus.OK); + } - 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; - } - } - } + private List convert_csv_to_lines(String csv) + { + List lines; - field = ""; - while (i < csv.length() && (csv.charAt(i) != ';' && csv.charAt(i) != '\n')) { - field += csv.charAt(i++); - } + lines = Arrays.asList(csv.split("\n")); - 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()); - } + return lines; - if (csv.charAt(i) == ';') { - i++; + } - field = ""; - while (i < csv.length() && csv.charAt(i) != '\n') { - field += csv.charAt(i++); - } - 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 List segment_line_to_entries(String lines) + { + List entries; + + entries = Arrays.asList(lines.split(";")); + + return entries; + } + + + + private Pet convert_string_list_to_pet( List list ) + { + + Pet pet = new Pet(); + pet.setName(list.get(0)); + pet.setBirthDate(convert_birth_date_from_string(list.get(1))); + pet.setType(search_for_pet_type_in_list(list.get(2))); - } else { - clinicService.savePet(pet); + // ADD EXCEPTION FOR NON-UNIQUE OWNERS ! + List matchingOwners = getMatchingOwners(list.get(3)); + if ( isUniqueOwner(matchingOwners) ) + { + //OLD CODE: pet.setOwner(matchingOwners.iterator().next()); + pet.setOwner(matchingOwners.get(0)); + } + + return pet; + } + + private Date convert_birth_date_from_string(String birth_date_string) + { + Date date = new Date(); + + SimpleDateFormat simple_date_format = new SimpleDateFormat("yyyy-MM-dd"); + + try { + date = simple_date_format.parse(birth_date_string); + } + catch (ParseException e) { + HttpHeaders headers = new HttpHeaders(); + headers.add("errors", "date " + birth_date_string + " not valid"); + + } + + return date; + + } + + private ArrayList get_pet_types_from_server() + { + ArrayList pet_type_list = (ArrayList) clinicService.findPetTypes(); + return pet_type_list; + } + + private PetType search_for_pet_type_in_list(String entry) + { + PetType pet_type = new PetType(); + + ArrayList pet_type_list = get_pet_types_from_server(); + + for (int petIndex = 0; petIndex < pet_type_list.size(); petIndex++) + { + if (pet_type_list.get(petIndex).getName().toLowerCase().equals(entry)) + { + pet_type= pet_type_list.get(petIndex); + break; } - i++; + } - pets.add(pet); + return pet_type; + } - } while (i < csv.length() && pet != null); + private boolean isUniqueOwner(List matchingOwners) + { + if (matchingOwners.size() == 1) { + return true; + } else if (matchingOwners.size() == 0) { + HttpHeaders headers = new HttpHeaders(); + headers.add("errors", "Owner not found"); + return false; + } else { //if (matchingOwners.size() > 1) + HttpHeaders headers = new HttpHeaders(); + headers.add("errors", "Owner not unique"); + return false; + } + } - return new ResponseEntity>(pets, HttpStatus.OK); + private List getMatchingOwners(String entry) + { + List matchingOwners = clinicService.findAllOwners() + .stream() + .filter(o -> o.getLastName().equals(entry)) + .collect(Collectors.toList()); + return matchingOwners; } + + private boolean isSamePet(Pet pet1, Pet pet2) + { + if (pet1.getName().equals(pet2.getName())) { + if (pet1.getType().getId().equals(pet2.getType().getId())) { + if (pet2.getBirthDate().equals(pet1.getBirthDate())) { + return true; + } + } + } + return false; + } + + private void removePetFromOwner(Pet pet) + { + for (Pet petFromOwnersList : pet.getOwner().getPets()) { + if (isSamePet(petFromOwnersList, pet)) { + clinicService.deletePet(petFromOwnersList); + } + + } + } + }