From 65ad48e7589df7e67865b15b474b4fbd54706ecb Mon Sep 17 00:00:00 2001 From: Flo Date: Mon, 6 Sep 2021 16:55:30 +0200 Subject: [PATCH 1/4] added new functions to structure the process of converting csv --- .../petclinic/rest/importcsv/ImportCSV.java | 97 +++++++++++++++++-- 1 file changed, 88 insertions(+), 9 deletions(-) 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..34057f1 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,16 @@ 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") @@ -41,19 +46,20 @@ public ResponseEntity> importPets(@RequestBody String csv) { pet = new Pet(); String field = ""; - while (i < csv.length() && csv.charAt(i) != ';') { + +/* while (i < csv.length() && csv.charAt(i) != ';') { field += csv.charAt(i++); } i++; pet.setName(field); - - field = ""; +*/ + /*field = ""; while (i < csv.length() && csv.charAt(i) != ';') { field += csv.charAt(i++); } - i++; - + i++;*/ +/* try { pet.setBirthDate((new SimpleDateFormat("yyyy-MM-dd")).parse(field)); } catch (ParseException e) { @@ -61,7 +67,7 @@ public ResponseEntity> importPets(@RequestBody String csv) { headers.add("errors", "date " + field + " not valid"); return new ResponseEntity>(headers, HttpStatus.BAD_REQUEST); } - +*/ field = ""; while (i < csv.length() && csv.charAt(i) != ';') { field += csv.charAt(i++); @@ -136,4 +142,77 @@ public ResponseEntity> importPets(@RequestBody String csv) { return new ResponseEntity>(pets, HttpStatus.OK); } + + + + private List segment_csv_int_string_list(String csv) + { + List entries; + + entries = Arrays.asList(csv.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))); + + 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 j = 0; j < pet_type_list.size(); j++) + { + if (pet_type_list.get(j).getName().toLowerCase().equals(entry)) + { + pet_type= pet_type_list.get(j); + break; + } + } + + return pet_type; + + } + + + /* + ArrayList ts = (ArrayList) clinicService.findPetTypes(); + + } +*/ + + } From c2e92b3d0c9f070ffa43d17b407cd5525502148b Mon Sep 17 00:00:00 2001 From: CMustard Date: Tue, 7 Sep 2021 08:42:30 +0200 Subject: [PATCH 2/4] Added setType, setOwner and removePet --- .../petclinic/rest/importcsv/ImportCSV.java | 167 +++++++----------- 1 file changed, 64 insertions(+), 103 deletions(-) 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 34057f1..46b0ff9 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 @@ -30,6 +30,7 @@ public class ImportCSV { @Autowired private ClinicService clinicService; + private HttpHeaders headers = new HttpHeaders(); @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @RequestMapping(value = "importPets", @@ -38,114 +39,28 @@ public class ImportCSV { produces = "application/json") public ResponseEntity> importPets(@RequestBody String csv) { - int i = 0; + /*int i = 0;*/ List pets = new LinkedList(); Pet pet; - do { - pet = new Pet(); + List splitCSV = segment_csv_into_string_list(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++; - - pet.setName(field); -*/ - /*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); - } -*/ - 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; - } - } - } - - field = ""; - while (i < csv.length() && (csv.charAt(i) != ';' && csv.charAt(i) != '\n')) { - field += csv.charAt(i++); - } - - 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()); - } - - 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); - } - } - } - } - } - - } else { - clinicService.savePet(pet); - } - i++; - - pets.add(pet); + if (splitCSV.get(4).toLowerCase().equals("add")) { + clinicService.savePet(pet); + } else { + removePetFromOwner(pet); + } - } while (i < csv.length() && pet != null); + pets.add(pet); return new ResponseEntity>(pets, HttpStatus.OK); } - - private List segment_csv_int_string_list(String csv) + private List segment_csv_into_string_list(String csv) { List entries; @@ -154,11 +69,20 @@ private List segment_csv_int_string_list(String csv) return entries; } - private Pet convert_string_list_to_pet( List list) + 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))); + + // 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; } @@ -204,15 +128,52 @@ private PetType search_for_pet_type_in_list(String entry) } return pet_type; - } + 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; + } + } - /* - ArrayList ts = (ArrayList) clinicService.findPetTypes(); + 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); + } + + } + } } From afed6432a9e416569a3032f4ddb081f6ef2d21a4 Mon Sep 17 00:00:00 2001 From: CMustard Date: Tue, 7 Sep 2021 09:57:17 +0200 Subject: [PATCH 3/4] Changed variable name from j to petIndex --- .../samples/petclinic/rest/importcsv/ImportCSV.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 46b0ff9..eacafb0 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 @@ -118,11 +118,11 @@ private PetType search_for_pet_type_in_list(String entry) ArrayList pet_type_list = get_pet_types_from_server(); - for (int j = 0; j < pet_type_list.size(); j++) + for (int petIndex = 0; petIndex < pet_type_list.size(); petIndex++) { - if (pet_type_list.get(j).getName().toLowerCase().equals(entry)) + if (pet_type_list.get(petIndex).getName().toLowerCase().equals(entry)) { - pet_type= pet_type_list.get(j); + pet_type= pet_type_list.get(petIndex); break; } } From f44c7dd43b29d1696f0872cc4e63d7af46d0e622 Mon Sep 17 00:00:00 2001 From: Flo Date: Tue, 7 Sep 2021 10:31:47 +0200 Subject: [PATCH 4/4] merged changes --- .../petclinic/rest/importcsv/ImportCSV.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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 eacafb0..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 @@ -17,6 +17,7 @@ //removed wildcard import import java.util.*; + import java.util.stream.Collectors; /* This class implements the Petclionic Service in the backend. @@ -39,11 +40,10 @@ public class ImportCSV { produces = "application/json") public ResponseEntity> importPets(@RequestBody String csv) { - /*int i = 0;*/ List pets = new LinkedList(); Pet pet; - List splitCSV = segment_csv_into_string_list(csv); + 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); @@ -59,18 +59,31 @@ public ResponseEntity> importPets(@RequestBody String csv) { return new ResponseEntity>(pets, HttpStatus.OK); } + private List convert_csv_to_lines(String csv) + { + List lines; + + lines = Arrays.asList(csv.split("\n")); + + return lines; + + } + - private List segment_csv_into_string_list(String csv) + private List segment_line_to_entries(String lines) { List entries; - entries = Arrays.asList(csv.split(";")); + 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)));