]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcontactsbusiness/model/department/JobsAdminDepartmentSessionBean.java
Updated copyright year
[jjobs-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / department / JobsAdminDepartmentSessionBean.java
1 /*
2  * Copyright (C) 2017 - 2024 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.department;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import javax.ejb.EJB;
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;
33
34 /**
35  * A stateless session bean for administrative department purposes
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Stateless (name = "adminDepartment", description = "An administrative statless bean for handling department data (all)")
40 public class JobsAdminDepartmentSessionBean extends BaseJobsEnterpriseBean implements AdminDepartmentSessionBeanRemote {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 58_467_386_571_703L;
46
47         /**
48          * General department bean
49          */
50         @EJB (lookup = "java:global/jjobs-ejb/department!org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote")
51         private DepartmentSessionBeanRemote departmentBean;
52
53         /**
54          * Default constructor
55          */
56         public JobsAdminDepartmentSessionBean () {
57                 // Call super constructor
58                 super();
59         }
60
61         @Override
62         public Department addDepartment (final Department department) throws DepartmentAlreadyAddedException {
63                 // Trace message
64                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addDepartment(): department={1} - CALLED!", this.getClass().getSimpleName(), department)); //NOI18N
65
66                 // Validate parameter
67                 if (null == department) {
68                         // Throw NPE
69                         throw new NullPointerException("department is null"); //NOI18N
70                 } else if (department.getDepartmentId() instanceof Long) {
71                         // Should not happen
72                         throw new IllegalArgumentException("department.departmentId should not be set."); //NOI18N
73                 } else if (department.getDepartmentCompany() == null) {
74                         // Throw NPE
75                         throw new NullPointerException("department.departmentCompany is null"); //NOI18N
76                 } else if (department.getDepartmentCompany().getBasicDataId() == null) {
77                         // Throw NPE again
78                         throw new NullPointerException("department.departmentCompany.basicDataId is null"); //NOI18N
79                 } else if (department.getDepartmentCompany().getBasicDataId() < 1) {
80                         // Throw IAE
81                         throw new IllegalArgumentException(MessageFormat.format("department.departmentCompany.basicDataId={0} is invalid", department.getDepartmentCompany().getBasicDataId())); //NOI18N
82                 } else if (department.getDepartmentI18nKey() == null) {
83                         // Throw NPE
84                         throw new NullPointerException("department.departmentName is null"); //NOI18N
85                 } else if (department.getDepartmentI18nKey().isEmpty()) {
86                         // Throw IAE
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);
91                 }
92
93                 // Set created timestamp
94                 department.setDepartmentEntryCreated(new Date());
95
96                 // Get managed basic data instance
97                 final BasicData managedBasicData = this.createManaged(department.getDepartmentCompany());
98
99                 // Set it back
100                 department.setDepartmentCompany(managedBasicData);
101
102                 // Is branch office set?
103                 if (department.getDepartmentBranchOffice() instanceof BranchOffice) {
104                         // Get managed branch office
105                         final BranchOffice managedBranchOffice = this.createManaged(department.getDepartmentBranchOffice());
106
107                         // Set it back
108                         department.setDepartmentBranchOffice(managedBranchOffice);
109                 }
110
111                 // Is headquarter set?
112                 if (department.getDepartmentHeadquarter() instanceof Headquarter) {
113                         // Get managed headquarter
114                         final Headquarter managedHeadquarter = this.createManaged(department.getDepartmentHeadquarter());
115
116                         // Set it back
117                         department.setDepartmentHeadquarter(managedHeadquarter);
118                 }
119
120                 // Is lead employee set?
121                 if (department.getDepartmentLead() instanceof Contact) {
122                         // Get managed lead contact
123                         final Contact managedContact = this.createManaged(department.getDepartmentLead());
124
125                         // Set it back
126                         department.setDepartmentLead(managedContact);
127                 }
128
129                 // Is "owning" user set?
130                 if (department.getDepartmentUserOwner() instanceof User) {
131                         // Get managed user
132                         final User managedUser = this.createManaged(department.getDepartmentUserOwner());
133
134                         // Set it back
135                         department.setDepartmentUserOwner(managedUser);
136                 }
137
138                 // Persist it
139                 this.getEntityManager().persist(department);
140
141                 // Trace message
142                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addDepartment(): department.departmentId={1} - EXIT!", this.getClass().getSimpleName(), department.getDepartmentId())); //NOI18N
143
144                 // Return updated instance
145                 return department;
146         }
147
148         @Override
149         public Department updateDepartment (final Department department) throws DepartmentNotFoundException {
150                 // Trace message
151                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateDepartment(): department={1} - CALLED!", this.getClass().getSimpleName(), department)); //NOI18N
152
153                 // Validate parameter
154                 if (null == department) {
155                         // Throw NPE
156                         throw new NullPointerException("department is null"); //NOI18N
157                 } else if (department.getDepartmentId() == null) {
158                         // Throw NPE again
159                         throw new NullPointerException("department.departmentId is null"); //NOI18N
160                 } else if (department.getDepartmentId() < 1) {
161                         // Throw IAE
162                         throw new IllegalArgumentException(MessageFormat.format("department.departmentId={0} is not valid", department.getDepartmentId())); //NOI18N
163                 } else if (department.getDepartmentCompany() == null) {
164                         // Throw NPE
165                         throw new NullPointerException("department.departmentCompany is null"); //NOI18N
166                 } else if (department.getDepartmentCompany().getBasicDataId() == null) {
167                         // Throw NPE again
168                         throw new NullPointerException("department.departmentCompany.basicDataId is null"); //NOI18N
169                 } else if (department.getDepartmentCompany().getBasicDataId() < 1) {
170                         // Throw IAE
171                         throw new IllegalArgumentException(MessageFormat.format("department.departmentCompany.basicDataId={0} is invalid", department.getDepartmentCompany().getBasicDataId())); //NOI18N
172                 } else if (department.getDepartmentI18nKey() == null) {
173                         // Throw NPE
174                         throw new NullPointerException("department.departmentName is null"); //NOI18N
175                 } else if (department.getDepartmentI18nKey().isEmpty()) {
176                         // Throw IAE
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);
181                 }
182
183                 // Merge data
184                 final Department updatedDepartment = this.mergeDepartmentData(department);
185
186                 // Trace message
187                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateDepartment(): updatedDepartment={1} - EXIT!", this.getClass().getSimpleName(), updatedDepartment));
188
189                 // Return updated instance
190                 return updatedDepartment;
191         }
192
193         /**
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.
197          * <p>
198          * @param department Branch office being checked
199          * <p>
200          * @return Whether it has been found
201          */
202         private boolean isDepartmentFound (final Department department) {
203                 // Get whole list
204                 final List<Department> departments = this.departmentBean.fetchAllDepartments();
205
206                 // Default is not found
207                 boolean isFound = false;
208
209                 // Check all single addresses
210                 for (final Department dep : departments) {
211                         // Is the same address found?
212                         if (DepartmentUtils.isSameDepartment(dep, department)) {
213                                 // Found one
214                                 isFound = true;
215                                 break;
216                         }
217                 }
218
219                 // Return flag
220                 return isFound;
221         }
222
223 }