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