2 * Copyright (C) 2017, 2018 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jcontactsbusiness.model.employee;
19 import java.text.MessageFormat;
20 import java.util.Date;
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;
36 * A stateless bean for administrative purposes for company employees.
38 * @author Roland Häder<roland@mxchange.org>
40 @Stateless (name = "adminEmployee", description = "An administrative statless bean for handling company employees")
41 public class AddressbookAdminEmployeeSessionBean extends BaseAddressbookEnterpriseBean implements AdminEmployeeSessionBeanRemote {
46 private static final long serialVersionUID = 26_458_796_703_761L;
49 * EJB for general company employee purposes
51 @EJB (lookup = "java:global/addressbook-ejb/employee!org.mxchange.jcontactsbusiness.model.employee.EmployeeSessionBeanRemote")
52 private EmployeeSessionBeanRemote employeeBean;
57 public AddressbookAdminEmployeeSessionBean () {
62 public Employable addEmployee (final Employable employee) throws EmployeeAlreadyAddedException {
64 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addEmployee(): employee={1} - CALLED!", this.getClass().getSimpleName(), employee)); //NOI18N
67 if (null == employee) {
69 throw new NullPointerException("employee is null"); //NOI18N
70 } else if (employee.getEmployeeId() != null) {
72 throw new IllegalArgumentException(MessageFormat.format("employee.employeeId={0} is unexpected.", employee.getEmployeeId())); //NOI18N
73 } else if (employee.getEmployeeBasicData() == null) {
75 throw new NullPointerException("employee.employeeCompany is null"); //NOI18N
76 } else if (employee.getEmployeeBasicData().getBasicDataId() == null) {
78 throw new NullPointerException("employee.employeeCompany.basicDataId is null"); //NOI18N
79 } else if (employee.getEmployeeBasicData().getBasicDataId() < 1) {
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) {
87 throw new NullPointerException("employee.employeeBranchOffice.branchId is null"); //NOI18N
88 } else if (employee.getEmployeeBranchOffice() != null && employee.getEmployeeBranchOffice().getBranchId() < 1) {
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) {
93 throw new NullPointerException("employee.employeeDepartment.departmentId is null"); //NOI18N
94 } else if (employee.getEmployeeDepartment() != null && employee.getEmployeeDepartment().getDepartmentId() < 1) {
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) {
99 throw new NullPointerException("employee.employeeHeadquarter.headquarterId is null"); //NOI18N
100 } else if (employee.getEmployeeHeadquarter() != null && employee.getEmployeeHeadquarter().getHeadquarterId() < 1) {
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)) {
105 throw new NullPointerException("employee.employeePersonalData and employee.employeeNumber are null"); //NOI18N
106 } else if ((employee.getEmployeePersonalData() != null) && (employee.getEmployeePersonalData().getContactId() == null)) {
108 throw new NullPointerException("employee.employeePersonalData.contactId is null"); //NOI18N
109 } else if ((employee.getEmployeePersonalData() != null) && (employee.getEmployeePersonalData().getContactId() < 1)) {
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())) {
114 throw new IllegalArgumentException("employee.employeeNumber is empty"); //NOI18N
115 } else if ((employee.getEmployeeMobileNumber() != null) && (employee.getEmployeeMobileNumber().getMobileId() != null)) {
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)) {
120 throw new NullPointerException("employee.employeePersonalData.contactId is null"); //NOI18N
121 } else if ((employee.getEmployeePersonalData() != null) && (employee.getEmployeePersonalData().getContactId() < 1)) {
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)) {
126 throw new NullPointerException("employee.employeePosition.jobPositionId is null"); //NOI18N
127 } else if ((employee.getEmployeeJobPosition() != null) && (employee.getEmployeeJobPosition().getJobPositionId() < 1)) {
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)) {
132 throw new NullPointerException("employee.employeeUserOwner.userId is null"); //NOI18N
133 } else if ((employee.getEmployeeUserOwner() != null) && (employee.getEmployeeUserOwner().getUserId() < 1)) {
135 throw new IllegalArgumentException(MessageFormat.format("employee.employeeUserOwner.userId={0} is unexpected.", employee.getEmployeeUserOwner().getUserId())); //NOI18N
136 } else if (this.isEmployeeAdded(employee)) {
138 throw new EmployeeAlreadyAddedException();
141 // Make company (basic data) managed
142 employee.setEmployeeBasicData(this.createManaged(employee.getEmployeeBasicData()));
144 // Is branch office set?
145 if (employee.getEmployeeBranchOffice() instanceof BranchOffice) {
146 // Then make it managed
147 final BranchOffice branchOffice = this.createManaged(employee.getEmployeeBranchOffice());
150 employee.setEmployeeBranchOffice(branchOffice);
153 // Is department set?
154 if (employee.getEmployeeDepartment() instanceof Department) {
155 // Then make it managed
156 final Department department = this.createManaged(employee.getEmployeeDepartment());
159 employee.setEmployeeDepartment(department);
162 // Is headquarter set?
163 if (employee.getEmployeeHeadquarter() instanceof Headquarter) {
164 // Then make it managed
165 final Headquarter headquarter = this.createManaged(employee.getEmployeeHeadquarter());
168 employee.setEmployeeHeadquarter(headquarter);
171 // Is personal data set?
172 if (employee.getEmployeePersonalData() instanceof Contact) {
173 // Then make it managed
174 final Contact contact = this.createManaged(employee.getEmployeePersonalData());
177 employee.setEmployeePersonalData(contact);
180 // Is headquarter set?
181 if (employee.getEmployeeJobPosition() instanceof HireableJobPosition) {
182 // Then make it managed
183 final HireableJobPosition jobPosition = this.createManaged(employee.getEmployeeJobPosition());
186 employee.setEmployeeJobPosition(jobPosition);
189 // Is headquarter set?
190 if (employee.getEmployeeUserOwner() instanceof User) {
191 // Then make it managed
192 final User user = this.createManaged(employee.getEmployeeUserOwner());
195 employee.setEmployeeUserOwner(user);
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());
203 // Make mobile provider managed
204 final MobileProvider mobileProvider = this.createManaged(employee.getEmployeeMobileNumber().getMobileProvider());
207 employee.getEmployeeMobileNumber().setMobileProvider(mobileProvider);
210 // Set created timestamp
211 employee.setEmployeeEntryCreated(new Date());
214 this.getEntityManager().persist(employee);
217 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addEmployee(): employee.employeeId={1} - EXIT!", this.getClass().getSimpleName(), employee.getEmployeeId())); //NOI18N
224 * Checks whether employee is already added
226 * @param employee Employee to be checked
228 * @return Whether employee is already added
230 private boolean isEmployeeAdded (final Employable employee) {
231 // Default is not added
232 boolean isFound = false;
235 for (final Employable otherEmployee : this.employeeBean.fetchAllEmployees()) {
237 if (EmployeeUtils.isSameEmployeeFound(employee, otherEmployee)) {