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