StudentLoanManagement
DataInitializer.java
Go to the documentation of this file.
1package com.student_loan;
2
3import java.util.ArrayList;
4import java.util.Date;
5import java.util.List;
6
7import org.slf4j.LoggerFactory;
8import org.springframework.boot.CommandLineRunner;
9import org.springframework.context.annotation.Bean;
10import org.springframework.context.annotation.Configuration;
11import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
12
13import com.student_loan.model.Item;
14import com.student_loan.model.Loan;
15import com.student_loan.model.User;
16import com.student_loan.repository.ItemRepository;
17import com.student_loan.repository.LoanRepository;
18import com.student_loan.repository.UserRepository;
19import com.student_loan.service.NotificationService;
20import java.util.Optional;
21
22@Configuration
23public class DataInitializer {
24
25 private final BCryptPasswordEncoder passwordEncoder;
26
27 public DataInitializer(BCryptPasswordEncoder passwordEncoder) {
28 this.passwordEncoder = passwordEncoder;
29 }
30
31 //Logger
32 private static final org.slf4j.Logger logger = LoggerFactory.getLogger(DataInitializer.class);
33 @Bean
34 public CommandLineRunner initData(UserRepository userRepository, ItemRepository itemRepository, LoanRepository loanRepository ,NotificationService notificationService) {
35
36 return args -> {
37 System.out.println("Initializing data...");
38 List<User> users = createUsers();
39 saveUsers(users, userRepository);
40
41 List<Item> items = createItems(userRepository);
42 saveItems(items, itemRepository);
43
44 List<Loan> loans = createLoans(userRepository, itemRepository);
45 saveLoans(loans, loanRepository, notificationService, userRepository, itemRepository);
46
47 System.out.println("Data initialized successfully.");
48 };
49 }
50
51 private List<User> createUsers() {
52 List <User> users = new ArrayList<>();
53
54 // Set of users to insert
55 users.add(new User(null, "Ana García", "ana.garcia@email.com", passwordEncoder.encode("dF!94vH*2kQ#bR"), "612 345 678", "Calle de Gran Vía, 23, 28013 Madrid", User.DegreeType.UNIVERSITY_DEGREE, 3, 0, 4.5, false));
56 users.add(new User(null, "Carlos Pérez", "carlos.perez@email.com", passwordEncoder.encode("mA3!9bL2zR@xZ0"), "633 111 222", "Carrer de Pau Claris, 46, 08010 Barcelona", User.DegreeType.MASTER, 2, 1, 4.2, false));
57 users.add(new User(null, "Laura Fernández", "laura.fernandez@email.com", passwordEncoder.encode("lauraFSevilla"), "622 333 444", "Avenida de la Constitución, 8, 41001 Sevilla", User.DegreeType.UNIVERSITY_DEGREE, 1, 0, 4.8, false));
58 users.add(new User(null, "David López", "david.lopez@email.com", passwordEncoder.encode("qZ6!L@j*0eD9fT"), "678 555 666", "Calle de Alcalá, 180, 28009 Madrid", User.DegreeType.DOCTORATE, 4, 0, 4.9, false));
59 users.add(new User(null, "Marta Rodríguez", "marta.rodriguez@email.com", passwordEncoder.encode("yX5!8OqR2jW9hZ"), "655 431 549", "Carrer de Roger de Llúria, 24, 08037 Barcelona", User.DegreeType.MASTER, 1, 0, 4.7, false));
60 users.add(new User(null, "José Martínez", "jose.martinez@email.com", passwordEncoder.encode("deusto#00"), "690 234 567", "Calle de Fuentecilla, 5, 14010 Córdoba", User.DegreeType.UNIVERSITY_DEGREE, 2, 0, 4.3, false));
61 users.add(new User(null, "Raquel Sánchez", "raquel.sanchez@email.com", passwordEncoder.encode("rG5uT$7vI2gO#p1"), "677 123 456", "Calle de la Paz, 34, 15001 A Coruña", User.DegreeType.UNIVERSITY_DEGREE, 3, 2, 3.8, false));
62 users.add(new User(null, "José Antonio Torres", "jose.antonio@email.com", passwordEncoder.encode("uZ0!W6tP#3rV!fL"), "616 741 982", "Calle San Fernando, 18, 41004 Sevilla", User.DegreeType.MASTER, 1, 0, 4.6, false));
63 users.add(new User(null, "Beatriz García", "beatriz.garcia@email.com", passwordEncoder.encode("wQ2bF!xL5tCj9Z"), "636 984 211", "Carrer de Valencia, 35, 08015 Barcelona", User.DegreeType.DOCTORATE, 3, 1, 4.4, false));
64 users.add(new User(null, "Pedro Gómez", "pedro.gomez@email.com", passwordEncoder.encode("micontraseña"), "688 234 123", "Calle Mayor, 12, 28013 Madrid", User.DegreeType.UNIVERSITY_DEGREE, 2, 0, 4.6, false));
65 users.add(new User(null, "Carmen Martínez", "carmen.martinez@email.com", passwordEncoder.encode("vX0nP9!fS3zT1dQ"), "676 453 782", "Avenida de la Paz, 40, 29012 Málaga", User.DegreeType.MASTER, 2, 1, 4.5, false));
66 users.add(new User(null, "Antonio Fernández", "antonio.fernandez@email.com", passwordEncoder.encode("antoFernan01"), "645 876 543", "Calle de Núñez de Balboa, 45, 28001 Madrid", User.DegreeType.UNIVERSITY_DEGREE, 3, 2, 4.7, false));
67 users.add(new User(null, "Elena López", "elena.lopez@email.com", passwordEncoder.encode("qZ5#V7tJ3kL2A*1"), "659 321 987", "Carrer de Sants, 58, 08014 Barcelona", User.DegreeType.MASTER, 1, 0, 4.8, false));
68 users.add(new User(null, "Juan Pérez", "juan.perez@email.com", passwordEncoder.encode("kF3uT0hS7!pVbD2"), "656 789 432", "Calle de Santiago, 14, 37001 Salamanca", User.DegreeType.DOCTORATE, 4, 0, 5.0, false));
69 users.add(new User(null, "Sara García", "sara.garcia@email.com", passwordEncoder.encode("contra123"), "602 123 654", "Calle del Sol, 22, 46001 Valencia", User.DegreeType.UNIVERSITY_DEGREE, 2, 0, 4.4, false));
70 users.add(new User(null, "Alex", "alexoladom@gmail.com", passwordEncoder.encode("123"), "602 123 654", "Calle del Sol, 22, 46001 Valencia", User.DegreeType.UNIVERSITY_DEGREE, 2, 0, 4.4, false));
71 // Admin users
72 users.add(new User(null, "Sabin Luja", "sabin.luja@opendeusto.es", passwordEncoder.encode("sabin"), "602 153 654", "Hermanos Aguirre Kalea, 2, 48014 Bilbao", User.DegreeType.UNIVERSITY_DEGREE, 4, 0, 5.0, true));
73
74 return users;
75 }
76
77 public void saveUsers(List<User> users, UserRepository userRepository) {
78 for (User user : users) {
79 if (userRepository.findByEmail(user.getEmail()) == null) {
80 userRepository.save(user);
81 }
82 }
83 }
84
85 private List<Item> createItems(UserRepository userRepository) {
86 // Get the ID of the User
87 Long owner1Id = userRepository.findById(1L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
88 Long owner2Id = userRepository.findById(2L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
89 Long owner3Id = userRepository.findById(3L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
90 Long owner4Id = userRepository.findById(4L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
91 Long owner5Id = userRepository.findById(5L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
92 Long owner6Id = userRepository.findById(6L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
93 Long owner7Id = userRepository.findById(7L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
94 Long owner8Id = userRepository.findById(8L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
95 Long owner9Id = userRepository.findById(9L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
96 Long owner10Id = userRepository.findById(10L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
97 Long owner11Id = userRepository.findById(11L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
98 Long owner12Id = userRepository.findById(12L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
99 Long owner13Id = userRepository.findById(13L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
100 Long owner14Id = userRepository.findById(14L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
101 Long owner15Id = userRepository.findById(15L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
102
103 // Admins user ID
104 Long admin00Id = userRepository.findById(16L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
105
106 // Set of items to insert
107 List <Item> items = new ArrayList<>();
108 items.add(new Item(null, "Artificial Intelligence Book", "Book on Artificial Intelligence for university students", "Books", Item.ItemStatus.BORROWED, owner1Id, new Date(122, 6, 15), 29.99, Item.ItemCondition.NEW, "/images/ArticialIntelligenceBook.jpg"));
109 items.add(new Item(null, "HDMI 2.0 Cable", "Premium quality HDMI 2.0 cable for video and audio transmission", "Electronics", Item.ItemStatus.BORROWED, owner2Id, new Date(122, 9, 10), 15.00, Item.ItemCondition.VERY_USED, "/images/HDMI20Cable.jpg"));
110 items.add(new Item(null, "Academic Planner", "A planner for organizing academic work and deadlines", "Stationery", Item.ItemStatus.BORROWED, owner3Id, new Date(122, 3, 5), 12.99, Item.ItemCondition.VERY_USED, "/images/AcademicPlanner.jpg"));
111 items.add(new Item(null, "Graphing Scientific Calculator", "Advanced scientific calculator with graphing capabilities", "Calculators", Item.ItemStatus.AVAILABLE, owner4Id, new Date(122, 1, 20), 50.00, Item.ItemCondition.LIKE_NEW, "/images/GraphingScientificCalculator.jpg"));
112 items.add(new Item(null, "Ergonomic Wireless Keyboard", "Ergonomically designed wireless keyboard for comfortable typing", "Electronics", Item.ItemStatus.AVAILABLE, owner5Id, new Date(122, 44, 12), 28.00, Item.ItemCondition.GOOD, "/images/ErgonomicWirelessKeyboard.jpg"));
113 items.add(new Item(null, "Portable Bluetooth Speaker", "Compact Bluetooth speaker for clear sound anywhere", "Electronics", Item.ItemStatus.BORROWED, owner6Id, new Date(122, 5, 30), 40.00, Item.ItemCondition.NEW, "/images/PortableBluetoothSpeaker.jpg"));
114 items.add(new Item(null, "Adjustable Smartphone Stand", "Multi-functional adjustable stand for smartphones and tablets", "Accessories", Item.ItemStatus.BORROWED, owner7Id, new Date(122, 10, 18), 12.00, Item.ItemCondition.USED, "/images/AdjustableSmartphoneStand.jpg"));
115 items.add(new Item(null, "32GB USB Flash Drive", "USB flash drive with 32GB capacity for fast data transfer", "Electronics", Item.ItemStatus.UNAVAILABLE, owner8Id, new Date(122, 12, 22), 10.00, Item.ItemCondition.LIKE_NEW, "/images/32GBUSBFlashDrive.jpg"));
116 items.add(new Item(null, "Compact Tool Kit", "Multi-purpose tool kit for small repairs and DIY projects", "Tools", Item.ItemStatus.AVAILABLE, owner9Id, new Date(123, 2, 8), 20.00, Item.ItemCondition.GOOD, "/images/CompactToolKit.jpg"));
117 items.add(new Item(null, "Digital Drawing Tablet", "Graphics tablet with high precision for digital art and design", "Electronics", Item.ItemStatus.AVAILABLE, owner10Id, new Date(123, 12, 14), 150.00, Item.ItemCondition.VERY_USED, "/images/DigitalDrawingTablet.jpg"));
118 items.add(new Item(null, "Introduction to Deep Learning", "Comprehensive textbook on Deep Learning concepts and applications for beginners and advanced students", "Books", Item.ItemStatus.AVAILABLE, owner11Id, new Date(123, 6, 3), 25.99, Item.ItemCondition.USED, "/images/IntroductiontoDeepLearning.jpg"));
119 items.add(new Item(null, "Fast USB-C Charging Cable", "High-speed USB-C charger for fast charging of mobile devices", "Chargers", Item.ItemStatus.AVAILABLE, owner12Id, new Date(123, 6, 17), 10.50, Item.ItemCondition.VERY_USED, "/images/FastUSB-CChargingCable.jpg"));
120 items.add(new Item(null, "Advanced Scientific Calculator", "Scientific calculator with advanced functions for university-level students", "Calculators", Item.ItemStatus.AVAILABLE, owner13Id, new Date(123, 9, 25), 15.00, Item.ItemCondition.GOOD, "/images/AdvancedScientificCalculator.jpg"));
121 items.add(new Item(null, "Academic Student Notebook", "100-page notebook for note-taking and academic purposes", "Stationery", Item.ItemStatus.AVAILABLE, owner14Id, new Date(123, 10, 9), 2.50, Item.ItemCondition.NEW, "/images/AcademicStudentNotebook.jpg"));
122 items.add(new Item(null, "Ergonomic Wireless Mouse", "Wireless ergonomic mouse designed for extended computer use", "Electronics", Item.ItemStatus.AVAILABLE, owner15Id, new Date(123, 1, 30), 12.00, Item.ItemCondition.LIKE_NEW, "/images/ErgonomicWirelessMouse.jpg"));
123 items.add(new Item(null, "MSI Crosshair 16 HX Laptop", "Laptop with powerful specs for academic purposes, programming, and research", "Electronics", Item.ItemStatus.AVAILABLE, admin00Id, new Date(123, 4, 12), 500.00, Item.ItemCondition.NEW, "/images/MSICrosshair16HXLaptop.jpg"));
124 items.add(new Item(null, "White-Out Correction Fluid", "High-quality white-out correction fluid for error-free corrections", "Stationery", Item.ItemStatus.AVAILABLE, owner2Id, new Date(123, 5, 20), 1.50, Item.ItemCondition.NEW, "/images/White-OutCorrectionFluid.jpg"));
125 items.add(new Item(null, "Noise-Cancelling Headphones", "Over-ear headphones with noise-cancelling technology for immersive sound", "Electronics", Item.ItemStatus.AVAILABLE, owner3Id, new Date(124, 7, 11), 40.00, Item.ItemCondition.NEW, "/images/Noise-CancellingHeadphones.jpg"));
126 items.add(new Item(null, "Precision Eraser", "Precision eraser for fine pencil erasures and clean work", "Stationery", Item.ItemStatus.AVAILABLE, owner4Id, new Date(1234, 12, 28), 0.50, Item.ItemCondition.NEW, "/images/PrecisionEraser.jpg"));
127 items.add(new Item(null, "Algorithms Textbook", "In-depth textbook covering algorithms for computer science students", "Books", Item.ItemStatus.AVAILABLE, owner5Id, new Date(123, 1, 7), 35.00, Item.ItemCondition.USED, "/images/AlgorithmsTextbook.jpg"));
128 items.add(new Item(null, "Arduino Development Kit", "Kit with Arduino board, sensors, and cables for electronics projects", "Electronics", Item.ItemStatus.BORROWED, owner6Id, new Date(123, 9, 15), 45.00, Item.ItemCondition.NEW, "/images/ArduinoDevelopmentKit.jpg"));
129 items.add(new Item(null, "Full HD Webcam", "High-definition webcam ideal for video conferences and recordings", "Electronics", Item.ItemStatus.BORROWED, owner7Id, new Date(123, 8, 4), 35.00, Item.ItemCondition.LIKE_NEW, "/images/FullHDWebcam.jpg"));
130 items.add(new Item(null, "1TB External Hard Drive", "Portable external hard drive for backups and data storage", "Electronics", Item.ItemStatus.AVAILABLE, owner8Id, new Date(124, 6, 19), 60.00, Item.ItemCondition.USED, "/images/1TBExternalHardDrive.jpg"));
131 items.add(new Item(null, "Digital Multimeter", "Digital multimeter for measuring voltage, current, and resistance", "Tools", Item.ItemStatus.AVAILABLE, owner9Id, new Date(124, 1, 5), 25.00, Item.ItemCondition.NEW, "/images/DigitalMultimeter.jpg"));
132 items.add(new Item(null, "Portable Printer", "Wireless portable printer for printing documents anywhere", "Electronics", Item.ItemStatus.BORROWED, owner10Id, new Date(124, 10, 22), 80.00, Item.ItemCondition.GOOD, "/images/PortablePrinter.jpg"));
133 items.add(new Item(null, "Portable WiFi Router", "Portable WiFi router for creating wireless networks in areas without connection", "Electronics", Item.ItemStatus.AVAILABLE, owner11Id, new Date(124, 4, 14), 55.00, Item.ItemCondition.GOOD, "/images/PortableWiFiRouter.jpg"));
134 items.add(new Item(null, "Rechargeable LED Desk Lamp", "LED desk lamp with rechargeable battery for studying at night", "Accessories", Item.ItemStatus.AVAILABLE, owner12Id, new Date(124, 6, 30), 20.00, Item.ItemCondition.USED, "/images/RechargeableLEDDeskLamp.jpg"));
135 items.add(new Item(null, "Universal Laptop Charger", "Universal charger compatible with multiple laptop models", "Electronics", Item.ItemStatus.AVAILABLE, owner13Id, new Date(124, 8, 8), 30.00, Item.ItemCondition.LIKE_NEW, "/images/UniversalLaptopCharger.jpg"));
136 items.add(new Item(null, "Raspberry Pi Board", "Raspberry Pi development board for programming and electronics projects", "Electronics", Item.ItemStatus.AVAILABLE, owner14Id, new Date(124, 3, 16), 35.00, Item.ItemCondition.USED, "/images/RaspberryPiBoard.jpg"));
137 items.add(new Item(null, "USB Digital Microscope", "Portable USB digital microscope for detailed analysis and studies", "Electronics", Item.ItemStatus.AVAILABLE, owner15Id, new Date(124, 11, 27), 70.00, Item.ItemCondition.GOOD, "/images/USBDigitalMicroscope.jpg"));
138 items.add(new Item(null, "Calculus Textbook", "A comprehensive textbook covering calculus concepts for university students", "Books", Item.ItemStatus.UNAVAILABLE, owner1Id, new Date(124, 0, 12), 55.00, Item.ItemCondition.USED, "/images/CalculusTextbook.jpg"));
139 items.add(new Item(null, "Campus Umbrella", "Durable umbrella to shield students from rain on campus", "Accessories", Item.ItemStatus.BORROWED, owner2Id, new Date(124, 1, 20), 20.00, Item.ItemCondition.GOOD, "/images/CampusUmbrella.jpg"));
140 items.add(new Item(null, "Wireless Earbuds", "Compact and high-quality wireless earbuds for everyday use", "Electronics", Item.ItemStatus.BORROWED, owner3Id, new Date(124, 7, 5), 35.00, Item.ItemCondition.GOOD, "/images/WirelessEarbuds.jpg"));
141 items.add(new Item(null, "Laptop Cooling Pad", "Cooling pad to enhance laptop performance during heavy use", "Electronics", Item.ItemStatus.AVAILABLE, owner4Id, new Date(124, 3, 18), 25.00, Item.ItemCondition.VERY_USED, "/images/LaptopCoolingPad.jpg"));
142 items.add(new Item(null, "Multiport USB Hub", "USB hub with multiple ports to connect several devices simultaneously", "Electronics", Item.ItemStatus.AVAILABLE, owner5Id, new Date(124, 11, 2), 18.00, Item.ItemCondition.NEW, "/images/MultiportUSBHub.jpg"));
143 items.add(new Item(null, "Stainless Steel Thermos", "Insulated thermos to keep beverages hot or cold for long hours", "Accessories", Item.ItemStatus.AVAILABLE, owner6Id, new Date(124, 10, 25), 30.00, Item.ItemCondition.VERY_USED, "/images/StainlessSteelThermos.jpg"));
144 items.add(new Item(null, "Bluetooth Presenter Remote", "Remote control for presentations with Bluetooth connectivity", "Electronics", Item.ItemStatus.AVAILABLE, owner7Id, new Date(124, 8, 14), 22.00, Item.ItemCondition.USED, "/images/BluetoothPresenterRemote.jpg"));
145 items.add(new Item(null, "Desk Lamp", "LED desk lamp with adjustable brightness for late-night studies", "Furniture", Item.ItemStatus.AVAILABLE, owner8Id, new Date(124, 5, 30), 40.00, Item.ItemCondition.NEW, "/images/DeskLamp.jpg"));
146 items.add(new Item(null, "USB 16GB", "portable USB for quick data transfer storage or presentations", "Electronics", Item.ItemStatus.AVAILABLE, owner9Id, new Date(124, 4, 7), 90.00, Item.ItemCondition.USED, "/images/USB16GB.jpg"));
147 items.add(new Item(null, "Advanced English Dictionary", "Comprehensive dictionary for academic writing and research", "Books", Item.ItemStatus.AVAILABLE, admin00Id, new Date(124, 2, 3), 40.00, Item.ItemCondition.NEW, "/images/AdvancedEnglishDictionary.jpg"));
148 items.add(new Item(null, "Basic Pencil Case", "Durable pencil case to store pens and pencils", "Stationery", Item.ItemStatus.AVAILABLE, owner1Id, new Date(125, 2, 14), 8.50, Item.ItemCondition.NEW, "/images/BasicPencilCase.jpg"));
149 items.add(new Item(null, "Ballpoint Pens Set", "Set of high-quality ballpoint pens for everyday writing", "Stationery", Item.ItemStatus.AVAILABLE, owner2Id, new Date(125, 0, 10), 5.00, Item.ItemCondition.VERY_USED, "/images/BallpointPensSet.jpg"));
150 items.add(new Item(null, "Scientific Calculator", "Reliable scientific calculator for complex calculations", "Calculators", Item.ItemStatus.AVAILABLE, owner3Id, new Date(125, 1, 25), 25.00, Item.ItemCondition.USED, "/images/ScientificCalculator.jpg"));
151 items.add(new Item(null, "Artificial Intelligence: A Modern Approach", "Comprehensive textbook covering modern AI concepts", "Books", Item.ItemStatus.AVAILABLE, owner4Id, new Date(125, 3, 5), 60.00, Item.ItemCondition.NEW, "/images/ArtificialIntelligenceAModernApproach.jpg"));
152 items.add(new Item(null, "Introduction to Computer Science", "Fundamental textbook on computer science principles", "Books", Item.ItemStatus.AVAILABLE, owner5Id, new Date(125, 2, 15), 55.00, Item.ItemCondition.NEW, "/images/IntroductiontoComputerScience.jpg"));
153 items.add(new Item(null, "Introduction to Business Administration (ADE)", "Fundamentals of Business Administration for university students", "Books", Item.ItemStatus.AVAILABLE, owner1Id, new Date(125, 0, 15), 40.00, Item.ItemCondition.USED, "/images/IntroductiontoBusinessAdministration.jpg"));
154 items.add(new Item(null, "Natural Language Processing with Python", "Comprehensive guide to NLP techniques using Python for text analysis", "Books", Item.ItemStatus.AVAILABLE, owner2Id, new Date(125,2, 2), 45.00, Item.ItemCondition.NEW, "/images/NaturalLanguageProcessingwithPython.jpg"));
155 items.add(new Item(null, "Reinforcement Learning: An Introduction", "A detailed introduction to reinforcement learning algorithms and applications", "Books", Item.ItemStatus.AVAILABLE, owner3Id, new Date(125, 1, 14), 50.00, Item.ItemCondition.NEW, "/images/ReinforcementLearningAnIntroduction.jpg"));
156 items.add(new Item(null, "Ethical Hacking: Practical Guide", "A practical guide to ethical hacking and penetration testing", "Books", Item.ItemStatus.AVAILABLE, owner4Id, new Date(125, 2, 9), 38.00, Item.ItemCondition.NEW, "/images/EthicalHackingPracticalGuide.jpg"));
157 items.add(new Item(null, "Information Systems: Theory and Practice", "Comprehensive textbook covering information systems concepts and applications", "Books", Item.ItemStatus.AVAILABLE, owner5Id, new Date(125, 3, 1), 42.00, Item.ItemCondition.NEW, "/images/InformationSystemsTheoryandPractice.jpg"));
158
159 return items;
160 }
161
162 public void saveItems(List<Item> items, ItemRepository itemRepository) {
163 for (Item item : items) {
164 if (itemRepository.findByName(item.getName()) == null) {
165 itemRepository.save(item);
166 }
167 }
168 }
169
170 private List<Loan> createLoans(UserRepository userRepository, ItemRepository itemRepository) {
171 // Get the ID of the User
172 Long user_loan1 = userRepository.findById(1L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
173 Long user_loan2 = userRepository.findById(2L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
174 Long user_loan3 = userRepository.findById(3L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
175 Long user_loan4 = userRepository.findById(4L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
176 Long user_loan5 = userRepository.findById(5L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
177 Long user_loan6 = userRepository.findById(6L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
178 Long user_loan7 = userRepository.findById(7L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
179 Long user_loan8 = userRepository.findById(8L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
180 Long user_loan9 = userRepository.findById(9L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
181 Long user_loan10 = userRepository.findById(10L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
182 //Long user_Alex =userRepository.findById(24L).map(User::getId).orElseThrow(() -> new RuntimeException("User not found"));
183
184 // Get the ID of the Item
185 Long item_loan1 = itemRepository.findById(1L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
186 Long item_loan2 = itemRepository.findById(2L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
187 Long item_loan3 = itemRepository.findById(3L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
188 Long item_loan4 = itemRepository.findById(4L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
189 Long item_loan5 = itemRepository.findById(5L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
190 Long item_loan6 = itemRepository.findById(6L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
191 Long item_loan7 = itemRepository.findById(7L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
192 Long item_loan8 = itemRepository.findById(8L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
193 Long item_loan9 = itemRepository.findById(9L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
194 Long item_loan20 = itemRepository.findById(10L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
195 Long item_loan21 = itemRepository.findById(21L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
196 Long item_loan22 = itemRepository.findById(22L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
197 Long item_loan23 = itemRepository.findById(23L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
198 Long item_loan24 = itemRepository.findById(24L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
199 Long item_loan25 = itemRepository.findById(25L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
200 Long item_loan31 = itemRepository.findById(31L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
201 Long item_loan32 = itemRepository.findById(32L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
202 Long item_loan33 = itemRepository.findById(33L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
203 Long item_loan34 = itemRepository.findById(34L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
204 Long item_loan35 = itemRepository.findById(35L).map(Item::getId).orElseThrow(() -> new RuntimeException("Item not found"));
205
206 // Set of loans to insert
207 List <Loan> loans = new ArrayList<>();
208 loans.add(new Loan(null, user_loan1, user_loan2, item_loan1, new Date(125, 1, 5), new Date(125, 43, 19), null, Loan.Status.IN_USE, null, ""));
209 loans.add(new Loan(null, user_loan2, user_loan4, item_loan2, new Date(125, 2, 8), new Date(125, 4, 15), null, Loan.Status.IN_USE, null, ""));
210 loans.add(new Loan(null, user_loan3, user_loan6, item_loan3, new Date(125, 0, 18), new Date(125, 3, 30), null, Loan.Status.IN_USE, null, ""));
211 loans.add(new Loan(null, user_loan4, user_loan8, item_loan4, new Date(125, 1, 12), new Date(125, 1, 20), new Date(125, 1, 20), Loan.Status.RETURNED, 5.0, "Excellent"));
212 loans.add(new Loan(null, user_loan5, user_loan10, item_loan5, new Date(125, 1, 28), new Date(125, 2, 7), new Date(125, 2, 6), Loan.Status.RETURNED, 4.8, "Returned early"));
213 loans.add(new Loan(null, user_loan6, user_loan9, item_loan6, new Date(125, 3, 1), new Date(125, 4, 15), null, Loan.Status.IN_USE, null, ""));
214 loans.add(new Loan(null, user_loan7, user_loan7, item_loan7, new Date(125, 3, 10), new Date(125, 5, 17), null, Loan.Status.IN_USE, null, ""));
215 loans.add(new Loan(null, user_loan8, user_loan6, item_loan8, new Date(125, 2, 8), new Date(125, 2, 22), null, Loan.Status.LOST, null, "Item lost during loan"));
216 loans.add(new Loan(null, user_loan9, user_loan1, item_loan9, new Date(125, 0, 10), new Date(125, 0, 18), new Date(125, 0, 19), Loan.Status.RETURNED, 4.9, "Returned with care"));
217 loans.add(new Loan(null, user_loan5, user_loan3, item_loan20, new Date(125, 2, 3), new Date(125, 2, 10), new Date(125, 2, 12), Loan.Status.RETURNED, 4.2, "Slight delay"));
218 loans.add(new Loan(null, user_loan6, user_loan5, item_loan21, new Date(125, 3, 1), new Date(125, 3, 10), null, Loan.Status.IN_USE, null, ""));
219 loans.add(new Loan(null, user_loan7, user_loan4, item_loan22, new Date(125, 2, 15), new Date(125, 3, 22), null, Loan.Status.IN_USE, null, ""));
220 loans.add(new Loan(null, user_loan8, user_loan2, item_loan23, new Date(125, 1, 5), new Date(125, 1, 12), new Date(125, 1, 11), Loan.Status.RETURNED, 4.6, "Early return"));
221 loans.add(new Loan(null, user_loan9, user_loan6, item_loan24, new Date(125, 0, 10), new Date(125, 0, 20), new Date(125, 0, 18), Loan.Status.RETURNED, 4.8, "Returned ahead of time"));
222 loans.add(new Loan(null, user_loan10, user_loan8, item_loan25, new Date(125, 1, 14), new Date(125, 2, 25), null, Loan.Status.IN_USE, null, ""));
223 loans.add(new Loan(null, user_loan1, user_loan1, item_loan31, new Date(125, 2, 3), new Date(125, 2, 10), null, Loan.Status.LOST, null, "Item lost during loan"));
224 loans.add(new Loan(null, user_loan2, user_loan7, item_loan32, new Date(125, 3, 2), new Date(125, 3, 30), null, Loan.Status.IN_USE, null, ""));
225 loans.add(new Loan(null, user_loan3, user_loan9, item_loan33, new Date(125, 2, 17), new Date(125, 3, 24), null, Loan.Status.IN_USE, null, ""));
226 loans.add(new Loan(null, user_loan4, user_loan10, item_loan34, new Date(125, 2, 11), new Date(125, 2, 20), new Date(125, 2, 20), Loan.Status.RETURNED, 5.0, "Excellent return"));
227 loans.add(new Loan(null, user_loan5, user_loan5, item_loan35, new Date(125, 1, 1), new Date(125, 1, 14), new Date(125, 1, 13), Loan.Status.RETURNED, 4.2, "Returned without issues"));
228 loans.add(new Loan(null, user_loan6, user_loan3, item_loan31, new Date(125, 0, 5), new Date(125, 0, 12), null, Loan.Status.IN_USE, null, ""));
229 //loans.add(new Loan(null, user_loan6, user_Alex, item_loan35, new Date(125, 0, 5), new Date(125, 4, 19), null, Loan.Status.IN_USE, null, ""));
230
231 return loans;
232 }
233
234 public void saveLoans(List<Loan> loans, LoanRepository loanRepository, NotificationService notificationService, UserRepository userRepository, ItemRepository itemRepo) {
235
236 for (Loan loan : loans) {
237 List<Loan> itemLoans = loanRepository.findByItem(loan.getItem());
238
239 boolean hasActiveLoan = false;
240 for (Loan loan2 : itemLoans) {
241 if (loan2.getLoanStatus() != Loan.Status.RETURNED) {
242 hasActiveLoan = true;
243 break;
244 }
245 }
246
247 if (!hasActiveLoan) {
248 loanRepository.save(loan);
249 }
250 }
251
252 List<Loan> allLoans = loanRepository.findAll();
253 for (Loan loan : allLoans) {
254 if (loan.getLoanStatus() == Loan.Status.IN_USE) {
255 Date returnDate = loan.getEstimatedReturnDate();
256 Date currentDate = new Date();
257 long diffInMillies = returnDate.getTime() - currentDate.getTime();
258 long diffInDays = diffInMillies / (1000 * 60 * 60 * 24);
259 if (diffInDays<10) {
260 Optional<User> optionalUser = userRepository.findById(loan.getBorrower());
261 Optional<Item> optionalItem = itemRepo.findById(loan.getItem());
262
263 if (optionalUser.isEmpty() || optionalItem.isEmpty()) {
264 continue; // skip this loan if user or item not found
265 }
266
267 User u = optionalUser.get();
268 Item i = optionalItem.get();
269 if (u.getEmail().contains("gmail.com")|| u.getEmail().contains("opendeusto.es")){
270 if (diffInDays<10 && diffInDays>0) {
271 notificationService.enviarCorreo(u.getEmail(), "RETURN REMINDER", "Return reminder:\n you have "+ (diffInDays+1)+" days to return the item "+i.getName()+".\nDo not be late!");
272 }else if( diffInDays<0) {
273 notificationService.enviarCorreo(u.getEmail(), "RETURN OVERDUE",
274 "Return overdue:\n you have to return the item " + i.getName()
275 + ". You are "+(Math.abs(diffInDays))+" days late!Please do it as soon as possible!");
276 }else if(diffInDays==0) {
277 notificationService.enviarCorreo(u.getEmail(), "RETURN TODAY",
278 "Return today:\n You have to return the item " + i.getName() + ".\nPlease do it today!");
279 }
280 }
281 }
282
283 }
284 }
285 }
286}
void saveItems(List< Item > items, ItemRepository itemRepository)
void saveLoans(List< Loan > loans, LoanRepository loanRepository, NotificationService notificationService, UserRepository userRepository, ItemRepository itemRepo)
DataInitializer(BCryptPasswordEncoder passwordEncoder)
void saveUsers(List< User > users, UserRepository userRepository)
CommandLineRunner initData(UserRepository userRepository, ItemRepository itemRepository, LoanRepository loanRepository, NotificationService notificationService)