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