]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/category/PizzaAdminCategoryWebRequestBean.java
35df9066701ca13d449f7c5c13220e1ff477bb1a
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / category / PizzaAdminCategoryWebRequestBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.jshopcore.events.category.AddedCategoryEvent;
29 import org.mxchange.jshopcore.events.category.ShopCategoryAddedEvent;
30 import org.mxchange.jshopcore.exceptions.CannotAddCategoryException;
31 import org.mxchange.jshopcore.exceptions.CategoryTitleAlreadyUsedException;
32 import org.mxchange.jshopcore.model.category.AdminCategorySessionBeanRemote;
33 import org.mxchange.jshopcore.model.category.Category;
34 import org.mxchange.jshopcore.model.category.ProductCategory;
35
36 /**
37  * Main application class
38  * <p>
39  * @author Roland Haeder<roland@mxchange.org>
40  */
41 @Named ("adminCategoryController")
42 @RequestScoped
43 public class PizzaAdminCategoryWebRequestBean implements PizzaAdminCategoryWebRequestController {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 5_819_375_183_472_871L;
49
50         /**
51          * Event for added shop categories
52          */
53         @Inject
54         @Any
55         private Event<AddedCategoryEvent> categoryAddedEvent;
56
57         /**
58          * Remote bean for categories
59          */
60         private AdminCategorySessionBeanRemote categoryBean;
61
62         /////////////////////// Properties /////////////////////
63         /**
64          * Category categoryTitle
65          */
66         private String categoryTitle;
67
68         /**
69          * Parent category
70          */
71         private Category parentCategory;
72
73         /**
74          * Default constructor
75          */
76         public PizzaAdminCategoryWebRequestBean () {
77                 // Try it
78                 try {
79                         // Get initial context
80                         Context context = new InitialContext();
81
82                         // Try to lookup the bean
83                         this.categoryBean = (AdminCategorySessionBeanRemote) context.lookup("java:global/jshop-ejb/admin_category!org.mxchange.jshopcore.model.category.AdminCategorySessionBeanRemote"); //NOI18N
84                 } catch (final NamingException e) {
85                         // Throw it again
86                         throw new FaceletException(e);
87                 }
88         }
89
90         @Override
91         public void addCategory () throws FaceletException {
92                 try {
93                         // Create category
94                         Category category = new ProductCategory();
95                         category.setParentCategory(this.getParentCategory());
96                         category.setCategoryTitle(this.getCategoryTitle());
97
98                         // Deligate to remote bean
99                         Category updatedCategory = this.categoryBean.doAdminAddCategory(category);
100
101                         // Fire event
102                         this.categoryAddedEvent.fire(new ShopCategoryAddedEvent(updatedCategory));
103
104                         // Unset all older values
105                         this.setCategoryTitle(""); //NOI18N
106                         this.setParentCategory(null);
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 }