2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.category;
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;
37 * General (product) category controller
39 * @author Roland Häder<roland@mxchange.org>
41 @Named ("categoryController")
43 public class PizzaCategoryWebRequestBean extends BasePizzaBean implements PizzaCategoryWebRequestController {
48 private static final long serialVersionUID = 58_137_539_530_279L;
53 private List<Category> categories;
56 public void afterShopCategoryAdded (@Observes final AddedCategoryEvent event) {
60 throw new NullPointerException("event is null"); //NOI18N
61 } else if (event.getAddedCategory() == null) {
63 throw new NullPointerException("event.addedCategory is null"); //NOI18N
64 } else if (event.getAddedCategory().getCategoryId() == null) {
66 throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N
67 } else if (event.getAddedCategory().getCategoryId() < 1) {
69 throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N
73 this.categories.add(event.getAddedCategory());
77 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
78 public List<Category> getAllCategories () throws FacesException {
80 return this.categories;
84 public List<Category> getAllCategoriesParent () throws FaceletException {
86 List<Category> list = new LinkedList<>(this.getAllCategories());
93 * Initialization of this bean
98 // Get initial context
99 Context context = new InitialContext();
101 // Try to lookup the bean
102 CategorySessionBeanRemote categoryBean = (CategorySessionBeanRemote) context.lookup("java:global/jshop-ejb/category!org.mxchange.jshopcore.model.category.CategorySessionBeanRemote"); //NOI18N
104 // Get all categories
105 this.categories = categoryBean.allCategories();
106 } catch (final NamingException e) {
107 // Continued to throw
108 throw new FacesException(e);