Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@
<appName>${heroku.app-name}</appName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
25 changes: 4 additions & 21 deletions src/main/java/com/hknp/controller/admin/AdBrandController.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,24 @@
package com.hknp.controller.admin;

import com.hknp.model.dao.BrandDAO;
import com.hknp.utils.ServletUtils;
import com.hknp.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = {"/admin/brand"})
public class AdBrandController extends HttpServlet {
public class AdBrandController implements HttpServletCon {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

Long totalRows = BrandDAO.getInstance().count();
String page = req.getParameter("page");

Long currentPage = StringUtils.toLong(page);
Long totalPage = (totalRows / 10) + ((totalRows % 10 == 0) ? 0 : 1);

if (currentPage > totalPage) {
currentPage = totalPage;
}
if (currentPage < 1) {
currentPage = 1L;
}

req.setAttribute("totalPage", totalPage);
req.setAttribute("currentPage", currentPage);
HttpServletCon.setPagesCountByPageParam(req);
ServletUtils.forward(req, resp, "/view/admin/ad-brand.jsp");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
22 changes: 4 additions & 18 deletions src/main/java/com/hknp/controller/admin/AdCategoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,16 @@
import java.io.IOException;

@WebServlet(urlPatterns = {"/admin/category"})
public class AdCategoryController extends HttpServlet {
public class AdCategoryController implements HttpServletCon{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

Long totalRows = ProductCategoryDAO.getInstance().count();
String page = req.getParameter("page");

Long currentPage = StringUtils.toLong(page);
Long totalPage = (totalRows / 10) + ((totalRows % 10 == 0) ? 0 : 1);

if (currentPage > totalPage) {
currentPage = totalPage;
}
if (currentPage < 1) {
currentPage = 1L;
}

req.setAttribute("totalPage", totalPage);
req.setAttribute("currentPage", currentPage);
HttpServletCon.setPagesCountByPageParam(req);
ServletUtils.forward(req, resp, "/view/admin/ad-category.jsp");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
22 changes: 4 additions & 18 deletions src/main/java/com/hknp/controller/admin/AdCustomerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,15 @@
import java.io.IOException;

@WebServlet(urlPatterns = {"/admin/customer"})
public class AdCustomerController extends HttpServlet {
public class AdCustomerController implements HttpServletCon {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Long totalRows = CustomerDAO.getInstance().count();
String page = req.getParameter("page");

Long currentPage = StringUtils.toLong(page);
Long totalPage = (totalRows / 10) + ((totalRows % 10 == 0) ? 0 : 1);

if (currentPage > totalPage) {
currentPage = totalPage;
}
if (currentPage < 1) {
currentPage = 1L;
}

req.setAttribute("totalPage", totalPage);
req.setAttribute("currentPage", currentPage);
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpServletCon.setPagesCountByPageParam(req);
ServletUtils.forward(req, resp, "/view/admin/ad-customer.jsp");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
18 changes: 3 additions & 15 deletions src/main/java/com/hknp/controller/admin/AdDeliveryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,14 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.hknp.controller.admin.HttpServletCon.setPagesCountByPageParam;

@WebServlet(urlPatterns = {"/admin/delivery"})
public class AdDeliveryController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

Long totalRows = DeliveryDAO.getInstance().count();
String page = req.getParameter("page");

Long currentPage = StringUtils.toLong(page);
Long totalPage = (totalRows / 10) + ((totalRows % 10 == 0) ? 0 : 1);

if (currentPage > totalPage) {
currentPage = totalPage;
}
if (currentPage < 1) {
currentPage = 1L;
}

req.setAttribute("totalPage", totalPage);
req.setAttribute("currentPage", currentPage);
setPagesCountByPageParam(req);
ServletUtils.forward(req, resp, "/view/admin/ad-delivery.jsp");
}

Expand Down
23 changes: 4 additions & 19 deletions src/main/java/com/hknp/controller/admin/AdEmployeeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,16 @@
import java.io.IOException;

@WebServlet(urlPatterns = {"/admin/employee"})
public class AdEmployeeController extends HttpServlet {
public class AdEmployeeController implements HttpServletCon {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Long totalRows = EmployeeDAO.getInstance().count();
String page = req.getParameter("page");
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//EmployeeDAO.getInstance().gets(0,10,"K",null,"userEntity.lastName",null);

Long currentPage = StringUtils.toLong(page);
Long totalPage = (totalRows / 10) + ((totalRows % 10 == 0) ? 0 : 1);

if (currentPage > totalPage) {
currentPage = totalPage;
}
if (currentPage < 1) {
currentPage = 1L;
}

req.setAttribute("totalPage", totalPage);
req.setAttribute("currentPage", currentPage);
HttpServletCon.setPagesCountByPageParam(req);
ServletUtils.forward(req, resp, "/view/admin/ad-employee.jsp");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

@WebServlet(urlPatterns = {"/admin/information/edit"})
public class AdInformationController extends HttpServlet {
protected Map<String, Object> parameterMap;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
Expand Down Expand Up @@ -48,57 +50,70 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se

}

protected UserEntity updateUserData(){

String id = (String) parameterMap.get("id");
String lastName = (String) parameterMap.get("last-name");
String firstName = (String) parameterMap.get("first-name");

String gender = (String) parameterMap.get("gender");
String dateOfBirth = (String) parameterMap.get("dob");
String phoneNumber = (String) parameterMap.get("phone-number");
String ssn = (String) parameterMap.get("ssn");
String email = (String) parameterMap.get("email");

String image = (String) parameterMap.get("image");

UserEntity updateUser = UserDAO.getInstance().getById(StringUtils.toLong(id));

updateUser.setFirstName(firstName);
updateUser.setLastName(lastName);
updateUser.setGender(gender);
updateUser.setDateOfBirth(DateTimeUtils.stringToDate(dateOfBirth, "yyyy-MM-dd"));
updateUser.setPhoneNumber(phoneNumber);
updateUser.setSsn(ssn);
updateUser.setEmail(email);
updateUser.setImage(image);

return updateUser;
}

private AddressEntity updateAddressData(UserEntity updateUser){
AddressEntity updateAddress = updateUser.getAddressEntities().get(0);

String provinceId = (String) parameterMap.get("province");
String districtId = (String) parameterMap.get("district");
String communeId = (String) parameterMap.get("commune");
String addressStreet = (String) parameterMap.get("address-street");

updateAddress.setStreet(addressStreet);
updateAddress.setProvinceEntity(ProvinceDAO.getInstance().getById(provinceId));
updateAddress.setDistrictEntity(DistrictDAO.getInstance().getById(districtId));
updateAddress.setCommuneEntity(CommuneDAO.getInstance().getById(communeId));

updateAddress.setFullName(updateUser.getLastName() + " " + updateUser.getFirstName());
updateAddress.setAddressName(Cons.Address.DEFAULT_ADDRESS_NAME);
updateAddress.setUserId(updateUser.getUserId());

return updateAddress;
}


@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String result = "";
Map<String, Object> parameterMap = ServletUtils.getParametersMap(req);
parameterMap = ServletUtils.getParametersMap(req);

UserEntity updateUser = updateUserData();
try {
String id = (String) parameterMap.get("id");
String lastName = (String) parameterMap.get("last-name");
String firstName = (String) parameterMap.get("first-name");

String gender = (String) parameterMap.get("gender");
String dateOfBirth = (String) parameterMap.get("dob");
String phoneNumber = (String) parameterMap.get("phone-number");
String ssn = (String) parameterMap.get("ssn");
String email = (String) parameterMap.get("email");

String provinceId = (String) parameterMap.get("province");
String districtId = (String) parameterMap.get("district");
String communeId = (String) parameterMap.get("commune");
String addressStreet = (String) parameterMap.get("address-street");

String image = (String) parameterMap.get("image");

UserEntity updateUser = UserDAO.getInstance().getById(StringUtils.toLong(id));

updateUser.setFirstName(firstName);
updateUser.setLastName(lastName);
updateUser.setGender(gender);
updateUser.setDateOfBirth(DateTimeUtils.stringToDate(dateOfBirth, "yyyy-MM-dd"));
updateUser.setPhoneNumber(phoneNumber);
updateUser.setSsn(ssn);
updateUser.setEmail(email);
updateUser.setImage(image);

Boolean updateResult = UserDAO.getInstance().update(updateUser);

if (updateResult != false) {
if (updateResult) {
if (updateUser.getAddressEntities().size() == 0) {
updateUser.setAddressEntities(Collections.singletonList(new AddressEntity()));
}
AddressEntity updateAddress = updateUser.getAddressEntities().get(0);

updateAddress.setStreet(addressStreet);
updateAddress.setProvinceEntity(ProvinceDAO.getInstance().getById(provinceId));
updateAddress.setDistrictEntity(DistrictDAO.getInstance().getById(districtId));
updateAddress.setCommuneEntity(CommuneDAO.getInstance().getById(communeId));

updateAddress.setFullName(lastName + " " + firstName);
updateAddress.setAddressName(Cons.Address.DEFAULT_ADDRESS_NAME);
updateAddress.setUserId(updateUser.getUserId());

AddressEntity updateAddress = updateAddressData(updateUser);
AddressDAO.getInstance().update(updateAddress);
result += "true\n" + updateUser.getUserId().toString();
} else {
Expand Down
22 changes: 4 additions & 18 deletions src/main/java/com/hknp/controller/admin/AdProductController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,15 @@
import java.io.IOException;

@WebServlet(urlPatterns = {"/admin/product"})
public class AdProductController extends HttpServlet {
public class AdProductController implements HttpServletCon {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Long totalRows = ProductDAO.getInstance().count();
String page = req.getParameter("page");

Long currentPage = StringUtils.toLong(page);
Long totalPage = (totalRows / 10) + ((totalRows % 10 == 0) ? 0 : 1);

if (currentPage > totalPage) {
currentPage = totalPage;
}
if (currentPage < 1) {
currentPage = 1L;
}

req.setAttribute("totalPage", totalPage);
req.setAttribute("currentPage", currentPage);
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpServletCon.setPagesCountByPageParam(req);
ServletUtils.forward(req, resp, "/view/admin/ad-product.jsp");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
23 changes: 4 additions & 19 deletions src/main/java/com/hknp/controller/admin/AdSellerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,15 @@
import java.io.IOException;

@WebServlet(urlPatterns = {"/admin/seller"})
public class AdSellerController extends HttpServlet {
public class AdSellerController implements HttpServletCon {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Long totalRows = SellerDAO.getInstance().count();
String page = req.getParameter("page");

Long currentPage = StringUtils.toLong(page);
Long totalPage = (totalRows / 10) + ((totalRows % 10 == 0) ? 0 : 1);

if (currentPage > totalPage) {
currentPage = totalPage;
}
if (currentPage < 1) {
currentPage = 1L;
}

req.setAttribute("totalPage", totalPage);
req.setAttribute("currentPage", currentPage);

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpServletCon.setPagesCountByPageParam(req);
ServletUtils.forward(req, resp, "/view/admin/ad-seller.jsp");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Loading