]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/category/AdminCategoryWebBean.java
changed ZIP code to Integer (enougth) + updated jar
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / category / AdminCategoryWebBean.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU 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.faces.view.facelets.FaceletException;
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import org.mxchange.jshopcore.exceptions.CannotAddCategoryException;
27 import org.mxchange.jshopcore.exceptions.CategoryTitleAlreadyUsedException;
28 import org.mxchange.jshopcore.model.category.AdminCategorySessionBeanRemote;
29 import org.mxchange.jshopcore.model.category.Category;
30 import org.mxchange.jshopcore.model.category.ProductCategory;
31 import org.mxchange.pizzaapplication.beans.shop.ShopWebController;
32
33 /**
34  * Main application class
35  * <p>
36  * @author Roland Haeder<roland@mxchange.org>
37  */
38 @Named ("admin_category")
39 @RequestScoped
40 public class AdminCategoryWebBean implements AdminCategoryWebController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 5_819_375_183_472_871L;
46
47         /**
48          * Remote bean for categories
49          */
50         private final AdminCategorySessionBeanRemote categoryBean;
51
52         ////////////////////// Bean injections ///////////////////////
53         /**
54          * Shop bean
55          */
56         @Inject
57         private ShopWebController shopController;
58
59         /////////////////////// Properties /////////////////////
60         /**
61          * Category categoryTitle
62          */
63         private String categoryTitle;
64
65         /**
66          * Parent category
67          */
68         private Category parentCategory;
69
70         /**
71          * Default constructor
72          */
73         public AdminCategoryWebBean () {
74                 // Try it
75                 try {
76                         // Get initial context
77                         Context context = new InitialContext();
78
79                         // Try to lookup the bean
80                         this.categoryBean = (AdminCategorySessionBeanRemote) context.lookup("ejb/stateless-admin-category"); //NOI18N
81                 } catch (final NamingException e) {
82                         // Throw it again
83                         throw new FaceletException(e);
84                 }
85         }
86
87         @Override
88         public void addCategory () throws FaceletException {
89                 try {
90                         // Create category
91                         Category category = new ProductCategory();
92                         category.setParentCategory(this.getParentCategory());
93                         category.setCategoryTitle(this.getCategoryTitle());
94
95                         // Deligate to remote bean
96                         Category updatedCategory = this.categoryBean.doAdminAddCategory(category);
97
98                         // Also send it to the controller bean
99                         this.shopController.addCategory(updatedCategory);
100
101                         // Unset all older values
102                         this.setCategoryTitle(""); //NOI18N
103                         this.setParentCategory(null);
104                 } catch (final CategoryTitleAlreadyUsedException | CannotAddCategoryException ex) {
105                         // Continue to throw
106                         throw new FaceletException(ex);
107                 }
108         }
109
110         @Override
111         public String getCategoryTitle () {
112                 return this.categoryTitle;
113         }
114
115         @Override
116         public void setCategoryTitle (final String categoryTitle) {
117                 this.categoryTitle = categoryTitle;
118         }
119
120         @Override
121         public Category getParentCategory () {
122                 return this.parentCategory;
123         }
124
125         @Override
126         public void setParentCategory (final Category parentCategory) {
127                 this.parentCategory = parentCategory;
128         }
129 }