2 * Copyright (C) 2017, 2018 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.jfinancials.beans.business.department;
19 import java.util.List;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import org.mxchange.jcontactsbusiness.events.department.added.DepartmentAddedEvent;
27 import org.mxchange.jcontactsbusiness.events.department.added.ObservableDepartmentAddedEvent;
28 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentAlreadyAddedException;
29 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
30 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
31 import org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote;
32 import org.mxchange.jcontactsbusiness.model.department.BusinessDepartment;
33 import org.mxchange.jcontactsbusiness.model.department.Department;
34 import org.mxchange.jcontactsbusiness.model.department.Departments;
35 import org.mxchange.jcontactsbusiness.model.employee.Employable;
36 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
38 import org.mxchange.jusercore.model.user.User;
41 * An administrative bean for departments
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("adminDepartmentController")
47 public class FinancialsAdminDepartmentWebRequestBean extends BaseFinancialsBean implements FinancialsAdminDepartmentWebRequestController {
52 private static final long serialVersionUID = 5_028_697_360_464L;
55 * EJB for administrative purposes
57 @EJB (lookup = "java:global/jfinancials-ejb/adminDepartment!org.mxchange.jcontactsbusiness.model.department.AdminDepartmentSessionBeanRemote")
58 private AdminDepartmentSessionBeanRemote adminDepartmentBean;
61 * An event being fired when a department has been successfully added
65 private Event<ObservableDepartmentAddedEvent> departmentAddedEvent;
68 * Assigned branch office (if apply-able)
70 private BranchOffice departmentBranchOffice;
73 * Assigned company for this department
75 private BasicData departmentCompany;
78 * A general department controller (backing bean)
81 private FinancialsDepartmentWebRequestController departmentController;
84 * Assigned headquarter (if apply-able)
86 private Headquarter departmentHeadquarter;
91 private String departmentI18nKey;
94 * Lead person of this department
96 private Employable departmentLead;
99 * Owning user instance (which this department is assigned to)
101 private User departmentUserOwner;
104 * Default constructor
106 public FinancialsAdminDepartmentWebRequestBean () {
107 // Call super constructor
112 * Adds department with all data from this backing bean. First this action
113 * method will validate if the department's address is already registered
114 * and if found, it will output a proper faces message.
116 * @return Redirect outcome
118 public String addDepartment () {
120 final Department department = this.createDepartment();
122 // Is the department not created yet?
123 if (this.isDepartmentCreatedByRequiredData(department)) {
124 // Then show proper faces message
125 this.showFacesMessage("form-admin-add-department:branchStreet", "ADMIN_DEPARTMENT_ALREADY_CREATED"); //NOI18N
129 // Delcare updated instance
130 final Department updatedDepartment;
134 updatedDepartment = this.adminDepartmentBean.addDepartment(department);
135 } catch (final DepartmentAlreadyAddedException ex) {
137 this.showFacesMessage("form-admin-add-department:departmentI18nKey", "ADMIN_DEPARTMENT_ALREADY_CREATED"); //NOI18N
142 this.departmentAddedEvent.fire(new DepartmentAddedEvent(updatedDepartment));
145 return "admin_list_department"; //NOI18N
149 * Getter for assigned branch office
151 * @return Branch office
153 public BranchOffice getDepartmentBranchOffice () {
154 return this.departmentBranchOffice;
158 * Setter for assigned branch office
160 * @param departmentBranchOffice Branch office
162 public void setDepartmentBranchOffice (final BranchOffice departmentBranchOffice) {
163 this.departmentBranchOffice = departmentBranchOffice;
167 * Getter for basic company data
169 * @return Basic company data
171 public BasicData getDepartmentCompany () {
172 return this.departmentCompany;
176 * Setter for basic company data
178 * @param departmentCompany Basic company data
180 public void setDepartmentCompany (final BasicData departmentCompany) {
181 this.departmentCompany = departmentCompany;
185 * Getter for assigned headquarter data
187 * @return Headquarter data
189 public Headquarter getDepartmentHeadquarter () {
190 return this.departmentHeadquarter;
194 * Setter for assigned headquarter data
196 * @param departmentHeadquarter Headquarter data
198 public void setDepartmentHeadquarter (final Headquarter departmentHeadquarter) {
199 this.departmentHeadquarter = departmentHeadquarter;
203 * Getter for department name
205 * @return Department name
207 public String getDepartmentI18nKey () {
208 return this.departmentI18nKey;
212 * Setter for department name
214 * @param departmentI18nKey Department name
216 public void setDepartmentI18nKey (final String departmentI18nKey) {
217 this.departmentI18nKey = departmentI18nKey;
221 * Getter for department contact person
223 * @return Department contact person
225 public Employable getDepartmentLead () {
226 return this.departmentLead;
230 * Setter for department contact person
232 * @param departmentLead Department contact person
234 public void setDepartmentLead (final Employable departmentLead) {
235 this.departmentLead = departmentLead;
239 * Getter for owning user instance
241 * @return Owning user instance
243 public User getDepartmentUserOwner () {
244 return this.departmentUserOwner;
248 * Setter for owning user instance
250 * @param departmentUserOwner Owning user instance
252 public void setDepartmentUserOwner (final User departmentUserOwner) {
253 this.departmentUserOwner = departmentUserOwner;
257 * Prepares an instance of a Department object (entity) with all data from
258 * this bean. If a complete fax number or land-line number was provided, it
259 * will be set in the instance as well.
261 * @return An instance of a Department class (entity)
263 private Department createDepartment () {
264 // Create new department instance
265 final Department department = new BusinessDepartment(this.getDepartmentCompany(), this.getDepartmentI18nKey());
267 // Add all optional fields
268 department.setDepartmentHeadquarter(this.getDepartmentHeadquarter());
269 department.setDepartmentBranchOffice(this.getDepartmentBranchOffice());
270 department.setDepartmentLead(this.getDepartmentLead());
271 department.setDepartmentUserOwner(this.getDepartmentUserOwner());
273 // Return fully prepared instance
278 * Checks whether the given department is already found in local cache.
279 * Please note that this method fully relies on the cache, so you must
280 * always fire proper events that add/update/delete entries in cache.
282 * @param department Department to check it's address
284 * @return Whether the address has been found
286 private boolean isDepartmentCreatedByRequiredData (final Department department) {
287 // Get full list from other bean
288 final List<Department> departments = this.departmentController.allDepartments();
290 // Default is not found
291 boolean isFound = false;
293 // Now check each entry
294 for (final Department dep : departments) {
296 if (Departments.isSameDepartment(dep, department)) {