]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/validator/business/basicdata/PizzaCompanyNameValidator.java
d2cb1afc4c267ac42f1d7ae9c64912c9ddfa8dd5
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / validator / business / basicdata / PizzaCompanyNameValidator.java
1 /*
2  * Copyright (C) 2022 Free Software Foundation
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.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.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote;
29 import org.mxchange.jcoreee.validator.string.BaseStringValidator;
30
31 /**
32  * A validator for company names
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @FacesValidator ("CompanyNameValidator")
37 public class PizzaCompanyNameValidator extends BaseStringValidator {
38
39         /**
40          * Business contact EJB
41          */
42         private static BasicCompanyDataSessionBeanRemote BASIC_DATA_BEAN;
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 57_283_657_476_561L;
48
49         @Override
50         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
51                 // Is the instance there?
52                 if (BASIC_DATA_BEAN == null) {
53                         try {
54                                 // Not yet, attempt lookup
55                                 final Context initial = new InitialContext();
56
57                                 // Lookup EJB
58                                 BASIC_DATA_BEAN = (BasicCompanyDataSessionBeanRemote) initial.lookup("java:global/pizzaservice-ejb/basicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote");
59                         } catch (final NamingException ex) {
60                                 // Throw it again
61                                 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
62                         }
63                 }
64
65                 // All accepted, required fields
66                 final String[] requiredFields = {"companyName"}; //NOI18N
67
68                 // Pre-validation (example: not null, not a string, empty string ...)
69                 super.preValidate(context, component, value, requiredFields, Boolean.FALSE);
70
71                 // Convert name to string (now securely checked in BaseStringValidator)
72                 final String companyName = (String) value;
73
74                 // Default is to check on existing names
75                 Boolean checkExisting = Boolean.TRUE;
76
77                 // Is attribute "checkExisting" set?
78                 if (component.getAttributes().containsKey("checkExisting")) { //NOI18N
79                         // Get attribute
80                         final Object attribute = component.getAttributes().get("checkExisting"); //NOI18N
81
82                         // Make sure, it is Boolean as no String is accepted anymore
83                         if (!(attribute instanceof String)) {
84                                 // Not valid attribute, please use "true" or "false" (default)
85                                 throw new IllegalArgumentException("checkExisting must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
86                         }
87
88                         // Securely cast it
89                         checkExisting = Boolean.valueOf((String) attribute);
90                 }
91
92                 // Check if name is already used
93                 final Boolean nameExists = BASIC_DATA_BEAN.isCompanyNameUsed(companyName);
94
95                 // Is the user id valid?
96                 if ((!nameExists) && (checkExisting)) {
97                         // Format message
98                         final String message = MessageFormat.format("No basic data found with comany name {0}.", companyName);
99
100                         // Name does not exist
101                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
102                 } else if ((nameExists) && (!checkExisting)) {
103                         // Format message
104                         final String message = MessageFormat.format("Found basic data with comany name {0}.", companyName);
105
106                         // Name already exists
107                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
108                 }
109         }
110
111 }