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.view.ViewScoped;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
29 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
30 import org.mxchange.jfinancials.beans.generic_product.list.FinancialsProductListWebViewController;
31 import org.mxchange.jproduct.events.product.added.AddedProductEvent;
32 import org.mxchange.jproduct.events.product.added.ProductAddedEvent;
33 import org.mxchange.jproduct.events.product.updated.ObservableProductUpdatedEvent;
34 import org.mxchange.jproduct.events.product.updated.ProductUpdatedEvent;
35 import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException;
36 import org.mxchange.jproduct.exceptions.product.ProductNotFoundException;
37 import org.mxchange.jproduct.model.category.Category;
38 import org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote;
39 import org.mxchange.jproduct.model.product.GenericProduct;
40 import org.mxchange.jproduct.model.product.Product;
41 import org.mxchange.jproduct.model.product.agegroup.AgeGroup;
44 * Main application class
46 * @author Roland Häder<roland@mxchange.org>
48 @Named ("adminGenericProductActionController")
50 public class FinancialAdminGenericProductActionWebViewBean extends BaseFinancialsBean implements FinancialAdminProductActionWebViewController {
55 private static final long serialVersionUID = 5_819_375_183_472_872L;
58 * Event for added currentProduct
62 private Event<AddedProductEvent> addedProductEvent;
65 * Remote bean for products
67 @EJB (lookup = "java:global/jfinancials-ejb/adminProduct!org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote")
68 private AdminProductSessionBeanRemote adminProductBean;
73 private Product currentProduct;
78 private AgeGroup productAgeGroup;
83 private Boolean productAvailability;
88 private String productBarCodeNumber;
93 private Category productCategory;
96 * Product's price currency code like EUR or USD
98 private String productCurrencyCode;
101 * Product's gross price
103 private BigDecimal productGrossPrice;
106 * I18n key of currentProduct
108 private String productI18nKey;
113 private Long productId;
116 * Product list controller
119 private FinancialsProductListWebViewController productListController;
122 * Product's manufacturing/producing company
124 private BasicData productManufacturer;
127 * Product's net price
129 private BigDecimal productNetPrice;
134 private Long productNumber;
137 * Product size (for shoes, clothings)
139 private String productSize;
144 private BigDecimal productTaxRate;
147 * Product's unit amount
149 private BigDecimal productUnitAmount;
154 private String productUnitI18nKey;
157 * Event for updated currentProduct
161 private Event<ObservableProductUpdatedEvent> updatedProductEvent;
164 * Default constructor
166 public FinancialAdminGenericProductActionWebViewBean () {
167 // Call super constructor
172 * Adds given product data from request to database
174 * @throws FaceletException If something unexpected happened
176 public void addProduct () throws FaceletException {
177 // Is product i18n key already used?
178 if (this.productListController.isProductI18nKeyAdded(this.getProductI18nKey())) {
179 // Then throw exception
180 throw new FaceletException("Product i18n key " + this.getProductI18nKey() + " already added.");
183 // Create current product instance
184 final Product product = this.createProductInstance();
186 // Declare updated current product instance
187 final Product updatedProduct;
191 updatedProduct = this.adminProductBean.addGenericProduct(product);
192 } catch (final ProductAlreadyAddedException ex) {
194 throw new FaceletException(ex);
198 this.addedProductEvent.fire(new ProductAddedEvent(updatedProduct));
202 * Copies all currentProduct's properties back to this bean.
204 public void copyAllProductProperties () {
205 // Validate current product instance
206 if (this.getCurrentProduct() == null) {
208 throw new NullPointerException("this.product is null"); //NOI18N
209 } else if (this.getCurrentProduct().getProductId() == null) {
211 throw new NullPointerException("this.product.productId is null"); //NOI18N
212 } else if (this.getCurrentProduct().getProductId() < 1) {
214 throw new IllegalStateException(MessageFormat.format("this.product.productId={0} is not valid.", this.getCurrentProduct().getProductId())); //NOI18N
217 // Now copy all fields
218 this.setProductAgeGroup(this.getCurrentProduct().getProductAgeGroup());
219 this.setProductAvailability(this.getCurrentProduct().getProductAvailability());
220 this.setProductBarCodeNumber(this.getCurrentProduct().getProductBarCodeNumber());
221 this.setProductCategory(this.getCurrentProduct().getProductCategory());
222 this.setProductCurrencyCode(this.getCurrentProduct().getProductCurrencyCode());
223 this.setProductGrossPrice(this.getCurrentProduct().getProductGrossPrice());
224 this.setProductI18nKey(this.getCurrentProduct().getProductI18nKey());
225 this.setProductId(this.getCurrentProduct().getProductId());
226 this.setProductManufacturer(this.getCurrentProduct().getProductManufacturer());
227 this.setProductNetPrice(this.getCurrentProduct().getProductNetPrice());
228 this.setProductNumber(this.getCurrentProduct().getProductNumber());
229 this.setProductSize(this.getCurrentProduct().getProductSize());
230 this.setProductTaxRate(this.getCurrentProduct().getProductTaxRate());
231 this.setProductUnitAmount(this.getCurrentProduct().getProductUnitAmount());
232 this.setProductUnitI18nKey(this.getCurrentProduct().getProductUnitI18nKey());
236 * Getter for current product instance
238 * @return Product instance
240 public Product getCurrentProduct () {
241 return this.currentProduct;
245 * Setter for current product instance
247 * @param currentProduct Current product instance
249 public void setCurrentProduct (final Product currentProduct) {
250 this.currentProduct = currentProduct;
254 * Getter for product's age group
256 * @return Product's age group
258 public AgeGroup getProductAgeGroup () {
259 return this.productAgeGroup;
263 * Setter for product's age group
265 * @param productAgeGroup Product's age group
267 public void setProductAgeGroup (final AgeGroup productAgeGroup) {
268 this.productAgeGroup = productAgeGroup;
272 * Getter for product's available
274 * @return Product's available
276 public Boolean getProductAvailability () {
277 return this.productAvailability;
281 * Setter for product's available
283 * @param productAvailability Product's available
285 public void setProductAvailability (final Boolean productAvailability) {
286 this.productAvailability = productAvailability;
290 * Getter for product's bar-code number
292 * @return Product's bar-code number
294 public String getProductBarCodeNumber () {
295 return this.productBarCodeNumber;
299 * Setter for product's bar-code number
301 * @param productBarCodeNumber Product's bar-code number
303 public void setProductBarCodeNumber (final String productBarCodeNumber) {
304 this.productBarCodeNumber = productBarCodeNumber;
308 * Getter for product's category
310 * @return Product's category
312 public Category getProductCategory () {
313 return this.productCategory;
317 * Setter for product's category instance
319 * @param productCategory Product's category instance
321 public void setProductCategory (final Category productCategory) {
322 this.productCategory = productCategory;
326 * Getter for product's price currency code
328 * @return Product's price currency code
330 public String getProductCurrencyCode () {
331 return this.productCurrencyCode;
335 * Setter for product's price currency code
337 * @param productCurrencyCode Product's price currency code
339 public void setProductCurrencyCode (final String productCurrencyCode) {
340 this.productCurrencyCode = productCurrencyCode;
344 * Getter for product's gross price
346 * @return Product's gross price
348 public BigDecimal getProductGrossPrice () {
349 return this.productGrossPrice;
353 * Setter for product's gross price
355 * @param productGrossPrice Product's gross price
357 public void setProductGrossPrice (final BigDecimal productGrossPrice) {
358 this.productGrossPrice = productGrossPrice;
362 * Getter for product unit's i18n key
364 * @return Product's i18n key
366 public String getProductI18nKey () {
367 return this.productI18nKey;
371 * Setter for product unit's i18n key
373 * @param productI18nKey Product's i18n key
375 public void setProductI18nKey (final String productI18nKey) {
376 this.productI18nKey = productI18nKey;
380 * Getter for product id
384 public Long getProductId () {
385 return this.productId;
389 * Setter for product id
391 * @param productId Product id
393 public void setProductId (final Long productId) {
394 this.productId = productId;
398 * Getter for product's manufacturing/producing company
400 * @return Product's manufacturing/producing company
402 public BasicData getProductManufacturer () {
403 return this.productManufacturer;
407 * Setter for product's manufacturing/producing company
409 * @param productManufacturer Product's manufacturing/producing company
411 public void setProductManufacturer (final BasicData productManufacturer) {
412 this.productManufacturer = productManufacturer;
416 * Getter for product's net price
418 * @return Product's net price
420 public BigDecimal getProductNetPrice () {
421 return this.productNetPrice;
425 * Setter for product's net price
427 * @param productNetPrice Product's net price
429 public void setProductNetPrice (final BigDecimal productNetPrice) {
430 this.productNetPrice = productNetPrice;
434 * Getter for product's number
436 * @return Product's number
438 public Long getProductNumber () {
439 return this.productNumber;
443 * Setter for product's number
445 * @param productNumber Product's number
447 public void setProductNumber (final Long productNumber) {
448 this.productNumber = productNumber;
452 * Getter for product's size
454 * @return Product's size
456 public String getProductSize () {
457 return this.productSize;
461 * Setter for product's size
463 * @param productSize Product's size
465 public void setProductSize (final String productSize) {
466 this.productSize = productSize;
470 * Getter for product's tax rate
472 * @return Product's tax rate
474 public BigDecimal getProductTaxRate () {
475 return this.productTaxRate;
479 * Setter for product's tax rate
481 * @param productTaxRate Product's tax rate
483 public void setProductTaxRate (final BigDecimal productTaxRate) {
484 this.productTaxRate = productTaxRate;
488 * Getter for product's unit amount
490 * @return Product's unit amount
492 public BigDecimal getProductUnitAmount () {
493 return this.productUnitAmount;
497 * Setter for product's unit amount
499 * @param productUnitAmount Product's unit amount
501 public void setProductUnitAmount (final BigDecimal productUnitAmount) {
502 this.productUnitAmount = productUnitAmount;
506 * Getter for product unit's i18n key
508 * @return Product's i18n key
510 public String getProductUnitI18nKey () {
511 return this.productUnitI18nKey;
515 * Setter for product unit's i18n key
517 * @param productUnitI18nKey Product unit's i18n key
519 public void setProductUnitI18nKey (final String productUnitI18nKey) {
520 this.productUnitI18nKey = productUnitI18nKey;
524 * Updates given product data from request to database
526 * @throws FaceletException If something unexpected happened
528 public void updateProduct () throws FaceletException {
529 // Get new product instance
530 final Product newProduct = this.createProductInstance();
532 // Check if current product instance is in helper and valid
533 if (null == newProduct) {
535 throw new NullPointerException("newProduct is null"); //NOI18N
536 } else if (newProduct.getProductId() == null) {
538 throw new NullPointerException("newProduct.productId is null"); //NOI18N //NOI18N
539 } else if (newProduct.getProductId() < 1) {
541 throw new IllegalStateException(MessageFormat.format("newProduct.productId={0} is invalid", newProduct.getProductId())); //NOI18N
545 final Product updatedProduct;
548 // Call EJB for updating product data
549 updatedProduct = this.adminProductBean.updateProductData(newProduct);
550 } catch (final ProductNotFoundException ex) {
552 throw new FaceletException(ex);
556 this.updatedProductEvent.fire(new ProductUpdatedEvent(updatedProduct));
563 * Clears this bean (example: product has been added)
565 private void clear () {
567 this.setProductAgeGroup(null);
568 this.setProductAvailability(Boolean.FALSE);
569 this.setProductBarCodeNumber(null);
570 this.setProductCategory(null);
571 this.setProductGrossPrice(null);
572 this.setProductI18nKey(null);
573 this.setProductId(null);
574 this.setProductManufacturer(null);
575 this.setProductNetPrice(null);
576 this.setProductNumber(null);
577 this.setProductSize(null);
578 this.setProductTaxRate(null);
579 this.setProductUnitAmount(null);
580 this.setProductUnitI18nKey(null);
584 * Creates a new product instance with all fields
586 * @return Product instance
588 private Product createProductInstance () {
589 // Create new product instance
590 final Product newProduct = new GenericProduct(
591 this.getProductI18nKey(),
592 this.getProductGrossPrice(),
593 this.getProductCurrencyCode(),
594 this.getProductCategory(),
595 this.getProductAvailability(),
596 this.getProductUnitAmount(),
597 this.getProductUnitI18nKey()
600 // Set all optional fields
601 newProduct.setProductAgeGroup(this.getProductAgeGroup());
602 newProduct.setProductBarCodeNumber(this.getProductBarCodeNumber());
603 newProduct.setProductManufacturer(this.getProductManufacturer());
604 newProduct.setProductNumber(this.getProductNumber());
605 newProduct.setProductNetPrice(this.getProductNetPrice());
606 newProduct.setProductSize(this.getProductSize());
607 newProduct.setProductTaxRate(this.getProductTaxRate());
609 // Is product id set?
610 if (this.getProductId() instanceof Long) {
612 newProduct.setProductId(this.getProductId());