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