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.category;
19 import javax.enterprise.context.RequestScoped;
20 import javax.faces.view.facelets.FaceletException;
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import org.mxchange.jshopcore.exceptions.CannotAddCategoryException;
27 import org.mxchange.jshopcore.exceptions.CategoryTitleAlreadyUsedException;
28 import org.mxchange.jshopcore.model.category.AdminCategorySessionBeanRemote;
29 import org.mxchange.jshopcore.model.category.Category;
30 import org.mxchange.jshopcore.model.category.ProductCategory;
31 import org.mxchange.pizzaapplication.beans.shop.ShopWebController;
34 * Main application class
36 * @author Roland Haeder<roland@mxchange.org>
38 @Named ("admin_category")
40 public class AdminCategoryWebBean implements AdminCategoryWebController {
45 private static final long serialVersionUID = 5_819_375_183_472_871L;
48 * Remote bean for categories
50 private final AdminCategorySessionBeanRemote categoryBean;
52 ////////////////////// Bean injections ///////////////////////
57 private ShopWebController shopController;
59 /////////////////////// Properties /////////////////////
61 * Category categoryTitle
63 private String categoryTitle;
68 private Category parentCategory;
73 public AdminCategoryWebBean () {
76 // Get initial context
77 Context context = new InitialContext();
79 // Try to lookup the bean
80 this.categoryBean = (AdminCategorySessionBeanRemote) context.lookup("ejb/stateless-admin-category"); //NOI18N
81 } catch (final NamingException e) {
83 throw new FaceletException(e);
88 public void addCategory () throws FaceletException {
91 Category category = new ProductCategory();
92 category.setParentCategory(this.getParentCategory());
93 category.setCategoryTitle(this.getCategoryTitle());
95 // Deligate to remote bean
96 Category updatedCategory = this.categoryBean.doAdminAddCategory(category);
98 // Also send it to the controller bean
99 this.shopController.addCategory(updatedCategory);
101 // Unset all older values
102 this.setCategoryTitle(""); //NOI18N
103 this.setParentCategory(null);
104 } catch (final CategoryTitleAlreadyUsedException | CannotAddCategoryException ex) {
106 throw new FaceletException(ex);
111 public String getCategoryTitle () {
112 return this.categoryTitle;
116 public void setCategoryTitle (final String categoryTitle) {
117 this.categoryTitle = categoryTitle;
121 public Category getParentCategory () {
122 return this.parentCategory;
126 public void setParentCategory (final Category parentCategory) {
127 this.parentCategory = parentCategory;