From cfeb8d56ab4d111c78ae98eee811a068a4934a16 Mon Sep 17 00:00:00 2001 From: Ethanell Date: Thu, 30 Jun 2022 22:05:00 +0200 Subject: [PATCH] Add constraint check on save/update --- .../WEB-INF/classes/DAO/MagasinHelper.class | Bin 15615 -> 15615 bytes .../classes/controller/BddController$1.class | Bin 828 -> 828 bytes .../classes/controller/BddController.class | Bin 11909 -> 15396 bytes build/web/WEB-INF/classes/jsp/confirm.jsp | 2 +- build/web/WEB-INF/classes/jsp/detail.jsp | 8 +- build/web/WEB-INF/classes/jsp/resultat.jsp | 2 +- build/web/WEB-INF/jsp/confirm.jsp | 2 +- build/web/WEB-INF/jsp/detail.jsp | 4 +- src/java/DAO/MagasinHelper.java | 4 +- src/java/Exceptions/InvalidParameter.java | 16 ++ src/java/controller/BddController.java | 215 +++++++++++++----- web/WEB-INF/jsp/confirm.jsp | 2 +- web/WEB-INF/jsp/detail.jsp | 4 +- 13 files changed, 184 insertions(+), 75 deletions(-) create mode 100644 src/java/Exceptions/InvalidParameter.java diff --git a/build/web/WEB-INF/classes/DAO/MagasinHelper.class b/build/web/WEB-INF/classes/DAO/MagasinHelper.class index 59fc643a20b3b0a9e406327b65bf1dff477d2a4b..6acfb5c20da8088beec040a2c53e8b97e1b9fb08 100644 GIT binary patch delta 986 zcmX}nYiNxD9LMqBe~m0*W_IitX6){9JPl(<*qZxgTWzUr?6}J%m&}Z@HMw6pdKOVi zA#%y;DTOT5q`V+bx(^cP9-+C*-26}A?k^aewRf6Ia zP@;lL)nG3);FB{{F&f4{!8vaRi$4OC^lyitOm9iwgk2eww%3{EUss94RkGw>sj2?z34@Qncmdo4rdFx z4qcDlhHgObK<`BFK{um!q4%KocWXZy9Axfc=n-fXdKBtGTj)JT?{UgcX_ZcBwN5g9 zN@sN1TR0@wk4?!k_bl`r^gQ$e^dj^U^fL4c)Q4V$wnBaAHGkm{KJ^CdChQih4R#xL zhuFKs-Y2%5*n7m@r{lr@*uXF|=OOeF^fB}a^eOZi^f~ku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pg@Zn~XvyWAFlF@rKm+e#$Rc2IHhuT0xb1U=pU{J%JMGk*m!6 z0wr+(#)zmkB(p>=A|D9UhvlNs0Xg1cgG~Al1xnGXg9 z8Gk`P8KFyXRQ6_S1vn3X#owf+M#x?4Gm(<5sk_+cas|neYf-*feGIwUZ1K$BMP7d` zc!%8oz7)nh)sZ3%QJ~1sJ?iv64RP9`a6dG)1NaL6kdCDWi!*f3JU&a*eM-mIKfP5w z(2b?tSX)x|tvLQ!*%i4{ePczYuIL}_48fUpSJALwk=GO5f!~zJbfK`i1A{y<+i|-m z+PcEJL#25r$u%d*^Chg{>4@Q2iB^TUs}jYm!Y~;DV|WH8@JyV?vz*31CUiu^PRxwh zDNy+Kh_l4b5zh5d!X28kV`TCulVg6O{6GH>-S2dCU-$F6PZHfHi|$iI_j5(}siONd g(S5q;K0|b$`G4tN^51pWWO7Qp@g45AV@GHI8-9RA$p8QV diff --git a/build/web/WEB-INF/classes/jsp/confirm.jsp b/build/web/WEB-INF/classes/jsp/confirm.jsp index 23837a2..5233c5f 100644 --- a/build/web/WEB-INF/classes/jsp/confirm.jsp +++ b/build/web/WEB-INF/classes/jsp/confirm.jsp @@ -14,7 +14,7 @@ <%@include file="header.jsp" %> -

${confirm}

+

${error != null ? error : confirm}

diff --git a/build/web/WEB-INF/classes/jsp/detail.jsp b/build/web/WEB-INF/classes/jsp/detail.jsp index 0a58f54..73ea3a9 100644 --- a/build/web/WEB-INF/classes/jsp/detail.jsp +++ b/build/web/WEB-INF/classes/jsp/detail.jsp @@ -60,7 +60,7 @@

