]> git.mxchange.org Git - jcore-utils.git/blobdiff - src/org/mxchange/jcoreee/validator/BaseObjectValidator.java
Updated copyright, changes in 2018 did take happen ...
[jcore-utils.git] / src / org / mxchange / jcoreee / validator / BaseObjectValidator.java
index 64daca07ee3c8d746cc18d178e5aa6fb1859f1dd..2e9d26f9c5a33ed1e5886804bb74f15170dda494 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 - 2018 Free Software Foundation
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 package org.mxchange.jcoreee.validator;
 
+import java.io.Serializable;
 import java.text.MessageFormat;
-import java.util.Arrays;
 import javax.faces.application.FacesMessage;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.validator.Validator;
 import javax.faces.validator.ValidatorException;
-import org.mxchange.jcoreee.BaseEeSystem;
 
 /**
- * A general object validation class. Please implement javax.faces.validator.Validator
- * (with import line!) and call preValidate(). You also may want to try out some
- * other BaseFooValidator classes before directly inheriting from this class.
- *
- * @author Roland Haeder
+ * A general object validation class. Please implement
+ * javax.faces.validator.Validator (with import line!) and call preValidate().
+ * You also may want to try out some other BaseFooValidator classes before
+ * directly inheriting from this class.
+ * <p>
+ * @param <Object> Any object that needs validation
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
  */
-public abstract class BaseObjectValidator extends BaseEeSystem implements Validator {
+public abstract class BaseObjectValidator<Object> implements Validator, Serializable {
 
        /**
-        * Needs to be implemented as the Validator interface needs to be implemented.
-        *
-        * @param context
-        * @param component
-        * @param value
-        * @throws ValidatorException 
+        * Serial number
         */
-       @Override
-       abstract public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException;
+       private static final long serialVersionUID = 48_574_878_176_939_512L;
 
        /**
-        * The method pre-validates the given value. It makes sure that the component's id is found in
-        * requiredFields and is not null. Once the component's id has been found, it stops iteration on
-        * requiredFields (which saves execution time).
-        *
-        * @param context FacesContext instance
-        * @param component UIComponent instance
-        * @param value Value to check
+        * The method pre-validates the given value. It makes sure that the
+        * component's id is found in requiredFields and is not null. Once the
+        * component's id has been found, it stops iteration on requiredFields
+        * (which saves execution time).
+        * <p>
+        * @param context        FacesContext instance
+        * @param component      UIComponent instance
+        * @param value          Value to check
         * @param requiredFields Array of required field names (ending with)
+        * @param allowNull      Whether null or empty values are allowed
+        * <p>
         * @throws ValidatorException If something more horrible went wrong
         */
-       protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields) throws ValidatorException {
+       protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException {
                // Trace message
-               this.getLogger().logTrace(MessageFormat.format("context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
 
                // Init message and key
-               FacesMessage facesMessage = null;
-               String errKey = "error.unknown_id"; //NOI18N
+               String requiredMessage = null;
 
                // Get client id
                final String clientId = component.getClientId();
 
-               // Default is no field is valid
-               boolean isValidField = false;
-
                // Check component's id against required fields and find a match
                for (final String field : requiredFields) {
                        // Get logger
-                       this.getLogger().logDebug(MessageFormat.format("field={0},clientId={1}", field, clientId)); //NOI18N
+                       //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
 
                        // Is it the same?
                        if (clientId.endsWith(field)) {
-                               // Is valid field
-                               isValidField = true;
-
                                // Is it null?
-                               if (null == value) {
-                                       errKey = String.format("error.%s.is_null", field); //NOI18N
-
+                               if ((!allowNull) && (null == value)) {
                                        // Value it null
-                                       facesMessage = new FacesMessage(getMessageStringFromKey(errKey));
+                                       requiredMessage = MessageFormat.format("Field {0} is null.", field); //NOI18N
                                }
 
                                // Abort here
@@ -94,23 +84,14 @@ public abstract class BaseObjectValidator extends BaseEeSystem implements Valida
                }
 
                // Debug message
-               this.getLogger().logDebug(MessageFormat.format("isValidField={0}", isValidField)); //NOI18N
-
-               // Valid field?
-               if (!isValidField) {
-                       // Invalid field
-                       facesMessage = new FacesMessage(MessageFormat.format(errKey, clientId));
-               }
-
-               // Debug message
-               this.getLogger().logDebug(MessageFormat.format("facesMessage={0}", facesMessage)); //NOI18N
-
+               //* NOISY-DEBUG: */ this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N
                // Is it not null?
-               if (null != facesMessage) {
-                       throw new ValidatorException(facesMessage);
+               if (null != requiredMessage) {
+                       throw new ValidatorException(new FacesMessage(MessageFormat.format("Value {0} for clientId={1} is not valid/unexpected.", value, clientId)));
                }
 
                // Trace message
-               this.getLogger().logTrace("EXIT!"); //NOI18N
+               //* NOISY-DEBUG: */ System.out.println("preValidate: EXIT!"); //NOI18N
        }
+
 }