|
| 1 | +package com.codedifferently.lesson12.factory.mekhiloader; |
| 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.IOException; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.time.Instant; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Collections; |
| 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("LibraryCsvDataLoader") |
| 22 | +public class CsvDataLoader implements LibraryCsvDataLoader { |
| 23 | + |
| 24 | + private static final int MEDIA_ITEM_FIELDS_COUNT = 8; |
| 25 | + |
| 26 | + private static final String MEDIA_ITEMS_FILE_PATH = "csv/media_items.csv"; |
| 27 | + private static final String GUESTS_FILE_PATH = "csv/guests.csv"; |
| 28 | + private static final String CHECKED_OUT_ITEMS_FILE_PATH = "csv/checked_out_items.csv"; |
| 29 | + |
| 30 | + @Override |
| 31 | + public LibraryDataModel loadData() { |
| 32 | + LibraryDataModel libraryData = new LibraryDataModel(); |
| 33 | + |
| 34 | + libraryData.guests = readGuestList(GUESTS_FILE_PATH); |
| 35 | + libraryData.mediaItems = readMediaItems(MEDIA_ITEMS_FILE_PATH); |
| 36 | + Map<String, List<CheckoutModel>> checkoutsByGuestEmail = |
| 37 | + readCheckedOutByEmail(CHECKED_OUT_ITEMS_FILE_PATH); |
| 38 | + |
| 39 | + for (var guest : libraryData.guests) { |
| 40 | + var checkouts = checkoutsByGuestEmail.getOrDefault(guest.email, Collections.emptyList()); |
| 41 | + guest.checkedOutItems = new ArrayList<>(checkouts); |
| 42 | + } |
| 43 | + |
| 44 | + return libraryData; |
| 45 | + } |
| 46 | + |
| 47 | + public List<MediaItemModel> readMediaItems(String path) { |
| 48 | + try (var reader = Files.newBufferedReader(Path.of(new ClassPathResource(path).getURI()))) { |
| 49 | + var items = new ArrayList<MediaItemModel>(); |
| 50 | + String line; |
| 51 | + reader.readLine(); |
| 52 | + while ((line = reader.readLine()) != null) { |
| 53 | + var parts = line.split(",", MEDIA_ITEM_FIELDS_COUNT); |
| 54 | + var item = new MediaItemModel(); |
| 55 | + |
| 56 | + item.type = parts[0]; |
| 57 | + item.id = UUID.fromString(parts[1]); |
| 58 | + item.title = parts[2]; |
| 59 | + item.isbn = parts[3]; |
| 60 | + item.authors = List.of(parts[4]); |
| 61 | + |
| 62 | + item.pages = parts[5].isEmpty() ? 0 : Integer.parseInt(parts[5]); |
| 63 | + item.runtime = parts[6].isEmpty() ? 0 : Integer.parseInt(parts[6]); |
| 64 | + item.edition = parts[7]; |
| 65 | + |
| 66 | + items.add(item); |
| 67 | + } |
| 68 | + return items; |
| 69 | + } catch (IOException e) { |
| 70 | + throw new RuntimeException("Failed to read media items from: " + path, e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private List<LibraryGuestModel> readGuestList(String path) { |
| 75 | + try (var reader = Files.newBufferedReader(Path.of(new ClassPathResource(path).getURI()))) { |
| 76 | + var guests = new ArrayList<LibraryGuestModel>(); |
| 77 | + String line; |
| 78 | + reader.readLine(); |
| 79 | + while ((line = reader.readLine()) != null) { |
| 80 | + var parts = line.split(","); |
| 81 | + var guest = new LibraryGuestModel(); |
| 82 | + |
| 83 | + guest.type = parts[0]; |
| 84 | + guest.name = parts[1]; |
| 85 | + guest.email = parts[2]; |
| 86 | + |
| 87 | + guests.add(guest); |
| 88 | + } |
| 89 | + return guests; |
| 90 | + } catch (IOException e) { |
| 91 | + throw new RuntimeException("Failed to read guest list from: " + path, e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private Map<String, List<CheckoutModel>> readCheckedOutByEmail(String path) { |
| 96 | + try (var reader = Files.newBufferedReader(Path.of(new ClassPathResource(path).getURI()))) { |
| 97 | + var checkedOutItems = new HashMap<String, List<CheckoutModel>>(); |
| 98 | + String line; |
| 99 | + reader.readLine(); |
| 100 | + while ((line = reader.readLine()) != null) { |
| 101 | + var parts = line.split(","); |
| 102 | + var item = new CheckoutModel(); |
| 103 | + |
| 104 | + item.itemId = UUID.fromString(parts[1]); |
| 105 | + item.dueDate = Instant.parse(parts[2]); |
| 106 | + |
| 107 | + checkedOutItems.computeIfAbsent(parts[0], k -> new ArrayList<>()).add(item); |
| 108 | + } |
| 109 | + return checkedOutItems; |
| 110 | + } catch (IOException e) { |
| 111 | + throw new RuntimeException("Failed to read checked out items from: " + path, e); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments