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