diff --git a/build/web/WEB-INF/classes/DAO/MagasinHelper.class b/build/web/WEB-INF/classes/DAO/MagasinHelper.class index 59fc643..6acfb5c 100644 Binary files a/build/web/WEB-INF/classes/DAO/MagasinHelper.class and b/build/web/WEB-INF/classes/DAO/MagasinHelper.class differ diff --git a/build/web/WEB-INF/classes/controller/BddController$1.class b/build/web/WEB-INF/classes/controller/BddController$1.class index ec22080..d02e32c 100644 Binary files a/build/web/WEB-INF/classes/controller/BddController$1.class and b/build/web/WEB-INF/classes/controller/BddController$1.class differ diff --git a/build/web/WEB-INF/classes/controller/BddController.class b/build/web/WEB-INF/classes/controller/BddController.class index a192dc1..6eb574a 100644 Binary files a/build/web/WEB-INF/classes/controller/BddController.class and b/build/web/WEB-INF/classes/controller/BddController.class differ 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 @@