]> git.mxchange.org Git - jfinancials-war.git/blob
17f337c421add3392daae07b906fdf4a5d229b0d
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2017 - 2020 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.business.employee;
18
19 import javax.ejb.EJB;
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;
43 import org.mxchange.jfinancials.beans.business.employee.list.FinancialsEmployeeListWebViewController;
44
45 /**
46  * A request-scoped bean for administrative purposes for administrative employee
47  * purposes.
48  * <p>
49  * @author Roland Häder<roland@mxchange.org>
50  */
51 @Named ("adminEmployeeController")
52 @RequestScoped
53 public class FinancialsAdminEmployeeWebRequestBean extends BaseFinancialsBean implements FinancialsAdminEmployeeWebRequestController {
54
55         /**
56          * Serial number
57          */
58         private static final long serialVersionUID = 12_886_968_547_360L;
59
60         /**
61          * An event being fired when an employee has been added
62          */
63         @Inject
64         @Any
65         private Event<ObservableEmployeeAddedEvent> addedEmployeeEvent;
66
67         /**
68          * EJB for administrative company employee purposes
69          */
70         @EJB (lookup = "java:global/jfinancials-ejb/adminEmployee!org.mxchange.jcontactsbusiness.model.employee.AdminEmployeeSessionBeanRemote")
71         private AdminEmployeeSessionBeanRemote adminEmployeeBean;
72
73         /**
74          * Administrative list-all-employees controller
75          */
76         @Inject
77         private FinancialsEmployeeListWebViewController adminEmployeeListController;
78
79         /**
80          * Assigned basic data instance
81          */
82         private BasicData employeeBasicData;
83
84         /**
85          * Assigned branch office
86          */
87         private BranchOffice employeeBranchOffice;
88
89         /**
90          * General employee controller
91          */
92         @Inject
93         private FinancialsEmployeeWebRequestController employeeController;
94
95         /**
96          * Department where employee works (alternative to headquarter)
97          */
98         private Department employeeDepartment;
99
100         /**
101          * Employee's business email address
102          */
103         private String employeeEmailAddress;
104
105         /**
106          * Employee's Headquarter data, if he is working there
107          */
108         private Headquarter employeeHeadquarter;
109
110         /**
111          * Employee's staff number
112          */
113         private String employeeNumber;
114
115         /**
116          * Assigned personal data of the employee
117          */
118         private Contact employeePersonalData;
119
120         /**
121          * Employee's phone extension (on top of a "main number")
122          */
123         private Integer employeePhoneExtension;
124
125         /**
126          * Assigned user account
127          */
128         private User employeeUserOwner;
129
130         /**
131          * Mobile number for employee's business mobile
132          */
133         private Long mobileNumber;
134
135         /**
136          * Mobile provider instance for employee's business mobile
137          */
138         private MobileProvider mobileProvider;
139
140         /**
141          * Default constructor
142          */
143         public FinancialsAdminEmployeeWebRequestBean () {
144                 // Call super constructor
145                 super();
146         }
147
148         /**
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.
152          */
153         public void addEmployee () {
154                 // Get instance
155                 final Employable employee = this.createEmployee();
156
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
161                         return;
162                 }
163
164                 // Delcare updated instance
165                 final Employable updatedOffice;
166
167                 try {
168                         // Try to call EJB
169                         updatedOffice = this.adminEmployeeBean.addEmployee(employee);
170                 } catch (final EmployeeAlreadyAddedException ex) {
171                         // Output message
172                         this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED", FacesMessage.SEVERITY_ERROR); //NOI18N
173                         return;
174                 }
175
176                 // Fire event
177                 this.addedEmployeeEvent.fire(new EmployeeAddedEvent(updatedOffice));
178
179                 // Clear this bean
180                 this.clear();
181         }
182
183         /**
184          * Getter for employee's assigned basic company data
185          * <p>
186          * @return Employee's assigned basic company data
187          */
188         public BasicData getEmployeeBasicData () {
189                 return this.employeeBasicData;
190         }
191
192         /**
193          * Setter for employee's assigned basic company data
194          * <p>
195          * @param employeeBasicData Employee's assigned basic company data
196          */
197         public void setEmployeeBasicData (final BasicData employeeBasicData) {
198                 this.employeeBasicData = employeeBasicData;
199         }
200
201         /**
202          * Getter for employee's assigned branch office
203          * <p>
204          * @return Employee's assigned branch office
205          */
206         public BranchOffice getEmployeeBranchOffice () {
207                 return this.employeeBranchOffice;
208         }
209
210         /**
211          * Setter for employee's assigned branch office
212          * <p>
213          * @param employeeBranchOffice Employee's assigned branch office
214          */
215         public void setEmployeeBranchOffice (final BranchOffice employeeBranchOffice) {
216                 this.employeeBranchOffice = employeeBranchOffice;
217         }
218
219         /**
220          * Getter for employee's assigned department
221          * <p>
222          * @return Employee's assigned department
223          */
224         public Department getEmployeeDepartment () {
225                 return this.employeeDepartment;
226         }
227
228         /**
229          * Setter for employee's assigned department
230          * <p>
231          * @param employeeDepartment Employee's assigned department
232          */
233         public void setEmployeeDepartment (final Department employeeDepartment) {
234                 this.employeeDepartment = employeeDepartment;
235         }
236
237         /**
238          * Getter for employee's business email address
239          * <p>
240          * @return Employee's business email address
241          */
242         public String getEmployeeEmailAddress () {
243                 return this.employeeEmailAddress;
244         }
245
246         /**
247          * Setter for employee's business email address
248          * <p>
249          * @param employeeEmailAddress Employee's business email address
250          */
251         public void setEmployeeEmailAddress (final String employeeEmailAddress) {
252                 this.employeeEmailAddress = employeeEmailAddress;
253         }
254
255         /**
256          * Getter for employee's assigned headquarter (if no branch office)
257          * <p>
258          * @return Employee's assigned headquarter
259          */
260         public Headquarter getEmployeeHeadquarter () {
261                 return this.employeeHeadquarter;
262         }
263
264         /**
265          * Setter for employee's assigned headquarter (if no branch office)
266          * <p>
267          * @param employeeHeadquarter Employee's assigned headquarter
268          */
269         public void setEmployeeHeadquarter (final Headquarter employeeHeadquarter) {
270                 this.employeeHeadquarter = employeeHeadquarter;
271         }
272
273         /**
274          * Getter for employee's staff number
275          * <p>
276          * @return Employee's staff number
277          */
278         public String getEmployeeNumber () {
279                 return this.employeeNumber;
280         }
281
282         /**
283          * Setter for employee's staff number
284          * <p>
285          * @param employeeNumber Employee's staff number
286          */
287         public void setEmployeeNumber (final String employeeNumber) {
288                 this.employeeNumber = employeeNumber;
289         }
290
291         /**
292          * Getter for employee's personal contact data
293          * <p>
294          * @return Employee's personal contact data
295          */
296         public Contact getEmployeePersonalData () {
297                 return this.employeePersonalData;
298         }
299
300         /**
301          * Setter for employee's personal contact data
302          * <p>
303          * @param employeePersonalData Employee's personal contact data
304          */
305         public void setEmployeePersonalData (final Contact employeePersonalData) {
306                 this.employeePersonalData = employeePersonalData;
307         }
308
309         /**
310          * Getter for employee's phone extension
311          * <p>
312          * @return Employee's phone extension
313          */
314         public Integer getEmployeePhoneExtension () {
315                 return this.employeePhoneExtension;
316         }
317
318         /**
319          * Getter for employee's phone extension
320          * <p>
321          * @param employeePhoneExtension Employee's phone extension
322          */
323         public void setEmployeePhoneExtension (final Integer employeePhoneExtension) {
324                 this.employeePhoneExtension = employeePhoneExtension;
325         }
326
327         /**
328          * Getter for employee's user "owner"
329          * <p>
330          * @return Employee's user "owner"
331          */
332         public User getEmployeeUserOwner () {
333                 return this.employeeUserOwner;
334         }
335
336         /**
337          * Setter for employee's user "owner"
338          * <p>
339          * @param employeeUserOwner Employee's user "owner"
340          */
341         public void setEmployeeUserOwner (final User employeeUserOwner) {
342                 this.employeeUserOwner = employeeUserOwner;
343         }
344
345         /**
346          * Getter for mobile number
347          * <p>
348          * @return Mobile number
349          */
350         public Long getMobileNumber () {
351                 return this.mobileNumber;
352         }
353
354         /**
355          * Setter for mobile number
356          * <p>
357          * @param mobileNumber Mobile number
358          */
359         public void setMobileNumber (final Long mobileNumber) {
360                 this.mobileNumber = mobileNumber;
361         }
362
363         /**
364          * Getter for mobile provider instance
365          * <p>
366          * @return Mobile provider instance
367          */
368         public MobileProvider getMobileProvider () {
369                 return this.mobileProvider;
370         }
371
372         /**
373          * Setter for mobile provider instance
374          * <p>
375          * @param mobileProvider Mobile provider instance
376          */
377         public void setMobileProvider (final MobileProvider mobileProvider) {
378                 this.mobileProvider = mobileProvider;
379         }
380
381         /**
382          * Clears all bean fields
383          */
384         private void clear () {
385                 // Clear all fields
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);
395         }
396
397         /**
398          * Creates employee instance with all data from this bean
399          * <p>
400          * @return Employee instance
401          */
402         private Employable createEmployee () {
403                 // Init instance
404                 final Employable employee = new BusinessEmployee(this.getEmployeeBasicData(), this.getEmployeeNumber(), this.getEmployeePersonalData());
405
406                 // Set all othewr data
407                 employee.setEmployeeBranchOffice(this.getEmployeeBranchOffice());
408                 employee.setEmployeeDepartment(this.getEmployeeDepartment());
409                 employee.setEmployeeHeadquarter(this.getEmployeeHeadquarter());
410                 employee.setEmployeePhoneExtension(this.getEmployeePhoneExtension());
411                 // @TODO employee.setEmployeePosition(this.getEmployeePosition());
412                 employee.setEmployeeUserOwner(this.getEmployeeUserOwner());
413
414                 // Is mobile provider and number set?
415                 if ((this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
416                         // Init mobile number instance
417                         final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
418
419                         // Set it in employee
420                         employee.setEmployeeMobileNumber(number);
421                 }
422
423                 // Return it
424                 return employee;
425         }
426
427         /**
428          * Checks whether given employee is already added
429          * <p>
430          * @param employee Employee to be checked
431          * <p>
432          * @return Whether the employee has already been added
433          */
434         private boolean isEmployeeCreatedByRequiredData (final Employable employee) {
435                 // Default is not found
436                 boolean isFound = false;
437
438                 // Check all employees
439                 for (final Employable otherEmployee : this.adminEmployeeListController.getAllEmployees()) {
440                         // Is same found?
441                         if (Employees.isSameEmployeeFound(employee, otherEmployee)) {
442                                 // Okay, found it
443                                 isFound = true;
444                                 break;
445                         }
446                 }
447
448                 // Return it
449                 return isFound;
450         }
451
452 }