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.department;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
23 import javax.ejb.Stateless;
24 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentAlreadyAddedException;
25 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
26 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
27 import org.mxchange.jcontactsbusiness.model.employee.Employable;
28 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
29 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
30 import org.mxchange.jusercore.model.user.User;
33 * A stateless session bean for administrative department purposes
35 * @author Roland Häder<roland@mxchange.org>
37 @Stateless (name = "adminDepartment", description = "An administrative statless bean for handling department data (all)")
38 public class JobsAdminDepartmentSessionBean extends BaseJobsEnterpriseBean implements AdminDepartmentSessionBeanRemote {
43 private static final long serialVersionUID = 58_467_386_571_701L;
46 * General department bean
48 @EJB (lookup = "java:global/jjobs-ejb/department!org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote")
49 private DepartmentSessionBeanRemote departmentBean;
54 public JobsAdminDepartmentSessionBean () {
55 // Call super constructor
60 public Department addDepartment (final Department department) throws DepartmentAlreadyAddedException {
62 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addDepartment(): department={1} - CALLED!", this.getClass().getSimpleName(), department)); //NOI18N
65 if (null == department) {
67 throw new NullPointerException("department is null"); //NOI18N
68 } else if (department.getDepartmentId() instanceof Long) {
70 throw new IllegalArgumentException("department.departmentId should not be set."); //NOI18N
71 } else if (department.getDepartmentCompany() == null) {
73 throw new NullPointerException("department.departmentCompany is null"); //NOI18N
74 } else if (department.getDepartmentCompany().getBasicDataId() == null) {
76 throw new NullPointerException("department.departmentCompany.basicDataId is null"); //NOI18N
77 } else if (department.getDepartmentCompany().getBasicDataId() < 1) {
79 throw new IllegalArgumentException(MessageFormat.format("department.departmentCompany.basicDataId={0} is invalid", department.getDepartmentCompany().getBasicDataId())); //NOI18N
80 } else if (department.getDepartmentI18nKey() == null) {
82 throw new NullPointerException("department.departmentName is null"); //NOI18N
83 } else if (department.getDepartmentI18nKey().isEmpty()) {
85 throw new NullPointerException("department.departmentName is empty"); //NOI18N
86 } else if (this.isDepartmentFound(department)) {
87 // Already added, abort here
88 throw new DepartmentAlreadyAddedException(department);
91 // Set created timestamp
92 department.setDepartmentCreated(new Date());
94 // Get managed basic data instance
95 final BasicData managedBasicData = this.createManaged(department.getDepartmentCompany());
98 department.setDepartmentCompany(managedBasicData);
100 // Is branch office set?
101 if (department.getDepartmentBranchOffice() instanceof BranchOffice) {
102 // Get managed branch office
103 final BranchOffice managedBranchOffice = this.createManaged(department.getDepartmentBranchOffice());
106 department.setDepartmentBranchOffice(managedBranchOffice);
109 // Is headquarter set?
110 if (department.getDepartmentHeadquarter() instanceof Headquarter) {
111 // Get managed headquarter
112 final Headquarter managedHeadquarter = this.createManaged(department.getDepartmentHeadquarter());
115 department.setDepartmentHeadquarter(managedHeadquarter);
118 // Is lead employee set?
119 if (department.getDepartmentLead()instanceof Employable) {
120 // Get managed lead employee
121 final Employable managedEmployee = this.createManaged(department.getDepartmentLead());
124 department.setDepartmentLead(managedEmployee);
127 // Is "owning" user set?
128 if (department.getDepartmentUserOwner()instanceof User) {
130 final User managedUser = this.createManaged(department.getDepartmentUserOwner());
133 department.setDepartmentUserOwner(managedUser);
137 this.getEntityManager().persist(department);
140 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addDepartment(): department.departmentId={1} - EXIT!", this.getClass().getSimpleName(), department.getDepartmentId())); //NOI18N
142 // Return updated instance
147 * Checks if given department's address is already persisted. The whole
148 * (persisted) list is being loaded and each address is being matched
149 * against the given department's address.
151 * @param department Branch office being checked
153 * @return Whether it has been found
155 private boolean isDepartmentFound (final Department department) {
157 final List<Department> departments = this.departmentBean.allDepartments();
159 // Default is not found
160 boolean isFound = false;
162 // Check all single addresses
163 for (final Department dep : departments) {
164 // Is the same address found?
165 if (Departments.isSameDepartment(dep, department)) {