From: Roland Haeder <roland@mxchange.org>
Date: Thu, 15 Oct 2015 11:45:51 +0000 (+0200)
Subject: fixed validator for long/integer numbers, thanks Mr. Plischke
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=98429f6ec5ca3ab16b3f34b99a326182495fc1e0;p=jcoreee.git

fixed validator for long/integer numbers, thanks Mr. Plischke
Signed-off-by:Roland Häder <roland@mxchange.org>
---

diff --git a/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java b/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java
index e45de73..f99ad47 100644
--- a/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java
+++ b/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java
@@ -47,7 +47,7 @@ public abstract class BaseLongValidator extends BaseObjectValidator implements V
 		// Get client id and init message + key
 		String clientId = component.getClientId();
 		FacesMessage facesMessage = null;
-		String requiredMessage;
+		String requiredMessage = null;
 
 		// So far all fine, no check if the field is fine
 		for (final String field : requiredFields) {
@@ -56,33 +56,27 @@ public abstract class BaseLongValidator extends BaseObjectValidator implements V
 
 			// Is it the same?
 			if (clientId.endsWith(field)) {
-				// Compare value's type
-				if ((!allowNull) && (!(value instanceof Long))) {
-					// Generate message
-					requiredMessage = MessageFormat.format("Field {0} is not Long.: {1}", field, value); //NOI18N
-
-					// Value is not right type
-					facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N
-					break;
-				} else if ((!allowNull) && (!(value instanceof Integer))) {
-					// Generate message
-					requiredMessage = MessageFormat.format("Field {0} is not Integer.: {1}", field, value); //NOI18N
-
-					// Value is not right type
-					facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N
-					break;
-				}
-
-				// Declare variables
+				// Init variables
 				Integer i = null;
 				Long l = null;
 
-				// Which type?
-				if (value instanceof Integer) {
-					// Cast to Long
+				// Compare value's type
+				if (value instanceof String) {
+					// Is a string, try long
+					try {
+						l = Long.valueOf((String) value);
+					} catch (final NumberFormatException ex) {
+						// Cannot parse string to long
+						requiredMessage = MessageFormat.format("Field {0} cannot be parsed to long: {1}, exception:{2}", field, value, ex);
+
+						// Abort processing here
+						break;
+					}
+				} else if (value instanceof Integer) {
+					// Is integer
 					i = (Integer) value;
-				} else {
-					// Cast to Long
+				} else if (value instanceof Long) {
+					// Is long
 					l = (Long) value;
 				}
 
@@ -92,19 +86,23 @@ public abstract class BaseLongValidator extends BaseObjectValidator implements V
 					requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", i, field); //NOI18N
 
 					// Abort processing here
-					facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
 					break;
 				} else if ((!allowNull) && ((l instanceof Long) && (l < 0))) {
 					// Generate message
 					requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", l, field); //NOI18N
 
 					// Abort processing here
-					facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
 					break;
 				}
 			}
 		}
 
+		// Is requiredMessage set?
+		if (null != requiredMessage) {
+			// Embed into FacesMessage
+			facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage);
+		}
+
 		// Is facesMessage set?
 		if (null != facesMessage) {
 			// Abort here