2 * Copyright (C) 2016 - 2022 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.product_category.action;
19 import java.text.MessageFormat;
20 import java.util.Objects;
22 import javax.enterprise.context.RequestScoped;
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.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
30 import org.mxchange.jfinancials.beans.product_category.list.FinancialsCategoryListWebViewController;
31 import org.mxchange.jproduct.events.category.added.AdminAddedCategoryEvent;
32 import org.mxchange.jproduct.events.category.added.ObservableAdminAddedCategoryEvent;
33 import org.mxchange.jproduct.events.category.updated.AdminUpdatedCategoryEvent;
34 import org.mxchange.jproduct.events.category.updated.ObservableAdminUpdatedCategoryEvent;
35 import org.mxchange.jproduct.exceptions.category.CategoryAlreadyAddedException;
36 import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException;
37 import org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote;
38 import org.mxchange.jproduct.model.utils.CategoryUtils;
39 import org.mxchange.jproduct.model.category.Category;
40 import org.mxchange.jproduct.model.category.ProductCategory;
43 * Administrative action backing bean for product categories
45 * @author Roland Häder<roland@mxchange.org>
47 @Named ("adminProductCategoryActionController")
49 public class FinancialAdminProductCategoryActionWebRequestBean extends BaseFinancialsBean implements FinancialAdminProductCategoryActionWebRequestController {
54 private static final long serialVersionUID = 5_819_375_183_472_873L;
57 * Event for added shop categories
61 private Event<ObservableAdminAddedCategoryEvent> adminAddedCategoryEvent;
64 * Remote bean for categories
66 @EJB (lookup = "java:global/jfinancials-ejb/adminCategory!org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote")
67 private AdminCategorySessionBeanRemote adminCategoryBean;
70 * Event for added shop categories
74 private Event<ObservableAdminUpdatedCategoryEvent> adminUpdatedCategoryEvent;
77 * Category categoryI18nKey
79 private String categoryI18nKey;
82 * Category's primary key
84 private Long categoryId;
87 * Whether this category is shown in statistics
89 private Boolean categoryShownInStatistics;
92 * Currently worked on category
94 private Category currentCategory;
99 private Category parentCategory;
102 * General category controller
105 private FinancialsCategoryListWebViewController productCategoryListController;
108 * Default constructor
110 public FinancialAdminProductCategoryActionWebRequestBean () {
111 // Call super constructor
116 * Adds given category data from request to database
118 public void addProductCategory () {
119 // Is i18n key already used?
120 if (this.productCategoryListController.isCategoryI18nKeyAdded(this.getCategoryI18nKey())) {
122 throw new FacesException(MessageFormat.format("Category i18n key {0} is already used.", this.getCategoryI18nKey())); //NOI18N
126 final Category category = this.createCategoryInstance();
128 // Declare updated category instance
129 final Category updatedCategory;
132 // Deligate to remote bean
133 updatedCategory = this.adminCategoryBean.addProductCategory(category);
134 } catch (final CategoryAlreadyAddedException ex) {
136 throw new FacesException(ex);
140 this.adminAddedCategoryEvent.fire(new AdminAddedCategoryEvent(updatedCategory));
144 * Copies all properties from current category instance to backing bean
147 public void copyAllCategoryProperties () {
148 // Check if current category is set
149 if (this.getCurrentCategory() == null) {
151 throw new NullPointerException("this.currentCategory is null"); //NOI18N
152 } else if (this.getCurrentCategory().getCategoryId() == null) {
154 throw new NullPointerException("this.currentCategory.categoryId is null"); //NOI18N
155 } else if (this.getCurrentCategory().getCategoryId() < 1) {
157 throw new IllegalArgumentException(MessageFormat.format("this.currentCategory.categoryId={0} is invalid", this.getCurrentCategory().getCategoryId())); //NOI18N
160 // Copy all fields, except timestamps
161 this.setCategoryI18nKey(this.getCurrentCategory().getCategoryI18nKey());
162 this.setCategoryId(this.getCurrentCategory().getCategoryId());
163 this.setCategoryShownInStatistics(this.getCurrentCategory().getCategoryShownInStatistics());
164 this.setParentCategory(this.getCurrentCategory().getParentCategory());
168 * Getter for category i18n key
170 * @return Category i18n key
172 public String getCategoryI18nKey () {
173 return this.categoryI18nKey;
177 * Setter for category i18n key
179 * @param categoryI18nKey Category i18n key
181 public void setCategoryI18nKey (final String categoryI18nKey) {
182 this.categoryI18nKey = categoryI18nKey;
186 * Getter for category's primary key
188 * @return Category's primary key
190 public Long getCategoryId () {
191 return this.categoryId;
195 * Setter for category's primary key
197 * @param categoryId Current category
199 public void setCategoryId (final Long categoryId) {
200 this.categoryId = categoryId;
204 * Getter for whether category is shown in statistics
206 * @return Whether category is shown in statistics
208 public Boolean getCategoryShownInStatistics () {
209 return this.categoryShownInStatistics;
213 * Setter for whether category is shown in statistics
215 * @param categoryShownInStatistics Whether category is shown in statistics
217 public void setCategoryShownInStatistics (final Boolean categoryShownInStatistics) {
218 this.categoryShownInStatistics = categoryShownInStatistics;
222 * Getter for current category
224 * @return Current category
226 public Category getCurrentCategory () {
227 return this.currentCategory;
231 * Setter for current category
233 * @param currentCategory Current category
235 public void setCurrentCategory (final Category currentCategory) {
236 this.currentCategory = currentCategory;
240 * Getter for parent id
244 public Category getParentCategory () {
245 return this.parentCategory;
249 * Setter for parent category
251 * @param parentCategory Parent category to set
253 public void setParentCategory (final Category parentCategory) {
254 this.parentCategory = parentCategory;
258 * Updates given product category data from request to database.
260 * @return Redirection outcome
262 public String updateProductCategory () {
263 // Check if current category is set
264 if (this.getCurrentCategory() == null) {
266 throw new NullPointerException("this.currentCategory is null"); //NOI18N
267 } else if (this.getCurrentCategory().getCategoryId() == null) {
269 throw new NullPointerException("this.currentCategory.categoryId is null"); //NOI18N
270 } else if (this.getCurrentCategory().getCategoryId() < 1) {
272 throw new IllegalArgumentException(MessageFormat.format("this.currentCategory.categoryId={0} is invalid.", this.getCurrentCategory().getCategoryId())); //NOI18N
276 final Category category = this.createCategoryInstance();
278 // Has the product changed?
279 if (Objects.equals(category, this.getCurrentCategory())) {
280 // Is the same product data, output message
281 this.showFacesMessage("admin-form-edit-product-category:categoryI18nKey", "ADMIN_PRODUCT_CATEGORY_NOT_UPDATED", FacesMessage.SEVERITY_WARN); //NOI18N
285 // Copy all fields from it to current
286 CategoryUtils.copyCategoryData(category, this.getCurrentCategory());
288 // Declare updated category instance
289 final Category updatedCategory;
292 // Deligate to remote bean
293 updatedCategory = this.adminCategoryBean.updateProductCategory(this.getCurrentCategory());
294 } catch (final CategoryNotFoundException ex) {
296 throw new FacesException(ex);
300 this.adminUpdatedCategoryEvent.fire(new AdminUpdatedCategoryEvent(updatedCategory));
302 // Return outcome to admin-list
303 return "admin_list_product_categories"; //NOI18N
307 * Creates a category instance with all fields (except primary key)
309 * @return Category instance
311 private Category createCategoryInstance () {
313 final Category category = new ProductCategory(
314 this.getCategoryI18nKey(),
315 this.getCategoryShownInStatistics()
318 // Set all optional fields
319 category.setCategoryId(this.getCategoryId());
320 category.setParentCategory(this.getParentCategory());