Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/java/com/library/controller/admin/ConfirmExtendController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
*/
package com.library.controller.admin;

import com.library.enums.RequestStatus;
import com.library.factory.ServiceFactory;
import com.library.model.dto.BorrowedBookDTO;
import com.library.model.dto.ExtendRequestDTO;
import com.library.model.entity.Borrowing;
import com.library.model.entity.ExtendRequest;
import com.library.model.entity.User;
import com.library.service.BorrowingService;
import com.library.service.ExtendBookService;
import com.library.service.UserService;
import com.library.util.MailTransfer;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
*
* @author hieuchu
*/
@WebServlet(name = "ConfirmExtendController", urlPatterns = {"/user/request-extend-book"})
public class ConfirmExtendController extends HttpServlet {

private final ExtendBookService extendSerivce = ServiceFactory.getExtendBookService();
private final BorrowingService borrowService = ServiceFactory.getBorrowService();
private final UserService userSerivce = ServiceFactory.getUserService();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
BorrowedBookDTO dto = (BorrowedBookDTO) session.getAttribute("bookExtend");
request.setAttribute("dto", dto);
request.getRequestDispatcher("/WEB-INF/views/user/request_extend_book.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

HttpSession session = request.getSession(false);
BorrowedBookDTO dto = (BorrowedBookDTO) session.getAttribute("bookExtend");
int extensionDays = Integer.parseInt(request.getParameter("extensionDays"));
String account = request.getParameter("account");
String fullName = request.getParameter("fullName");
int borrowingID = borrowService.getBorrowingID(dto.getBookID(), account);
Borrowing borrow = new Borrowing();
borrow.setBorrowingID(borrowingID);
int userID = userSerivce.getUserIDByAccount(account);
User u = new User();
u.setUserID(userID);
ExtendRequestDTO e = new ExtendRequestDTO();
e.setBorrowing(borrow);
e.setUser(u);
if (extendSerivce.limitExtend(dto.getBookID(), account) > 4) {
extendSerivce.insertData(e);
session.removeAttribute("bookExtend");
session.removeAttribute("targetBookID");
response.sendRedirect(request.getContextPath() + "/user/dashboard");
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
*/
package com.library.controller.admin;

import com.library.factory.ServiceFactory;
import com.library.model.dto.ExtendRequestViewDTO;
import com.library.service.ExtendBookService;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
*
* @author hieuchu
*/
@WebServlet(name = "CExtendRequestManagerController", urlPatterns = {"/admin/extend-request-manger"})
public class ExtendRequestManagerController extends HttpServlet {

private final ExtendBookService extendSerivce = ServiceFactory.getExtendBookService();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

List<ExtendRequestViewDTO> list = extendSerivce.getAllExtendRequests();
request.setAttribute("list", list);
request.getRequestDispatcher("/WEB-INF/views/admin/extend_request_manager.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public class BorrowedBooksListController extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("account") == null) {
response.sendRedirect(request.getContextPath() + "/Login");
return ;
}

String account = (String) session.getAttribute("account");

List<BorrowedBookDTO> borrowedBooks = borrowDao.borrowedBooksList(account);
Expand Down
40 changes: 28 additions & 12 deletions src/java/com/library/controller/borrowing/ExtendBookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import com.library.dao.BorrowingDao;
import com.library.dao.BorrowingDaoImpl;
import com.library.factory.ServiceFactory;
import com.library.model.dto.BorrowedBookDTO;
import com.library.service.BorrowingService;
import com.library.service.ExtendBookService;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
Expand All @@ -26,7 +29,8 @@
@WebServlet(name = "ExtendBook", urlPatterns = {"/borrowing/extend"})
public class ExtendBookController extends HttpServlet {

BorrowingDao borrowDao = new BorrowingDaoImpl();
BorrowingService borrowService = ServiceFactory.getBorrowService();
ExtendBookService extendBookService = ServiceFactory.getExtendBookService();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
Expand All @@ -39,28 +43,40 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("account") == null) {
response.sendRedirect(request.getContextPath() + "/Login");
return;
}

String account = (String) session.getAttribute("account");
int bookID = Integer.valueOf(request.getParameter("bookID"));

LocalDate borrowDate = borrowDao.getBorrowDate(bookID);
LocalDate borrowDate = borrowService.getBorrowDate(bookID);
String dueDate = request.getParameter("newDueDate");
LocalDate newDueDate = LocalDate.parse(dueDate);

List<BorrowedBookDTO> borrowedBooks = borrowService.borrowedBooksList(account);

BorrowedBookDTO dto = extendBookService.getBorrowdBookFromList(borrowedBooks);

request.setAttribute("borrowedBooks", borrowedBooks);

String title = "Extend DueDate - Library System";

long day = ChronoUnit.DAYS.between(borrowDate, newDueDate); //take day
if (borrowService.getExtendCount(bookID, account) > 4) {
session.setAttribute("bookExtend", dto);
session.setAttribute("error", "You’re out of renewal");
session.setAttribute("targetBookID", bookID); // show popup when update book is failed
request.getRequestDispatcher("/WEB-INF/views/borrowing/borrowedbooks.jsp").forward(request, response);
return;
}
if (day > 60) {
request.setAttribute("error", "you must not extend the due date by more than 2 months ");
// load list
List<BorrowedBookDTO> borrowedBooks = borrowDao.borrowedBooksList(account);
request.setAttribute("borrowedBooks", borrowedBooks);
request.setAttribute("targetBookID", bookID); // show popup when update book is failed
session.setAttribute("error", "you must not extend the due date by more than 2 months ");
// load list
session.setAttribute("targetBookID", bookID); // show popup when update book is failed
request.getRequestDispatcher("/WEB-INF/views/borrowing/borrowedbooks.jsp").forward(request, response);
return;
}
boolean commitExtraDate = borrowDao.extendDueDay(bookID, newDueDate, account);
boolean commitExtraDate = extendBookService.extendDueDay(bookID, newDueDate, account);
if (commitExtraDate) {
borrowService.incrementExtendCount(bookID, account);
session.setAttribute("extendSuccess", " Due date updated successfully!");
response.sendRedirect(request.getContextPath() + "/borrowing/borrowed?bookID=" + bookID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ public static final String[] pageForUser() {
"/borrowing/returned",
"/borrowing/return",
"/borrowing/extend",
"/user/request-extend-book",
// Favorites
"/favorite/add-book",
"/favorite/books",
// Books
"/book/list",
"/book/detail",
"/book/search",
"/book/category",

// User info
"/user/dashboard",
"/user/setting",
Expand Down Expand Up @@ -54,7 +52,8 @@ public static final String[] pageForAdmin() {
"/admin/user-borrowing-record",
"/admin/user/delete",
"/admin/user/logout",
"/admin/users/logout-all"
"/admin/users/logout-all",
"/admin/extend-request-manger"
};
return tmp ;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,41 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo

String[] publicPaths = {
"/login", "/register", "/book/list",
"/resource/", "/images/", ".css", ".js", ".png", ".jpg", "/user/forgot-password"
"/resource/", "/images/", ".css", ".js", ".png", ".jpg", "/user/forgot-password",
"/book/list",
"/book/detail",
"/book/search",
"/book/category",
};
for (String p : publicPaths) {
if (path.contains(p)) {
chain.doFilter(servletRequest, servletResponse);
return;
}
}

if (user == null) {
response.sendRedirect(request.getContextPath() + "/user/login");
return;
}
if (user.getRole().equals("user")) {
if (user.getRole().equals("user")) {
for (String url : AutholizationURLController.pageForUser()) {
if(path.contains(url)){
chain.doFilter(servletRequest, servletResponse);
return ;
}
if (path.contains(url)) {
chain.doFilter(servletRequest, servletResponse);
return;
}
}
}
if (user.getRole().equals("admin")) {

if (user.getRole().equals("admin")) {
for (String url : AutholizationURLController.pageForAdmin()) {
if(path.contains(url)){
chain.doFilter(servletRequest, servletResponse);
return ;
}
if (path.contains(url)) {
chain.doFilter(servletRequest, servletResponse);
return;
}
}
}
}

}

}
6 changes: 3 additions & 3 deletions src/java/com/library/controller/user/ForgotPassword.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.library.exception.AccountNotExistException;
import com.library.exception.ValidationException;
import com.library.factory.ServiceFactory;
import com.library.service.MailService;
import com.library.util.MailTransfer;
import com.library.service.TrackingUserService;
import com.library.service.UserService;
import com.library.util.HashPassword;
Expand Down Expand Up @@ -52,9 +52,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
userService.isAccountExist(account);
String tmp = RandomPassword.generatePassword();
String title = "Password Recovery - Library System";
String message = "<p>Your New Pass : <b>" + tmp + "</b> </p>";
String newPassword= "<p>Your New Pass : <b>" + tmp + "</b> </p>";
userService.updatePassword(account, HashPassword.hash(tmp));
MailService.send(account, title, message);
MailTransfer.send(account, title, newPassword);
session.setAttribute("message", "we have sent your password via email");
response.sendRedirect(request.getContextPath() + "/user/forgot-password");
} catch (ValidationException e) {
Expand Down
6 changes: 6 additions & 0 deletions src/java/com/library/dao/BorrowingDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ public interface BorrowingDao {
int numberOfBorrowBookOnPerUser();

void approveBorrowing(Connection conn, int borrowId, int adminId);

int getExtendCount(int bookId, String account);

boolean incrementExtendCount(int bookId, String account);

int getBorrowingIdByBookId(int bookId, String account);
}
Loading