]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/validator/business/basicdata/PizzaCompanyNameValidator.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / validator / business / basicdata / PizzaCompanyNameValidator.java
1 /*
2  * Copyright (C) 2017 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.validator.business.basicdata;
18
19 import java.text.MessageFormat;
20 import javax.ejb.EJB;
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.jcoreee.validator.string.BaseStringValidator;
27
28 /**
29  * A validator for company names
30  * <p>
31  * @author Roland Häder<roland@mxchange.org>
32  */
33 @FacesValidator (value = "CompanyNameValidator", managed = true)
34 public class PizzaCompanyNameValidator extends BaseStringValidator {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 57_283_657_476_561L;
40
41         /**
42          * Business contact EJB
43          */
44         @EJB (lookup = "java:global/jfinancials-ejb/businessData!org.mxchange.jcontactsbusiness.basicdata.BusinessDataSessionBeanRemote")
45         private BusinessDataSessionBeanRemote basicDataBean;
46
47         @Override
48         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
49                 // All accepted, required fields
50                 String[] requiredFields = {"companyName"}; //NOI18N
51
52                 // Pre-validation (example: not null, not a string, empty string ...)
53                 super.preValidate(context, component, value, requiredFields, false);
54
55                 // Convert name to string (now securely checked in BaseStringValidator)
56                 String companyName = (String) value;
57
58                 // Default is to check on existing names
59                 Boolean checkExisting = Boolean.TRUE;
60
61                 // Is attribute "allowEmptyValue" set?
62                 if (component.getAttributes().containsKey("checkExisting")) { //NOI18N
63                         // Get attribute
64                         Object attribute = component.getAttributes().get("checkExisting"); //NOI18N
65
66                         // Make sure, it is Boolean as no String is accepted anymore
67                         if (!(attribute instanceof String)) {
68                                 // Not valid attribute, please use "true" or "false" (default)
69                                 throw new IllegalArgumentException("checkExisting must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
70                         }
71
72                         // Securely cast it
73                         checkExisting = Boolean.parseBoolean((String) attribute);
74                 }
75
76                 // Check if name is already used
77                 Boolean nameExists = this.basicDataBean.isCompanyNameUsed(companyName);
78
79                 // Is the user id valid?
80                 if ((!nameExists) && (checkExisting)) {
81                         // Format message
82                         String message = MessageFormat.format("No basic data found with comany name {0}.", companyName);
83
84                         // Name does not exist
85                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
86                 } else if ((nameExists) && (!checkExisting)) {
87                         // Format message
88                         String message = MessageFormat.format("Found basic data with comany name {0}.", companyName);
89
90                         // Name already exists
91                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
92                 }
93         }
94
95 }