@@ -175,7 +175,7 @@

@@ -189,7 +189,7 @@ - + @@ -199,8 +199,6 @@ - -
diff --git a/build/web/WEB-INF/classes/jsp/resultat.jsp b/build/web/WEB-INF/classes/jsp/resultat.jsp index 92a31ea..bb96101 100644 --- a/build/web/WEB-INF/classes/jsp/resultat.jsp +++ b/build/web/WEB-INF/classes/jsp/resultat.jsp @@ -20,7 +20,7 @@

${error}

-
+
diff --git a/build/web/WEB-INF/jsp/confirm.jsp b/build/web/WEB-INF/jsp/confirm.jsp index 23837a2..5233c5f 100644 --- a/build/web/WEB-INF/jsp/confirm.jsp +++ b/build/web/WEB-INF/jsp/confirm.jsp @@ -14,7 +14,7 @@ <%@include file="header.jsp" %> -

${confirm}

+

${error != null ? error : confirm}

diff --git a/build/web/WEB-INF/jsp/detail.jsp b/build/web/WEB-INF/jsp/detail.jsp index 88185e8..73ea3a9 100644 --- a/build/web/WEB-INF/jsp/detail.jsp +++ b/build/web/WEB-INF/jsp/detail.jsp @@ -60,7 +60,7 @@

@@ -175,7 +175,7 @@

