2 * Copyright (C) 2016 Roland Haeder
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 javax.enterprise.context.RequestScoped;
20 import javax.enterprise.event.Event;
21 import javax.enterprise.inject.Any;
22 import javax.faces.view.facelets.FaceletException;
23 import javax.inject.Inject;
24 import javax.inject.Named;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jshopcore.events.category.AddedCategoryEvent;
29 import org.mxchange.jshopcore.events.category.ShopCategoryAddedEvent;
30 import org.mxchange.jshopcore.exceptions.CannotAddCategoryException;
31 import org.mxchange.jshopcore.exceptions.CategoryTitleAlreadyUsedException;
32 import org.mxchange.jshopcore.model.category.AdminCategorySessionBeanRemote;
33 import org.mxchange.jshopcore.model.category.Category;
34 import org.mxchange.jshopcore.model.category.ProductCategory;
37 * Main application class
39 * @author Roland Haeder<roland@mxchange.org>
41 @Named ("adminCategoryController")
43 public class PizzaAdminCategoryWebRequestBean implements PizzaAdminCategoryWebRequestController {
48 private static final long serialVersionUID = 5_819_375_183_472_871L;
51 * Event for added shop categories
55 private Event<AddedCategoryEvent> categoryAddedEvent;
58 * Remote bean for categories
60 private AdminCategorySessionBeanRemote categoryBean;
62 /////////////////////// Properties /////////////////////
64 * Category categoryTitle
66 private String categoryTitle;
71 private Category parentCategory;
76 public PizzaAdminCategoryWebRequestBean () {
79 // Get initial context
80 Context context = new InitialContext();
82 // Try to lookup the bean
83 this.categoryBean = (AdminCategorySessionBeanRemote) context.lookup("java:global/jshop-ejb/admin_category!org.mxchange.jshopcore.model.category.AdminCategorySessionBeanRemote"); //NOI18N
84 } catch (final NamingException e) {
86 throw new FaceletException(e);
91 public void addCategory () throws FaceletException {
94 Category category = new ProductCategory();
95 category.setParentCategory(this.getParentCategory());
96 category.setCategoryTitle(this.getCategoryTitle());
98 // Deligate to remote bean
99 Category updatedCategory = this.categoryBean.doAdminAddCategory(category);
102 this.categoryAddedEvent.fire(new ShopCategoryAddedEvent(updatedCategory));
104 // Unset all older values
105 this.setCategoryTitle(""); //NOI18N
106 this.setParentCategory(null);
107 } catch (final CategoryTitleAlreadyUsedException | CannotAddCategoryException ex) {
109 throw new FaceletException(ex);
114 public String getCategoryTitle () {
115 return this.categoryTitle;
119 public void setCategoryTitle (final String categoryTitle) {
120 this.categoryTitle = categoryTitle;
124 public Category getParentCategory () {
125 return this.parentCategory;
129 public void setParentCategory (final Category parentCategory) {
130 this.parentCategory = parentCategory;