2 * Copyright (C) 2016 - 2020 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.beans.generic_product.action;
19 import java.math.BigDecimal;
20 import java.text.MessageFormat;
21 import java.util.Objects;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.FacesException;
26 import javax.faces.application.FacesMessage;
27 import javax.faces.view.ViewScoped;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
32 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
33 import org.mxchange.jfinancials.beans.generic_product.list.FinancialsProductListWebViewController;
34 import org.mxchange.jproduct.events.product.added.AddedProductEvent;
35 import org.mxchange.jproduct.events.product.added.ProductAddedEvent;
36 import org.mxchange.jproduct.events.product.updated.ObservableProductUpdatedEvent;
37 import org.mxchange.jproduct.events.product.updated.ProductUpdatedEvent;
38 import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException;
39 import org.mxchange.jproduct.exceptions.product.ProductNotFoundException;
40 import org.mxchange.jproduct.model.category.Category;
41 import org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote;
42 import org.mxchange.jproduct.model.product.GenericProduct;
43 import org.mxchange.jproduct.model.product.Product;
44 import org.mxchange.jproduct.model.product.agegroup.AgeGroup;
47 * Main application class
49 * @author Roland Häder<roland@mxchange.org>
51 @Named ("adminGenericProductActionController")
53 public class FinancialAdminGenericProductActionWebViewBean extends BaseFinancialsBean implements FinancialAdminProductActionWebViewController {
58 private static final long serialVersionUID = 5_819_375_183_472_872L;
61 * Event for added currentProduct
65 private Event<AddedProductEvent> addedProductEvent;
68 * Remote bean for products
70 @EJB (lookup = "java:global/jfinancials-ejb/adminProduct!org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote")
71 private AdminProductSessionBeanRemote adminProductBean;
76 private Product currentProduct;
81 private AgeGroup productAgeGroup;
86 private Boolean productAvailability;
91 private String productBarCodeNumber;
96 private Category productCategory;
99 * Product's price currency code like EUR or USD
101 private String productCurrencyCode;
104 * Product's FSC number
106 private String productFscNumber;
109 * Product's gross price
111 private BigDecimal productGrossPrice;
114 * I18n key of currentProduct
116 private String productI18nKey;
121 private Long productId;
124 * Product list controller
127 private FinancialsProductListWebViewController productListController;
130 * Product's manufacturing/producing company
132 private BasicData productManufacturer;
135 * Product's net price
137 private BigDecimal productNetPrice;
142 private Long productNumber;
145 * Product size (for shoes, clothings)
147 private String productSize;
152 private BigDecimal productTaxRate;
155 * Product's unit amount
157 private BigDecimal productUnitAmount;
162 private String productUnitI18nKey;
165 * Event for updated currentProduct
169 private Event<ObservableProductUpdatedEvent> updatedProductEvent;
172 * Default constructor
174 public FinancialAdminGenericProductActionWebViewBean () {
175 // Call super constructor
180 * Adds given product data from request to database
182 * @throws FaceletException If something unexpected happened
184 public void addProduct () throws FaceletException {
185 // Is product i18n key already used?
186 if (this.productListController.isProductI18nKeyAdded(this.getProductI18nKey())) {
187 // Then throw exception
188 throw new FacesException(MessageFormat.format("Product i18n key {0} already added.", this.getProductI18nKey())); //NOI18N
191 // Create current product instance
192 final Product product = this.createProductInstance();
194 // Declare updated current product instance
195 final Product updatedProduct;
199 updatedProduct = this.adminProductBean.addGenericProduct(product);
200 } catch (final ProductAlreadyAddedException ex) {
202 throw new FacesException(ex);
206 this.addedProductEvent.fire(new ProductAddedEvent(updatedProduct));
210 * Copies all currentProduct's properties back to this bean.
212 public void copyAllProductProperties () {
213 // Validate current product instance
214 if (this.getCurrentProduct() == null) {
216 throw new NullPointerException("this.product is null"); //NOI18N
217 } else if (this.getCurrentProduct().getProductId() == null) {
219 throw new NullPointerException("this.product.productId is null"); //NOI18N
220 } else if (this.getCurrentProduct().getProductId() < 1) {
222 throw new IllegalStateException(MessageFormat.format("this.product.productId={0} is not valid.", this.getCurrentProduct().getProductId())); //NOI18N
225 // Now copy all fields
226 this.setProductAgeGroup(this.getCurrentProduct().getProductAgeGroup());
227 this.setProductAvailability(this.getCurrentProduct().getProductAvailability());
228 this.setProductBarCodeNumber(this.getCurrentProduct().getProductBarCodeNumber());
229 this.setProductCategory(this.getCurrentProduct().getProductCategory());
230 this.setProductCurrencyCode(this.getCurrentProduct().getProductCurrencyCode());
231 this.setProductFscNumber(this.getCurrentProduct().getProductFscNumber());
232 this.setProductGrossPrice(this.getCurrentProduct().getProductGrossPrice());
233 this.setProductI18nKey(this.getCurrentProduct().getProductI18nKey());
234 this.setProductId(this.getCurrentProduct().getProductId());
235 this.setProductManufacturer(this.getCurrentProduct().getProductManufacturer());
236 this.setProductNetPrice(this.getCurrentProduct().getProductNetPrice());
237 this.setProductNumber(this.getCurrentProduct().getProductNumber());
238 this.setProductSize(this.getCurrentProduct().getProductSize());
239 this.setProductTaxRate(this.getCurrentProduct().getProductTaxRate());
240 this.setProductUnitAmount(this.getCurrentProduct().getProductUnitAmount());
241 this.setProductUnitI18nKey(this.getCurrentProduct().getProductUnitI18nKey());
245 * Getter for current product instance
247 * @return Product instance
249 public Product getCurrentProduct () {
250 return this.currentProduct;
254 * Setter for current product instance
256 * @param currentProduct Current product instance
258 public void setCurrentProduct (final Product currentProduct) {
259 this.currentProduct = currentProduct;
263 * Getter for product's age group
265 * @return Product's age group
267 public AgeGroup getProductAgeGroup () {
268 return this.productAgeGroup;
272 * Setter for product's age group
274 * @param productAgeGroup Product's age group
276 public void setProductAgeGroup (final AgeGroup productAgeGroup) {
277 this.productAgeGroup = productAgeGroup;
281 * Getter for product's available
283 * @return Product's available
285 public Boolean getProductAvailability () {
286 return this.productAvailability;
290 * Setter for product's available
292 * @param productAvailability Product's available
294 public void setProductAvailability (final Boolean productAvailability) {
295 this.productAvailability = productAvailability;
299 * Getter for product's bar-code number
301 * @return Product's bar-code number
303 public String getProductBarCodeNumber () {
304 return this.productBarCodeNumber;
308 * Setter for product's bar-code number
310 * @param productBarCodeNumber Product's bar-code number
312 public void setProductBarCodeNumber (final String productBarCodeNumber) {
313 this.productBarCodeNumber = productBarCodeNumber;
317 * Getter for product's category
319 * @return Product's category
321 public Category getProductCategory () {
322 return this.productCategory;
326 * Setter for product's category instance
328 * @param productCategory Product's category instance
330 public void setProductCategory (final Category productCategory) {
331 this.productCategory = productCategory;
335 * Getter for product's price currency code
337 * @return Product's price currency code
339 public String getProductCurrencyCode () {
340 return this.productCurrencyCode;
344 * Setter for product's price currency code
346 * @param productCurrencyCode Product's price currency code
348 public void setProductCurrencyCode (final String productCurrencyCode) {
349 this.productCurrencyCode = productCurrencyCode;
353 * Getter for product's FSC number
355 * @return Product's FSC number
357 public String getProductFscNumber () {
358 return this.productFscNumber;
362 * Setter for product's FSC number
364 * @param productFscNumber Product's FSC number
366 public void setProductFscNumber (final String productFscNumber) {
367 this.productFscNumber = productFscNumber;
371 * Getter for product's gross price
373 * @return Product's gross price
375 public BigDecimal getProductGrossPrice () {
376 return this.productGrossPrice;
380 * Setter for product's gross price
382 * @param productGrossPrice Product's gross price
384 public void setProductGrossPrice (final BigDecimal productGrossPrice) {
385 this.productGrossPrice = productGrossPrice;
389 * Getter for product unit's i18n key
391 * @return Product's i18n key
393 public String getProductI18nKey () {
394 return this.productI18nKey;
398 * Setter for product unit's i18n key
400 * @param productI18nKey Product's i18n key
402 public void setProductI18nKey (final String productI18nKey) {
403 this.productI18nKey = productI18nKey;
407 * Getter for product id
411 public Long getProductId () {
412 return this.productId;
416 * Setter for product id
418 * @param productId Product id
420 public void setProductId (final Long productId) {
421 this.productId = productId;
425 * Getter for product's manufacturing/producing company
427 * @return Product's manufacturing/producing company
429 public BasicData getProductManufacturer () {
430 return this.productManufacturer;
434 * Setter for product's manufacturing/producing company
436 * @param productManufacturer Product's manufacturing/producing company
438 public void setProductManufacturer (final BasicData productManufacturer) {
439 this.productManufacturer = productManufacturer;
443 * Getter for product's net price
445 * @return Product's net price
447 public BigDecimal getProductNetPrice () {
448 return this.productNetPrice;
452 * Setter for product's net price
454 * @param productNetPrice Product's net price
456 public void setProductNetPrice (final BigDecimal productNetPrice) {
457 this.productNetPrice = productNetPrice;
461 * Getter for product's number
463 * @return Product's number
465 public Long getProductNumber () {
466 return this.productNumber;
470 * Setter for product's number
472 * @param productNumber Product's number
474 public void setProductNumber (final Long productNumber) {
475 this.productNumber = productNumber;
479 * Getter for product's size
481 * @return Product's size
483 public String getProductSize () {
484 return this.productSize;
488 * Setter for product's size
490 * @param productSize Product's size
492 public void setProductSize (final String productSize) {
493 this.productSize = productSize;
497 * Getter for product's tax rate
499 * @return Product's tax rate
501 public BigDecimal getProductTaxRate () {
502 return this.productTaxRate;
506 * Setter for product's tax rate
508 * @param productTaxRate Product's tax rate
510 public void setProductTaxRate (final BigDecimal productTaxRate) {
511 this.productTaxRate = productTaxRate;
515 * Getter for product's unit amount
517 * @return Product's unit amount
519 public BigDecimal getProductUnitAmount () {
520 return this.productUnitAmount;
524 * Setter for product's unit amount
526 * @param productUnitAmount Product's unit amount
528 public void setProductUnitAmount (final BigDecimal productUnitAmount) {
529 this.productUnitAmount = productUnitAmount;
533 * Getter for product unit's i18n key
535 * @return Product's i18n key
537 public String getProductUnitI18nKey () {
538 return this.productUnitI18nKey;
542 * Setter for product unit's i18n key
544 * @param productUnitI18nKey Product unit's i18n key
546 public void setProductUnitI18nKey (final String productUnitI18nKey) {
547 this.productUnitI18nKey = productUnitI18nKey;
551 * Updates given product data from request to database
553 * @return Redirection outcome
555 * @throws FaceletException If something unexpected happened
557 public String updateProduct () throws FaceletException {
558 // Get new product instance
559 final Product newProduct = this.createProductInstance();
561 // Check if current product instance is in helper and valid
562 if (null == newProduct) {
564 throw new NullPointerException("newProduct is null"); //NOI18N
565 } else if (newProduct.getProductId() == null) {
567 throw new NullPointerException("newProduct.productId is null"); //NOI18N
568 } else if (newProduct.getProductId() < 1) {
570 throw new IllegalStateException(MessageFormat.format("newProduct.productId={0} is invalid", newProduct.getProductId())); //NOI18N
573 // Has the product changed?
574 if (Objects.equals(newProduct, this.getCurrentProduct())) {
575 // Is the same product data, output message
576 this.showFacesMessage("admin-form-edit-generic-product:productI18nKey", "ADMIN_GENERIC_PRODUCT_NOT_UPDATED", FacesMessage.SEVERITY_WARN); //NOI18N
581 final Product updatedProduct;
584 // Call EJB for updating product data
585 updatedProduct = this.adminProductBean.updateProductData(newProduct);
586 } catch (final ProductNotFoundException ex) {
588 throw new FacesException(ex);
592 this.updatedProductEvent.fire(new ProductUpdatedEvent(updatedProduct));
597 // Return to admin-list view
598 return "admin_list_generic_products"; //NOI18N
602 * Clears this bean (example: product has been added)
604 private void clear () {
606 this.setProductAgeGroup(null);
607 this.setProductAvailability(Boolean.FALSE);
608 this.setProductBarCodeNumber(null);
609 this.setProductCategory(null);
610 this.setProductFscNumber(null);
611 this.setProductGrossPrice(null);
612 this.setProductI18nKey(null);
613 this.setProductId(null);
614 this.setProductManufacturer(null);
615 this.setProductNetPrice(null);
616 this.setProductNumber(null);
617 this.setProductSize(null);
618 this.setProductTaxRate(null);
619 this.setProductUnitAmount(null);
620 this.setProductUnitI18nKey(null);
624 * Creates a new product instance with all fields
626 * @return Product instance
628 private Product createProductInstance () {
629 // Create new product instance
630 final Product newProduct = new GenericProduct(
631 this.getProductI18nKey(),
632 this.getProductGrossPrice(),
633 this.getProductCurrencyCode(),
634 this.getProductCategory(),
635 this.getProductAvailability(),
636 this.getProductUnitAmount(),
637 this.getProductUnitI18nKey()
640 // Set all optional fields
641 newProduct.setProductAgeGroup(this.getProductAgeGroup());
642 newProduct.setProductBarCodeNumber(this.getProductBarCodeNumber());
643 newProduct.setProductFscNumber(this.getProductFscNumber());
644 newProduct.setProductId(this.getProductId());
645 newProduct.setProductManufacturer(this.getProductManufacturer());
646 newProduct.setProductNetPrice(this.getProductNetPrice());
647 newProduct.setProductNumber(this.getProductNumber());
648 newProduct.setProductSize(this.getProductSize());
649 newProduct.setProductTaxRate(this.getProductTaxRate());