2 * Copyright (C) 2016 - 2018 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.updated.ObservableProductUpdatedEvent;
32 import org.mxchange.jproduct.events.product.updated.ProductUpdatedEvent;
33 import org.mxchange.jproduct.exceptions.product.ProductNotFoundException;
34 import org.mxchange.jproduct.model.category.Category;
35 import org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote;
36 import org.mxchange.jproduct.model.product.GenericProduct;
37 import org.mxchange.jproduct.model.product.Product;
38 import org.mxchange.jproduct.model.product.agegroup.AgeGroup;
41 * Main application class
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("adminGenericProductActionController")
47 public class FinancialAdminGenericProductActionWebViewBean extends BaseFinancialsBean implements FinancialAdminProductActionWebViewController {
52 private static final long serialVersionUID = 5_819_375_183_472_872L;
55 * Remote bean for products
57 @EJB (lookup = "java:global/jfinancials-ejb/adminProduct!org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote")
58 private AdminProductSessionBeanRemote adminProductBean;
63 private Product product;
68 private AgeGroup productAgeGroup;
73 private Boolean productAvailability;
78 private String productBarCodeNumber;
83 private Category productCategory;
86 * Product's price currency code like EUR or USD
88 private String productCurrencyCode;
91 * Product's gross price
93 private BigDecimal productGrossPrice;
98 private String productI18nKey;
103 private Long productId;
106 * Product list controller
109 private FinancialsProductListWebViewController productListController;
112 * Product's manufacturing/producing company
114 private BasicData productManufacturer;
117 * Product's net price
119 private BigDecimal productNetPrice;
124 private Long productNumber;
127 * Product size (for shoes, clothings)
129 private String productSize;
134 private BigDecimal productTaxRate;
137 * Product's unit amount
139 private BigDecimal productUnitAmount;
144 private String productUnitI18nKey;
147 * Event for updated product
151 private Event<ObservableProductUpdatedEvent> updatedProductEvent;
154 * Default constructor
156 public FinancialAdminGenericProductActionWebViewBean () {
157 // Call super constructor
162 * Copies all product's properties back to this bean.
164 public void copyAllProductProperties () {
165 // Validate product instance
166 if (this.getProduct() == null) {
168 throw new NullPointerException("this.product is null"); //NOI18N
169 } else if (this.getProduct().getProductId() == null) {
171 throw new NullPointerException("this.product.productId is null"); //NOI18N
172 } else if (this.getProduct().getProductId() < 1) {
174 throw new IllegalStateException(MessageFormat.format("this.product.productId={0} is not valid.", this.getProduct().getProductId())); //NOI18N
177 // Now copy all fields
178 this.setProductAgeGroup(this.getProduct().getProductAgeGroup());
179 this.setProductAvailability(this.getProduct().getProductAvailability());
180 this.setProductBarCodeNumber(this.getProduct().getProductBarCodeNumber());
181 this.setProductCategory(this.getProduct().getProductCategory());
182 this.setProductCurrencyCode(this.getProduct().getProductCurrencyCode());
183 this.setProductGrossPrice(this.getProduct().getProductGrossPrice());
184 this.setProductI18nKey(this.getProduct().getProductI18nKey());
185 this.setProductId(this.getProduct().getProductId());
186 this.setProductManufacturer(this.getProduct().getProductManufacturer());
187 this.setProductNetPrice(this.getProduct().getProductNetPrice());
188 this.setProductNumber(this.getProduct().getProductNumber());
189 this.setProductSize(this.getProduct().getProductSize());
190 this.setProductTaxRate(this.getProduct().getProductTaxRate());
191 this.setProductUnitAmount(this.getProduct().getProductUnitAmount());
192 this.setProductUnitI18nKey(this.getProduct().getProductUnitI18nKey());
196 * Getter for product instance
198 * @return Product instance
200 public Product getProduct () {
205 * Setter for product instance
207 * @param product Product instance
209 public void setProduct (final Product product) {
210 this.product = product;
214 * Getter for product's age group
216 * @return Product's age group
218 public AgeGroup getProductAgeGroup () {
219 return this.productAgeGroup;
223 * Setter for product's age group
225 * @param productAgeGroup Product's age group
227 public void setProductAgeGroup (final AgeGroup productAgeGroup) {
228 this.productAgeGroup = productAgeGroup;
232 * Getter for product's available
234 * @return Product's available
236 public Boolean getProductAvailability () {
237 return this.productAvailability;
241 * Setter for product's available
243 * @param productAvailability Product's available
245 public void setProductAvailability (final Boolean productAvailability) {
246 this.productAvailability = productAvailability;
250 * Getter for product's bar-code number
252 * @return Product's bar-code number
254 public String getProductBarCodeNumber () {
255 return this.productBarCodeNumber;
259 * Setter for product's bar-code number
261 * @param productBarCodeNumber Product's bar-code number
263 public void setProductBarCodeNumber (final String productBarCodeNumber) {
264 this.productBarCodeNumber = productBarCodeNumber;
268 * Getter for product's category
270 * @return Product's category
272 public Category getProductCategory () {
273 return this.productCategory;
277 * Setter for product's category instance
279 * @param productCategory Product's category instance
281 public void setProductCategory (final Category productCategory) {
282 this.productCategory = productCategory;
286 * Getter for product's price currency code
288 * @return Product's price currency code
290 public String getProductCurrencyCode () {
291 return this.productCurrencyCode;
295 * Setter for product's price currency code
297 * @param productCurrencyCode Product's price currency code
299 public void setProductCurrencyCode (final String productCurrencyCode) {
300 this.productCurrencyCode = productCurrencyCode;
304 * Getter for product's gross price
306 * @return Product's gross price
308 public BigDecimal getProductGrossPrice () {
309 return this.productGrossPrice;
313 * Setter for product's gross price
315 * @param productGrossPrice Product's gross price
317 public void setProductGrossPrice (final BigDecimal productGrossPrice) {
318 this.productGrossPrice = productGrossPrice;
322 * Getter for product unit's i18n key
324 * @return Product's i18n key
326 public String getProductI18nKey () {
327 return this.productI18nKey;
331 * Setter for product unit's i18n key
333 * @param productI18nKey Product's i18n key
335 public void setProductI18nKey (final String productI18nKey) {
336 this.productI18nKey = productI18nKey;
340 * Getter for product id
344 public Long getProductId () {
345 return this.productId;
349 * Setter for product id
351 * @param productId Product id
353 public void setProductId (final Long productId) {
354 this.productId = productId;
358 * Getter for product's manufacturing/producing company
360 * @return Product's manufacturing/producing company
362 public BasicData getProductManufacturer () {
363 return this.productManufacturer;
367 * Setter for product's manufacturing/producing company
369 * @param productManufacturer Product's manufacturing/producing company
371 public void setProductManufacturer (final BasicData productManufacturer) {
372 this.productManufacturer = productManufacturer;
376 * Getter for product's net price
378 * @return Product's net price
380 public BigDecimal getProductNetPrice () {
381 return this.productNetPrice;
385 * Setter for product's net price
387 * @param productNetPrice Product's net price
389 public void setProductNetPrice (final BigDecimal productNetPrice) {
390 this.productNetPrice = productNetPrice;
394 * Getter for product's number
396 * @return Product's number
398 public Long getProductNumber () {
399 return this.productNumber;
403 * Setter for product's number
405 * @param productNumber Product's number
407 public void setProductNumber (final Long productNumber) {
408 this.productNumber = productNumber;
412 * Getter for product's size
414 * @return Product's size
416 public String getProductSize () {
417 return this.productSize;
421 * Setter for product's size
423 * @param productSize Product's size
425 public void setProductSize (final String productSize) {
426 this.productSize = productSize;
430 * Getter for product's tax rate
432 * @return Product's tax rate
434 public BigDecimal getProductTaxRate () {
435 return this.productTaxRate;
439 * Setter for product's tax rate
441 * @param productTaxRate Product's tax rate
443 public void setProductTaxRate (final BigDecimal productTaxRate) {
444 this.productTaxRate = productTaxRate;
448 * Getter for product's unit amount
450 * @return Product's unit amount
452 public BigDecimal getProductUnitAmount () {
453 return this.productUnitAmount;
457 * Setter for product's unit amount
459 * @param productUnitAmount Product's unit amount
461 public void setProductUnitAmount (final BigDecimal productUnitAmount) {
462 this.productUnitAmount = productUnitAmount;
466 * Getter for product unit's i18n key
468 * @return Product's i18n key
470 public String getProductUnitI18nKey () {
471 return this.productUnitI18nKey;
475 * Setter for product unit's i18n key
477 * @param productUnitI18nKey Product unit's i18n key
479 public void setProductUnitI18nKey (final String productUnitI18nKey) {
480 this.productUnitI18nKey = productUnitI18nKey;
484 * Updates given product data from request to database
486 * @throws FaceletException If something unexpected happened
488 public void updateProduct () throws FaceletException {
489 // Get product instance
490 final Product newProduct = this.createProductInstance();
491 // Check if product instance is in helper and valid
493 if (null == newProduct) {
495 throw new NullPointerException("newProduct is null"); //NOI18N
496 } else if (newProduct.getProductId() == null) {
498 throw new NullPointerException("newProduct.productId is null"); //NOI18N //NOI18N
499 } else if (newProduct.getProductId() < 1) {
501 throw new IllegalStateException(MessageFormat.format("newProduct.productId={0} is invalid", newProduct.getProductId())); //NOI18N
505 final Product updatedProduct;
508 // Call EJB for updating product data
509 updatedProduct = this.adminProductBean.updateProductData(newProduct);
510 } catch (final ProductNotFoundException ex) {
512 throw new FaceletException(ex);
516 this.updatedProductEvent.fire(new ProductUpdatedEvent(updatedProduct));
523 * Clears this bean (example: product has been added)
525 private void clear () {
527 this.setProductAgeGroup(null);
528 this.setProductAvailability(Boolean.FALSE);
529 this.setProductBarCodeNumber(null);
530 this.setProductCategory(null);
531 this.setProductGrossPrice(null);
532 this.setProductI18nKey(null);
533 this.setProductId(null);
534 this.setProductManufacturer(null);
535 this.setProductNetPrice(null);
536 this.setProductNumber(null);
537 this.setProductSize(null);
538 this.setProductTaxRate(null);
539 this.setProductUnitAmount(null);
540 this.setProductUnitI18nKey(null);
544 * Creates a product instance with all fields
546 * @return Product instance
548 private Product createProductInstance () {
549 // Create product instance
550 final Product newProduct = new GenericProduct(this.getProductI18nKey(), this.getProductGrossPrice(), this.getProductCurrencyCode(), this.getProductCategory(), this.getProductAvailability(), this.getProductUnitAmount(), this.getProductUnitI18nKey());
552 // Set all optional fields
553 newProduct.setProductAgeGroup(this.getProductAgeGroup());
554 newProduct.setProductBarCodeNumber(this.getProductBarCodeNumber());
555 newProduct.setProductManufacturer(this.getProductManufacturer());
556 newProduct.setProductNumber(this.getProductNumber());
557 newProduct.setProductNetPrice(this.getProductNetPrice());
558 newProduct.setProductSize(this.getProductSize());
559 newProduct.setProductTaxRate(this.getProductTaxRate());
561 // Is product id set?
562 if (this.getProductId() instanceof Long) {
564 newProduct.setProductId(this.getProductId());