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;
}
}
+ @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;
+ }
+
}
--- /dev/null
+/*
+ * 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
+ }
+ }
+
+}