]> 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 - 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.jcontacts.model.contact.Contact;
25 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
26 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
27 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
28 import org.mxchange.jcountry.model.data.Country;
29 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
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 JobsAdminBranchOfficeSessionBean extends BaseJobsEnterpriseBean 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 (lookup = "java:global/jjobs-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
49         private BranchOfficeSessionBeanRemote branchOfficeBean;
50
51         /**
52          * Default constructor
53          */
54         public JobsAdminBranchOfficeSessionBean () {
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.setBranchEntryCreated(new Date());
78
79                 // Is contact employee set?
80                 if (branchOffice.getBranchContactEmployee() instanceof Contact) {
81                         // Get managed lead contact
82                         final Contact managedContact = this.createManaged(branchOffice.getBranchContactEmployee());
83
84                         // Set it back
85                         branchOffice.setBranchContactEmployee(managedContact);
86                 }
87
88                 // Is owner employee set?
89                 if (branchOffice.getBranchOwnerEmployee() instanceof Contact) {
90                         // Get managed lead contact
91                         final Contact managedContact = this.createManaged(branchOffice.getBranchOwnerEmployee());
92
93                         // Set it back
94                         branchOffice.setBranchOwnerEmployee(managedContact);
95                 }
96
97                 // Is user instance set?
98                 if (branchOffice.getBranchCompany() instanceof BasicData) {
99                         // Get managed instance back
100                         final BasicData managedBasicData = this.createManaged(branchOffice.getBranchCompany());
101
102                         // Set it back in branch office
103                         branchOffice.setBranchCompany(managedBasicData);
104                 }
105
106                 // Is user instance set?
107                 if (branchOffice.getBranchUserOwner() instanceof User) {
108                         // Get managed instance back
109                         final User managedUser = this.createManaged(branchOffice.getBranchUserOwner());
110
111                         // Set it back in branch office
112                         branchOffice.setBranchUserOwner(managedUser);
113                 }
114
115                 // Is user instance set?
116                 if (branchOffice.getBranchCountry() instanceof Country) {
117                         // Get managed instance back
118                         final Country managedCountry = this.createManaged(branchOffice.getBranchCountry());
119
120                         // Set it back in branch office
121                         branchOffice.setBranchCountry(managedCountry);
122                 }
123
124                 // Set "created" timestamp on any number assigned
125                 this.setAllPhoneEntriesCreated(branchOffice);
126
127                 // Get opening times
128                 final List<OpeningTime> openingTimes = branchOffice.getBranchOpeningTimes();
129
130                 // Debugging:
131                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchOfficeOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes));
132
133                 // Is opening times set and not empty?
134                 if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) {
135                         // Add created timestamp for all times
136                         this.setAllOpeningTimesCreated(openingTimes);
137                 } else {
138                         // Set all to null
139                         branchOffice.setBranchOpeningTimes(null);
140                 }
141
142                 // Persist it
143                 this.getEntityManager().persist(branchOffice);
144
145                 // Trace message
146                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
147
148                 // Return updated instance
149                 return branchOffice;
150         }
151
152         /**
153          * Checks if given branch office's address is already persisted. The whole
154          * (persisted) list is being loaded and each address is being matched
155          * against the given branch office's address.
156          * <p>
157          * @param branchOffice Branch office being checked
158          * <p>
159          * @return Whether it has been found
160          */
161         private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
162                 // Get whole list
163                 final List<BranchOffice> branchOffices = this.branchOfficeBean.fetchAllBranchOffices();
164
165                 // Default is not found
166                 boolean isFound = false;
167
168                 // Check all single addresses
169                 for (final BranchOffice bo : branchOffices) {
170                         // Is the same address found?
171                         if (BranchOffices.isSameAddress(bo, branchOffice)) {
172                                 // Found one
173                                 isFound = true;
174                                 break;
175                         }
176                 }
177
178                 // Return flag
179                 return isFound;
180         }
181
182 }