2 * Copyright (C) 2017 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jcontactsbusiness.model.branchoffice;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
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.exceptions.branchoffice.BranchOfficeNotFoundException;
27 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
28 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
29 import org.mxchange.jcountry.model.data.Country;
30 import org.mxchange.jfinancials.enterprise.BaseFinancialsEnterpriseBean;
31 import org.mxchange.jusercore.model.user.User;
34 * A stateless session bean for administrative branch office purposes
36 * @author Roland Häder<roland@mxchange.org>
38 @Stateless (name = "adminBranchOffice", description = "An administrative statless bean for handling branch office data (all)")
39 public class FinancialsAdminBranchOfficeSessionBean extends BaseFinancialsEnterpriseBean implements AdminBranchOfficeSessionBeanRemote {
44 private static final long serialVersionUID = 58_467_386_571_701L;
47 * General branch office bean
49 @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
50 private BranchOfficeSessionBeanRemote branchOfficeBean;
55 public FinancialsAdminBranchOfficeSessionBean () {
56 // Call super constructor
61 public BranchOffice addBranchOffice (final BranchOffice branchOffice) throws BranchOfficeAlreadyAddedException {
63 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
66 if (null == branchOffice) {
68 throw new NullPointerException("branchOffice is null"); //NOI18N
69 } else if (branchOffice.getBranchId() instanceof Long) {
71 throw new IllegalArgumentException("branchOffice.branchId should not be set."); //NOI18N
72 } else if (this.isBranchOfficeFound(branchOffice)) {
73 // Already added, abort here
74 throw new BranchOfficeAlreadyAddedException(branchOffice);
77 // Add created timestamp
78 branchOffice.setBranchEntryCreated(new Date());
80 // Is contact employee set?
81 if (branchOffice.getBranchContactEmployee() instanceof Contact) {
82 // Get managed lead contact
83 final Contact managedContact = this.createManaged(branchOffice.getBranchContactEmployee());
86 branchOffice.setBranchContactEmployee(managedContact);
89 // Is owner employee set?
90 if (branchOffice.getBranchOwnerEmployee() instanceof Contact) {
91 // Get managed lead contact
92 final Contact managedContact = this.createManaged(branchOffice.getBranchOwnerEmployee());
95 branchOffice.setBranchOwnerEmployee(managedContact);
98 // Is user instance set?
99 if (branchOffice.getBranchCompany() instanceof BasicData) {
100 // Get managed instance back
101 final BasicData managedBasicData = this.createManaged(branchOffice.getBranchCompany());
103 // Set it back in branch office
104 branchOffice.setBranchCompany(managedBasicData);
107 // Is user instance set?
108 if (branchOffice.getBranchUserOwner() instanceof User) {
109 // Get managed instance back
110 final User managedUser = this.createManaged(branchOffice.getBranchUserOwner());
112 // Set it back in branch office
113 branchOffice.setBranchUserOwner(managedUser);
116 // Is user instance set?
117 if (branchOffice.getBranchCountry() instanceof Country) {
118 // Get managed instance back
119 final Country managedCountry = this.createManaged(branchOffice.getBranchCountry());
121 // Set it back in branch office
122 branchOffice.setBranchCountry(managedCountry);
125 // Set "created" timestamp on any number assigned
126 this.setAllPhoneEntriesCreated(branchOffice);
129 final List<OpeningTime> openingTimes = branchOffice.getBranchOpeningTimes();
132 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchOfficeOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes)); //NOI18N
134 // Is opening times set and not empty?
135 if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) {
136 // Add created timestamp for all times
137 this.setAllOpeningTimesCreated(openingTimes);
140 branchOffice.setBranchOpeningTimes(null);
144 this.getEntityManager().persist(branchOffice);
147 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
149 // Return updated instance
154 public BranchOffice updateBranchOffice (final BranchOffice branchOffice) throws BranchOfficeNotFoundException {
156 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
158 // Is parameter valid?
159 if (null == branchOffice) {
161 throw new NullPointerException("branchOffice is null"); //NOI18N
162 } else if (branchOffice.getBranchId() == null) {
164 throw new NullPointerException("branchOffice.branchId is null"); //NOI18N
165 } else if (branchOffice.getBranchId() < 1) {
167 throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchId={0} is invalid", branchOffice.getBranchId())); //NOI18N
168 } else if (!this.isBranchOfficeFound(branchOffice)) {
170 throw new BranchOfficeNotFoundException(branchOffice.getBranchId());
174 final BranchOffice updatedBranchOffice = this.mergeBranchOfficeData(branchOffice);
177 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): updatedBranchOffice={1} - EXIT!", this.getClass().getSimpleName(), updatedBranchOffice));
179 // Return updated instance
180 return updatedBranchOffice;
184 * Checks if given branch office's address is already persisted. The whole
185 * (persisted) list is being loaded and each address is being matched
186 * against the given branch office's address.
188 * @param branchOffice Branch office being checked
190 * @return Whether it has been found
192 private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
194 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
197 final List<BranchOffice> branchOffices = this.branchOfficeBean.fetchAllBranchOffices();
199 // Default is not found
200 boolean isFound = false;
202 // Check all single addresses
203 for (final BranchOffice bo : branchOffices) {
204 // Is the same address found?
205 if (BranchOffices.isSameAddress(bo, branchOffice)) {
213 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N