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