2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.shop;
19 import java.util.Collections;
20 import java.util.LinkedList;
21 import java.util.List;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.ApplicationScoped;
24 import javax.faces.FacesException;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jshopcore.model.category.Category;
31 import org.mxchange.jshopcore.model.category.CategorySessionBeanRemote;
32 import org.mxchange.jshopcore.model.product.Product;
33 import org.mxchange.jshopcore.model.product.ProductSessionBeanRemote;
34 import org.mxchange.pizzaapplication.beans.AbstractWebBean;
37 * General shop controller
39 * @author Roland Haeder<roland@mxchange.org>
43 public class ShopWebBean extends AbstractWebBean implements ShopWebController {
48 private static final long serialVersionUID = 58_137_539_530_279L;
51 * "Cache" for all available products
53 private List<Product> availableProducts;
58 private List<Category> categories;
61 public void addCategory (final Category category) {
63 this.categories.add(category);
67 public void addProduct (final Product product) {
68 // Is the product available?
69 if (product.getProductAvailability()) {
71 this.availableProducts.add(product);
76 public List<Category> getAllCategories () throws FacesException {
78 // TODO Find something better here to prevent warning
79 return Collections.unmodifiableList(this.categories);
83 public List<Category> getAllCategoriesParent () throws FaceletException {
85 List<Category> list = new LinkedList<>(this.getAllCategories());
92 public List<Product> getAvailableProducts () throws FacesException {
94 // TODO Find something better here to prevent warning
95 return Collections.unmodifiableList(this.availableProducts);
101 // Get initial context
102 Context context = new InitialContext();
104 // Try to lookup the bean
105 CategorySessionBeanRemote categoryBean = (CategorySessionBeanRemote) context.lookup("ejb/stateless-category"); //NOI18N
107 // Get all categories
108 this.categories = categoryBean.getAllCategories();
110 // Try to lookup the bean
111 ProductSessionBeanRemote productBean = (ProductSessionBeanRemote) context.lookup("ejb/stateless-product"); //NOI18N
113 // Get available products list
114 this.availableProducts = productBean.getAvailableProducts();
115 } catch (final NamingException e) {
116 // Continued to throw
117 throw new FacesException(e);