diff --git a/src/java/DAO/MagasinHelper.java b/src/java/DAO/MagasinHelper.java index 5fbfff9..75dea46 100644 --- a/src/java/DAO/MagasinHelper.java +++ b/src/java/DAO/MagasinHelper.java @@ -8,6 +8,7 @@ import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util.*; import java.sql.*; +import org.hibernate.exception.ConstraintViolationException; /** * @@ -393,8 +394,7 @@ public void add (Object data) { tx=session.beginTransaction(); session.save(data); tx.commit(); - } - catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); tx.rollback(); throw e; diff --git a/src/java/Exceptions/InvalidParameter.java b/src/java/Exceptions/InvalidParameter.java new file mode 100644 index 0000000..41c0691 --- /dev/null +++ b/src/java/Exceptions/InvalidParameter.java @@ -0,0 +1,16 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package Exceptions; + +/** + * + * @author flifloo + */ +public class InvalidParameter extends Exception { + public InvalidParameter(String message) { + super(message); + } +} diff --git a/src/java/controller/BddController.java b/src/java/controller/BddController.java index db69163..6d49517 100644 --- a/src/java/controller/BddController.java +++ b/src/java/controller/BddController.java @@ -5,6 +5,7 @@ */ package controller; import DAO.*; +import Exceptions.InvalidParameter; import java.math.BigDecimal; import java.text.SimpleDateFormat; import service.User; @@ -17,6 +18,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import javax.servlet.http.*; import javax.servlet.*; +import org.hibernate.exception.ConstraintViolationException; @@ -252,64 +254,146 @@ public class BddController extends MultiActionController { } + private char paramChar(HttpServletRequest request, String name) throws InvalidParameter { + String param = paramNotNull(request, name); + if (param.length() > 1) + throw new InvalidParameter("Invalid ".concat(name)); + return param.charAt(0); + } + + private int parseParamInt(HttpServletRequest request, String name) throws InvalidParameter { + try { + return Integer.parseInt(request.getParameter(name)); + } catch (Exception e) { + throw new InvalidParameter("Invalid numer for ".concat(name)); + } + } + + private int parseParamInt(String param, String name) throws InvalidParameter { + try { + return Integer.parseInt(param); + } catch (Exception e) { + throw new InvalidParameter("Invalid numer for ".concat(name)); + } + } + + private BigDecimal parseParamBigDecimal(HttpServletRequest request, String name, int precision) throws InvalidParameter { + BigDecimal bc; + try { + bc = BigDecimal.valueOf(Long.parseLong(request.getParameter("rate"))); + } catch (Exception e) { + throw new InvalidParameter("Invalid big int for ".concat(name)); + } + + if (bc.precision() > precision) + throw new InvalidParameter("Precision for ".concat(name).concat(" shounld be over ".concat(String.valueOf(precision)))); + return bc; + } + + private Short parseParamShort(HttpServletRequest request, String name) throws InvalidParameter { + try { + return Short.valueOf(request.getParameter("rate")); + } catch (Exception e) { + throw new InvalidParameter("Invalid short number for ".concat(name)); + } + } + + private String paramNotNull(HttpServletRequest request, String name) throws InvalidParameter { + String param = request.getParameter(name); + if (param == null || param.isEmpty()) + throw new InvalidParameter(name.concat(" should not be empty")); + return param; + } + + private String paramLength(HttpServletRequest request, String name, int length) throws InvalidParameter { + String param = request.getParameter(name); + if (param.length() > length) + throw new InvalidParameter(name.concat(" should not be more than ".concat(String.valueOf(length)).concat(" characters"))); + return param; + } + + private String paramLength(String param, String name, int length) throws InvalidParameter { + if (param.length() > length) + throw new InvalidParameter(name.concat(" should not be more than ".concat(String.valueOf(length)).concat(" characters"))); + return param; + } + + private String paramAvailable(HttpServletRequest request, String name) throws InvalidParameter { + String param = request.getParameter(name).toUpperCase(); + if (!param.equals("TRUE") && !param.equals("FALSE")) + throw new InvalidParameter("It's true or false for ".concat(name)); + return param; + } + + private Date paramDate(HttpServletRequest request, String name) throws InvalidParameter { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + try { + return format.parse(request.getParameter(name)); + } catch(Exception e) { + throw new InvalidParameter("Invalid date for ".concat(name)); + } + } + private Object fetchDetailsData(HttpServletRequest request, ModelAndView mv) throws Exception { String type = request.getParameter("type"); Object data = null; - switch (type) { - case "customer": - int customerId = Integer.parseInt(request.getParameter("customerId")); - char discountCode = request.getParameter("discountCode").charAt(0); - String zip = request.getParameter("zip"); - String name = request.getParameter("name"); - String addressline1 = request.getParameter("addressline1"); - String addressline2 = request.getParameter("addressline2"); - String city = request.getParameter("city"); - String state = request.getParameter("state"); - String phone = request.getParameter("phone"); - String fax = request.getParameter("fax"); - String email = request.getParameter("email"); - int creditLimit = Integer.parseInt(request.getParameter("creditLimit")); - data = new Customer(customerId, discountCode, zip, name, addressline1, addressline2, city, state, phone, fax, email, creditLimit); - break; - case "product": - int productId = Integer.parseInt(request.getParameter("productId")); - int manufacturerId = Integer.parseInt(request.getParameter("manufacturerId")); - String productCode = request.getParameter("productCode"); - BigDecimal purchaseCost = BigDecimal.valueOf(Double.parseDouble(request.getParameter("purchaseCost"))); - int quantityOnHand = Integer.parseInt(request.getParameter("quantityOnHand")); - BigDecimal markup = BigDecimal.valueOf(Double.parseDouble(request.getParameter("markup"))); - String available = request.getParameter("available"); - String description = request.getParameter("description"); - data = new Product(productId, manufacturerId, productCode, purchaseCost, quantityOnHand, markup, available, description); - break; - case "purchase": - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - - int orderNum = Integer.parseInt(request.getParameter("orderNum")); - customerId = Integer.parseInt(request.getParameter("customerId")); - productId = Integer.parseInt(request.getParameter("productId")); - Short quantity = Short.parseShort(request.getParameter("quantity")); - BigDecimal shippingCost = BigDecimal.valueOf(Double.parseDouble(request.getParameter("shippingCost"))); - Date salesDate = format.parse(request.getParameter("salesDate")); - Date shippingDate = format.parse(request.getParameter("shippingDate")); - String freightCompany = request.getParameter("freightCompany"); - data = new PurchaseOrder(orderNum, customerId, productId, quantity, shippingCost, salesDate, shippingDate, freightCompany); + try { + switch (type) { + case "customer": + int customerId = parseParamInt(request, "customerId"); + char discountCode = paramChar(request, "discountCode"); + String zip = paramLength(paramNotNull(request, "zip"), "zip", 10); + String name = paramLength(request, "name", 30); + String addressline1 = paramLength(request, "addressline1", 30); + String addressline2 = paramLength(request, "addressline2", 30); + String city = paramLength(request, "city", 25); + String state = paramLength(request, "state", 2); + String phone = paramLength(request, "phone", 12); + String fax = paramLength(request, "fax", 12); + String email = paramLength(request, "email", 40); + int creditLimit = parseParamInt(request, "creditLimit"); + data = new Customer(customerId, discountCode, zip, name, addressline1, addressline2, city, state, phone, fax, email, creditLimit); break; - case "discount": - discountCode = request.getParameter("discountCode").charAt(0); - BigDecimal rate = BigDecimal.valueOf(Double.parseDouble(request.getParameter("rate"))); - data = new DiscountCode(discountCode, rate); - break; - case "prodCode": - productCode = request.getParameter("prodCode"); - discountCode = request.getParameter("discountCode").charAt(0); - description = request.getParameter("description"); - data = new ProductCode(productCode, discountCode, description); - break; - default: - mv.addObject("error", "Type not found"); - return mv; + case "product": + int productId = parseParamInt(request, "productId"); + int manufacturerId = parseParamInt(paramNotNull(request, "manufacturerId"), "manufacturerId"); + String productCode = paramLength(paramNotNull(request, "productCode"), "productCode", 2); + BigDecimal purchaseCost = parseParamBigDecimal(request, "purchaseCost", 12); + int quantityOnHand = parseParamInt(request, "quantityOnHand"); + BigDecimal markup = parseParamBigDecimal(request, "markup", 12); + String available = paramAvailable(request, "available"); + String description = paramLength(request, "description", 50); + data = new Product(productId, manufacturerId, productCode, purchaseCost, quantityOnHand, markup, available, description); + break; + case "purchase": + int orderNum = parseParamInt(request, "orderNum"); + customerId = parseParamInt(paramNotNull(request, "customerId"), "customerId"); + productId = parseParamInt(paramNotNull(request, "productId"), "productId"); + Short quantity = parseParamShort(request, "quantity"); + BigDecimal shippingCost = parseParamBigDecimal(request, "shippingCost", 12); + Date salesDate = paramDate(request, "salesDate"); + Date shippingDate = paramDate(request, "shippingDate"); + String freightCompany = paramLength(request, "freightCompany", 30); + data = new PurchaseOrder(orderNum, customerId, productId, quantity, shippingCost, salesDate, shippingDate, freightCompany); + break; + case "discount": + discountCode = paramChar(request, "discountCode"); + BigDecimal rate = parseParamBigDecimal(request, "rate", 4); + data = new DiscountCode(discountCode, rate); + break; + case "prodCode": + productCode = paramLength(request, "prodCode", 2); + discountCode = paramChar(request, "discountCode"); + description = paramLength(request, "description", 10); + data = new ProductCode(productCode, discountCode, description); + break; + default: + mv.addObject("error", "Type not found"); + return mv; + } + } catch (InvalidParameter e) { + mv.addObject("error", e.getMessage()); } return data; @@ -324,8 +408,16 @@ public class BddController extends MultiActionController { mv.addObject("type", type); Object data = fetchDetailsData(request, mv); - new MagasinHelper().add(data); - mv.addObject("confirm","Save completed"); + if (data != null) { + try { + new MagasinHelper().add(data); + mv.addObject("confirm","Save completed"); + } catch (ConstraintViolationException e) { + mv.addObject("error", e.getSQLException().getMessage()); + } catch (Exception e) { + mv.addObject("error", e.getMessage()); + } + } return mv; } @@ -338,11 +430,14 @@ public class BddController extends MultiActionController { mv.addObject("type", type); Object data = fetchDetailsData(request, mv); - new MagasinHelper().update(data); - mv.addObject("confirm","Update completed"); + if (data != null) { + try { + new MagasinHelper().update(data); + mv.addObject("confirm","Update completed"); + } catch (Exception e) { + mv.addObject("error", e.getMessage()); + } + } return mv; } } - - - diff --git a/web/WEB-INF/jsp/confirm.jsp b/web/WEB-INF/jsp/confirm.jsp index 23837a2..5233c5f 100644 --- a/web/WEB-INF/jsp/confirm.jsp +++ b/web/WEB-INF/jsp/confirm.jsp @@ -14,7 +14,7 @@ <%@include file="header.jsp" %> -

${confirm}

+

${error != null ? error : confirm}

diff --git a/web/WEB-INF/jsp/detail.jsp b/web/WEB-INF/jsp/detail.jsp index 88185e8..73ea3a9 100644 --- a/web/WEB-INF/jsp/detail.jsp +++ b/web/WEB-INF/jsp/detail.jsp @@ -60,7 +60,7 @@

@@ -175,7 +175,7 @@