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