]> git.mxchange.org Git - jfinancials-war.git/blob
87d7c80ed0772bab31c446ecf8f6f8d907a0d7a4
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.product_category.action;
18
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import javax.ejb.EJB;
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.category.Categories;
39 import org.mxchange.jproduct.model.category.Category;
40 import org.mxchange.jproduct.model.category.ProductCategory;
41
42 /**
43  * Administrative action backing bean for product categories
44  * <p>
45  * @author Roland Häder<roland@mxchange.org>
46  */
47 @Named ("adminProductCategoryActionController")
48 @RequestScoped
49 public class FinancialAdminProductCategoryActionWebRequestBean extends BaseFinancialsBean implements FinancialAdminProductCategoryActionWebRequestController {
50
51         /**
52          * Serial number
53          */
54         private static final long serialVersionUID = 5_819_375_183_472_873L;
55
56         /**
57          * Event for added shop categories
58          */
59         @Inject
60         @Any
61         private Event<ObservableAdminAddedCategoryEvent> adminAddedCategoryEvent;
62
63         /**
64          * Remote bean for categories
65          */
66         @EJB (lookup = "java:global/jfinancials-ejb/adminCategory!org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote")
67         private AdminCategorySessionBeanRemote adminCategoryBean;
68
69         /**
70          * Event for added shop categories
71          */
72         @Inject
73         @Any
74         private Event<ObservableAdminUpdatedCategoryEvent> adminUpdatedCategoryEvent;
75
76         /**
77          * Category categoryI18nKey
78          */
79         private String categoryI18nKey;
80
81         /**
82          * Category's primary key
83          */
84         private Long categoryId;
85
86         /**
87          * Whether this category is shown in statistics
88          */
89         private Boolean categoryShownInStatistics;
90
91         /**
92          * Currently worked on category
93          */
94         private Category currentCategory;
95
96         /**
97          * Parent category
98          */
99         private Category parentCategory;
100
101         /**
102          * General category controller
103          */
104         @Inject
105         private FinancialsCategoryListWebViewController productCategoryListController;
106
107         /**
108          * Default constructor
109          */
110         public FinancialAdminProductCategoryActionWebRequestBean () {
111                 // Call super constructor
112                 super();
113         }
114
115         /**
116          * Adds given category data from request to database
117          */
118         public void addCategory () {
119                 // Is i18n key already used?
120                 if (this.productCategoryListController.isCategoryI18nKeyAdded(this.getCategoryI18nKey())) {
121                         // Throw exception
122                         throw new FacesException(MessageFormat.format("Category i18n key {0} is already used.", this.getCategoryI18nKey())); //NOI18N
123                 }
124
125                 // Create category
126                 final Category category = this.createCategoryInstance();
127
128                 // Declare updated category instance
129                 final Category updatedCategory;
130
131                 try {
132                         // Deligate to remote bean
133                         updatedCategory = this.adminCategoryBean.addProductCategory(category);
134                 } catch (final CategoryAlreadyAddedException ex) {
135                         // Continue to throw
136                         throw new FacesException(ex);
137                 }
138
139                 // Fire event
140                 this.adminAddedCategoryEvent.fire(new AdminAddedCategoryEvent(updatedCategory));
141         }
142
143         /**
144          * Copies all properties from current category instance to backing bean
145          * properties.
146          */
147         public void copyAllCategoryProperties () {
148                 // Check if current category is set
149                 if (this.getCurrentCategory() == null) {
150                         // Throw NPE
151                         throw new NullPointerException("this.currentCategory is null"); //NOI18N
152                 } else if (this.getCurrentCategory().getCategoryId() == null) {
153                         // Throw NPE again
154                         throw new NullPointerException("this.currentCategory.categoryId is null"); //NOI18N
155                 } else if (this.getCurrentCategory().getCategoryId() < 1) {
156                         // Throw IAE
157                         throw new IllegalArgumentException(MessageFormat.format("this.currentCategory.categoryId={0} is invalid", this.getCurrentCategory().getCategoryId())); //NOI18N
158                 }
159
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());
165         }
166
167         /**
168          * Getter for category i18n key
169          * <p>
170          * @return Category i18n key
171          */
172         public String getCategoryI18nKey () {
173                 return this.categoryI18nKey;
174         }
175
176         /**
177          * Setter for category i18n key
178          * <p>
179          * @param categoryI18nKey Category i18n key
180          */
181         public void setCategoryI18nKey (final String categoryI18nKey) {
182                 this.categoryI18nKey = categoryI18nKey;
183         }
184
185         /**
186          * Getter for category's primary key
187          * <p>
188          * @return Category's primary key
189          */
190         public Long getCategoryId () {
191                 return this.categoryId;
192         }
193
194         /**
195          * Setter for category's primary key
196          * <p>
197          * @param categoryId Current category
198          */
199         public void setCategoryId (final Long categoryId) {
200                 this.categoryId = categoryId;
201         }
202
203         /**
204          * Getter for whether category is shown in statistics
205          * <p>
206          * @return Whether category is shown in statistics
207          */
208         public Boolean getCategoryShownInStatistics () {
209                 return this.categoryShownInStatistics;
210         }
211
212         /**
213          * Setter for whether category is shown in statistics
214          * <p>
215          * @param categoryShownInStatistics Whether category is shown in statistics
216          */
217         public void setCategoryShownInStatistics (final Boolean categoryShownInStatistics) {
218                 this.categoryShownInStatistics = categoryShownInStatistics;
219         }
220
221         /**
222          * Getter for current category
223          * <p>
224          * @return Current category
225          */
226         public Category getCurrentCategory () {
227                 return this.currentCategory;
228         }
229
230         /**
231          * Setter for current category
232          * <p>
233          * @param currentCategory Current category
234          */
235         public void setCurrentCategory (final Category currentCategory) {
236                 this.currentCategory = currentCategory;
237         }
238
239         /**
240          * Getter for parent id
241          * <p>
242          * @return Parent id
243          */
244         public Category getParentCategory () {
245                 return this.parentCategory;
246         }
247
248         /**
249          * Setter for parent category
250          * <p>
251          * @param parentCategory Parent category to set
252          */
253         public void setParentCategory (final Category parentCategory) {
254                 this.parentCategory = parentCategory;
255         }
256
257         /**
258          * Updates given category data from request to database
259          * <p>
260          * @return Redirection outcome
261          */
262         public String updateCategory () {
263                 // Check if current category is set
264                 if (this.getCurrentCategory() == null) {
265                         // Throw NPE
266                         throw new NullPointerException("this.currentCategory is null"); //NOI18N
267                 } else if (this.getCurrentCategory().getCategoryId() == null) {
268                         // Throw NPE again
269                         throw new NullPointerException("this.currentCategory.categoryId is null"); //NOI18N
270                 } else if (this.getCurrentCategory().getCategoryId() < 1) {
271                         // Throw IAE
272                         throw new IllegalArgumentException(MessageFormat.format("this.currentCategory.categoryId={0} is invalid.", this.getCurrentCategory().getCategoryId())); //NOI18N
273                 }
274
275                 // Create category
276                 final Category category = this.createCategoryInstance();
277
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
282                         return ""; //NOI18N
283                 }
284
285                 // Copy all fields from it to current
286                 Categories.copyCategoryData(category, this.getCurrentCategory());
287
288                 // Declare updated category instance
289                 final Category updatedCategory;
290
291                 try {
292                         // Deligate to remote bean
293                         updatedCategory = this.adminCategoryBean.updateProductCategory(this.getCurrentCategory());
294                 } catch (final CategoryNotFoundException ex) {
295                         // Continue to throw
296                         throw new FacesException(ex);
297                 }
298
299                 // Fire event
300                 this.adminUpdatedCategoryEvent.fire(new AdminUpdatedCategoryEvent(updatedCategory));
301
302                 // Return outcome to admin-list
303                 return "admin_list_product_categories"; //NOI18N
304         }
305
306         /**
307          * Creates a category instance with all fields (except primary key)
308          * <p>
309          * @return Category instance
310          */
311         private Category createCategoryInstance () {
312                 // Create category
313                 final Category category = new ProductCategory(
314                                            this.getCategoryI18nKey(),
315                                            this.getCategoryShownInStatistics()
316                            );
317
318                 // Set all optional fields
319                 category.setCategoryId(this.getCategoryId());
320                 category.setParentCategory(this.getParentCategory());
321
322                 // Return it
323                 return category;
324         }
325
326 }