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