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