]> git.mxchange.org Git - jfinancials-war.git/blob
7da132bfbfc55667e1e1bef896b378b9a3763a23
[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          */
145         public void addEmployee () {
146                 // Get instance
147                 final Employable employee = this.createEmployee();
148
149                 // Is the branch office not created yet?
150                 if (this.isEmployeeCreatedByRequiredData(employee)) {
151                         // Then show proper faces message
152                         this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED"); //NOI18N
153                         return;
154                 }
155
156                 // Delcare updated instance
157                 final Employable updatedOffice;
158
159                 try {
160                         // Try to call EJB
161                         updatedOffice = this.adminEmployeeBean.addEmployee(employee);
162                 } catch (final EmployeeAlreadyAddedException ex) {
163                         // Output message
164                         this.showFacesMessage("form-admin-add-employee:employeeNumber", "ADMIN_EMPLOYEE_ALREADY_CREATED"); //NOI18N
165                         return;
166                 }
167
168                 // Fire event
169                 this.addedEmployeeEvent.fire(new EmployeeAddedEvent(updatedOffice));
170
171                 // Clear this bean
172                 this.clear();
173         }
174
175         /**
176          * Getter for employee's assigned basic company data
177          * <p>
178          * @return Employee's assigned basic company data
179          */
180         public BasicData getEmployeeBasicData () {
181                 return this.employeeBasicData;
182         }
183
184         /**
185          * Setter for employee's assigned basic company data
186          * <p>
187          * @param employeeBasicData Employee's assigned basic company data
188          */
189         public void setEmployeeBasicData (final BasicData employeeBasicData) {
190                 this.employeeBasicData = employeeBasicData;
191         }
192
193         /**
194          * Getter for employee's assigned branch office
195          * <p>
196          * @return Employee's assigned branch office
197          */
198         public BranchOffice getEmployeeBranchOffice () {
199                 return this.employeeBranchOffice;
200         }
201
202         /**
203          * Setter for employee's assigned branch office
204          * <p>
205          * @param employeeBranchOffice Employee's assigned branch office
206          */
207         public void setEmployeeBranchOffice (final BranchOffice employeeBranchOffice) {
208                 this.employeeBranchOffice = employeeBranchOffice;
209         }
210
211         /**
212          * Getter for employee's assigned department
213          * <p>
214          * @return Employee's assigned department
215          */
216         public Department getEmployeeDepartment () {
217                 return this.employeeDepartment;
218         }
219
220         /**
221          * Setter for employee's assigned department
222          * <p>
223          * @param employeeDepartment Employee's assigned department
224          */
225         public void setEmployeeDepartment (final Department employeeDepartment) {
226                 this.employeeDepartment = employeeDepartment;
227         }
228
229         /**
230          * Getter for employee's business email address
231          * <p>
232          * @return Employee's business email address
233          */
234         public String getEmployeeEmailAddress () {
235                 return this.employeeEmailAddress;
236         }
237
238         /**
239          * Setter for employee's business email address
240          * <p>
241          * @param employeeEmailAddress Employee's business email address
242          */
243         public void setEmployeeEmailAddress (final String employeeEmailAddress) {
244                 this.employeeEmailAddress = employeeEmailAddress;
245         }
246
247         /**
248          * Getter for employee's assigned headquarter (if no branch office)
249          * <p>
250          * @return Employee's assigned headquarter
251          */
252         public Headquarter getEmployeeHeadquarter () {
253                 return this.employeeHeadquarter;
254         }
255
256         /**
257          * Setter for employee's assigned headquarter (if no branch office)
258          * <p>
259          * @param employeeHeadquarter Employee's assigned headquarter
260          */
261         public void setEmployeeHeadquarter (final Headquarter employeeHeadquarter) {
262                 this.employeeHeadquarter = employeeHeadquarter;
263         }
264
265         /**
266          * Getter for employee's staff number
267          * <p>
268          * @return Employee's staff number
269          */
270         public String getEmployeeNumber () {
271                 return this.employeeNumber;
272         }
273
274         /**
275          * Setter for employee's staff number
276          * <p>
277          * @param employeeNumber Employee's staff number
278          */
279         public void setEmployeeNumber (final String employeeNumber) {
280                 this.employeeNumber = employeeNumber;
281         }
282
283         /**
284          * Getter for employee's personal contact data
285          * <p>
286          * @return Employee's personal contact data
287          */
288         public Contact getEmployeePersonalData () {
289                 return this.employeePersonalData;
290         }
291
292         /**
293          * Setter for employee's personal contact data
294          * <p>
295          * @param employeePersonalData Employee's personal contact data
296          */
297         public void setEmployeePersonalData (final Contact employeePersonalData) {
298                 this.employeePersonalData = employeePersonalData;
299         }
300
301         /**
302          * Getter for employee's phone extension
303          * <p>
304          * @return Employee's phone extension
305          */
306         public Integer getEmployeePhoneExtension () {
307                 return this.employeePhoneExtension;
308         }
309
310         /**
311          * Getter for employee's phone extension
312          * <p>
313          * @param employeePhoneExtension Employee's phone extension
314          */
315         public void setEmployeePhoneExtension (final Integer employeePhoneExtension) {
316                 this.employeePhoneExtension = employeePhoneExtension;
317         }
318
319         /**
320          * Getter for employee's user "owner"
321          * <p>
322          * @return Employee's user "owner"
323          */
324         public User getEmployeeUserOwner () {
325                 return this.employeeUserOwner;
326         }
327
328         /**
329          * Setter for employee's user "owner"
330          * <p>
331          * @param employeeUserOwner Employee's user "owner"
332          */
333         public void setEmployeeUserOwner (final User employeeUserOwner) {
334                 this.employeeUserOwner = employeeUserOwner;
335         }
336
337         /**
338          * Getter for mobile number
339          * <p>
340          * @return Mobile number
341          */
342         public Long getMobileNumber () {
343                 return this.mobileNumber;
344         }
345
346         /**
347          * Setter for mobile number
348          * <p>
349          * @param mobileNumber Mobile number
350          */
351         public void setMobileNumber (final Long mobileNumber) {
352                 this.mobileNumber = mobileNumber;
353         }
354
355         /**
356          * Getter for mobile provider instance
357          * <p>
358          * @return Mobile provider instance
359          */
360         public MobileProvider getMobileProvider () {
361                 return this.mobileProvider;
362         }
363
364         /**
365          * Setter for mobile provider instance
366          * <p>
367          * @param mobileProvider Mobile provider instance
368          */
369         public void setMobileProvider (final MobileProvider mobileProvider) {
370                 this.mobileProvider = mobileProvider;
371         }
372
373         /**
374          * Clears all bean fields
375          */
376         private void clear () {
377                 // Clear all fields
378                 this.setEmployeeBranchOffice(null);
379                 this.setEmployeeBasicData(null);
380                 this.setEmployeeDepartment(null);
381                 this.setEmployeeEmailAddress(null);
382                 this.setEmployeeHeadquarter(null);
383                 this.setEmployeePersonalData(null);
384                 this.setEmployeePhoneExtension(null);
385                 this.setEmployeeNumber(null);
386                 this.setEmployeeUserOwner(null);
387         }
388
389         /**
390          * Creates employee instance with all data from this bean
391          * <p>
392          * @return Employee instance
393          */
394         private Employable createEmployee () {
395                 // Init instance
396                 final Employable employee = new BusinessEmployee(this.getEmployeeBasicData(), this.getEmployeeNumber(), this.getEmployeePersonalData());
397
398                 // Set all othewr data
399                 employee.setEmployeeBranchOffice(this.getEmployeeBranchOffice());
400                 employee.setEmployeeDepartment(this.getEmployeeDepartment());
401                 employee.setEmployeeHeadquarter(this.getEmployeeHeadquarter());
402                 employee.setEmployeePhoneExtension(this.getEmployeePhoneExtension());
403                 // @TODO employee.setEmployeePosition(this.getEmployeePosition());
404                 employee.setEmployeeUserOwner(this.getEmployeeUserOwner());
405
406                 // Is mobile provider and number set?
407                 if ((this.getMobileProvider() instanceof MobileProvider) && (this.getMobileNumber() != null) && (this.getMobileNumber() > 0)) {
408                         // Init mobile number instance
409                         final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
410
411                         // Set it in employee
412                         employee.setEmployeeMobileNumber(number);
413                 }
414
415                 // Return it
416                 return employee;
417         }
418
419         /**
420          * Checks whether given employee is already added
421          * <p>
422          * @param employee Employee to be checked
423          * <p>
424          * @return Whether the employee has already been added
425          */
426         private boolean isEmployeeCreatedByRequiredData (final Employable employee) {
427                 // Default is not found
428                 boolean isFound = false;
429
430                 // Check all employees
431                 for (final Employable otherEmployee : this.employeeController.allEmployees()) {
432                         // Is same found?
433                         if (Employees.isSameEmployeeFound(employee, otherEmployee)) {
434                                 // Okay, found it
435                                 isFound = true;
436                                 break;
437                         }
438                 }
439
440                 // Return it
441                 return isFound;
442         }
443
444 }