2 * Copyright (C) 2017 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jfinancials.validator.product_category;
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.product_category.list.FinancialsCategoryListWebViewBean;
28 import org.mxchange.jfinancials.beans.product_category.list.FinancialsCategoryListWebViewController;
31 * A validator for product categories, will fail when category i18n key is
34 * @author Roland Häder<roland@mxchange.org>
36 @FacesValidator (value = "ProductCategoryValidator")
37 public class FinancialsProductCategoryValidator extends BaseStringValidator {
40 * Backing bean for product categories
42 private static FinancialsCategoryListWebViewController CATEGORY_LIST_CONTROLLER;
47 private static final long serialVersionUID = 1_086_256_763_716_450L;
50 * Whether bypass duplicate i18n key check
52 private Boolean bypassDuplicateI18nKey;
57 public FinancialsProductCategoryValidator () {
58 // Set allowEmpty to FALSE by default
59 this.bypassDuplicateI18nKey = Boolean.FALSE;
63 * Setter for bypassDuplicateI18nKey flag
65 * @param bypassDuplicateI18nKey Whether to bypass i18n key duplicate-check
67 public void setBypassDuplicateI18nKey (final Boolean bypassDuplicateI18nKey) {
68 this.bypassDuplicateI18nKey = bypassDuplicateI18nKey;
72 public void validate (final FacesContext context, final UIComponent component, final Object categoryI18nKey) throws ValidatorException {
74 final String[] requiredFields = {"categoryI18nKey"}; //NOI18N
77 super.preValidate(context, component, categoryI18nKey, requiredFields, Boolean.FALSE);
79 // Is the instance there?
80 if (null == CATEGORY_LIST_CONTROLLER) {
81 // Then get it from CDI
82 CATEGORY_LIST_CONTROLLER = CDI.current().select(FinancialsCategoryListWebViewBean.class).get();
85 // Check, if the name has already been used
86 if (!this.bypassDuplicateI18nKey && CATEGORY_LIST_CONTROLLER.isCategoryI18nKeyAdded((String) categoryI18nKey)) {
88 final String message = MessageFormat.format("I18n key {0} is already used. Please type an other.", categoryI18nKey);
91 throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));