]> git.mxchange.org Git - jjobs-war.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Thu, 19 Jan 2023 05:34:49 +0000 (06:34 +0100)
committerRoland Häder <roland@mxchange.org>
Thu, 19 Jan 2023 05:34:49 +0000 (06:34 +0100)
- added valitator for skill names, including JSF tag entry
- added this validator to form

src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewBean.java
src/java/org/mxchange/jjobs/beans/jobs/skill/list/JobsSkillListWebViewController.java
src/java/org/mxchange/jjobs/validator/jobs/skill/JobsSkillNameValidator.java [new file with mode: 0644]
web/WEB-INF/validators.jsf.taglib.xml
web/admin/jobs/skill/admin_skill_list.xhtml

index bbed97d78f11aa28a0b8e509410a2423f9c04242..eeb5f4ba0646cba9a3d6ca6a2e58f8d1facf7894 100644 (file)
@@ -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;
+       }
+
 }
index f7a3ab2dc5b6c44c8e4d0eb3570822602471e5de..f286a2ee49b9e942b8d6681d3e0fe62b5cad4f81 100644 (file)
@@ -27,6 +27,15 @@ import org.mxchange.jjobs.model.skill.Skillable;
  */
 public interface JobsSkillListWebViewController extends Serializable {
 
+       /**
+        * Checks if given skill name is already there.
+        * <p>
+        * @param skillName Skill name
+        * <p>
+        * @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 (file)
index 0000000..ce41db1
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+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.
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@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
+               }
+       }
+
+}
index ef5b1deda2fb8b862dd9c056ca3a6483e0443c05..c6e85ac46fd640c303606e1e7103daf761d376a6 100644 (file)
@@ -70,4 +70,16 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
                        <required>false</required>
                </attribute>
        </tag>
+       <tag>
+               <tag-name>skillNameValidator</tag-name>
+               <validator>
+                       <validator-id>SkillNameValidator</validator-id>
+               </validator>
+               <attribute>
+                       <description>Whether ALSO check existing entries (default: check all=true).</description>
+                       <name>checkExisting</name>
+                       <type>java.lang.Boolean</type>
+                       <required>false</required>
+               </attribute>
+       </tag>
 </facelet-taglib>
index 2b1bb172a17897cbe58a1bac1d1fb28827d5cf59..8b218e5d9e8ec3d415a521f3f79d86c2b348206c 100644 (file)
@@ -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"
        >
 
                                                                required="true"
                                                                requiredMessage="#{msg.ADMIN_SKILL_NAME_REQUIRED}"
                                                                >
+                                                               <validator:skillNameValidator checkExisting="false" />
                                                        </p:inputText>
                                                </p:panelGrid>
                                        </p:fieldset>