]> git.mxchange.org Git - jjobs-war.git/commitdiff
Please cherry-pick:
authorRoland Häder <roland@mxchange.org>
Thu, 23 Apr 2020 00:34:19 +0000 (02:34 +0200)
committerRoland Häder <roland@mxchange.org>
Wed, 10 Jun 2020 17:33:48 +0000 (19:33 +0200)
- ops, missed to add the validator for companyRoadNumber field

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jjobs/validator/business/basicdata/roadnumber/JobsBasicDataCompanyRoadNumberValidator.java [new file with mode: 0644]

diff --git a/src/java/org/mxchange/jjobs/validator/business/basicdata/roadnumber/JobsBasicDataCompanyRoadNumberValidator.java b/src/java/org/mxchange/jjobs/validator/business/basicdata/roadnumber/JobsBasicDataCompanyRoadNumberValidator.java
new file mode 100644 (file)
index 0000000..1480915
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2017 - 2020 Free Software Foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jjobs.validator.business.basicdata.roadnumber;
+
+import java.text.MessageFormat;
+import javax.enterprise.inject.spi.CDI;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.FacesValidator;
+import javax.faces.validator.ValidatorException;
+import org.mxchange.jcoreee.validator.string.BaseStringValidator;
+import org.mxchange.jjobs.beans.business.basicdata.list.JobsBasicDataListWebViewBean;
+import org.mxchange.jjobs.beans.business.basicdata.list.JobsBasicDataListWebViewController;
+
+/**
+ * A validator for basic data company road number
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@FacesValidator (value = "BasicDataCompanyRoadNumberValidator")
+public class JobsBasicDataCompanyRoadNumberValidator extends BaseStringValidator {
+
+       /**
+        * Business basic data backing bean
+        */
+       private static JobsBasicDataListWebViewController BASIC_DATA_LIST_CONTROLLER;
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 57_283_657_476_561L;
+
+       @Override
+       public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
+               // Is the instance there?
+               if (null == BASIC_DATA_LIST_CONTROLLER) {
+                       // Get bean from CDI directly
+                       BASIC_DATA_LIST_CONTROLLER = CDI.current().select(JobsBasicDataListWebViewBean.class).get();
+               }
+
+               // All accepted, required fields
+               final String[] requiredFields = {"companyRoadNumber"}; //NOI18N
+
+               // Pre-validation (example: not null, not a string, empty string ...)
+               super.preValidate(context, component, value, requiredFields, true);
+
+               // Convert name to string (now securely checked in BaseStringValidator)
+               final String companyRoadNumber = (String) value;
+
+               // Default is to check on existing names
+               Boolean checkExisting = Boolean.TRUE;
+
+               // Is attribute "checkExisting" set?
+               if (component.getAttributes().containsKey("checkExisting")) { //NOI18N
+                       // Get attribute
+                       final Object attribute = component.getAttributes().get("checkExisting"); //NOI18N
+
+                       // Make sure, it is Boolean as no String is accepted anymore
+                       if (!(attribute instanceof String)) {
+                               // Not valid attribute, please use "true" or "false" (default)
+                               throw new IllegalArgumentException("checkExisting must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
+                       }
+
+                       // Securely cast it
+                       checkExisting = Boolean.parseBoolean((String) attribute);
+               }
+
+               // Check if name is already used
+               final Boolean nameExists = (companyRoadNumber != null && BASIC_DATA_LIST_CONTROLLER.isCompanyRoadNumberUsed(companyRoadNumber));
+
+               // Is the user id valid?
+               if ((!nameExists) && (checkExisting)) {
+                       // Format message
+                       final String message = MessageFormat.format("Comany with road number {0} does not exist.", companyRoadNumber);
+
+                       // Name does not exist
+                       throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
+               } else if ((nameExists) && (!checkExisting)) {
+                       // Format message
+                       final String message = MessageFormat.format("Company with road number {0} already exist.", companyRoadNumber);
+
+                       // Name already exists
+                       throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
+               }
+       }
+
+}