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