2 * Copyright (C) 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jjobs.validator.business.basicdata;
19 import java.text.MessageFormat;
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.validator.FacesValidator;
24 import javax.faces.validator.ValidatorException;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
29 import org.mxchange.jjobs.beans.business.basicdata.JobsBusinessDataWebRequestBean;
30 import org.mxchange.jjobs.beans.business.basicdata.JobsBusinessDataWebRequestController;
33 * A validator for company names
35 * @author Roland Häder<roland@mxchange.org>
37 @FacesValidator ("CompanyNameValidator")
38 public class JobsCompanyNameValidator extends BaseStringValidator {
41 * Business basic data backing bean
43 private static JobsBusinessDataWebRequestController BASIC_DATA_CONTROLLER;
48 private static final long serialVersionUID = 57_283_657_476_561L;
51 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
52 // Is the instance there?
53 if (BASIC_DATA_CONTROLLER == null) {
55 // Not yet, attempt lookup
56 final Context initial = new InitialContext();
59 BASIC_DATA_CONTROLLER = (JobsBusinessDataWebRequestController) initial.lookup(String.format("java:module/%s", JobsBusinessDataWebRequestBean.class.getSimpleName())); //NOI18N
60 } catch (final NamingException ex) {
62 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup backing bean", ex.getMessage()), ex);
66 // All accepted, required fields
67 final String[] requiredFields = {"companyName"}; //NOI18N
69 // Pre-validation (example: not null, not a string, empty string ...)
70 super.preValidate(context, component, value, requiredFields, false);
72 // Convert name to string (now securely checked in BaseStringValidator)
73 final String companyName = (String) value;
75 // Default is to check on existing names
76 Boolean checkExisting = Boolean.TRUE;
78 // Is attribute "allowEmptyValue" set?
79 if (component.getAttributes().containsKey("checkExisting")) { //NOI18N
81 final Object attribute = component.getAttributes().get("checkExisting"); //NOI18N
83 // Make sure, it is Boolean as no String is accepted anymore
84 if (!(attribute instanceof String)) {
85 // Not valid attribute, please use "true" or "false" (default)
86 throw new IllegalArgumentException("checkExisting must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
90 checkExisting = Boolean.parseBoolean((String) attribute);
93 // Check if name is already used
94 final Boolean nameExists = BASIC_DATA_CONTROLLER.isCompanyNameUsed(companyName);
96 // Is the user id valid?
97 if ((!nameExists) && (checkExisting)) {
99 final String message = MessageFormat.format("No basic data found with comany name {0}.", companyName);
101 // Name does not exist
102 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
103 } else if ((nameExists) && (!checkExisting)) {
105 final String message = MessageFormat.format("Found basic data with comany name {0}.", companyName);
107 // Name already exists
108 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N