2 * Copyright (C) 2017, 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.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 public void addDepartment () {
118 final Department department = this.createDepartment();
120 // Is the department not created yet?
121 if (this.isDepartmentCreatedByRequiredData(department)) {
122 // Then show proper faces message
123 this.showFacesMessage("form-admin-add-department:branchStreet", "ADMIN_DEPARTMENT_ALREADY_CREATED"); //NOI18N
127 // Delcare updated instance
128 final Department updatedDepartment;
132 updatedDepartment = this.adminDepartmentBean.addDepartment(department);
133 } catch (final DepartmentAlreadyAddedException ex) {
135 this.showFacesMessage("form-admin-add-department:departmentI18nKey", "ADMIN_DEPARTMENT_ALREADY_CREATED"); //NOI18N
140 this.departmentAddedEvent.fire(new DepartmentAddedEvent(updatedDepartment));
144 * Getter for assigned branch office
146 * @return Branch office
148 public BranchOffice getDepartmentBranchOffice () {
149 return this.departmentBranchOffice;
153 * Setter for assigned branch office
155 * @param departmentBranchOffice Branch office
157 public void setDepartmentBranchOffice (final BranchOffice departmentBranchOffice) {
158 this.departmentBranchOffice = departmentBranchOffice;
162 * Getter for basic company data
164 * @return Basic company data
166 public BasicData getDepartmentCompany () {
167 return this.departmentCompany;
171 * Setter for basic company data
173 * @param departmentCompany Basic company data
175 public void setDepartmentCompany (final BasicData departmentCompany) {
176 this.departmentCompany = departmentCompany;
180 * Getter for assigned headquarter data
182 * @return Headquarter data
184 public Headquarter getDepartmentHeadquarter () {
185 return this.departmentHeadquarter;
189 * Setter for assigned headquarter data
191 * @param departmentHeadquarter Headquarter data
193 public void setDepartmentHeadquarter (final Headquarter departmentHeadquarter) {
194 this.departmentHeadquarter = departmentHeadquarter;
198 * Getter for department name
200 * @return Department name
202 public String getDepartmentI18nKey () {
203 return this.departmentI18nKey;
207 * Setter for department name
209 * @param departmentI18nKey Department name
211 public void setDepartmentI18nKey (final String departmentI18nKey) {
212 this.departmentI18nKey = departmentI18nKey;
216 * Getter for department contact person
218 * @return Department contact person
220 public Employable getDepartmentLead () {
221 return this.departmentLead;
225 * Setter for department contact person
227 * @param departmentLead Department contact person
229 public void setDepartmentLead (final Employable departmentLead) {
230 this.departmentLead = departmentLead;
234 * Getter for owning user instance
236 * @return Owning user instance
238 public User getDepartmentUserOwner () {
239 return this.departmentUserOwner;
243 * Setter for owning user instance
245 * @param departmentUserOwner Owning user instance
247 public void setDepartmentUserOwner (final User departmentUserOwner) {
248 this.departmentUserOwner = departmentUserOwner;
252 * Prepares an instance of a Department object (entity) with all data from
253 * this bean. If a complete fax number or land-line number was provided, it
254 * will be set in the instance as well.
256 * @return An instance of a Department class (entity)
258 private Department createDepartment () {
259 // Create new department instance
260 final Department department = new BusinessDepartment(this.getDepartmentCompany(), this.getDepartmentI18nKey());
262 // Add all optional fields
263 department.setDepartmentHeadquarter(this.getDepartmentHeadquarter());
264 department.setDepartmentBranchOffice(this.getDepartmentBranchOffice());
265 department.setDepartmentLead(this.getDepartmentLead());
266 department.setDepartmentUserOwner(this.getDepartmentUserOwner());
268 // Return fully prepared instance
273 * Checks whether the given department is already found in local cache.
274 * Please note that this method fully relies on the cache, so you must
275 * always fire proper events that add/update/delete entries in cache.
277 * @param department Department to check it's address
279 * @return Whether the address has been found
281 private boolean isDepartmentCreatedByRequiredData (final Department department) {
282 // Get full list from other bean
283 final List<Department> departments = this.departmentController.allDepartments();
285 // Default is not found
286 boolean isFound = false;
288 // Now check each entry
289 for (final Department dep : departments) {
291 if (Departments.isSameDepartment(dep, department)) {