-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Group 1 - Petclinic refactoring WIP #21
base: master
Are you sure you want to change the base?
Changes from all commits
65ad48e
c2e92b3
afed643
7fcd953
f44c7dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,18 +13,25 @@ | |
|
||
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") | ||
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<List<Pet>> importPets(@RequestBody String csv) { | ||
|
||
int i = 0; | ||
List<Pet> pets = new LinkedList<Pet>(); | ||
Pet pet; | ||
|
||
do { | ||
pet = new Pet(); | ||
List<String> 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<List<Pet>>(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<List<Pet>>(headers, HttpStatus.BAD_REQUEST); | ||
} | ||
return new ResponseEntity<List<Pet>>(pets, HttpStatus.OK); | ||
} | ||
|
||
field = ""; | ||
while (i < csv.length() && csv.charAt(i) != ';') { | ||
field += csv.charAt(i++); | ||
} | ||
i++; | ||
|
||
if (pet != null) { | ||
ArrayList<PetType> ts = (ArrayList<PetType>) 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<String> convert_csv_to_lines(String csv) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like your idea to create function |
||
{ | ||
List<String> 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<Owner> 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<List<Pet>>(headers, HttpStatus.BAD_REQUEST); | ||
} | ||
if (matchingOwners.size() > 1) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("errors", "Owner not unique"); | ||
return new ResponseEntity<List<Pet>>(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<String> segment_line_to_entries(String lines) | ||
{ | ||
List<String> entries; | ||
|
||
entries = Arrays.asList(lines.split(";")); | ||
|
||
return entries; | ||
} | ||
|
||
|
||
|
||
private Pet convert_string_list_to_pet( List<String> 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<Owner> 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<PetType> get_pet_types_from_server() | ||
{ | ||
ArrayList<PetType> pet_type_list = (ArrayList<PetType>) clinicService.findPetTypes(); | ||
return pet_type_list; | ||
} | ||
|
||
private PetType search_for_pet_type_in_list(String entry) | ||
{ | ||
PetType pet_type = new PetType(); | ||
|
||
ArrayList<PetType> 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<Owner> 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<List<Pet>>(pets, HttpStatus.OK); | ||
private List<Owner> getMatchingOwners(String entry) | ||
{ | ||
List<Owner> matchingOwners = clinicService.findAllOwners() | ||
.stream() | ||
.filter(o -> o.getLastName().equals(entry)) | ||
.collect(Collectors.toList()); | ||
return matchingOwners; | ||
} | ||
|
||
private boolean isSamePet(Pet pet1, Pet pet2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that you did not use more than 2 indentations (except in this method |
||
{ | ||
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); | ||
} | ||
|
||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could think about extracting this content to a new method to separate the responsibilities further