]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/category/PizzaAdminCategoryWebRequestBean.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / category / PizzaAdminCategoryWebRequestBean.java
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.pizzaapplication.beans.category;
18
19 import javax.enterprise.context.RequestScoped;
20 import javax.enterprise.event.Event;
21 import javax.enterprise.inject.Any;
22 import javax.faces.view.facelets.FaceletException;
23 import javax.inject.Inject;
24 import javax.inject.Named;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jproduct.events.category.AddedCategoryEvent;
29 import org.mxchange.jproduct.events.category.CategoryAddedEvent;
30 import org.mxchange.jproduct.exceptions.CannotAddCategoryException;
31 import org.mxchange.jproduct.exceptions.CategoryTitleAlreadyUsedException;
32 import org.mxchange.jproduct.model.category.Category;
33 import org.mxchange.jproduct.model.category.ProductCategory;
34 import org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote;
35 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
36
37 /**
38  * Main application class
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("adminCategoryController")
43 @RequestScoped
44 public class PizzaAdminCategoryWebRequestBean extends BasePizzaBean implements PizzaAdminCategoryWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 5_819_375_183_472_871L;
50
51         /**
52          * Event for added shop categories
53          */
54         @Inject
55         @Any
56         private Event<AddedCategoryEvent> categoryAddedEvent;
57
58         /**
59          * Remote bean for categories
60          */
61         private AdminCategorySessionBeanRemote categoryBean;
62
63         /////////////////////// Properties /////////////////////
64         /**
65          * Category categoryTitle
66          */
67         private String categoryTitle;
68
69         /**
70          * Parent category
71          */
72         private Category parentCategory;
73
74         /**
75          * Default constructor
76          */
77         public PizzaAdminCategoryWebRequestBean () {
78                 // Try it
79                 try {
80                         // Get initial context
81                         Context context = new InitialContext();
82
83                         // Try to lookup the bean
84                         this.categoryBean = (AdminCategorySessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminCategory!org.mxchange.jshopcore.model.category.AdminCategorySessionBeanRemote"); //NOI18N
85                 } catch (final NamingException e) {
86                         // Throw it again
87                         throw new FaceletException(e);
88                 }
89         }
90
91         @Override
92         public void addCategory () throws FaceletException {
93                 try {
94                         // Create category
95                         Category category = new ProductCategory();
96                         category.setParentCategory(this.getParentCategory());
97                         category.setCategoryTitle(this.getCategoryTitle());
98
99                         // Deligate to remote bean
100                         Category updatedCategory = this.categoryBean.doAdminAddCategory(category);
101
102                         // Fire event
103                         this.categoryAddedEvent.fire(new CategoryAddedEvent(updatedCategory));
104
105                         // Unset all older values
106                         this.clear();
107                 } catch (final CategoryTitleAlreadyUsedException | CannotAddCategoryException ex) {
108                         // Continue to throw
109                         throw new FaceletException(ex);
110                 }
111         }
112
113         @Override
114         public String getCategoryTitle () {
115                 return this.categoryTitle;
116         }
117
118         @Override
119         public void setCategoryTitle (final String categoryTitle) {
120                 this.categoryTitle = categoryTitle;
121         }
122
123         @Override
124         public Category getParentCategory () {
125                 return this.parentCategory;
126         }
127
128         @Override
129         public void setParentCategory (final Category parentCategory) {
130                 this.parentCategory = parentCategory;
131         }
132
133         /**
134          * Clears this bean (example: when category has been added)
135          */
136         private void clear () {
137                 this.setCategoryTitle(""); //NOI18N
138                 this.setParentCategory(null);
139         }
140
141 }