@@ -205,21 +205,23 @@ public void checkIssueFine(int loanId) {
205205 // Calculate the fine cost
206206 double fineCost = 2.5 * daysDifferenceInt ;
207207
208- String insertFine = "INSERT INTO fines"
209- + "(LOAN_ID, FINE_AMOUNT, FINE_DATE, PAID)" + "VALUES ("
210- + loanId + ","
211- + fineCost + ","
212- + "CURRENT_TIMESTAMP,"
213- + "0)" ;
214-
215- Statement stmt2 = null ;
216- try {
217- stmt2 = con .createStatement ();
218- System .out .println (insertFine );
219- stmt2 .executeUpdate (insertFine );
220- stmt2 .close ();
221- } catch (SQLException e ) {
222- e .printStackTrace ();
208+ if (fineCost > 0 ) {
209+ String insertFine = "INSERT INTO fines"
210+ + "(LOAN_ID, FINE_AMOUNT, FINE_DATE, PAID)" + "VALUES ("
211+ + loanId + ","
212+ + fineCost + ","
213+ + "CURRENT_TIMESTAMP,"
214+ + "0)" ;
215+
216+ Statement stmt2 = null ;
217+ try {
218+ stmt2 = con .createStatement ();
219+ System .out .println (insertFine );
220+ stmt2 .executeUpdate (insertFine );
221+ stmt2 .close ();
222+ } catch (SQLException e ) {
223+ e .printStackTrace ();
224+ }
223225 }
224226 }
225227 } catch (SQLException e ) {
@@ -240,44 +242,45 @@ public void markAsPaid(int fineId) {
240242 }
241243 }
242244
243- /* public ArrayList<fineModel> geFineReportForCustomer(ObjectId customerId, Date startDate, Date endDate) {
244- // Client
245- MongoClient mongo = mongoClientProviderBean.getMongoClient();
246- // Get DB
247- MongoDatabase db = mongo.getDatabase("library");
248- // Get loans
249- MongoCollection<Document> fines = db.getCollection("fines");
250- MongoCollection<Document> loans = db.getCollection("loans");
251- MongoCollection<Document> books = db.getCollection("books");
252- MongoCollection<Document> users = db.getCollection("users");
253-
254- Bson matchStage = Aggregates.match(
255- Filters.and(
256- Filters.eq("user_id", customerId), // Filter loans by customer ID
257- Filters.gte("fine_date", startDate), // Filter fine_date >= startDate
258- Filters.lte("fine_date", endDate) // Filter fine_date <= endDate
259- )
260- );
261-
262- List<Bson> pipeline = Arrays.asList(
263- matchStage,
264- Aggregates.lookup("books", "book_id", "_id", "bookData"),
265- Aggregates.lookup("users", "user_id", "_id", "userData"),
266-
267- Aggregates.unwind("$bookData"),
268- Aggregates.unwind("$userData"),
269- Aggregates.project(
270- Projections.fields(
271- Projections.include("paid"), // Include loan-specific fields
272- Projections.include("fine_amount"),
273- Projections.include("fine_date"),
274- Projections.computed("id", new Document("$toString", "$_id")), // Convert _id to a string
275- Projections.computed("bookData.Title", "$bookData.Title"), // Include book title
276- Projections.computed("bookData.Author", "$bookData.Author")
277- )
278- )
279- );
280- // Execute the aggregation and return the result as an AggregateIterable<Document>
281- return fines.aggregate(pipeline);
282- }*/
245+ public ArrayList <fineModel > geFineReportForCustomer (int customerId , Date startDate , Date endDate ) {
246+ String query = "SELECT f.FINE_ID, f.FINE_AMOUNT, f.FINE_DATE, f.PAID, " +
247+ "b.TITLE, u.LAST_NAME, u.FIRST_NAME, " +
248+ "a.FIRST_NAME AS AUTHOR_FIRST_NAME, a.LAST_NAME AS AUTHOR_LAST_NAME " +
249+ "FROM fines f " +
250+ "INNER JOIN loans l ON l.LOAN_ID = f.LOAN_ID " +
251+ "INNER JOIN books b ON b.BOOK_ID = l.BOOK_ID " +
252+ "INNER JOIN AUTHORS a ON a.AUTHOR_ID = b.AUTHOR_ID " +
253+ "INNER JOIN LIBRARY_USERS u ON u.USER_ID = l.USER_ID " +
254+ "WHERE l.USER_ID = ? AND f.FINE_DATE >= ? AND f.FINE_DATE <= ?" ;
255+
256+ ArrayList <fineModel > fineList = new ArrayList <>();
257+
258+ try (Connection con = oracleClientProviderBean .getOracleClient ();
259+ PreparedStatement pstmt = con .prepareStatement (query )) {
260+
261+ pstmt .setInt (1 , customerId );
262+ pstmt .setObject (2 , new java .sql .Date (startDate .getTime ()));
263+ pstmt .setObject (3 , new java .sql .Date (endDate .getTime ()));
264+
265+ try (ResultSet rs = pstmt .executeQuery ()) {
266+ while (rs .next ()) {
267+ fineModel fine = new fineModel ();
268+ fine .setFineId (rs .getLong ("FINE_ID" ));
269+ fine .setFineAmount (rs .getDouble ("FINE_AMOUNT" ));
270+ fine .setFineDate (rs .getDate ("FINE_DATE" ));
271+ fine .setPaid (rs .getBoolean ("PAID" ));
272+ fine .setBookTitle (rs .getString ("TITLE" ));
273+ fine .setUserFirstName (rs .getString ("FIRST_NAME" ));
274+ fine .setUserLastName (rs .getString ("LAST_NAME" ));
275+ fine .setAuthorFirstName (rs .getString ("AUTHOR_FIRST_NAME" ));
276+ fine .setAuthorLastName (rs .getString ("AUTHOR_LAST_NAME" ));
277+ fineList .add (fine );
278+ }
279+ }
280+ } catch (SQLException e ) {
281+ e .printStackTrace ();
282+ }
283+
284+ return fineList ;
285+ }
283286}
0 commit comments