]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/controller/PizzaServiceWebBean.java
cbaf01b30486bc886b594fab70e3223ba873443e
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / controller / PizzaServiceWebBean.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.controller;
18
19 import java.rmi.RemoteException;
20 import java.util.Deque;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.SessionScoped;
23 import javax.faces.FacesException;
24 import javax.inject.Named;
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import org.mxchange.jcoreee.beans.BaseFrameworkBean;
28 import org.mxchange.jshopcore.model.category.Category;
29 import org.mxchange.jshopcore.model.category.CategorySessionBeanRemote;
30 import org.mxchange.jshopcore.model.category.ProductCategory;
31 import org.mxchange.jshopcore.model.product.Product;
32 import org.mxchange.jshopcore.model.product.ProductSessionBeanRemote;
33
34 /**
35  * Main application class
36  *
37  * @author Roland Haeder
38  */
39 @Named("controller")
40 @SessionScoped
41 public class PizzaServiceWebBean extends BaseFrameworkBean implements PizzaWebBean {
42         /**
43          * Serial id
44          */
45         private static final long serialVersionUID = 58_137_539_530_279L;
46
47         /**
48          * Remote bean for categories
49          */
50         private final CategorySessionBeanRemote category;
51
52         /**
53          * Remote bean for products
54          */
55         private final ProductSessionBeanRemote product;
56
57         /**
58          * Default constructor
59          * 
60          * @throws javax.naming.NamingException Something happened here?
61          */
62         public PizzaServiceWebBean () throws NamingException {
63                 // Get initial context
64                 InitialContext context = new InitialContext();
65
66                 // Try to lookup the bean
67                 this.category = (CategorySessionBeanRemote) context.lookup("ejb/stateless-category"); //NOI18N
68
69                 // Try to lookup the bean
70                 this.product = (ProductSessionBeanRemote) context.lookup("ejb/stateless-product"); //NOI18N
71         }
72
73         @PostConstruct
74         public void init () throws RuntimeException {
75                 // Call super init first
76                 super.genericInit();
77         }
78
79         @Override
80         public Deque<Product> getAvailableProducts () throws FacesException {
81                 try {
82                         return this.getProduct().getAvailableProducts();
83                 } catch (final RemoteException ex) {
84                         // Continue to throw
85                         throw new FacesException(ex);
86                 }
87         }
88
89         @Override
90         public Deque<Category> getAllCategories () throws FacesException {
91                 try {
92                         // Fake zero category
93                         Category c = new ProductCategory(0L, "Ist oberste Kategorie", 0L);
94
95                         // Get List back
96                         Deque<Category> deque = this.getCategory().getAllCategories();
97
98                         // Add fake category
99                         deque.addFirst(c);
100
101                         // Return it
102                         return deque;
103                 } catch (final RemoteException ex) {
104                         // Continue to throw
105                         throw new FacesException(ex);
106                 }
107         }
108
109         /**
110          * Getter for shop remote bean
111          *
112          * @return Remote shop bean
113          */
114         private CategorySessionBeanRemote getCategory () {
115                 return this.category;
116         }
117
118         /**
119          * Getter for shop remote bean
120          *
121          * @return Remote shop bean
122          */
123         private ProductSessionBeanRemote getProduct () {
124                 return this.product;
125         }
126 }