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