]> git.mxchange.org Git - jfinancials-ejb.git/blob - src/java/org/mxchange/jcontactsbusiness/model/department/FinancialsAdminDepartmentSessionBean.java
Please cherry-pick:
[jfinancials-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / department / FinancialsAdminDepartmentSessionBean.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.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.jfinancials.enterprise.BaseFinancialsEnterpriseBean;
30 import org.mxchange.jusercore.model.user.User;
31
32 /**
33  * A stateless session bean for administrative department purposes
34  * <p>
35  * @author Roland Häder<roland@mxchange.org>
36  */
37 @Stateless (name = "adminDepartment", description = "An administrative statless bean for handling department data (all)")
38 public class FinancialsAdminDepartmentSessionBean extends BaseFinancialsEnterpriseBean implements AdminDepartmentSessionBeanRemote {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 58_467_386_571_703L;
44
45         /**
46          * General department bean
47          */
48         @EJB (lookup = "java:global/jfinancials-ejb/department!org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote")
49         private DepartmentSessionBeanRemote departmentBean;
50
51         /**
52          * Default constructor
53          */
54         public FinancialsAdminDepartmentSessionBean () {
55                 // Call super constructor
56                 super();
57         }
58
59         @Override
60         public Department addDepartment (final Department department) throws DepartmentAlreadyAddedException {
61                 // Trace message
62                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addDepartment(): department={1} - CALLED!", this.getClass().getSimpleName(), department)); //NOI18N
63
64                 // Validate parameter
65                 if (null == department) {
66                         // Throw NPE
67                         throw new NullPointerException("department is null"); //NOI18N
68                 } else if (department.getDepartmentId() instanceof Long) {
69                         // Should not happen
70                         throw new IllegalArgumentException("department.departmentId should not be set."); //NOI18N
71                 } else if (department.getDepartmentCompany() == null) {
72                         // Throw NPE
73                         throw new NullPointerException("department.departmentCompany is null"); //NOI18N
74                 } else if (department.getDepartmentCompany().getBasicDataId() == null) {
75                         // Throw NPE again
76                         throw new NullPointerException("department.departmentCompany.basicDataId is null"); //NOI18N
77                 } else if (department.getDepartmentCompany().getBasicDataId() < 1) {
78                         // Throw IAE
79                         throw new IllegalArgumentException(MessageFormat.format("department.departmentCompany.basicDataId={0} is invalid", department.getDepartmentCompany().getBasicDataId())); //NOI18N
80                 } else if (department.getDepartmentI18nKey() == null) {
81                         // Throw NPE
82                         throw new NullPointerException("department.departmentName is null"); //NOI18N
83                 } else if (department.getDepartmentI18nKey().isEmpty()) {
84                         // Throw IAE
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);
89                 }
90
91                 // Set created timestamp
92                 department.setDepartmentCreated(new Date());
93
94                 // Get managed basic data instance
95                 final BasicData managedBasicData = this.createManaged(department.getDepartmentCompany());
96
97                 // Set it back
98                 department.setDepartmentCompany(managedBasicData);
99
100                 // Is branch office set?
101                 if (department.getDepartmentBranchOffice() instanceof BranchOffice) {
102                         // Get managed branch office
103                         final BranchOffice managedBranchOffice = this.createManaged(department.getDepartmentBranchOffice());
104
105                         // Set it back
106                         department.setDepartmentBranchOffice(managedBranchOffice);
107                 }
108
109                 // Is headquarter set?
110                 if (department.getDepartmentHeadquarter() instanceof Headquarter) {
111                         // Get managed headquarter
112                         final Headquarter managedHeadquarter = this.createManaged(department.getDepartmentHeadquarter());
113
114                         // Set it back
115                         department.setDepartmentHeadquarter(managedHeadquarter);
116                 }
117
118                 // Is lead employee set?
119                 if (department.getDepartmentLead() instanceof Employable) {
120                         // Get managed lead employee
121                         final Employable managedEmployee = this.createManaged(department.getDepartmentLead());
122
123                         // Set it back
124                         department.setDepartmentLead(managedEmployee);
125                 }
126
127                 // Is "owning" user set?
128                 if (department.getDepartmentUserOwner() instanceof User) {
129                         // Get managed user
130                         final User managedUser = this.createManaged(department.getDepartmentUserOwner());
131
132                         // Set it back
133                         department.setDepartmentUserOwner(managedUser);
134                 }
135
136                 // Persist it
137                 this.getEntityManager().persist(department);
138
139                 // Trace message
140                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addDepartment(): department.departmentId={1} - EXIT!", this.getClass().getSimpleName(), department.getDepartmentId())); //NOI18N
141
142                 // Return updated instance
143                 return department;
144         }
145
146         /**
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.
150          * <p>
151          * @param department Branch office being checked
152          * <p>
153          * @return Whether it has been found
154          */
155         private boolean isDepartmentFound (final Department department) {
156                 // Get whole list
157                 final List<Department> departments = this.departmentBean.fetchAllDepartments();
158
159                 // Default is not found
160                 boolean isFound = false;
161
162                 // Check all single addresses
163                 for (final Department dep : departments) {
164                         // Is the same address found?
165                         if (Departments.isSameDepartment(dep, department)) {
166                                 // Found one
167                                 isFound = true;
168                                 break;
169                         }
170                 }
171
172                 // Return flag
173                 return isFound;
174         }
175
176 }