]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/validator/business/headquarter/JobsHeadquarterCompanyNameValidator.java
Updated copyright year
[jjobs-war.git] / src / java / org / mxchange / jjobs / validator / business / headquarter / JobsHeadquarterCompanyNameValidator.java
1 /*
2  * Copyright (C) 2017 - 2020 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.jjobs.validator.business.headquarter;
18
19 import java.text.MessageFormat;
20 import javax.enterprise.inject.spi.CDI;
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 import org.mxchange.jjobs.beans.business.headquarter.JobsHeadquarterWebRequestBean;
28 import org.mxchange.jjobs.beans.business.headquarter.JobsHeadquarterWebRequestController;
29
30 /**
31  * A validator for headquarter company names
32  * <p>
33  * @author Roland Häder<roland@mxchange.org>
34  */
35 @FacesValidator (value = "HeadquarterCompanyNameValidator")
36 public class JobsHeadquarterCompanyNameValidator extends BaseStringValidator {
37
38         /**
39          * Headquarter backing bean
40          */
41         private static JobsHeadquarterWebRequestController HEADQUARTER_CONTROLLER;
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 57_283_657_476_561L;
47
48         @Override
49         public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
50                 // Is the instance there?
51                 if (null == HEADQUARTER_CONTROLLER) {
52                         // Get bean from CDI directly
53                         HEADQUARTER_CONTROLLER = CDI.current().select(JobsHeadquarterWebRequestBean.class).get();
54                 }
55
56                 // All accepted, required fields
57                 final String[] requiredFields = {"companyName"}; //NOI18N
58
59                 // Pre-validation (example: not null, not a string, empty string ...)
60                 super.preValidate(context, component, value, requiredFields, false);
61
62                 // Convert name to string (now securely checked in BaseStringValidator)
63                 final String companyName = (String) value;
64
65                 // Default is to check on existing names
66                 Boolean checkExisting = Boolean.TRUE;
67
68                 // Is attribute "allowEmptyRequiredData" set?
69                 if (component.getAttributes().containsKey("checkExisting")) { //NOI18N
70                         // Get attribute
71                         final Object attribute = component.getAttributes().get("checkExisting"); //NOI18N
72
73                         // Make sure, it is Boolean as no String is accepted anymore
74                         if (!(attribute instanceof String)) {
75                                 // Not valid attribute, please use "true" or "false" (default)
76                                 throw new IllegalArgumentException("checkExisting must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
77                         }
78
79                         // Securely cast it
80                         checkExisting = Boolean.parseBoolean((String) attribute);
81                 }
82
83                 // Check if name is already used
84                 final Boolean nameExists = HEADQUARTER_CONTROLLER.isCompanyNameUsed(companyName);
85
86                 // Is the user id valid?
87                 if ((!nameExists) && (checkExisting)) {
88                         // Format message
89                         final String message = MessageFormat.format("No basic data found with comany name {0}.", companyName);
90
91                         // Name does not exist
92                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
93                 } else if ((nameExists) && (!checkExisting)) {
94                         // Format message
95                         final String message = MessageFormat.format("Found basic data with comany name {0}.", companyName);
96
97                         // Name already exists
98                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
99                 }
100         }
101
102 }