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;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.validator.FacesValidator;
25 import javax.faces.validator.ValidatorException;
26 import org.mxchange.jcontactsbusiness.basicdata.BusinessDataSessionBeanRemote;
27 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
30 * A validator for company names
32 * @author Roland Häder<roland@mxchange.org>
34 @FacesValidator ("CompanyNameValidator")
35 public class JobsCompanyNameValidator extends BaseStringValidator {
40 private static final long serialVersionUID = 57_283_657_476_561L;
43 * Business contact EJB
45 @EJB (lookup = "java:global/jjobs-ejb/businessData!org.mxchange.jcontactsbusiness.basicdata.BusinessDataSessionBeanRemote")
46 private BusinessDataSessionBeanRemote basicDataBean;
49 public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
50 // All accepted, required fields
51 String[] requiredFields = {"companyName"}; //NOI18N
53 // Pre-validation (example: not null, not a string, empty string ...)
54 super.preValidate(context, component, value, requiredFields, false);
56 // Convert name to string (now securely checked in BaseStringValidator)
57 String companyName = (String) value;
59 // Default is to check on existing names
60 Boolean checkExisting = Boolean.TRUE;
62 // Is attribute "allowEmptyValue" set?
63 if (component.getAttributes().containsKey("checkExisting")) { //NOI18N
65 Object attribute = component.getAttributes().get("checkExisting"); //NOI18N
67 // Make sure, it is Boolean as no String is accepted anymore
68 if (!(attribute instanceof String)) {
69 // Not valid attribute, please use "true" or "false" (default)
70 throw new IllegalArgumentException("checkExisting must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
74 checkExisting = Boolean.parseBoolean((String) attribute);
77 // Check if name is already used
78 Boolean nameExists = this.basicDataBean.isCompanyNameUsed(companyName);
80 // Is the user id valid?
81 if ((!nameExists) && (checkExisting)) {
83 String message = MessageFormat.format("No basic data found with comany name {0}.", companyName);
85 // Name does not exist
86 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
87 } else if ((nameExists) && (!checkExisting)) {
89 String message = MessageFormat.format("Found basic data with comany name {0}.", companyName);
91 // Name already exists
92 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N