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;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.FacesException;
25 import javax.faces.view.ViewScoped;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
30 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
31 import org.mxchange.jfinancials.beans.generic_product.list.FinancialsProductListWebViewController;
32 import org.mxchange.jproduct.events.product.added.AddedProductEvent;
33 import org.mxchange.jproduct.events.product.added.ProductAddedEvent;
34 import org.mxchange.jproduct.events.product.updated.ObservableProductUpdatedEvent;
35 import org.mxchange.jproduct.events.product.updated.ProductUpdatedEvent;
36 import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException;
37 import org.mxchange.jproduct.exceptions.product.ProductNotFoundException;
38 import org.mxchange.jproduct.model.category.Category;
39 import org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote;
40 import org.mxchange.jproduct.model.product.GenericProduct;
41 import org.mxchange.jproduct.model.product.Product;
42 import org.mxchange.jproduct.model.product.agegroup.AgeGroup;
45 * Main application class
47 * @author Roland Häder<roland@mxchange.org>
49 @Named ("adminGenericProductActionController")
51 public class FinancialAdminGenericProductActionWebViewBean extends BaseFinancialsBean implements FinancialAdminProductActionWebViewController {
56 private static final long serialVersionUID = 5_819_375_183_472_872L;
59 * Event for added currentProduct
63 private Event<AddedProductEvent> addedProductEvent;
66 * Remote bean for products
68 @EJB (lookup = "java:global/jfinancials-ejb/adminProduct!org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote")
69 private AdminProductSessionBeanRemote adminProductBean;
74 private Product currentProduct;
79 private AgeGroup productAgeGroup;
84 private Boolean productAvailability;
89 private String productBarCodeNumber;
94 private Category productCategory;
97 * Product's price currency code like EUR or USD
99 private String productCurrencyCode;
102 * Product's gross price
104 private BigDecimal productGrossPrice;
107 * I18n key of currentProduct
109 private String productI18nKey;
114 private Long productId;
117 * Product list controller
120 private FinancialsProductListWebViewController productListController;
123 * Product's manufacturing/producing company
125 private BasicData productManufacturer;
128 * Product's net price
130 private BigDecimal productNetPrice;
135 private Long productNumber;
138 * Product size (for shoes, clothings)
140 private String productSize;
145 private BigDecimal productTaxRate;
148 * Product's unit amount
150 private BigDecimal productUnitAmount;
155 private String productUnitI18nKey;
158 * Event for updated currentProduct
162 private Event<ObservableProductUpdatedEvent> updatedProductEvent;
165 * Default constructor
167 public FinancialAdminGenericProductActionWebViewBean () {
168 // Call super constructor
173 * Adds given product data from request to database
175 * @throws FaceletException If something unexpected happened
177 public void addProduct () throws FaceletException {
178 // Is product i18n key already used?
179 if (this.productListController.isProductI18nKeyAdded(this.getProductI18nKey())) {
180 // Then throw exception
181 throw new FacesException(MessageFormat.format("Product i18n key {0} already added.", this.getProductI18nKey()));
184 // Create current product instance
185 final Product product = this.createProductInstance();
187 // Declare updated current product instance
188 final Product updatedProduct;
192 updatedProduct = this.adminProductBean.addGenericProduct(product);
193 } catch (final ProductAlreadyAddedException ex) {
195 throw new FacesException(ex);
199 this.addedProductEvent.fire(new ProductAddedEvent(updatedProduct));
203 * Copies all currentProduct's properties back to this bean.
205 public void copyAllProductProperties () {
206 // Validate current product instance
207 if (this.getCurrentProduct() == null) {
209 throw new NullPointerException("this.product is null"); //NOI18N
210 } else if (this.getCurrentProduct().getProductId() == null) {
212 throw new NullPointerException("this.product.productId is null"); //NOI18N
213 } else if (this.getCurrentProduct().getProductId() < 1) {
215 throw new IllegalStateException(MessageFormat.format("this.product.productId={0} is not valid.", this.getCurrentProduct().getProductId())); //NOI18N
218 // Now copy all fields
219 this.setProductAgeGroup(this.getCurrentProduct().getProductAgeGroup());
220 this.setProductAvailability(this.getCurrentProduct().getProductAvailability());
221 this.setProductBarCodeNumber(this.getCurrentProduct().getProductBarCodeNumber());
222 this.setProductCategory(this.getCurrentProduct().getProductCategory());
223 this.setProductCurrencyCode(this.getCurrentProduct().getProductCurrencyCode());
224 this.setProductGrossPrice(this.getCurrentProduct().getProductGrossPrice());
225 this.setProductI18nKey(this.getCurrentProduct().getProductI18nKey());
226 this.setProductId(this.getCurrentProduct().getProductId());
227 this.setProductManufacturer(this.getCurrentProduct().getProductManufacturer());
228 this.setProductNetPrice(this.getCurrentProduct().getProductNetPrice());
229 this.setProductNumber(this.getCurrentProduct().getProductNumber());
230 this.setProductSize(this.getCurrentProduct().getProductSize());
231 this.setProductTaxRate(this.getCurrentProduct().getProductTaxRate());
232 this.setProductUnitAmount(this.getCurrentProduct().getProductUnitAmount());
233 this.setProductUnitI18nKey(this.getCurrentProduct().getProductUnitI18nKey());
237 * Getter for current product instance
239 * @return Product instance
241 public Product getCurrentProduct () {
242 return this.currentProduct;
246 * Setter for current product instance
248 * @param currentProduct Current product instance
250 public void setCurrentProduct (final Product currentProduct) {
251 this.currentProduct = currentProduct;
255 * Getter for product's age group
257 * @return Product's age group
259 public AgeGroup getProductAgeGroup () {
260 return this.productAgeGroup;
264 * Setter for product's age group
266 * @param productAgeGroup Product's age group
268 public void setProductAgeGroup (final AgeGroup productAgeGroup) {
269 this.productAgeGroup = productAgeGroup;
273 * Getter for product's available
275 * @return Product's available
277 public Boolean getProductAvailability () {
278 return this.productAvailability;
282 * Setter for product's available
284 * @param productAvailability Product's available
286 public void setProductAvailability (final Boolean productAvailability) {
287 this.productAvailability = productAvailability;
291 * Getter for product's bar-code number
293 * @return Product's bar-code number
295 public String getProductBarCodeNumber () {
296 return this.productBarCodeNumber;
300 * Setter for product's bar-code number
302 * @param productBarCodeNumber Product's bar-code number
304 public void setProductBarCodeNumber (final String productBarCodeNumber) {
305 this.productBarCodeNumber = productBarCodeNumber;
309 * Getter for product's category
311 * @return Product's category
313 public Category getProductCategory () {
314 return this.productCategory;
318 * Setter for product's category instance
320 * @param productCategory Product's category instance
322 public void setProductCategory (final Category productCategory) {
323 this.productCategory = productCategory;
327 * Getter for product's price currency code
329 * @return Product's price currency code
331 public String getProductCurrencyCode () {
332 return this.productCurrencyCode;
336 * Setter for product's price currency code
338 * @param productCurrencyCode Product's price currency code
340 public void setProductCurrencyCode (final String productCurrencyCode) {
341 this.productCurrencyCode = productCurrencyCode;
345 * Getter for product's gross price
347 * @return Product's gross price
349 public BigDecimal getProductGrossPrice () {
350 return this.productGrossPrice;
354 * Setter for product's gross price
356 * @param productGrossPrice Product's gross price
358 public void setProductGrossPrice (final BigDecimal productGrossPrice) {
359 this.productGrossPrice = productGrossPrice;
363 * Getter for product unit's i18n key
365 * @return Product's i18n key
367 public String getProductI18nKey () {
368 return this.productI18nKey;
372 * Setter for product unit's i18n key
374 * @param productI18nKey Product's i18n key
376 public void setProductI18nKey (final String productI18nKey) {
377 this.productI18nKey = productI18nKey;
381 * Getter for product id
385 public Long getProductId () {
386 return this.productId;
390 * Setter for product id
392 * @param productId Product id
394 public void setProductId (final Long productId) {
395 this.productId = productId;
399 * Getter for product's manufacturing/producing company
401 * @return Product's manufacturing/producing company
403 public BasicData getProductManufacturer () {
404 return this.productManufacturer;
408 * Setter for product's manufacturing/producing company
410 * @param productManufacturer Product's manufacturing/producing company
412 public void setProductManufacturer (final BasicData productManufacturer) {
413 this.productManufacturer = productManufacturer;
417 * Getter for product's net price
419 * @return Product's net price
421 public BigDecimal getProductNetPrice () {
422 return this.productNetPrice;
426 * Setter for product's net price
428 * @param productNetPrice Product's net price
430 public void setProductNetPrice (final BigDecimal productNetPrice) {
431 this.productNetPrice = productNetPrice;
435 * Getter for product's number
437 * @return Product's number
439 public Long getProductNumber () {
440 return this.productNumber;
444 * Setter for product's number
446 * @param productNumber Product's number
448 public void setProductNumber (final Long productNumber) {
449 this.productNumber = productNumber;
453 * Getter for product's size
455 * @return Product's size
457 public String getProductSize () {
458 return this.productSize;
462 * Setter for product's size
464 * @param productSize Product's size
466 public void setProductSize (final String productSize) {
467 this.productSize = productSize;
471 * Getter for product's tax rate
473 * @return Product's tax rate
475 public BigDecimal getProductTaxRate () {
476 return this.productTaxRate;
480 * Setter for product's tax rate
482 * @param productTaxRate Product's tax rate
484 public void setProductTaxRate (final BigDecimal productTaxRate) {
485 this.productTaxRate = productTaxRate;
489 * Getter for product's unit amount
491 * @return Product's unit amount
493 public BigDecimal getProductUnitAmount () {
494 return this.productUnitAmount;
498 * Setter for product's unit amount
500 * @param productUnitAmount Product's unit amount
502 public void setProductUnitAmount (final BigDecimal productUnitAmount) {
503 this.productUnitAmount = productUnitAmount;
507 * Getter for product unit's i18n key
509 * @return Product's i18n key
511 public String getProductUnitI18nKey () {
512 return this.productUnitI18nKey;
516 * Setter for product unit's i18n key
518 * @param productUnitI18nKey Product unit's i18n key
520 public void setProductUnitI18nKey (final String productUnitI18nKey) {
521 this.productUnitI18nKey = productUnitI18nKey;
525 * Updates given product data from request to database
527 * @throws FaceletException If something unexpected happened
529 public void updateProduct () throws FaceletException {
530 // Get new product instance
531 final Product newProduct = this.createProductInstance();
533 // Check if current product instance is in helper and valid
534 if (null == newProduct) {
536 throw new NullPointerException("newProduct is null"); //NOI18N
537 } else if (newProduct.getProductId() == null) {
539 throw new NullPointerException("newProduct.productId is null"); //NOI18N //NOI18N
540 } else if (newProduct.getProductId() < 1) {
542 throw new IllegalStateException(MessageFormat.format("newProduct.productId={0} is invalid", newProduct.getProductId())); //NOI18N
546 final Product updatedProduct;
549 // Call EJB for updating product data
550 updatedProduct = this.adminProductBean.updateProductData(newProduct);
551 } catch (final ProductNotFoundException ex) {
553 throw new FacesException(ex);
557 this.updatedProductEvent.fire(new ProductUpdatedEvent(updatedProduct));
564 * Clears this bean (example: product has been added)
566 private void clear () {
568 this.setProductAgeGroup(null);
569 this.setProductAvailability(Boolean.FALSE);
570 this.setProductBarCodeNumber(null);
571 this.setProductCategory(null);
572 this.setProductGrossPrice(null);
573 this.setProductI18nKey(null);
574 this.setProductId(null);
575 this.setProductManufacturer(null);
576 this.setProductNetPrice(null);
577 this.setProductNumber(null);
578 this.setProductSize(null);
579 this.setProductTaxRate(null);
580 this.setProductUnitAmount(null);
581 this.setProductUnitI18nKey(null);
585 * Creates a new product instance with all fields
587 * @return Product instance
589 private Product createProductInstance () {
590 // Create new product instance
591 final Product newProduct = new GenericProduct(
592 this.getProductI18nKey(),
593 this.getProductGrossPrice(),
594 this.getProductCurrencyCode(),
595 this.getProductCategory(),
596 this.getProductAvailability(),
597 this.getProductUnitAmount(),
598 this.getProductUnitI18nKey()
601 // Set all optional fields
602 newProduct.setProductAgeGroup(this.getProductAgeGroup());
603 newProduct.setProductBarCodeNumber(this.getProductBarCodeNumber());
604 newProduct.setProductManufacturer(this.getProductManufacturer());
605 newProduct.setProductNumber(this.getProductNumber());
606 newProduct.setProductNetPrice(this.getProductNetPrice());
607 newProduct.setProductSize(this.getProductSize());
608 newProduct.setProductTaxRate(this.getProductTaxRate());
610 // Is product id set?
611 if (this.getProductId() instanceof Long) {
613 newProduct.setProductId(this.getProductId());