]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/validator/generic_product/FinancialsGenericProductValidator.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / validator / generic_product / FinancialsGenericProductValidator.java
1 /*
2  * Copyright (C) 2017 - 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.jfinancials.validator.generic_product;
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.jfinancials.beans.generic_product.list.FinancialsProductListWebViewBean;
28 import org.mxchange.jfinancials.beans.generic_product.list.FinancialsProductListWebViewController;
29
30 /**
31  * A validator for generic products, will fail when product i18n key is already
32  * used.
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @FacesValidator (value = "GenericProductValidator")
37 public class FinancialsGenericProductValidator extends BaseStringValidator {
38
39         /**
40          * Backing bean for product categories
41          */
42         private static FinancialsProductListWebViewController PRODUCT_LIST_CONTROLLER;
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 1_086_256_763_716_450L;
48
49         @Override
50         public void validate (final FacesContext context, final UIComponent component, final Object productI18nKey) throws ValidatorException {
51                 // The required field
52                 final String[] requiredFields = {"productI18nKey"}; //NOI18N
53
54                 // Pre-validate it
55                 super.preValidate(context, component, productI18nKey, requiredFields, Boolean.FALSE);
56
57                 // Is the instance there?
58                 if (null == PRODUCT_LIST_CONTROLLER) {
59                         // Then get it from CDI
60                         PRODUCT_LIST_CONTROLLER = CDI.current().select(FinancialsProductListWebViewBean.class).get();
61                 }
62
63                 // Don't allow duplicates by default
64                 Boolean allowDuplicates = Boolean.FALSE;
65
66                 // Is attribute "allowEmptyRequiredData" set?
67                 if (component.getAttributes().containsKey("allowDuplicates")) { //NOI18N
68                         // Get attribute
69                         final Object attribute = component.getAttributes().get("allowDuplicates"); //NOI18N
70
71                         // Make sure, it is Boolean as no String is accepted anymore
72                         if (!(attribute instanceof String)) {
73                                 // Not valid attribute, please use "true" or "false" (default)
74                                 throw new IllegalArgumentException("allowDuplicates must be of type String. Please use \"true\" or \"false\" for f:attribute value."); //NOI18N
75                         }
76
77                         // Securely cast it
78                         allowDuplicates = Boolean.parseBoolean((String) attribute);
79                 }
80
81                 // Check, if the name has already been used
82                 if (!allowDuplicates && PRODUCT_LIST_CONTROLLER.isProductI18nKeyAdded((String) productI18nKey)) {
83                         // Create message
84                         final String message = MessageFormat.format("I18n key {0} is already used. Please type an other.", productI18nKey);
85
86                         // Throw exception
87                         throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
88                 }
89         }
90
91 }