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.employee;
20 import javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.application.FacesMessage;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import org.mxchange.jcontacts.model.contact.Contact;
27 import org.mxchange.jcontactsbusiness.events.employee.added.EmployeeAddedEvent;
28 import org.mxchange.jcontactsbusiness.events.employee.added.ObservableEmployeeAddedEvent;
29 import org.mxchange.jcontactsbusiness.exceptions.employee.EmployeeAlreadyAddedException;
30 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
31 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
32 import org.mxchange.jcontactsbusiness.model.department.Department;
33 import org.mxchange.jcontactsbusiness.model.employee.AdminEmployeeSessionBeanRemote;
34 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
35 import org.mxchange.jcontactsbusiness.model.employee.Employable;
36 import org.mxchange.jcontactsbusiness.model.employee.Employees;
37 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
38 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
39 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
40 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
41 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
42 import org.mxchange.jusercore.model.user.User;
45 * A request-scoped bean for administrative purposes for administrative employee
48 * @author Roland Häder<roland@mxchange.org>
50 @Named ("adminEmployeeController")
52 public class FinancialsAdminEmployeeWebRequestBean extends BaseFinancialsBean implements FinancialsAdminEmployeeWebRequestController {
57 private static final long serialVersionUID = 12_886_968_547_360L;
60 * An event being fired when an employee has been added
64 private Event<ObservableEmployeeAddedEvent> addedEmployeeEvent;
67 * EJB for administrative company employee purposes
69 @EJB (lookup = "java:global/jfinancials-ejb/adminEmployee!org.mxchange.jcontactsbusiness.model.employee.AdminEmployeeSessionBeanRemote")
70 private AdminEmployeeSessionBeanRemote adminEmployeeBean;
73 * Assigned basic data instance
75 private BasicData employeeBasicData;
78 * Assigned branch office
80 private BranchOffice employeeBranchOffice;
83 * General employee controller
86 private FinancialsEmployeeWebRequestController employeeController;
89 * Department where employee works (alternative to headquarter)
91 private Department employeeDepartment;
94 * Employee's business email address
96 private String employeeEmailAddress;
99 * Employee's Headquarter data, if he is working there
101 private Headquarter employeeHeadquarter;
104 * Employee's staff number
106 private String employeeNumber;
109 * Assigned personal data of the employee
111 private Contact employeePersonalData;
114 * Employee's phone extension (on top of a "main number")
116 private Integer employeePhoneExtension;
119 * Assigned user account
121 private User employeeUserOwner;
124 * Mobile number for employee's business mobile
126 private Long mobileNumber;
129 * Mobile provider instance for employee's business mobile
131 private MobileProvider mobileProvider;
134 * Default constructor
136 public FinancialsAdminEmployeeWebRequestBean () {
137 // Call super constructor
142 * Adds branch office with all data from this backing bean. First this
143 * action method will validate if the branch office's address is already
144 * registered and if found, it will output a proper faces message.
146 public void addEmployee () {
148 final Employable employee = this.createEmployee();
150 // Is the branch office not created yet?
151 if (this.isEmployeeCreatedByRequiredData(employee)) {
152 // Then show proper faces message
153 this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N
157 // Delcare updated instance
158 final Employable updatedOffice;
162 updatedOffice = this.adminEmployeeBean.addEmployee(employee);
163 } catch (final EmployeeAlreadyAddedException ex) {
165 this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED", FacesMessage.SEVERITY_ERROR); //NOI18N
170 this.addedEmployeeEvent.fire(new EmployeeAddedEvent(updatedOffice));
177 * Getter for employee's assigned basic company data
179 * @return Employee's assigned basic company data
181 public BasicData getEmployeeBasicData () {
182 return this.employeeBasicData;
186 * Setter for employee's assigned basic company data
188 * @param employeeBasicData Employee's assigned basic company data
190 public void setEmployeeBasicData (final BasicData employeeBasicData) {
191 this.employeeBasicData = employeeBasicData;
195 * Getter for employee's assigned branch office
197 * @return Employee's assigned branch office
199 public BranchOffice getEmployeeBranchOffice () {
200 return this.employeeBranchOffice;
204 * Setter for employee's assigned branch office
206 * @param employeeBranchOffice Employee's assigned branch office
208 public void setEmployeeBranchOffice (final BranchOffice employeeBranchOffice) {
209 this.employeeBranchOffice = employeeBranchOffice;
213 * Getter for employee's assigned department
215 * @return Employee's assigned department
217 public Department getEmployeeDepartment () {
218 return this.employeeDepartment;
222 * Setter for employee's assigned department
224 * @param employeeDepartment Employee's assigned department
226 public void setEmployeeDepartment (final Department employeeDepartment) {
227 this.employeeDepartment = employeeDepartment;
231 * Getter for employee's business email address
233 * @return Employee's business email address
235 public String getEmployeeEmailAddress () {
236 return this.employeeEmailAddress;
240 * Setter for employee's business email address
242 * @param employeeEmailAddress Employee's business email address
244 public void setEmployeeEmailAddress (final String employeeEmailAddress) {
245 this.employeeEmailAddress = employeeEmailAddress;
249 * Getter for employee's assigned headquarter (if no branch office)
251 * @return Employee's assigned headquarter
253 public Headquarter getEmployeeHeadquarter () {
254 return this.employeeHeadquarter;
258 * Setter for employee's assigned headquarter (if no branch office)
260 * @param employeeHeadquarter Employee's assigned headquarter
262 public void setEmployeeHeadquarter (final Headquarter employeeHeadquarter) {
263 this.employeeHeadquarter = employeeHeadquarter;
267 * Getter for employee's staff number
269 * @return Employee's staff number
271 public String getEmployeeNumber () {
272 return this.employeeNumber;
276 * Setter for employee's staff number
278 * @param employeeNumber Employee's staff number
280 public void setEmployeeNumber (final String employeeNumber) {
281 this.employeeNumber = employeeNumber;
285 * Getter for employee's personal contact data
287 * @return Employee's personal contact data
289 public Contact getEmployeePersonalData () {
290 return this.employeePersonalData;
294 * Setter for employee's personal contact data
296 * @param employeePersonalData Employee's personal contact data
298 public void setEmployeePersonalData (final Contact employeePersonalData) {
299 this.employeePersonalData = employeePersonalData;
303 * Getter for employee's phone extension
305 * @return Employee's phone extension
307 public Integer getEmployeePhoneExtension () {
308 return this.employeePhoneExtension;
312 * Getter for employee's phone extension
314 * @param employeePhoneExtension Employee's phone extension
316 public void setEmployeePhoneExtension (final Integer employeePhoneExtension) {
317 this.employeePhoneExtension = employeePhoneExtension;
321 * Getter for employee's user "owner"
323 * @return Employee's user "owner"
325 public User getEmployeeUserOwner () {
326 return this.employeeUserOwner;
330 * Setter for employee's user "owner"
332 * @param employeeUserOwner Employee's user "owner"
334 public void setEmployeeUserOwner (final User employeeUserOwner) {
335 this.employeeUserOwner = employeeUserOwner;
339 * Getter for mobile number
341 * @return Mobile number
343 public Long getMobileNumber () {
344 return this.mobileNumber;
348 * Setter for mobile number
350 * @param mobileNumber Mobile number
352 public void setMobileNumber (final Long mobileNumber) {
353 this.mobileNumber = mobileNumber;
357 * Getter for mobile provider instance
359 * @return Mobile provider instance
361 public MobileProvider getMobileProvider () {
362 return this.mobileProvider;
366 * Setter for mobile provider instance
368 * @param mobileProvider Mobile provider instance
370 public void setMobileProvider (final MobileProvider mobileProvider) {
371 this.mobileProvider = mobileProvider;
375 * Clears all bean fields
377 private void clear () {
379 this.setEmployeeBranchOffice(null);
380 this.setEmployeeBasicData(null);
381 this.setEmployeeDepartment(null);
382 this.setEmployeeEmailAddress(null);
383 this.setEmployeeHeadquarter(null);
384 this.setEmployeePersonalData(null);
385 this.setEmployeePhoneExtension(null);
386 this.setEmployeeNumber(null);
387 this.setEmployeeUserOwner(null);
391 * Creates employee instance with all data from this bean
393 * @return Employee instance
395 private Employable createEmployee () {
397 final Employable employee = new BusinessEmployee(this.getEmployeeBasicData(), this.getEmployeeNumber(), this.getEmployeePersonalData());
399 // Set all othewr data
400 employee.setEmployeeBranchOffice(this.getEmployeeBranchOffice());
401 employee.setEmployeeDepartment(this.getEmployeeDepartment());
402 employee.setEmployeeHeadquarter(this.getEmployeeHeadquarter());
403 employee.setEmployeePhoneExtension(this.getEmployeePhoneExtension());
404 // @TODO employee.setEmployeePosition(this.getEmployeePosition());
405 employee.setEmployeeUserOwner(this.getEmployeeUserOwner());
407 // Is mobile provider and number set?
408 if ((this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
409 // Init mobile number instance
410 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
412 // Set it in employee
413 employee.setEmployeeMobileNumber(number);
421 * Checks whether given employee is already added
423 * @param employee Employee to be checked
425 * @return Whether the employee has already been added
427 private boolean isEmployeeCreatedByRequiredData (final Employable employee) {
428 // Default is not found
429 boolean isFound = false;
431 // Check all employees
432 for (final Employable otherEmployee : this.employeeController.allEmployees()) {
434 if (Employees.isSameEmployeeFound(employee, otherEmployee)) {