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.jjobs.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.jjobs.beans.BaseJobsBean;
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;
43 import org.mxchange.jjobs.beans.business.employee.list.JobsEmployeeListWebViewController;
46 * A request-scoped bean for administrative purposes for administrative employee
49 * @author Roland Häder<roland@mxchange.org>
51 @Named ("adminEmployeeController")
53 public class JobsAdminEmployeeWebRequestBean extends BaseJobsBean implements JobsAdminEmployeeWebRequestController {
58 private static final long serialVersionUID = 12_886_968_547_360L;
61 * An event being fired when an employee has been added
65 private Event<ObservableEmployeeAddedEvent> addedEmployeeEvent;
68 * EJB for administrative company employee purposes
70 @EJB (lookup = "java:global/jjobs-ejb/adminEmployee!org.mxchange.jcontactsbusiness.model.employee.AdminEmployeeSessionBeanRemote")
71 private AdminEmployeeSessionBeanRemote adminEmployeeBean;
74 * Administrative list-all-employees controller
77 private JobsEmployeeListWebViewController adminEmployeeListController;
80 * Assigned basic data instance
82 private BasicData employeeBasicData;
85 * Assigned branch office
87 private BranchOffice employeeBranchOffice;
90 * General employee controller
93 private JobsEmployeeWebRequestController employeeController;
96 * Department where employee works (alternative to headquarter)
98 private Department employeeDepartment;
101 * Employee's business email address
103 private String employeeEmailAddress;
106 * Employee's Headquarter data, if he is working there
108 private Headquarter employeeHeadquarter;
111 * Employee's staff number
113 private String employeeNumber;
116 * Assigned personal data of the employee
118 private Contact employeePersonalData;
121 * Employee's phone extension (on top of a "main number")
123 private Integer employeePhoneExtension;
126 * Assigned user account
128 private User employeeUserOwner;
131 * Mobile number for employee's business mobile
133 private Long mobileNumber;
136 * Mobile provider instance for employee's business mobile
138 private MobileProvider mobileProvider;
141 * Default constructor
143 public JobsAdminEmployeeWebRequestBean () {
144 // Call super constructor
149 * Adds branch office with all data from this backing bean. First this
150 * action method will validate if the branch office's address is already
151 * registered and if found, it will output a proper faces message.
153 public void addEmployee () {
155 final Employable employee = this.createEmployee();
157 // Is the branch office not created yet?
158 if (this.isEmployeeCreatedByRequiredData(employee)) {
159 // Then show proper faces message
160 this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED", FacesMessage.SEVERITY_WARN); //NOI18N
164 // Delcare updated instance
165 final Employable updatedOffice;
169 updatedOffice = this.adminEmployeeBean.addEmployee(employee);
170 } catch (final EmployeeAlreadyAddedException ex) {
172 this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED", FacesMessage.SEVERITY_ERROR); //NOI18N
177 this.addedEmployeeEvent.fire(new EmployeeAddedEvent(updatedOffice));
184 * Getter for employee's assigned basic company data
186 * @return Employee's assigned basic company data
188 public BasicData getEmployeeBasicData () {
189 return this.employeeBasicData;
193 * Setter for employee's assigned basic company data
195 * @param employeeBasicData Employee's assigned basic company data
197 public void setEmployeeBasicData (final BasicData employeeBasicData) {
198 this.employeeBasicData = employeeBasicData;
202 * Getter for employee's assigned branch office
204 * @return Employee's assigned branch office
206 public BranchOffice getEmployeeBranchOffice () {
207 return this.employeeBranchOffice;
211 * Setter for employee's assigned branch office
213 * @param employeeBranchOffice Employee's assigned branch office
215 public void setEmployeeBranchOffice (final BranchOffice employeeBranchOffice) {
216 this.employeeBranchOffice = employeeBranchOffice;
220 * Getter for employee's assigned department
222 * @return Employee's assigned department
224 public Department getEmployeeDepartment () {
225 return this.employeeDepartment;
229 * Setter for employee's assigned department
231 * @param employeeDepartment Employee's assigned department
233 public void setEmployeeDepartment (final Department employeeDepartment) {
234 this.employeeDepartment = employeeDepartment;
238 * Getter for employee's business email address
240 * @return Employee's business email address
242 public String getEmployeeEmailAddress () {
243 return this.employeeEmailAddress;
247 * Setter for employee's business email address
249 * @param employeeEmailAddress Employee's business email address
251 public void setEmployeeEmailAddress (final String employeeEmailAddress) {
252 this.employeeEmailAddress = employeeEmailAddress;
256 * Getter for employee's assigned headquarter (if no branch office)
258 * @return Employee's assigned headquarter
260 public Headquarter getEmployeeHeadquarter () {
261 return this.employeeHeadquarter;
265 * Setter for employee's assigned headquarter (if no branch office)
267 * @param employeeHeadquarter Employee's assigned headquarter
269 public void setEmployeeHeadquarter (final Headquarter employeeHeadquarter) {
270 this.employeeHeadquarter = employeeHeadquarter;
274 * Getter for employee's staff number
276 * @return Employee's staff number
278 public String getEmployeeNumber () {
279 return this.employeeNumber;
283 * Setter for employee's staff number
285 * @param employeeNumber Employee's staff number
287 public void setEmployeeNumber (final String employeeNumber) {
288 this.employeeNumber = employeeNumber;
292 * Getter for employee's personal contact data
294 * @return Employee's personal contact data
296 public Contact getEmployeePersonalData () {
297 return this.employeePersonalData;
301 * Setter for employee's personal contact data
303 * @param employeePersonalData Employee's personal contact data
305 public void setEmployeePersonalData (final Contact employeePersonalData) {
306 this.employeePersonalData = employeePersonalData;
310 * Getter for employee's phone extension
312 * @return Employee's phone extension
314 public Integer getEmployeePhoneExtension () {
315 return this.employeePhoneExtension;
319 * Getter for employee's phone extension
321 * @param employeePhoneExtension Employee's phone extension
323 public void setEmployeePhoneExtension (final Integer employeePhoneExtension) {
324 this.employeePhoneExtension = employeePhoneExtension;
328 * Getter for employee's user "owner"
330 * @return Employee's user "owner"
332 public User getEmployeeUserOwner () {
333 return this.employeeUserOwner;
337 * Setter for employee's user "owner"
339 * @param employeeUserOwner Employee's user "owner"
341 public void setEmployeeUserOwner (final User employeeUserOwner) {
342 this.employeeUserOwner = employeeUserOwner;
346 * Getter for mobile number
348 * @return Mobile number
350 public Long getMobileNumber () {
351 return this.mobileNumber;
355 * Setter for mobile number
357 * @param mobileNumber Mobile number
359 public void setMobileNumber (final Long mobileNumber) {
360 this.mobileNumber = mobileNumber;
364 * Getter for mobile provider instance
366 * @return Mobile provider instance
368 public MobileProvider getMobileProvider () {
369 return this.mobileProvider;
373 * Setter for mobile provider instance
375 * @param mobileProvider Mobile provider instance
377 public void setMobileProvider (final MobileProvider mobileProvider) {
378 this.mobileProvider = mobileProvider;
382 * Clears all bean fields
384 private void clear () {
386 this.setEmployeeBranchOffice(null);
387 this.setEmployeeBasicData(null);
388 this.setEmployeeDepartment(null);
389 this.setEmployeeEmailAddress(null);
390 this.setEmployeeHeadquarter(null);
391 this.setEmployeePersonalData(null);
392 this.setEmployeePhoneExtension(null);
393 this.setEmployeeNumber(null);
394 this.setEmployeeUserOwner(null);
398 * Creates employee instance with all data from this bean
400 * @return Employee instance
402 private Employable createEmployee () {
404 final Employable employee = new BusinessEmployee(
405 this.getEmployeeBasicData(),
406 this.getEmployeeNumber(),
407 this.getEmployeePersonalData()
410 // Set all othewr data
411 employee.setEmployeeBranchOffice(this.getEmployeeBranchOffice());
412 employee.setEmployeeDepartment(this.getEmployeeDepartment());
413 employee.setEmployeeHeadquarter(this.getEmployeeHeadquarter());
414 employee.setEmployeePhoneExtension(this.getEmployeePhoneExtension());
415 // @TODO employee.setEmployeePosition(this.getEmployeePosition());
416 employee.setEmployeeUserOwner(this.getEmployeeUserOwner());
418 // Is mobile provider and number set?
419 if ((this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
420 // Init mobile number instance
421 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
423 // Set it in employee
424 employee.setEmployeeMobileNumber(number);
432 * Checks whether given employee is already added
434 * @param employee Employee to be checked
436 * @return Whether the employee has already been added
438 private boolean isEmployeeCreatedByRequiredData (final Employable employee) {
439 // Default is not found
440 boolean isFound = false;
442 // Check all employees
443 for (final Employable otherEmployee : this.adminEmployeeListController.getAllEmployees()) {
445 if (Employees.isSameEmployeeFound(employee, otherEmployee)) {