]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcontactsbusiness/model/branchoffice/JobsAdminBranchOfficeSessionBean.java
Please cherry-pick:
[jjobs-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / branchoffice / JobsAdminBranchOfficeSessionBean.java
1 /*
2  * Copyright (C) 2017 - 2022 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.branchoffice;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.ejb.EJB;
24 import javax.ejb.Stateless;
25 import org.mxchange.jcontacts.model.contact.Contact;
26 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
27 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
28 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
29 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
30 import org.mxchange.jcountry.model.data.Country;
31 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
32 import org.mxchange.jusercore.model.user.User;
33
34 /**
35  * A stateless session bean for administrative branch office purposes
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Stateless (name = "adminBranchOffice", description = "An administrative statless bean for handling branch office data (all)")
40 public class JobsAdminBranchOfficeSessionBean extends BaseJobsEnterpriseBean implements AdminBranchOfficeSessionBeanRemote {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 58_467_386_571_701L;
46
47         /**
48          * General branch office bean
49          */
50         @EJB (lookup = "java:global/jjobs-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
51         private BranchOfficeSessionBeanRemote branchOfficeBean;
52
53         /**
54          * Default constructor
55          */
56         public JobsAdminBranchOfficeSessionBean () {
57                 // Call super constructor
58                 super();
59         }
60
61         @Override
62         public BranchOffice addBranchOffice (final BranchOffice branchOffice) throws BranchOfficeAlreadyAddedException {
63                 // Trace message
64                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
65
66                 // Validate parameter
67                 if (null == branchOffice) {
68                         // Throw NPE
69                         throw new NullPointerException("branchOffice is null"); //NOI18N
70                 } else if (branchOffice.getBranchId() instanceof Long) {
71                         // Should not happen
72                         throw new IllegalArgumentException("branchOffice.branchId should not be set."); //NOI18N
73                 } else if (this.isBranchOfficeFound(branchOffice)) {
74                         // Already added, abort here
75                         throw new BranchOfficeAlreadyAddedException(branchOffice);
76                 }
77
78                 // Add created timestamp
79                 branchOffice.setBranchEntryCreated(new Date());
80
81                 // Is contact employee set?
82                 if (branchOffice.getBranchContactEmployee() instanceof Contact) {
83                         // Get managed lead contact
84                         final Contact managedContact = this.createManaged(branchOffice.getBranchContactEmployee());
85
86                         // Set it back
87                         branchOffice.setBranchContactEmployee(managedContact);
88                 }
89
90                 // Is owner employee set?
91                 if (branchOffice.getBranchOwnerEmployee() instanceof Contact) {
92                         // Get managed lead contact
93                         final Contact managedContact = this.createManaged(branchOffice.getBranchOwnerEmployee());
94
95                         // Set it back
96                         branchOffice.setBranchOwnerEmployee(managedContact);
97                 }
98
99                 // Is user instance set?
100                 if (branchOffice.getBranchCompany() instanceof BasicData) {
101                         // Get managed instance back
102                         final BasicData managedBasicData = this.createManaged(branchOffice.getBranchCompany());
103
104                         // Set it back in branch office
105                         branchOffice.setBranchCompany(managedBasicData);
106                 }
107
108                 // Is user instance set?
109                 if (branchOffice.getBranchUserOwner() instanceof User) {
110                         // Get managed instance back
111                         final User managedUser = this.createManaged(branchOffice.getBranchUserOwner());
112
113                         // Set it back in branch office
114                         branchOffice.setBranchUserOwner(managedUser);
115                 }
116
117                 // Is user instance set?
118                 if (branchOffice.getBranchCountry() instanceof Country) {
119                         // Get managed instance back
120                         final Country managedCountry = this.createManaged(branchOffice.getBranchCountry());
121
122                         // Set it back in branch office
123                         branchOffice.setBranchCountry(managedCountry);
124                 }
125
126                 // Set "created" timestamp on any number assigned
127                 this.setAllPhoneEntriesCreated(branchOffice);
128
129                 // Get opening times
130                 final List<OpeningTime> openingTimes = branchOffice.getBranchOpeningTimes();
131
132                 // Debugging:
133                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchOfficeOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes)); //NOI18N
134
135                 // Is opening times set and not empty?
136                 if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) {
137                         // Add created timestamp for all times
138                         this.setAllOpeningTimesCreated(openingTimes);
139                 } else {
140                         // Set all to null
141                         branchOffice.setBranchOpeningTimes(null);
142                 }
143
144                 // Persist it
145                 this.getEntityManager().persist(branchOffice);
146
147                 // Trace message
148                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
149
150                 // Return updated instance
151                 return branchOffice;
152         }
153
154         @Override
155         public BranchOffice updateBranchOffice (final BranchOffice branchOffice) throws BranchOfficeNotFoundException {
156                 // Trace message
157                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
158
159                 // Is parameter valid?
160                 if (null == branchOffice) {
161                         // Throw NPE
162                         throw new NullPointerException("branchOffice is null"); //NOI18N
163                 } else if (branchOffice.getBranchId() == null) {
164                         // Throw NPE again
165                         throw new NullPointerException("branchOffice.branchId is null"); //NOI18N
166                 } else if (branchOffice.getBranchId() < 1) {
167                         // Throw IAE again
168                         throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchId={0} is invalid", branchOffice.getBranchId())); //NOI18N
169                 } else if (!this.isBranchOfficeFound(branchOffice)) {
170                         // Not found
171                         throw new BranchOfficeNotFoundException(branchOffice.getBranchId());
172                 }
173
174                 // Merge data
175                 final BranchOffice updatedBranchOffice = this.mergeBranchOfficeData(branchOffice);
176
177                 // Trace message
178                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): updatedBranchOffice={1} - EXIT!", this.getClass().getSimpleName(), updatedBranchOffice));
179
180                 // Return updated instance
181                 return updatedBranchOffice;
182         }
183
184         /**
185          * Checks if given branch office's address is already persisted. The whole
186          * (persisted) list is being loaded and each address is being matched
187          * against the given branch office's address.
188          * <p>
189          * @param branchOffice Branch office being checked
190          * <p>
191          * @return Whether it has been found
192          */
193         private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
194                 // Trace message
195                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
196
197                 // Get whole list
198                 final List<BranchOffice> branchOffices = this.branchOfficeBean.fetchAllBranchOffices();
199
200                 // Default is not found
201                 boolean isFound = false;
202
203                 // Check all single addresses
204                 for (final BranchOffice bo : branchOffices) {
205                         // Is the same address found?
206                         if (Objects.equals(bo.getBranchId(), branchOffice.getBranchId()) || BranchOffices.isSameAddress(bo, branchOffice)) {
207                                 // Found one
208                                 isFound = true;
209                                 break;
210                         }
211                 }
212
213                 // Trace message
214                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
215
216                 // Return flag
217                 return isFound;
218         }
219
220 }