]> git.mxchange.org Git - addressbook-ejb.git/blob - src/java/org/mxchange/jcontactsbusiness/model/branchoffice/AddressbookAdminBranchOfficeSessionBean.java
Please cherry-pick:
[addressbook-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / branchoffice / AddressbookAdminBranchOfficeSessionBean.java
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                 // Set "created" timestamp on any number assigned
105                 this.setAllPhoneEntriesCreated(branchOffice);
106
107                 // Persist it
108                 this.getEntityManager().persist(branchOffice);
109
110                 // Trace message
111                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
112
113                 // Return updated instance
114                 return branchOffice;
115         }
116
117         /**
118          * Checks if given branch office's address is already persisted. The whole
119          * (persisted) list is being loaded and each address is being matched
120          * against the given branch office's address.
121          * <p>
122          * @param branchOffice Branch office being checked
123          * <p>
124          * @return Whether it has been found
125          */
126         private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
127                 // Get whole list
128                 final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
129
130                 // Default is not found
131                 boolean isFound = false;
132
133                 // Check all single addresses
134                 for (final BranchOffice bo : branchOffices) {
135                         // Is the same address found?
136                         if (BranchOffices.isSameAddress(bo, branchOffice)) {
137                                 // Found one
138                                 isFound = true;
139                                 break;
140                         }
141                 }
142
143                 // Return flag
144                 return isFound;
145         }
146
147 }