]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/category/PizzaCategoryWebApplicationBean.java
Rewrites:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / category / PizzaCategoryWebApplicationBean.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 java.text.MessageFormat;
20 import java.util.LinkedList;
21 import java.util.List;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.ApplicationScoped;
24 import javax.enterprise.event.Observes;
25 import javax.faces.FacesException;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jshopcore.events.category.AddedCategoryEvent;
32 import org.mxchange.jshopcore.model.category.Category;
33 import org.mxchange.jshopcore.model.category.CategorySessionBeanRemote;
34
35 /**
36  * General (product) category controller
37  * <p>
38  * @author Roland Haeder<roland@mxchange.org>
39  */
40 @Named ("categoryController")
41 @ApplicationScoped
42 public class PizzaCategoryWebApplicationBean implements PizzaCategoryWebApplicationController {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 58_137_539_530_279L;
48
49         /**
50          * All categories
51          */
52         private List<Category> categories;
53
54         @Override
55         public void addCategory (final Category category) {
56                 // Add the category
57                 this.categories.add(category);
58         }
59
60         @Override
61         public void afterShopCategoryAdded (@Observes final AddedCategoryEvent event) {
62                 // Is all valid?
63                 if (null == event) {
64                         // Throw NPE
65                         throw new NullPointerException("event is null"); //NOI18N
66                 } else if (event.getAddedCategory() == null) {
67                         // Throw again ...
68                         throw new NullPointerException("event.addedCategory is null"); //NOI18N
69                 } else if (event.getAddedCategory().getCategoryId() == null) {
70                         // And again ...
71                         throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N
72                 } else if (event.getAddedCategory().getCategoryId() < 1) {
73                         // Id is invalid
74                         throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N
75                 }
76
77                 // Add it here, too.
78                 this.addCategory(event.getAddedCategory());
79         }
80
81         @Override
82         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
83         public List<Category> getAllCategories () throws FacesException {
84                 // Return it
85                 return this.categories;
86         }
87
88         @Override
89         public List<Category> getAllCategoriesParent () throws FaceletException {
90                 // Get regular list
91                 List<Category> list = new LinkedList<>(this.getAllCategories());
92
93                 // Return it
94                 return list;
95         }
96
97         /**
98          * Initialization of this bean
99          */
100         @PostConstruct
101         public void init () {
102                 try {
103                         // Get initial context
104                         Context context = new InitialContext();
105
106                         // Try to lookup the bean
107                         CategorySessionBeanRemote categoryBean = (CategorySessionBeanRemote) context.lookup("java:global/jshop-ejb/category!org.mxchange.jshopcore.model.category.CategorySessionBeanRemote"); //NOI18N
108
109                         // Get all categories
110                         this.categories = categoryBean.getAllCategories();
111                 } catch (final NamingException e) {
112                         // Continued to throw
113                         throw new FacesException(e);
114                 }
115         }
116
117 }