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 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.jproduct.events.category.AddedCategoryEvent;
29 import org.mxchange.jproduct.events.category.CategoryAddedEvent;
30 import org.mxchange.jproduct.exceptions.CannotAddCategoryException;
31 import org.mxchange.jproduct.exceptions.CategoryTitleAlreadyUsedException;
32 import org.mxchange.jproduct.model.category.Category;
33 import org.mxchange.jproduct.model.category.ProductCategory;
34 import org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote;
35 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
38 * Main application class
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("adminCategoryController")
44 public class PizzaAdminCategoryWebRequestBean extends BasePizzaBean implements PizzaAdminCategoryWebRequestController {
49 private static final long serialVersionUID = 5_819_375_183_472_871L;
52 * Event for added shop categories
56 private Event<AddedCategoryEvent> categoryAddedEvent;
59 * Remote bean for categories
61 private AdminCategorySessionBeanRemote categoryBean;
63 /////////////////////// Properties /////////////////////
65 * Category categoryTitle
67 private String categoryTitle;
72 private Category parentCategory;
77 public PizzaAdminCategoryWebRequestBean () {
80 // Get initial context
81 Context context = new InitialContext();
83 // Try to lookup the bean
84 this.categoryBean = (AdminCategorySessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminCategory!org.mxchange.jshopcore.model.category.AdminCategorySessionBeanRemote"); //NOI18N
85 } catch (final NamingException e) {
87 throw new FaceletException(e);
92 public void addCategory () throws FaceletException {
95 Category category = new ProductCategory();
96 category.setParentCategory(this.getParentCategory());
97 category.setCategoryTitle(this.getCategoryTitle());
99 // Deligate to remote bean
100 Category updatedCategory = this.categoryBean.doAdminAddCategory(category);
103 this.categoryAddedEvent.fire(new CategoryAddedEvent(updatedCategory));
105 // Unset all older values
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;
134 * Clears this bean (example: when category has been added)
136 private void clear () {
137 this.setCategoryTitle(""); //NOI18N
138 this.setParentCategory(null);