|
3 | 3 | import com.javafullstacklibrary.dao.ItemCopyDAO; |
4 | 4 | import com.javafullstacklibrary.dao.LoanDAO; |
5 | 5 |
|
| 6 | +import com.javafullstacklibrary.model.Item; |
6 | 7 | import com.javafullstacklibrary.model.ItemCopy; |
7 | 8 | import com.javafullstacklibrary.model.Journal; |
8 | 9 | import com.javafullstacklibrary.model.LibraryUser; |
|
12 | 13 | import jakarta.persistence.EntityManagerFactory; |
13 | 14 | import jakarta.persistence.Persistence; |
14 | 15 |
|
| 16 | +import java.time.LocalDate; |
| 17 | +import java.util.List; |
15 | 18 | import java.util.Optional; |
16 | 19 |
|
17 | 20 | public class LoanValidationService implements AutoCloseable { |
@@ -96,6 +99,90 @@ public ValidationResult<ItemCopy> validateBarcodeForLoan(String barcode) { |
96 | 99 | return ValidationResult.success(itemCopy); |
97 | 100 | } |
98 | 101 |
|
| 102 | + /** |
| 103 | + * Validates if the user can confirm all pending loans without exceeding their limit |
| 104 | + * @param libraryUser The user requesting the loans |
| 105 | + * @param pendingTransactionManager The pending transaction manager containing items to loan |
| 106 | + * @return ValidationResult indicating success or failure with appropriate message |
| 107 | + */ |
| 108 | + public ValidationResult<Void> validateLoanConfirmation(LibraryUser libraryUser, PendingTransactionManager pendingTransactionManager) { |
| 109 | + if (pendingTransactionManager.getPending().isEmpty()) { |
| 110 | + return ValidationResult.failure("No items selected for loan"); |
| 111 | + } |
| 112 | + |
| 113 | + int maxLoans = loanDAO.getMaxLoansByUser(libraryUser); |
| 114 | + int currentActiveLoans = Math.toIntExact(loanDAO.countActiveLoansByUser(libraryUser)); |
| 115 | + int pendingCount = pendingTransactionManager.getPendingCount(); |
| 116 | + |
| 117 | + if (maxLoans != -1 && (currentActiveLoans + pendingCount) > maxLoans) { |
| 118 | + return ValidationResult.failure(String.format( |
| 119 | + "Cannot confirm loans: would exceed maximum limit of %d loans (current: %d, pending: %d)", |
| 120 | + maxLoans, currentActiveLoans, pendingCount)); |
| 121 | + } |
| 122 | + |
| 123 | + return ValidationResult.success(null); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Validates if a user can add one more item to their pending loans |
| 128 | + * @param libraryUser The user requesting to add an item |
| 129 | + * @param pendingTransactionManager The pending transaction manager |
| 130 | + * @return ValidationResult indicating success or failure |
| 131 | + */ |
| 132 | + public ValidationResult<Void> validateAddToPending(LibraryUser libraryUser, PendingTransactionManager pendingTransactionManager) { |
| 133 | + int maxLoans = loanDAO.getMaxLoansByUser(libraryUser); |
| 134 | + int currentActiveLoans = Math.toIntExact(loanDAO.countActiveLoansByUser(libraryUser)); |
| 135 | + int pendingCount = pendingTransactionManager.getPendingCount(); |
| 136 | + |
| 137 | + if (maxLoans != -1 && (currentActiveLoans + pendingCount + 1) > maxLoans) { |
| 138 | + return ValidationResult.failure(String.format( |
| 139 | + "You have reached the maximum number of loans allowed for your user type (%d)", maxLoans)); |
| 140 | + } |
| 141 | + |
| 142 | + return ValidationResult.success(null); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Processes a list of pending loans by creating loan records in the database |
| 147 | + * @param itemCopies List of ItemCopy objects to loan |
| 148 | + * @param libraryUser The user taking out the loans |
| 149 | + * @return ValidationResult indicating success or failure with appropriate message |
| 150 | + */ |
| 151 | + public ValidationResult<Void> processLoans(List<ItemCopy> itemCopies, LibraryUser libraryUser) { |
| 152 | + if (itemCopies == null || itemCopies.isEmpty()) { |
| 153 | + return ValidationResult.failure("No items to process for loan"); |
| 154 | + } |
| 155 | + |
| 156 | + if (libraryUser == null) { |
| 157 | + return ValidationResult.failure("Library user is required for processing loans"); |
| 158 | + } |
| 159 | + |
| 160 | + try { |
| 161 | + // Process each loan |
| 162 | + for (ItemCopy itemCopy : itemCopies) { |
| 163 | + LocalDate returnDate = getReturnDateByItemCopy(itemCopy); |
| 164 | + loanDAO.save(itemCopy, libraryUser, returnDate); |
| 165 | + } |
| 166 | + |
| 167 | + return ValidationResult.success(null); |
| 168 | + } catch (IllegalStateException e) { |
| 169 | + return ValidationResult.failure("Loan processing failed: " + e.getMessage()); |
| 170 | + } catch (Exception e) { |
| 171 | + return ValidationResult.failure("An error occurred while processing loans"); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Calculates the return date for an item copy based on its maximum loan time |
| 177 | + * @param itemCopy The ItemCopy to calculate return date for |
| 178 | + * @return LocalDate representing when the item should be returned |
| 179 | + */ |
| 180 | + private LocalDate getReturnDateByItemCopy(ItemCopy itemCopy) { |
| 181 | + Item item = itemCopy.getItem(); |
| 182 | + int daysToAdd = item.getMaxLoanTimeDays(); |
| 183 | + return LocalDate.now().plusDays(daysToAdd); |
| 184 | + } |
| 185 | + |
99 | 186 | @Override |
100 | 187 | public void close() { |
101 | 188 | if (em != null && em.isOpen()) { |
|
0 commit comments