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.inject.Inject;
24 import javax.inject.Named;
25 import org.mxchange.jcontacts.model.contact.Contact;
26 import org.mxchange.jcontactsbusiness.events.employee.added.EmployeeAddedEvent;
27 import org.mxchange.jcontactsbusiness.events.employee.added.ObservableEmployeeAddedEvent;
28 import org.mxchange.jcontactsbusiness.exceptions.employee.EmployeeAlreadyAddedException;
29 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
30 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
31 import org.mxchange.jcontactsbusiness.model.department.Department;
32 import org.mxchange.jcontactsbusiness.model.employee.AdminEmployeeSessionBeanRemote;
33 import org.mxchange.jcontactsbusiness.model.employee.BusinessEmployee;
34 import org.mxchange.jcontactsbusiness.model.employee.Employable;
35 import org.mxchange.jcontactsbusiness.model.employee.Employees;
36 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
38 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
39 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
40 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
41 import org.mxchange.jusercore.model.user.User;
44 * A request-scoped bean for administrative purposes for administrative employee
47 * @author Roland Häder<roland@mxchange.org>
49 @Named ("adminEmployeeController")
51 public class FinancialsAdminEmployeeWebRequestBean extends BaseFinancialsBean implements FinancialsAdminEmployeeWebRequestController {
56 private static final long serialVersionUID = 12_886_968_547_360L;
59 * An event being fired when an employee has been added
63 private Event<ObservableEmployeeAddedEvent> addedEmployeeEvent;
66 * EJB for administrative company employee purposes
68 @EJB (lookup = "java:global/jfinancials-ejb/adminEmployee!org.mxchange.jcontactsbusiness.model.employee.AdminEmployeeSessionBeanRemote")
69 private AdminEmployeeSessionBeanRemote adminEmployeeBean;
72 * Assigned basic data instance
74 private BasicData employeeBasicData;
77 * Assigned branch office
79 private BranchOffice employeeBranchOffice;
82 * General employee controller
85 private FinancialsEmployeeWebRequestController employeeController;
88 * Department where employee works (alternative to headquarter)
90 private Department employeeDepartment;
93 * Employee's business email address
95 private String employeeEmailAddress;
98 * Employee's Headquarter data, if he is working there
100 private Headquarter employeeHeadquarter;
103 * Employee's staff number
105 private String employeeNumber;
108 * Assigned personal data of the employee
110 private Contact employeePersonalData;
113 * Employee's phone extension (on top of a "main number")
115 private Integer employeePhoneExtension;
118 * Assigned user account
120 private User employeeUserOwner;
123 * Mobile number for employee's business mobile
125 private Long mobileNumber;
128 * Mobile provider instance for employee's business mobile
130 private MobileProvider mobileProvider;
133 * Default constructor
135 public FinancialsAdminEmployeeWebRequestBean () {
136 // Call super constructor
141 * Adds branch office with all data from this backing bean. First this
142 * action method will validate if the branch office's address is already
143 * registered and if found, it will output a proper faces message.
145 public void addEmployee () {
147 final Employable employee = this.createEmployee();
149 // Is the branch office not created yet?
150 if (this.isEmployeeCreatedByRequiredData(employee)) {
151 // Then show proper faces message
152 this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED"); //NOI18N
156 // Delcare updated instance
157 final Employable updatedOffice;
161 updatedOffice = this.adminEmployeeBean.addEmployee(employee);
162 } catch (final EmployeeAlreadyAddedException ex) {
164 this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED"); //NOI18N
169 this.addedEmployeeEvent.fire(new EmployeeAddedEvent(updatedOffice));
176 * Getter for employee's assigned basic company data
178 * @return Employee's assigned basic company data
180 public BasicData getEmployeeBasicData () {
181 return this.employeeBasicData;
185 * Setter for employee's assigned basic company data
187 * @param employeeBasicData Employee's assigned basic company data
189 public void setEmployeeBasicData (final BasicData employeeBasicData) {
190 this.employeeBasicData = employeeBasicData;
194 * Getter for employee's assigned branch office
196 * @return Employee's assigned branch office
198 public BranchOffice getEmployeeBranchOffice () {
199 return this.employeeBranchOffice;
203 * Setter for employee's assigned branch office
205 * @param employeeBranchOffice Employee's assigned branch office
207 public void setEmployeeBranchOffice (final BranchOffice employeeBranchOffice) {
208 this.employeeBranchOffice = employeeBranchOffice;
212 * Getter for employee's assigned department
214 * @return Employee's assigned department
216 public Department getEmployeeDepartment () {
217 return this.employeeDepartment;
221 * Setter for employee's assigned department
223 * @param employeeDepartment Employee's assigned department
225 public void setEmployeeDepartment (final Department employeeDepartment) {
226 this.employeeDepartment = employeeDepartment;
230 * Getter for employee's business email address
232 * @return Employee's business email address
234 public String getEmployeeEmailAddress () {
235 return this.employeeEmailAddress;
239 * Setter for employee's business email address
241 * @param employeeEmailAddress Employee's business email address
243 public void setEmployeeEmailAddress (final String employeeEmailAddress) {
244 this.employeeEmailAddress = employeeEmailAddress;
248 * Getter for employee's assigned headquarter (if no branch office)
250 * @return Employee's assigned headquarter
252 public Headquarter getEmployeeHeadquarter () {
253 return this.employeeHeadquarter;
257 * Setter for employee's assigned headquarter (if no branch office)
259 * @param employeeHeadquarter Employee's assigned headquarter
261 public void setEmployeeHeadquarter (final Headquarter employeeHeadquarter) {
262 this.employeeHeadquarter = employeeHeadquarter;
266 * Getter for employee's staff number
268 * @return Employee's staff number
270 public String getEmployeeNumber () {
271 return this.employeeNumber;
275 * Setter for employee's staff number
277 * @param employeeNumber Employee's staff number
279 public void setEmployeeNumber (final String employeeNumber) {
280 this.employeeNumber = employeeNumber;
284 * Getter for employee's personal contact data
286 * @return Employee's personal contact data
288 public Contact getEmployeePersonalData () {
289 return this.employeePersonalData;
293 * Setter for employee's personal contact data
295 * @param employeePersonalData Employee's personal contact data
297 public void setEmployeePersonalData (final Contact employeePersonalData) {
298 this.employeePersonalData = employeePersonalData;
302 * Getter for employee's phone extension
304 * @return Employee's phone extension
306 public Integer getEmployeePhoneExtension () {
307 return this.employeePhoneExtension;
311 * Getter for employee's phone extension
313 * @param employeePhoneExtension Employee's phone extension
315 public void setEmployeePhoneExtension (final Integer employeePhoneExtension) {
316 this.employeePhoneExtension = employeePhoneExtension;
320 * Getter for employee's user "owner"
322 * @return Employee's user "owner"
324 public User getEmployeeUserOwner () {
325 return this.employeeUserOwner;
329 * Setter for employee's user "owner"
331 * @param employeeUserOwner Employee's user "owner"
333 public void setEmployeeUserOwner (final User employeeUserOwner) {
334 this.employeeUserOwner = employeeUserOwner;
338 * Getter for mobile number
340 * @return Mobile number
342 public Long getMobileNumber () {
343 return this.mobileNumber;
347 * Setter for mobile number
349 * @param mobileNumber Mobile number
351 public void setMobileNumber (final Long mobileNumber) {
352 this.mobileNumber = mobileNumber;
356 * Getter for mobile provider instance
358 * @return Mobile provider instance
360 public MobileProvider getMobileProvider () {
361 return this.mobileProvider;
365 * Setter for mobile provider instance
367 * @param mobileProvider Mobile provider instance
369 public void setMobileProvider (final MobileProvider mobileProvider) {
370 this.mobileProvider = mobileProvider;
374 * Clears all bean fields
376 private void clear () {
378 this.setEmployeeBranchOffice(null);
379 this.setEmployeeBasicData(null);
380 this.setEmployeeDepartment(null);
381 this.setEmployeeEmailAddress(null);
382 this.setEmployeeHeadquarter(null);
383 this.setEmployeePersonalData(null);
384 this.setEmployeePhoneExtension(null);
385 this.setEmployeeNumber(null);
386 this.setEmployeeUserOwner(null);
390 * Creates employee instance with all data from this bean
392 * @return Employee instance
394 private Employable createEmployee () {
396 final Employable employee = new BusinessEmployee(this.getEmployeeBasicData(), this.getEmployeeNumber(), this.getEmployeePersonalData());
398 // Set all othewr data
399 employee.setEmployeeBranchOffice(this.getEmployeeBranchOffice());
400 employee.setEmployeeDepartment(this.getEmployeeDepartment());
401 employee.setEmployeeHeadquarter(this.getEmployeeHeadquarter());
402 employee.setEmployeePhoneExtension(this.getEmployeePhoneExtension());
403 // @TODO employee.setEmployeePosition(this.getEmployeePosition());
404 employee.setEmployeeUserOwner(this.getEmployeeUserOwner());
406 // Is mobile provider and number set?
407 if ((this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
408 // Init mobile number instance
409 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
411 // Set it in employee
412 employee.setEmployeeMobileNumber(number);
420 * Checks whether given employee is already added
422 * @param employee Employee to be checked
424 * @return Whether the employee has already been added
426 private boolean isEmployeeCreatedByRequiredData (final Employable employee) {
427 // Default is not found
428 boolean isFound = false;
430 // Check all employees
431 for (final Employable otherEmployee : this.employeeController.allEmployees()) {
433 if (Employees.isSameEmployeeFound(employee, otherEmployee)) {