From: Roland Häder Date: Sat, 4 Nov 2017 00:16:54 +0000 (+0100) Subject: Maybe cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=7b28255d68e4a33ba6825da7f831cacb1ee8f5d7;p=jfinancials-war.git Maybe cherry-pick: - renamed product -> generic_product - renamed category -> product_category - added validators for both product/category i18n keys as direct titles are not localizable - added product manufacturer with is a connection to basic_data entity - general controller may have reusable methods like allFoos() or isFooI18nKeyAdded(), by exposing them in corresponding interface, they can be accessed by the administrative bean to check if entity Foo is really not there. - code convention applied: in comparison, first value then variable Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java index 14ee0cc4..e405fad3 100644 --- a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java @@ -23,6 +23,7 @@ import javax.enterprise.inject.Any; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; +import org.mxchange.jcontactsbusiness.model.basicdata.BasicData; import org.mxchange.jfinancials.beans.BaseFinancialsBean; import org.mxchange.jproduct.events.product.AddedProductEvent; import org.mxchange.jproduct.events.product.ProductAddedEvent; @@ -73,6 +74,11 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl */ private Float productGrossPrice; + /** + * Product's manufacturing/producing company + */ + private BasicData productManufacturer; + /** * Product's net price */ @@ -84,6 +90,12 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl @EJB (lookup = "java:global/jfinancials-ejb/adminProduct!org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote") private AdminProductSessionBeanRemote adminProductBean; + /** + * General product controller + */ + @Inject + private FinancialProductWebRequestController productController; + /** * Product's tax rate */ @@ -118,6 +130,12 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl * @throws FaceletException If something unexpected happened */ public void addProduct () throws FaceletException { + // Is product i18n key already used? + if (this.productController.isProductI18nKeyAdded(this.getProductI18nKey())) { + // Then throw exception + throw new FaceletException("Product i18n key " + this.getProductI18nKey() + " already added."); + } + // Create product instance final Product product = this.createProductInstance(); @@ -211,6 +229,24 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl this.productGrossPrice = productGrossPrice; } + /** + * Getter for product's manufacturing/producing company + *

+ * @return Product's manufacturing/producing company + */ + public BasicData getProductManufacturer () { + return this.productManufacturer; + } + + /** + * Setter for product's manufacturing/producing company + *

+ * @param productManufacturer Product's manufacturing/producing company + */ + public void setProductManufacturer (final BasicData productManufacturer) { + this.productManufacturer = productManufacturer; + } + /** * Getter for product's net price *

@@ -307,10 +343,11 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl private void clear () { this.setProductAvailability(Boolean.FALSE); this.setProductCategory(null); - this.setProductNetPrice(null); - this.setProductTaxRate(null); this.setProductGrossPrice(null); this.setProductI18nKey(null); + this.setProductNetPrice(null); + this.setProductManufacturer(null); + this.setProductTaxRate(null); this.setProductUnitAmount(null); this.setProductUnitType(null); } @@ -326,6 +363,7 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl // Set all optional fields product.setProductNetPrice(this.getProductNetPrice()); + product.setProductManufacturer(this.getProductManufacturer()); product.setProductTaxRate(this.getProductTaxRate()); product.setProductUnitAmount(this.getProductUnitAmount()); product.setProductUnitType(this.getProductUnitType()); diff --git a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestBean.java index 55ec5325..ba765fdd 100644 --- a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestBean.java @@ -22,6 +22,7 @@ import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import javax.annotation.PostConstruct; import javax.cache.Cache; import javax.ejb.EJB; @@ -53,7 +54,7 @@ public class FinancialProductWebRequestBean extends BaseFinancialsBean implement /** * List for all products */ - private List allProducts; + private final List allProducts; /** * List for filtered products @@ -91,7 +92,7 @@ public class FinancialProductWebRequestBean extends BaseFinancialsBean implement *

* @todo Move this to own controller */ - public void afterProductAdded (@Observes final AddedProductEvent event) { + public void afterProductAddedEvent (@Observes final AddedProductEvent event) { // Is all valid? if (null == event) { // Throw NPE @@ -107,11 +108,9 @@ public class FinancialProductWebRequestBean extends BaseFinancialsBean implement throw new IllegalArgumentException(MessageFormat.format("event.addedProduct.productId={0} is not valid.", event.getAddedProduct().getProductId())); //NOI18N } - // Is the product available? - if (event.getAddedProduct().getProductAvailability()) { - // Add it - this.allProducts.add(event.getAddedProduct()); - } + // Add it + this.productCache.put(event.getAddedProduct().getProductId(), event.getAddedProduct()); + this.allProducts.add(event.getAddedProduct()); } @Override @@ -129,7 +128,7 @@ public class FinancialProductWebRequestBean extends BaseFinancialsBean implement throw new NullPointerException("productId is null"); //NOI18N } else if (productId < 1) { // Throw IAE - throw new IllegalArgumentException("productId=" + productId + " is invalid"); //NOI18N + throw new IllegalArgumentException("productId=" + productId + " is invalid"); //NOI18N //NOI18N } else if (!this.productCache.containsKey(productId)) { // Not found throw new ProductNotFoundException(productId); @@ -204,4 +203,32 @@ public class FinancialProductWebRequestBean extends BaseFinancialsBean implement } } + @Override + public boolean isProductI18nKeyAdded (final String productI18nKey) { + // Validate parameter + if (null == productI18nKey) { + // Throw NPE + throw new NullPointerException("productI18nKey is null"); //NOI18N + } else if (productI18nKey.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("productI18nKey is empty"); //NOI18N + } + + // Default is not the same + boolean isFound = false; + + // Check all added,products + for (final Product product : this.allProducts()) { + // Is i18n key the same? + if (Objects.equals(product.getProductI18nKey(), productI18nKey)) { + // Found it + isFound = true; + break; + } + } + + // Return flag + return isFound; + } + } diff --git a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestController.java index 7dbe48cc..52490c10 100644 --- a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestController.java +++ b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestController.java @@ -31,6 +31,15 @@ import org.mxchange.jproduct.model.product.Product; @Local public interface FinancialProductWebRequestController extends Serializable { + /** + * Checks whether the given product i18n key has already used. + *

+ * @param productI18nKey Product i18n key + *

+ * @return Whether the i18n key has been used + */ + boolean isProductI18nKeyAdded (final String productI18nKey); + /** * Some "getter" for a linked list of only available products *

diff --git a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestBean.java index fb45265a..4c8a31ae 100644 --- a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestBean.java @@ -58,6 +58,12 @@ public class FinancialAdminCategoryWebRequestBean extends BaseFinancialsBean imp @EJB (lookup = "java:global/jfinancials-ejb/adminCategory!org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote") private AdminCategorySessionBeanRemote categoryBean; + /** + * General category controller + */ + @Inject + private FinancialCategoryWebRequestController categoryController; + /** * Whether this category is shown in statistics */ @@ -84,10 +90,15 @@ public class FinancialAdminCategoryWebRequestBean extends BaseFinancialsBean imp /** * Adds given category data from request to database *

- * @throws javax.faces.view.facelets.FaceletException If something - * unexpected happened + * @throws FaceletException If something unexpected happened */ public void addCategory () throws FaceletException { + // Is i18n key already used? + if (this.categoryController.isCategoryI18nKeyAdded(this.getCategoryI18nKey())) { + // Throw exception + throw new FaceletException("Category i18n key " + this.getCategoryI18nKey() + " is already used."); + } + // Create category final Category category = this.createCategoryInstance(); diff --git a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestBean.java index 3dac53be..6fec0526 100644 --- a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestBean.java @@ -22,6 +22,7 @@ import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import javax.annotation.PostConstruct; import javax.cache.Cache; import javax.ejb.EJB; @@ -88,7 +89,7 @@ public class FinancialCategoryWebRequestBean extends BaseFinancialsBean implemen *

* @param event Event to be observed */ - public void afterShopCategoryAdded (@Observes final AddedCategoryEvent event) { + public void afterCategoryAddedEvent (@Observes final AddedCategoryEvent event) { // Is all valid? if (null == event) { // Throw NPE @@ -105,6 +106,7 @@ public class FinancialCategoryWebRequestBean extends BaseFinancialsBean implemen } // Add the category + this.categoryCache.put(event.getAddedCategory().getCategoryId(), event.getAddedCategory()); this.allCategories.add(event.getAddedCategory()); } @@ -202,4 +204,32 @@ public class FinancialCategoryWebRequestBean extends BaseFinancialsBean implemen } } + @Override + public boolean isCategoryI18nKeyAdded (final String categoryI18nKey) { + // Validate parameter + if (null == categoryI18nKey) { + // Throw NPE + throw new NullPointerException("categoryI18nKey is null"); //NOI18N + } else if (categoryI18nKey.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("categoryI18nKey is empty"); //NOI18N + } + + // Default is not the same + boolean isFound = false; + + // Check all added,products + for (final Category category : this.allCategories()) { + // Is i18n key the same? + if (Objects.equals(category.getCategoryI18nKey(), categoryI18nKey)) { + // Found it + isFound = true; + break; + } + } + + // Return flag + return isFound; + } + } diff --git a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestController.java index 69fde5ee..b2bfca38 100644 --- a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestController.java +++ b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestController.java @@ -40,4 +40,14 @@ public interface FinancialCategoryWebRequestController extends Serializable { */ Category findCategoryById (final Long categoryId) throws CategoryNotFoundException; + /** + * Checks whether given category i18n key has already been used. + *

+ * @param categoryI18nKey Category i18n key + *

+ * @return Whether i18n key is already added + *

+ */ + boolean isCategoryI18nKeyAdded (final String categoryI18nKey); + } diff --git a/src/java/org/mxchange/jfinancials/converter/generic_product/FinancialsGenericProductConverter.java b/src/java/org/mxchange/jfinancials/converter/generic_product/FinancialsGenericProductConverter.java index 4c78bd34..68452ea8 100644 --- a/src/java/org/mxchange/jfinancials/converter/generic_product/FinancialsGenericProductConverter.java +++ b/src/java/org/mxchange/jfinancials/converter/generic_product/FinancialsGenericProductConverter.java @@ -43,7 +43,7 @@ public class FinancialsGenericProductConverter implements Converter { @Override public Product getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) { // Is the instance there? - if (PRODUCT_CONTROLLER == null) { + if (null == PRODUCT_CONTROLLER) { // Get bean from CDI directly PRODUCT_CONTROLLER = CDI.current().select(FinancialProductWebRequestBean.class).get(); } diff --git a/src/java/org/mxchange/jfinancials/converter/product_category/FinancialsProductCategoryConverter.java b/src/java/org/mxchange/jfinancials/converter/product_category/FinancialsProductCategoryConverter.java index 4978f233..025ec3a1 100644 --- a/src/java/org/mxchange/jfinancials/converter/product_category/FinancialsProductCategoryConverter.java +++ b/src/java/org/mxchange/jfinancials/converter/product_category/FinancialsProductCategoryConverter.java @@ -43,7 +43,7 @@ public class FinancialsProductCategoryConverter implements Converter { @Override public Category getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) { // Is the instance there? - if (CATEGORY_CONTROLLER == null) { + if (null == CATEGORY_CONTROLLER) { // Get bean from CDI directly CATEGORY_CONTROLLER = CDI.current().select(FinancialCategoryWebRequestBean.class).get(); } diff --git a/src/java/org/mxchange/jfinancials/validator/generic_product/FinancialsGenericProductValidator.java b/src/java/org/mxchange/jfinancials/validator/generic_product/FinancialsGenericProductValidator.java new file mode 100644 index 00000000..dd633d76 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/validator/generic_product/FinancialsGenericProductValidator.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jfinancials.validator.generic_product; + +import java.text.MessageFormat; +import javax.enterprise.inject.spi.CDI; +import javax.faces.application.FacesMessage; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.validator.FacesValidator; +import javax.faces.validator.ValidatorException; +import org.mxchange.jcoreee.validator.string.BaseStringValidator; +import org.mxchange.jfinancials.beans.generic_product.FinancialProductWebRequestBean; +import org.mxchange.jfinancials.beans.generic_product.FinancialProductWebRequestController; + +/** + * A validator for generic products, will fail when product i18n key is already + * used. + *

+ * @author Roland Häder + */ +@FacesValidator (value = "GenericProductValidator") +public class FinancialsGenericProductValidator extends BaseStringValidator { + + /** + * Backing bean for product categories + */ + private static FinancialProductWebRequestController PRODUCT_CONTROLLER; + + /** + * Serial number + */ + private static final long serialVersionUID = 1_086_256_763_716_450L; + + @Override + public void validate (final FacesContext context, final UIComponent component, final Object productI18nKey) throws ValidatorException { + // The required field + final String[] requiredFields = {"categoryI18nKey"}; //NOI18N + + // Pre-validate it + super.preValidate(context, component, productI18nKey, requiredFields, Boolean.FALSE); + + // Is the instance there? + if (null == PRODUCT_CONTROLLER) { + // Then get it from CDI + PRODUCT_CONTROLLER = CDI.current().select(FinancialProductWebRequestBean.class).get(); + } + + // Check, if the name has already been used + if (PRODUCT_CONTROLLER.isProductI18nKeyAdded((String) productI18nKey)) { + // Create message + final String message = MessageFormat.format("I18n key {0} is already used. Please type an other.", productI18nKey); + + // Throw exception + throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); + } + } + +} diff --git a/src/java/org/mxchange/jfinancials/validator/product_category/FinancialsProductCategoryValidator.java b/src/java/org/mxchange/jfinancials/validator/product_category/FinancialsProductCategoryValidator.java new file mode 100644 index 00000000..4da80994 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/validator/product_category/FinancialsProductCategoryValidator.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jfinancials.validator.product_category; + +import java.text.MessageFormat; +import javax.enterprise.inject.spi.CDI; +import javax.faces.application.FacesMessage; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.validator.FacesValidator; +import javax.faces.validator.ValidatorException; +import org.mxchange.jcoreee.validator.string.BaseStringValidator; +import org.mxchange.jfinancials.beans.product_category.FinancialCategoryWebRequestBean; +import org.mxchange.jfinancials.beans.product_category.FinancialCategoryWebRequestController; + +/** + * A validator for product categories, will fail when category i18n key is + * already used. + *

+ * @author Roland Häder + */ +@FacesValidator(value = "ProductCategoryValidator") +public class FinancialsProductCategoryValidator extends BaseStringValidator { + + /** + * Backing bean for product categories + */ + private static FinancialCategoryWebRequestController CATEGORY_CONTROLLER; + + /** + * Serial number + */ + private static final long serialVersionUID = 1_086_256_763_716_450L; + + @Override + public void validate (final FacesContext context, final UIComponent component, final Object categoryI18nKey) throws ValidatorException { + // The required field + final String[] requiredFields = {"categoryI18nKey"}; //NOI18N + + // Pre-validate it + super.preValidate(context, component, categoryI18nKey, requiredFields, Boolean.FALSE); + + // Is the instance there? + if (null == CATEGORY_CONTROLLER) { + // Then get it from CDI + CATEGORY_CONTROLLER = CDI.current().select(FinancialCategoryWebRequestBean.class).get(); + } + + // Check, if the name has already been used + if (CATEGORY_CONTROLLER.isCategoryI18nKeyAdded((String) categoryI18nKey)) { + // Create message + final String message = MessageFormat.format("I18n key {0} is already used. Please type an other.", categoryI18nKey); + + // Throw exception + throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); + } + } + +} diff --git a/web/WEB-INF/faces-config.xml b/web/WEB-INF/faces-config.xml index 2a0b6203..442d848a 100644 --- a/web/WEB-INF/faces-config.xml +++ b/web/WEB-INF/faces-config.xml @@ -957,6 +957,10 @@ admin_delete_generic_product /admin/generic_product/admin_generic_product_delete.xhtml + + admin_show_product_category + /admin/product_category/admin_product_category_show.xhtml + + + + + + + + + + + + + + + + + + + + + diff --git a/web/WEB-INF/resources/tags/input/panel_grid/product/product_price_input_panel_grid.tpl b/web/WEB-INF/resources/tags/input/panel_grid/product/product_price_input_panel_grid.tpl deleted file mode 100644 index 4dfc5447..00000000 --- a/web/WEB-INF/resources/tags/input/panel_grid/product/product_price_input_panel_grid.tpl +++ /dev/null @@ -1,53 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/web/WEB-INF/templates/admin/generic_product/admin_form_product_data.tpl b/web/WEB-INF/templates/admin/generic_product/admin_form_product_data.tpl index 2587060e..772b573a 100644 --- a/web/WEB-INF/templates/admin/generic_product/admin_form_product_data.tpl +++ b/web/WEB-INF/templates/admin/generic_product/admin_form_product_data.tpl @@ -35,7 +35,23 @@ required="true" requiredMessage="#{project.ADMIN_PRODUCT_I18N_KEY_REQUIRED}" title="#{project.ADMIN_ENTER_PRODUCT_I18N_KEY_TITLE}" - /> + validatorMessage="#{project.ADMIN_ENTERED_PRODUCT_I18N_KEY_ALREADY_ADDED}" + > + + + + + + + + + + validatorMessage="#{project.ADMIN_ENTERED_CATEGORY_I18N_KEY_ALREADY_ADDED}" + > + +