2 * Copyright (C) 2017 - 2024 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.jcontacts.model.contact.Contact;
25 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentAlreadyAddedException;
26 import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentNotFoundException;
27 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
28 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
29 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
30 import org.mxchange.jcontactsbusiness.model.utils.DepartmentUtils;
31 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
32 import org.mxchange.jusercore.model.user.User;
35 * A stateless session bean for administrative department purposes
37 * @author Roland Häder<roland@mxchange.org>
39 @Stateless (name = "adminDepartment", description = "An administrative statless bean for handling department data (all)")
40 public class JobsAdminDepartmentSessionBean extends BaseJobsEnterpriseBean implements AdminDepartmentSessionBeanRemote {
45 private static final long serialVersionUID = 58_467_386_571_703L;
48 * General department bean
50 @EJB (lookup = "java:global/jjobs-ejb/department!org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote")
51 private DepartmentSessionBeanRemote departmentBean;
56 public JobsAdminDepartmentSessionBean () {
57 // Call super constructor
62 public Department addDepartment (final Department department) throws DepartmentAlreadyAddedException {
64 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addDepartment(): department={1} - CALLED!", this.getClass().getSimpleName(), department)); //NOI18N
67 if (null == department) {
69 throw new NullPointerException("department is null"); //NOI18N
70 } else if (department.getDepartmentId() instanceof Long) {
72 throw new IllegalArgumentException("department.departmentId should not be set."); //NOI18N
73 } else if (department.getDepartmentCompany() == null) {
75 throw new NullPointerException("department.departmentCompany is null"); //NOI18N
76 } else if (department.getDepartmentCompany().getBasicDataId() == null) {
78 throw new NullPointerException("department.departmentCompany.basicDataId is null"); //NOI18N
79 } else if (department.getDepartmentCompany().getBasicDataId() < 1) {
81 throw new IllegalArgumentException(MessageFormat.format("department.departmentCompany.basicDataId={0} is invalid", department.getDepartmentCompany().getBasicDataId())); //NOI18N
82 } else if (department.getDepartmentI18nKey() == null) {
84 throw new NullPointerException("department.departmentName is null"); //NOI18N
85 } else if (department.getDepartmentI18nKey().isEmpty()) {
87 throw new NullPointerException("department.departmentName is empty"); //NOI18N
88 } else if (this.isDepartmentFound(department)) {
89 // Already added, abort here
90 throw new DepartmentAlreadyAddedException(department);
93 // Set created timestamp
94 department.setDepartmentEntryCreated(new Date());
96 // Get managed basic data instance
97 final BasicData managedBasicData = this.createManaged(department.getDepartmentCompany());
100 department.setDepartmentCompany(managedBasicData);
102 // Is branch office set?
103 if (department.getDepartmentBranchOffice() instanceof BranchOffice) {
104 // Get managed branch office
105 final BranchOffice managedBranchOffice = this.createManaged(department.getDepartmentBranchOffice());
108 department.setDepartmentBranchOffice(managedBranchOffice);
111 // Is headquarter set?
112 if (department.getDepartmentHeadquarter() instanceof Headquarter) {
113 // Get managed headquarter
114 final Headquarter managedHeadquarter = this.createManaged(department.getDepartmentHeadquarter());
117 department.setDepartmentHeadquarter(managedHeadquarter);
120 // Is lead employee set?
121 if (department.getDepartmentLead() instanceof Contact) {
122 // Get managed lead contact
123 final Contact managedContact = this.createManaged(department.getDepartmentLead());
126 department.setDepartmentLead(managedContact);
129 // Is "owning" user set?
130 if (department.getDepartmentUserOwner() instanceof User) {
132 final User managedUser = this.createManaged(department.getDepartmentUserOwner());
135 department.setDepartmentUserOwner(managedUser);
139 this.getEntityManager().persist(department);
142 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addDepartment(): department.departmentId={1} - EXIT!", this.getClass().getSimpleName(), department.getDepartmentId())); //NOI18N
144 // Return updated instance
149 public Department updateDepartment (final Department department) throws DepartmentNotFoundException {
151 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateDepartment(): department={1} - CALLED!", this.getClass().getSimpleName(), department)); //NOI18N
153 // Validate parameter
154 if (null == department) {
156 throw new NullPointerException("department is null"); //NOI18N
157 } else if (department.getDepartmentId() == null) {
159 throw new NullPointerException("department.departmentId is null"); //NOI18N
160 } else if (department.getDepartmentId() < 1) {
162 throw new IllegalArgumentException(MessageFormat.format("department.departmentId={0} is not valid", department.getDepartmentId())); //NOI18N
163 } else if (department.getDepartmentCompany() == null) {
165 throw new NullPointerException("department.departmentCompany is null"); //NOI18N
166 } else if (department.getDepartmentCompany().getBasicDataId() == null) {
168 throw new NullPointerException("department.departmentCompany.basicDataId is null"); //NOI18N
169 } else if (department.getDepartmentCompany().getBasicDataId() < 1) {
171 throw new IllegalArgumentException(MessageFormat.format("department.departmentCompany.basicDataId={0} is invalid", department.getDepartmentCompany().getBasicDataId())); //NOI18N
172 } else if (department.getDepartmentI18nKey() == null) {
174 throw new NullPointerException("department.departmentName is null"); //NOI18N
175 } else if (department.getDepartmentI18nKey().isEmpty()) {
177 throw new NullPointerException("department.departmentName is empty"); //NOI18N
178 } else if (!this.isDepartmentFound(department)) {
179 // Already added, abort here
180 throw new DepartmentNotFoundException(department);
184 final Department updatedDepartment = this.mergeDepartmentData(department);
187 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateDepartment(): updatedDepartment={1} - EXIT!", this.getClass().getSimpleName(), updatedDepartment));
189 // Return updated instance
190 return updatedDepartment;
194 * Checks if given department's address is already persisted. The whole
195 * (persisted) list is being loaded and each address is being matched
196 * against the given department's address.
198 * @param department Branch office being checked
200 * @return Whether it has been found
202 private boolean isDepartmentFound (final Department department) {
204 final List<Department> departments = this.departmentBean.fetchAllDepartments();
206 // Default is not found
207 boolean isFound = false;
209 // Check all single addresses
210 for (final Department dep : departments) {
211 // Is the same address found?
212 if (DepartmentUtils.isSameDepartment(dep, department)) {