2 * Copyright (C) 2017 RRoland Häder
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 employeePersonalContact;
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 * @return Redirect outcome
147 public String addEmployee () {
149 final Employable employee = this.createEmployee();
151 // Is the branch office not created yet?
152 if (this.isEmployeeCreatedByRequiredData(employee)) {
153 // Then show proper faces message
154 this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED"); //NOI18N
158 // Delcare updated instance
159 final Employable updatedOffice;
163 updatedOffice = this.adminEmployeeBean.addEmployee(employee);
164 } catch (final EmployeeAlreadyAddedException ex) {
166 this.showFacesMessage("form-admin-add-branch-office:branchStreet", "ADMIN_BRANCH_OFFICE_ALREADY_CREATED"); //NOI18N
171 this.addedEmployeeEvent.fire(new EmployeeAddedEvent(updatedOffice));
177 return "admin_list_branch_office"; //NOI18N
181 * Getter for employee's assigned branch office
183 * @return Employee's assigned branch office
185 public BranchOffice getEmployeeBranchOffice () {
186 return this.employeeBranchOffice;
190 * Setter for employee's assigned branch office
192 * @param employeeBranchOffice Employee's assigned branch office
194 public void setEmployeeBranchOffice (final BranchOffice employeeBranchOffice) {
195 this.employeeBranchOffice = employeeBranchOffice;
199 * Getter for employee's assigned basic company data
201 * @return Employee's assigned basic company data
203 public BasicData getEmployeeCompany () {
204 return this.employeeBasicData;
208 * Setter for employee's assigned basic company data
210 * @param employeeBasicData Employee's assigned basic company data
212 public void setEmployeeCompany (final BasicData employeeBasicData) {
213 this.employeeBasicData = employeeBasicData;
217 * Getter for employee's assigned department
219 * @return Employee's assigned department
221 public Department getEmployeeDepartment () {
222 return this.employeeDepartment;
226 * Setter for employee's assigned department
228 * @param employeeDepartment Employee's assigned department
230 public void setEmployeeDepartment (final Department employeeDepartment) {
231 this.employeeDepartment = employeeDepartment;
235 * Getter for employee's business email address
237 * @return Employee's business email address
239 public String getEmployeeEmailAddress () {
240 return this.employeeEmailAddress;
244 * Setter for employee's business email address
246 * @param employeeEmailAddress Employee's business email address
248 public void setEmployeeEmailAddress (final String employeeEmailAddress) {
249 this.employeeEmailAddress = employeeEmailAddress;
253 * Getter for employee's assigned headquarter (if no branch office)
255 * @return Employee's assigned headquarter
257 public Headquarter getEmployeeHeadquarter () {
258 return this.employeeHeadquarter;
262 * Setter for employee's assigned headquarter (if no branch office)
264 * @param employeeHeadquarter Employee's assigned headquarter
266 public void setEmployeeHeadquarter (final Headquarter employeeHeadquarter) {
267 this.employeeHeadquarter = employeeHeadquarter;
271 * Getter for employee's staff number
273 * @return Employee's staff number
275 public String getEmployeeNumber () {
276 return this.employeeNumber;
280 * Setter for employee's staff number
282 * @param employeeNumber Employee's staff number
284 public void setEmployeeNumber (final String employeeNumber) {
285 this.employeeNumber = employeeNumber;
289 * Getter for employee's personal contact data
291 * @return Employee's personal contact data
293 public Contact getEmployeePersonalContact () {
294 return this.employeePersonalContact;
298 * Setter for employee's personal contact data
300 * @param employeePersonalContact Employee's personal contact data
302 public void setEmployeePersonalContact (final Contact employeePersonalContact) {
303 this.employeePersonalContact = employeePersonalContact;
307 * Getter for employee's phone extension
309 * @return Employee's phone extension
311 public Integer getEmployeePhoneExtension () {
312 return this.employeePhoneExtension;
316 * Getter for employee's phone extension
318 * @param employeePhoneExtension Employee's phone extension
320 public void setEmployeePhoneExtension (final Integer employeePhoneExtension) {
321 this.employeePhoneExtension = employeePhoneExtension;
325 * Getter for employee's user "owner"
327 * @return Employee's user "owner"
329 public User getEmployeeUserOwner () {
330 return this.employeeUserOwner;
334 * Setter for employee's user "owner"
336 * @param employeeUserOwner Employee's user "owner"
338 public void setEmployeeUserOwner (final User employeeUserOwner) {
339 this.employeeUserOwner = employeeUserOwner;
343 * Getter for mobile number
345 * @return Mobile number
347 public Long getMobileNumber () {
348 return this.mobileNumber;
352 * Setter for mobile number
354 * @param mobileNumber Mobile number
356 public void setMobileNumber (final Long mobileNumber) {
357 this.mobileNumber = mobileNumber;
361 * Getter for mobile provider instance
363 * @return Mobile provider instance
365 public MobileProvider getMobileProvider () {
366 return this.mobileProvider;
370 * Setter for mobile provider instance
372 * @param mobileProvider Mobile provider instance
374 public void setMobileProvider (final MobileProvider mobileProvider) {
375 this.mobileProvider = mobileProvider;
379 * Clears all bean fields
381 private void clear () {
383 this.setEmployeeBranchOffice(null);
384 this.setEmployeeCompany(null);
385 this.setEmployeeDepartment(null);
386 this.setEmployeeEmailAddress(null);
387 this.setEmployeeHeadquarter(null);
388 this.setEmployeePersonalContact(null);
389 this.setEmployeePhoneExtension(null);
390 this.setEmployeeNumber(null);
391 this.setEmployeeUserOwner(null);
395 * Creates employee instance with all data from this bean
397 * @return Employee instance
399 private Employable createEmployee () {
401 final Employable employee = new BusinessEmployee(this.getEmployeeCompany(), this.getEmployeeNumber(), this.getEmployeePersonalContact());
403 // Set all othewr data
404 employee.setEmployeeBranchOffice(this.getEmployeeBranchOffice());
405 employee.setEmployeeDepartment(this.getEmployeeDepartment());
406 employee.setEmployeeHeadquarter(this.getEmployeeHeadquarter());
407 employee.setEmployeePhoneExtension(this.getEmployeePhoneExtension());
408 // @TODO employee.setEmployeePosition(this.getEmployeePosition());
409 employee.setEmployeeUserOwner(this.getEmployeeUserOwner());
411 // Is mobile provider and number set?
412 if ((this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
413 // Init mobile number instance
414 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
416 // Set it in employee
417 employee.setEmployeeMobileNumber(number);
425 * Checks whether given employee is already added
427 * @param employee Employee to be checked
429 * @return Whether the employee has already been added
431 private boolean isEmployeeCreatedByRequiredData (final Employable employee) {
432 // Default is not found
433 boolean isFound = false;
435 // Check all employees
436 for (final Employable otherEmployee : this.employeeController.allEmployees()) {
438 if (Employees.isSameEmployeeFound(employee, otherEmployee)) {