Permalink
Browse files

Added a validation helper for future use.

  • Loading branch information...
1 parent bd820e4 commit 54ce465b0df8f45113fd05015b35c4523942c8e8 @shsu committed Feb 27, 2014
Showing with 33 additions and 0 deletions.
  1. +33 −0 src/main/java/ca/bcit/infosys/comp4911/helper/ValidationHelper.java
@@ -0,0 +1,33 @@
+package ca.bcit.infosys.comp4911.helper;
+
+import org.json.JSONArray;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Response;
+import java.util.Set;
+
+/**
+ * Validation Helper
+ */
+public class ValidationHelper {
+
+ private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+
+ public static Validator getValidator() {
+ return factory.getValidator();
+ }
+
+ public static boolean validateEntity(Object object){
+ Set<ConstraintViolation<Object>> constraintViolations = ValidationHelper.getValidator().validate(object);
+ if (constraintViolations.size() > 0) {
+ JSONArray jsonArray = new JSONArray(constraintViolations.toArray());
+ throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(jsonArray.toString()).build());
+ }
+
+ return true;
+ }
+}

0 comments on commit 54ce465

Please sign in to comment.