|
| 1 | +package com.codedifferently.lesson12.factory.sherllin; |
| 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.BufferedReader; |
| 9 | +import java.io.FileReader; |
| 10 | +import java.io.IOException; |
| 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 | +@Service |
| 22 | +public class CsvDataLoader implements LibraryCsvDataLoader { |
| 23 | + |
| 24 | + @Override |
| 25 | + public LibraryDataModel loadData() { |
| 26 | + // Read media items |
| 27 | + List<MediaItemModel> mediaItems = readMediaItems("csv/media_items.csv"); |
| 28 | + |
| 29 | + // Read guests |
| 30 | + List<LibraryGuestModel> guests = readGuests("csv/guests.csv"); |
| 31 | + |
| 32 | + // Read checked-out items |
| 33 | + Map<String, List<CheckoutModel>> checkoutsByGuestEmail = |
| 34 | + readCheckoutItems("csv/checked_out_items.csv"); |
| 35 | + |
| 36 | + for (LibraryGuestModel guest : guests) { |
| 37 | + List<CheckoutModel> checkouts = |
| 38 | + checkoutsByGuestEmail.getOrDefault(guest.email, new ArrayList<>()); |
| 39 | + guest.checkedOutItems = checkouts; |
| 40 | + } |
| 41 | + |
| 42 | + // Construct LibraryDataModel instance with initialized fields |
| 43 | + LibraryDataModel libraryDataModel = new LibraryDataModel(); |
| 44 | + libraryDataModel.mediaItems = mediaItems; |
| 45 | + libraryDataModel.guests = guests; |
| 46 | + |
| 47 | + return libraryDataModel; |
| 48 | + } |
| 49 | + |
| 50 | + private List<MediaItemModel> readMediaItems(String filePath) { |
| 51 | + try (var reader = |
| 52 | + new BufferedReader(new FileReader(new ClassPathResource(filePath).getFile()))) { |
| 53 | + var items = new ArrayList<MediaItemModel>(); |
| 54 | + String line; |
| 55 | + boolean isFirstLine = true; |
| 56 | + while ((line = reader.readLine()) != null) { |
| 57 | + if (isFirstLine) { |
| 58 | + isFirstLine = false; |
| 59 | + continue; |
| 60 | + } |
| 61 | + var parts = line.split(","); |
| 62 | + var item = new MediaItemModel(); |
| 63 | + item.type = parts[0]; |
| 64 | + item.id = UUID.fromString(parts[1]); |
| 65 | + item.title = parts[2]; |
| 66 | + item.isbn = parts[3]; |
| 67 | + item.authors = Arrays.asList(parts[4].split(",")); |
| 68 | + item.pages = parts.length > 5 && !parts[5].isEmpty() ? Integer.parseInt(parts[5]) : 0; |
| 69 | + item.runtime = parts.length > 6 && !parts[6].isEmpty() ? Integer.parseInt(parts[6]) : 0; |
| 70 | + item.edition = parts.length > 7 ? parts[7] : ""; |
| 71 | + items.add(item); |
| 72 | + } |
| 73 | + return items; |
| 74 | + } catch (IOException e) { |
| 75 | + throw new RuntimeException("Failed to read media items", e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private List<LibraryGuestModel> readGuests(String filePath) { |
| 80 | + try (var reader = |
| 81 | + new BufferedReader(new FileReader(new ClassPathResource(filePath).getFile()))) { |
| 82 | + var items = new ArrayList<LibraryGuestModel>(); |
| 83 | + String line; |
| 84 | + boolean isFirstLine = true; |
| 85 | + while ((line = reader.readLine()) != null) { |
| 86 | + if (isFirstLine) { |
| 87 | + isFirstLine = false; |
| 88 | + continue; |
| 89 | + } |
| 90 | + var parts = line.split(","); |
| 91 | + var item = new LibraryGuestModel(); |
| 92 | + item.type = parts[0]; |
| 93 | + item.name = parts[1]; |
| 94 | + item.email = parts[2]; |
| 95 | + items.add(item); |
| 96 | + } |
| 97 | + return items; |
| 98 | + } catch (IOException e) { |
| 99 | + throw new RuntimeException("Failed to read guests", e); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private Map<String, List<CheckoutModel>> readCheckoutItems(String filePath) { |
| 104 | + try (var reader = |
| 105 | + new BufferedReader(new FileReader(new ClassPathResource(filePath).getFile()))) { |
| 106 | + var checkoutMap = new HashMap<String, List<CheckoutModel>>(); |
| 107 | + String line; |
| 108 | + boolean isFirstLine = true; |
| 109 | + while ((line = reader.readLine()) != null) { |
| 110 | + if (isFirstLine) { |
| 111 | + isFirstLine = false; |
| 112 | + continue; |
| 113 | + } |
| 114 | + var parts = line.split(","); |
| 115 | + var checkoutModel = new CheckoutModel(); |
| 116 | + checkoutModel.itemId = UUID.fromString(parts[1]); |
| 117 | + checkoutModel.dueDate = Instant.parse(parts[2]); |
| 118 | + |
| 119 | + List<CheckoutModel> checkoutModels = checkoutMap.getOrDefault(parts[0], new ArrayList<>()); |
| 120 | + |
| 121 | + checkoutModels.add(checkoutModel); |
| 122 | + |
| 123 | + checkoutMap.put(parts[0], checkoutModels); |
| 124 | + } |
| 125 | + return checkoutMap; |
| 126 | + } catch (IOException e) { |
| 127 | + throw new RuntimeException("Failed to read checkout items", e); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments