From b39d24f569452a0285fb3920899c87044a516c9f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 19 Jan 2023 06:34:49 +0100 Subject: [PATCH] Continued: - added valitator for skill names, including JSF tag entry - added this validator to form --- .../skill/list/JobsSkillListWebViewBean.java | 29 +++++ .../list/JobsSkillListWebViewController.java | 9 ++ .../jobs/skill/JobsSkillNameValidator.java | 102 ++++++++++++++++++ web/WEB-INF/validators.jsf.taglib.xml | 12 +++ web/admin/jobs/skill/admin_skill_list.xhtml | 2 +- 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 src/java/org/mxchange/jjobs/validator/jobs/skill/JobsSkillNameValidator.java diff --git a/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewBean.java b/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewBean.java index bbed97d7..eeb5f4ba 100644 --- a/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewBean.java +++ b/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewBean.java @@ -21,6 +21,7 @@ import java.text.MessageFormat; import java.util.Comparator; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import javax.annotation.PostConstruct; import javax.cache.Cache; import javax.ejb.EJB; @@ -224,4 +225,32 @@ public class JobsSkillListWebViewBean extends BaseJobsBean implements JobsSkillL } } + @Override + public Boolean isSkillNameUsed (final String skillName) { + // Validate parameter + if (null == skillName) { + // Throw NPE + throw new NullPointerException("Parameter 'skillName' is null"); //NOI18N + } else if (skillName.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("Parameter 'skillName' is empty"); //NOI18N + } + + // Default is not found + boolean isFound = false; + + // Check all entries + for (final Skillable skillable : this.getAllSkills()) { + // Is same company name? + if (Objects.equals(skillable.getSkillName(), skillName)) { + // Found it + isFound = true; + break; + } + } + + // Return flag + return isFound; + } + } diff --git a/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewController.java b/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewController.java index f7a3ab2d..f286a2ee 100644 --- a/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewController.java +++ b/src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewController.java @@ -27,6 +27,15 @@ import org.mxchange.jjobs.model.skill.Skillable; */ public interface JobsSkillListWebViewController extends Serializable { + /** + * Checks if given skill name is already there. + *

+ * @param skillName Skill name + *

+ * @return Whether it has been found + */ + Boolean isSkillNameUsed (final String skillName); + /** * Retrieves a single skill entity for given id number or throws a proper * exception if not found. diff --git a/src/java/org/mxchange/jjobs/validator/jobs/skill/JobsSkillNameValidator.java b/src/java/org/mxchange/jjobs/validator/jobs/skill/JobsSkillNameValidator.java new file mode 100644 index 00000000..ce41db10 --- /dev/null +++ b/src/java/org/mxchange/jjobs/validator/jobs/skill/JobsSkillNameValidator.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2016 - 2022 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 + * 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 . + */ +package org.mxchange.jjobs.validator.jobs.skill; + +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.jobs.skill.list.JobsSkillListWebViewBean; +import org.mxchange.jjobs.beans.jobs.skill.list.JobsSkillListWebViewController; + +/** + * A validation class for skill names. + *

+ * @author Roland Häder + */ +@FacesValidator ("SkillNameValidator") +public class JobsSkillNameValidator extends BaseStringValidator { + + /** + * Skills backing bean + */ + private static JobsSkillListWebViewController SKILL_LIST_CONTROLLER; + + /** + * Serial number + */ + private static final long serialVersionUID = 27_587_896_710_689_461L; + + @Override + public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException { + // All accepted, required fields + final String[] requiredFields = {"skillName"}; //NOI18N + + // Pre-validation (example: not null, not a string, empty string ...) + super.preValidate(context, component, value, requiredFields, Boolean.FALSE); + + // Convert name to string (now securely checked in BaseStringValidator) + final String skillName = (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); + } + + // Is the instance there? + if (null == SKILL_LIST_CONTROLLER) { + // Get bean from CDI directly + SKILL_LIST_CONTROLLER = CDI.current().select(JobsSkillListWebViewBean.class).get(); + } + + // Check if name is already used + final Boolean nameExists = SKILL_LIST_CONTROLLER.isSkillNameUsed(skillName); + + // Is the user id valid? + if ((!nameExists) && (checkExisting)) { + // Format message + final String message = MessageFormat.format("No basic data found with comany name {0}.", skillName); + + // 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("Found basic data with comany name {0}.", skillName); + + // Name already exists + throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N + } + } + +} diff --git a/web/WEB-INF/validators.jsf.taglib.xml b/web/WEB-INF/validators.jsf.taglib.xml index ef5b1ded..c6e85ac4 100644 --- a/web/WEB-INF/validators.jsf.taglib.xml +++ b/web/WEB-INF/validators.jsf.taglib.xml @@ -70,4 +70,16 @@ along with this program. If not, see . false + + skillNameValidator + + SkillNameValidator + + + Whether ALSO check existing entries (default: check all=true). + checkExisting + java.lang.Boolean + false + + diff --git a/web/admin/jobs/skill/admin_skill_list.xhtml b/web/admin/jobs/skill/admin_skill_list.xhtml index 2b1bb172..8b218e5d 100644 --- a/web/admin/jobs/skill/admin_skill_list.xhtml +++ b/web/admin/jobs/skill/admin_skill_list.xhtml @@ -6,7 +6,6 @@ xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:p="http://primefaces.org/ui" - xmlns:core="http://mxchange.org/jsf/core/widgets" xmlns:validator="http://mxchange.org/jsf/core/validators" > @@ -245,6 +244,7 @@ required="true" requiredMessage="#{msg.ADMIN_SKILL_NAME_REQUIRED}" > + -- 2.39.5