]> git.mxchange.org Git - addressbook-ejb.git/blob - src/java/org/mxchange/jcontactsbusiness/model/employee/AddressbookAdminEmployeeSessionBean.java
Updated copyright year
[addressbook-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / employee / AddressbookAdminEmployeeSessionBean.java
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.jcontactsbusiness.model.employee;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import javax.ejb.EJB;
22 import javax.ejb.Stateless;
23 import org.mxchange.addressbook.enterprise.BaseAddressbookEnterpriseBean;
24 import org.mxchange.jcontacts.model.contact.Contact;
25 import org.mxchange.jcontactsbusiness.exceptions.employee.EmployeeAlreadyAddedException;
26 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
27 import org.mxchange.jcontactsbusiness.model.department.Department;
28 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
29 import org.mxchange.jcontactsbusiness.model.jobposition.HireableJobPosition;
30 import org.mxchange.jcontactsbusiness.model.utils.EmployeeUtils;
31 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
32 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
33 import org.mxchange.jusercore.model.user.User;
34
35 /**
36  * A stateless bean for administrative purposes for company employees.
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @Stateless (name = "adminEmployee", description = "An administrative statless bean for handling company employees")
41 public class AddressbookAdminEmployeeSessionBean extends BaseAddressbookEnterpriseBean implements AdminEmployeeSessionBeanRemote {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 26_458_796_703_761L;
47
48         /**
49          * EJB for general company employee purposes
50          */
51         @EJB (lookup = "java:global/addressbook-ejb/employee!org.mxchange.jcontactsbusiness.model.employee.EmployeeSessionBeanRemote")
52         private EmployeeSessionBeanRemote employeeBean;
53
54         /**
55          * Default constructor
56          */
57         public AddressbookAdminEmployeeSessionBean () {
58                 super();
59         }
60
61         @Override
62         public Employable addEmployee (final Employable employee) throws EmployeeAlreadyAddedException {
63                 // Trace message
64                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addEmployee(): employee={1} - CALLED!", this.getClass().getSimpleName(), employee)); //NOI18N
65
66                 // Validate parameter
67                 if (null == employee) {
68                         // Throw NPE
69                         throw new NullPointerException("employee is null"); //NOI18N
70                 } else if (employee.getEmployeeId() != null) {
71                         // Throw IAE
72                         throw new IllegalArgumentException(MessageFormat.format("employee.employeeId={0} is unexpected.", employee.getEmployeeId())); //NOI18N
73                 } else if (employee.getEmployeeBasicData() == null) {
74                         // Throw NPE
75                         throw new NullPointerException("employee.employeeCompany is null"); //NOI18N
76                 } else if (employee.getEmployeeBasicData().getBasicDataId() == null) {
77                         // Throw it again
78                         throw new NullPointerException("employee.employeeCompany.basicDataId is null"); //NOI18N
79                 } else if (employee.getEmployeeBasicData().getBasicDataId() < 1) {
80                         // Throw IAE
81                         throw new IllegalArgumentException(MessageFormat.format("employee.employeeCompany.basicDataId={0} is invalid.", employee.getEmployeeBasicData().getBasicDataId())); //NOI18N
82                 } else if (employee.getEmployeeBranchOffice() == null && employee.getEmployeeDepartment() == null && employee.getEmployeeHeadquarter() == null) {
83                         // At least one must be set
84                         throw new NullPointerException("employee.employeeBranchOffice, employee.employeeDepartment and employee.employeeHeadquarter is null"); //NOI18N
85                 } else if (employee.getEmployeeBranchOffice() != null && employee.getEmployeeBranchOffice().getBranchId() == null) {
86                         // Throw NPE
87                         throw new NullPointerException("employee.employeeBranchOffice.branchId is null"); //NOI18N
88                 } else if (employee.getEmployeeBranchOffice() != null && employee.getEmployeeBranchOffice().getBranchId() < 1) {
89                         // Throw IAE
90                         throw new IllegalArgumentException(MessageFormat.format("employee.employeeBranchOffice.branchId={0} is invalid.", employee.getEmployeeBranchOffice().getBranchId())); //NOI18N
91                 } else if (employee.getEmployeeDepartment() != null && employee.getEmployeeDepartment().getDepartmentId() == null) {
92                         // Throw NPE
93                         throw new NullPointerException("employee.employeeDepartment.departmentId is null"); //NOI18N
94                 } else if (employee.getEmployeeDepartment() != null && employee.getEmployeeDepartment().getDepartmentId() < 1) {
95                         // Throw IAE
96                         throw new IllegalArgumentException(MessageFormat.format("employee.employeeDepartment.departmentId={0} is invalid.", employee.getEmployeeDepartment().getDepartmentId())); //NOI18N
97                 } else if (employee.getEmployeeHeadquarter() != null && employee.getEmployeeHeadquarter().getHeadquarterId() == null) {
98                         // Throw NPE
99                         throw new NullPointerException("employee.employeeHeadquarter.headquarterId is null"); //NOI18N
100                 } else if (employee.getEmployeeHeadquarter() != null && employee.getEmployeeHeadquarter().getHeadquarterId() < 1) {
101                         // Throw IAE
102                         throw new IllegalArgumentException(MessageFormat.format("employee.employeeHeadquarter.headquarterId={0} is invalid.", employee.getEmployeeHeadquarter().getHeadquarterId())); //NOI18N
103                 } else if ((employee.getEmployeePersonalData() == null) && (employee.getEmployeeNumber() == null)) {
104                         // Both are null
105                         throw new NullPointerException("employee.employeePersonalData and employee.employeeNumber are null"); //NOI18N
106                 } else if ((employee.getEmployeePersonalData() != null) && (employee.getEmployeePersonalData().getContactId() == null)) {
107                         // Throw NPE
108                         throw new NullPointerException("employee.employeePersonalData.contactId is null"); //NOI18N
109                 } else if ((employee.getEmployeePersonalData() != null) && (employee.getEmployeePersonalData().getContactId() < 1)) {
110                         // Throw IAE
111                         throw new IllegalArgumentException(MessageFormat.format("employee.employeePersonalData.contactId={0} is invalid.", employee.getEmployeePersonalData().getContactId())); //NOI18N
112                 } else if ((employee.getEmployeeNumber() != null) && (employee.getEmployeeNumber().isEmpty())) {
113                         // Throw it again
114                         throw new IllegalArgumentException("employee.employeeNumber is empty"); //NOI18N
115                 } else if ((employee.getEmployeeMobileNumber() != null) && (employee.getEmployeeMobileNumber().getMobileId() != null)) {
116                         // Throw once more
117                         throw new IllegalArgumentException(MessageFormat.format("employee.employeeMobileNumber.phoneId={0} is unexpected.", employee.getEmployeeMobileNumber().getMobileId())); //NOI18N
118                 } else if ((employee.getEmployeePersonalData() != null) && (employee.getEmployeePersonalData().getContactId() == null)) {
119                         // Throw NPE
120                         throw new NullPointerException("employee.employeePersonalData.contactId is null"); //NOI18N
121                 } else if ((employee.getEmployeePersonalData() != null) && (employee.getEmployeePersonalData().getContactId() < 1)) {
122                         // Throw IAE
123                         throw new IllegalArgumentException(MessageFormat.format("employee.employeePersonalData.contactId={0} is unexpected.", employee.getEmployeePersonalData().getContactId())); //NOI18N
124                 } else if ((employee.getEmployeeJobPosition() != null) && (employee.getEmployeeJobPosition().getJobPositionId() == null)) {
125                         // Throw NPE
126                         throw new NullPointerException("employee.employeePosition.jobPositionId is null"); //NOI18N
127                 } else if ((employee.getEmployeeJobPosition() != null) && (employee.getEmployeeJobPosition().getJobPositionId() < 1)) {
128                         // Throw IAE
129                         throw new IllegalArgumentException(MessageFormat.format("employee.employeePosition.jobPositionId={0} is unexpected.", employee.getEmployeeJobPosition().getJobPositionId())); //NOI18N
130                 } else if ((employee.getEmployeeUserOwner() != null) && (employee.getEmployeeUserOwner().getUserId() == null)) {
131                         // Throw NPE
132                         throw new NullPointerException("employee.employeeUserOwner.userId is null"); //NOI18N
133                 } else if ((employee.getEmployeeUserOwner() != null) && (employee.getEmployeeUserOwner().getUserId() < 1)) {
134                         // Throw IAE
135                         throw new IllegalArgumentException(MessageFormat.format("employee.employeeUserOwner.userId={0} is unexpected.", employee.getEmployeeUserOwner().getUserId())); //NOI18N
136                 } else if (this.isEmployeeAdded(employee)) {
137                         // Throw exception
138                         throw new EmployeeAlreadyAddedException();
139                 }
140
141                 // Make company (basic data) managed
142                 employee.setEmployeeBasicData(this.createManaged(employee.getEmployeeBasicData()));
143
144                 // Is branch office set?
145                 if (employee.getEmployeeBranchOffice() instanceof BranchOffice) {
146                         // Then make it managed
147                         final BranchOffice branchOffice = this.createManaged(employee.getEmployeeBranchOffice());
148
149                         // Set it back
150                         employee.setEmployeeBranchOffice(branchOffice);
151                 }
152
153                 // Is department set?
154                 if (employee.getEmployeeDepartment() instanceof Department) {
155                         // Then make it managed
156                         final Department department = this.createManaged(employee.getEmployeeDepartment());
157
158                         // Set it back
159                         employee.setEmployeeDepartment(department);
160                 }
161
162                 // Is headquarter set?
163                 if (employee.getEmployeeHeadquarter() instanceof Headquarter) {
164                         // Then make it managed
165                         final Headquarter headquarter = this.createManaged(employee.getEmployeeHeadquarter());
166
167                         // Set it back
168                         employee.setEmployeeHeadquarter(headquarter);
169                 }
170
171                 // Is personal data set?
172                 if (employee.getEmployeePersonalData() instanceof Contact) {
173                         // Then make it managed
174                         final Contact contact = this.createManaged(employee.getEmployeePersonalData());
175
176                         // Set it back
177                         employee.setEmployeePersonalData(contact);
178                 }
179
180                 // Is headquarter set?
181                 if (employee.getEmployeeJobPosition() instanceof HireableJobPosition) {
182                         // Then make it managed
183                         final HireableJobPosition jobPosition = this.createManaged(employee.getEmployeeJobPosition());
184
185                         // Set it back
186                         employee.setEmployeeJobPosition(jobPosition);
187                 }
188
189                 // Is headquarter set?
190                 if (employee.getEmployeeUserOwner() instanceof User) {
191                         // Then make it managed
192                         final User user = this.createManaged(employee.getEmployeeUserOwner());
193
194                         // Set it back
195                         employee.setEmployeeUserOwner(user);
196                 }
197
198                 // Is mobile number set?
199                 if (employee.getEmployeeMobileNumber() instanceof DialableMobileNumber) {
200                         // Yes, then set created there as well
201                         employee.getEmployeeMobileNumber().setMobileEntryCreated(new Date());
202
203                         // Make mobile provider managed
204                         final MobileProvider mobileProvider = this.createManaged(employee.getEmployeeMobileNumber().getMobileProvider());
205
206                         // Set it back
207                         employee.getEmployeeMobileNumber().setMobileProvider(mobileProvider);
208                 }
209
210                 // Set created timestamp
211                 employee.setEmployeeEntryCreated(new Date());
212
213                 // Persist it
214                 this.getEntityManager().persist(employee);
215
216                 // Trace message
217                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addEmployee(): employee.employeeId={1} - EXIT!", this.getClass().getSimpleName(), employee.getEmployeeId())); //NOI18N
218
219                 // Return it
220                 return employee;
221         }
222
223         /**
224          * Checks whether employee is already added
225          * <p>
226          * @param employee Employee to be checked
227          * <p>
228          * @return Whether employee is already added
229          */
230         private boolean isEmployeeAdded (final Employable employee) {
231                 // Default is not added
232                 boolean isFound = false;
233
234                 // Check all entries
235                 for (final Employable otherEmployee : this.employeeBean.fetchAllEmployees()) {
236                         // Is same found?
237                         if (EmployeeUtils.isSameEmployeeFound(employee, otherEmployee)) {
238                                 // Found it
239                                 isFound = true;
240                                 break;
241                         }
242                 }
243
244                 // Return flag
245                 return isFound;
246         }
247
248 }