]> git.mxchange.org Git - jcore-utils.git/commitdiff
added generic date validator which needs to be extended
authorRoland Häder <roland@mxchange.org>
Fri, 20 May 2016 15:17:08 +0000 (17:17 +0200)
committerRoland Häder <roland@mxchange.org>
Fri, 20 May 2016 15:17:08 +0000 (17:17 +0200)
src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java
src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java [new file with mode: 0644]

index cdb26d93e50202e926e5b16227a5f1f6e6881711..5a9e57357732e0478d0780f7e81ee539d38b7013 100644 (file)
@@ -110,4 +110,5 @@ public abstract class BaseBooleanValidator extends BaseObjectValidator implement
                        throw new ValidatorException(facesMessage);
                }
        }
+
 }
diff --git a/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java b/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java
new file mode 100644 (file)
index 0000000..d9df554
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2016 Roland Haeder
+ *
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcoreee.validator.date;
+
+import java.text.MessageFormat;
+import java.util.Date;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.ValidatorException;
+import org.mxchange.jcoreee.validator.BaseObjectValidator;
+
+/**
+ * A generic validator class for dates
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+public abstract class BaseDateValidator extends BaseObjectValidator {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 57_341_298_601_276L;
+
+       @Override
+       public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, boolean allowNull) throws ValidatorException {
+               // Trace message
+               //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N
+
+               // Pre-validate
+               super.preValidate(context, component, value, requiredFields, allowNull);
+
+               // Get client id and init message + key
+               String clientId = component.getClientId();
+               FacesMessage facesMessage = null;
+               String requiredMessage;
+
+               // So far all fine, no check if the field is fine
+               for (final String field : requiredFields) {
+                       // Debug message
+                       //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N
+
+                       // Is it the same?
+                       if (clientId.endsWith(field)) {
+                               // Compare value's type
+                               if (!(value instanceof Date)) {
+                                       // Generate message
+                                       requiredMessage = MessageFormat.format("Field {0} is not Date: {1}", field, value); //NOI18N
+
+                                       // Value is not right type
+                                       facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N
+                                       break;
+                               }
+
+                               // Cast to date
+                               Date date = (Date) value;
+
+                               // @TODO Continue here?
+                       }
+               }
+
+               // Is facesMessage set?
+               if (null != facesMessage) {
+                       // Abort here
+                       throw new ValidatorException(facesMessage);
+               }
+       }
+
+}