|
| 1 | +package com.codedifferently.lesson12.factory.vicentevigueras; |
| 2 | + |
| 3 | +import com.codedifferently.lesson12.factory.LibraryCsvDataLoader; |
| 4 | +import com.codedifferently.lesson12.models.CheckoutModel; |
| 5 | +import com.codedifferently.lesson12.models.LibraryDataModel; |
| 6 | +import com.codedifferently.lesson12.models.LibraryGuestModel; |
| 7 | +import com.codedifferently.lesson12.models.MediaItemModel; |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.time.Instant; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.UUID; |
| 18 | +import org.springframework.core.io.ClassPathResource; |
| 19 | +import org.springframework.stereotype.Service; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author vscode |
| 23 | + */ |
| 24 | +@Service |
| 25 | +public class CsvDataLoader implements LibraryCsvDataLoader { |
| 26 | + |
| 27 | + @Override |
| 28 | + public LibraryDataModel loadData() throws IOException { |
| 29 | + // Create object named model that is an instance of LibraryDataModel |
| 30 | + var model = new LibraryDataModel(); |
| 31 | + // Creates a list and loads with everything inside of file path |
| 32 | + model.mediaItems = readMediaItems("csv/media_items.csv"); |
| 33 | + model.guests = readGuests("csv/guests.csv"); |
| 34 | + Map<String, List<CheckoutModel>> checkoutsByGuestEmail = |
| 35 | + getCheckedOutItems("csv/checked_out_items.csv"); |
| 36 | + for (var guest : model.guests) { |
| 37 | + var checkouts = checkoutsByGuestEmail.get(guest.email); |
| 38 | + if (checkouts == null) { |
| 39 | + checkouts = new ArrayList<>(); |
| 40 | + } |
| 41 | + guest.checkedOutItems = checkouts; |
| 42 | + } |
| 43 | + |
| 44 | + return model; |
| 45 | + } |
| 46 | + |
| 47 | + // Read data after loading it. |
| 48 | + private List<MediaItemModel> readMediaItems(String filePath) throws IOException { |
| 49 | + String[] eachLine = getFileContent(filePath).split("\n"); |
| 50 | + |
| 51 | + var items = new ArrayList<MediaItemModel>(); |
| 52 | + |
| 53 | + for (int i = 1; i < eachLine.length; i++) { |
| 54 | + String[] parts = eachLine[i].split(",", -1); |
| 55 | + var item = new MediaItemModel(); |
| 56 | + |
| 57 | + // Parse as proper data types |
| 58 | + // if else statements to handle empty spots |
| 59 | + item.type = parts[0]; |
| 60 | + item.id = UUID.fromString(parts[1]); |
| 61 | + item.title = parts[2]; |
| 62 | + if (parts.length > 3) { |
| 63 | + item.isbn = parts[3]; |
| 64 | + } |
| 65 | + if (parts.length > 4) { |
| 66 | + item.authors = Arrays.asList(parts[4]); |
| 67 | + } |
| 68 | + if (parts.length > 5) { |
| 69 | + item.pages = Integer.parseInt(parts[5].isEmpty() ? "0" : parts[5]); |
| 70 | + } |
| 71 | + if (parts.length > 6) { |
| 72 | + item.runtime = Integer.parseInt(parts[6].isEmpty() ? "0" : parts[6]); |
| 73 | + } |
| 74 | + if (parts.length > 7) { |
| 75 | + item.edition = parts[7]; |
| 76 | + } |
| 77 | + items.add(item); |
| 78 | + } |
| 79 | + return items; |
| 80 | + } |
| 81 | + |
| 82 | + private List<LibraryGuestModel> readGuests(String filePath) throws IOException { |
| 83 | + String[] eachLine = getFileContent(filePath).split("\n"); |
| 84 | + |
| 85 | + var guests = new ArrayList<LibraryGuestModel>(); |
| 86 | + |
| 87 | + for (int i = 1; i < eachLine.length; i++) { |
| 88 | + String[] parts = eachLine[i].split(",", -1); |
| 89 | + var guest = new LibraryGuestModel(); |
| 90 | + guest.type = parts[0]; |
| 91 | + guest.name = parts[1]; |
| 92 | + guest.email = parts[2]; |
| 93 | + guests.add(guest); |
| 94 | + } |
| 95 | + return guests; |
| 96 | + } |
| 97 | + |
| 98 | + private Map<String, List<CheckoutModel>> getCheckedOutItems(String filePath) throws IOException { |
| 99 | + String fileContent = getFileContent(filePath); |
| 100 | + |
| 101 | + Map<String, List<CheckoutModel>> checkedOutItems = new HashMap<>(); |
| 102 | + String[] eachLine = fileContent.split("\n"); |
| 103 | + |
| 104 | + for (int i = 1; i < eachLine.length; i++) { |
| 105 | + // separate elements |
| 106 | + String[] parts = eachLine[i].split(",", -1); |
| 107 | + |
| 108 | + // read parts of the line |
| 109 | + String email = parts[0]; |
| 110 | + |
| 111 | + CheckoutModel newCheckoutModel = new CheckoutModel(); |
| 112 | + newCheckoutModel.itemId = UUID.fromString(parts[1]); |
| 113 | + newCheckoutModel.dueDate = Instant.parse(parts[2]); |
| 114 | + |
| 115 | + List<CheckoutModel> existingCheckoutModel = checkedOutItems.get(email); |
| 116 | + // validate there is not checkout items in the map |
| 117 | + if (existingCheckoutModel == null) { |
| 118 | + existingCheckoutModel = new ArrayList<>(); |
| 119 | + } |
| 120 | + existingCheckoutModel.add(newCheckoutModel); |
| 121 | + // store full thing in the map |
| 122 | + checkedOutItems.put(email, existingCheckoutModel); |
| 123 | + } |
| 124 | + |
| 125 | + return checkedOutItems; |
| 126 | + } |
| 127 | + |
| 128 | + private String getFileContent(String filePath) throws IOException { |
| 129 | + File myResource = new ClassPathResource(filePath).getFile(); |
| 130 | + return new String(Files.readAllBytes(myResource.toPath())); |
| 131 | + } |
| 132 | +} |
0 commit comments