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;
22 import java.util.Objects;
24 import javax.ejb.Stateless;
25 import org.mxchange.jcontacts.model.contact.Contact;
26 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
27 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
28 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
29 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
30 import org.mxchange.jcontactsbusiness.model.utils.BranchOfficeUtils;
31 import org.mxchange.jcountry.model.data.Country;
32 import org.mxchange.jfinancials.enterprise.BaseFinancialsEnterpriseBean;
33 import org.mxchange.jusercore.model.user.User;
36 * A stateless session bean for administrative branch office purposes
38 * @author Roland Häder<roland@mxchange.org>
40 @Stateless (name = "adminBranchOffice", description = "An administrative statless bean for handling branch office data (all)")
41 public class FinancialsAdminBranchOfficeSessionBean extends BaseFinancialsEnterpriseBean implements AdminBranchOfficeSessionBeanRemote {
46 private static final long serialVersionUID = 58_467_386_571_701L;
49 * General branch office bean
51 @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
52 private BranchOfficeSessionBeanRemote branchOfficeBean;
57 public FinancialsAdminBranchOfficeSessionBean () {
58 // Call super constructor
63 public BranchOffice addBranchOffice (final BranchOffice branchOffice) throws BranchOfficeAlreadyAddedException {
65 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
68 if (null == branchOffice) {
70 throw new NullPointerException("branchOffice is null"); //NOI18N
71 } else if (branchOffice.getBranchId() instanceof Long) {
73 throw new IllegalArgumentException("branchOffice.branchId should not be set."); //NOI18N
74 } else if (this.isBranchOfficeFound(branchOffice)) {
75 // Already added, abort here
76 throw new BranchOfficeAlreadyAddedException(branchOffice);
79 // Add created timestamp
80 branchOffice.setBranchEntryCreated(new Date());
82 // Is contact employee set?
83 if (branchOffice.getBranchContactEmployee() instanceof Contact) {
84 // Get managed lead contact
85 final Contact managedContact = this.createManaged(branchOffice.getBranchContactEmployee());
88 branchOffice.setBranchContactEmployee(managedContact);
91 // Is owner employee set?
92 if (branchOffice.getBranchOwnerEmployee() instanceof Contact) {
93 // Get managed lead contact
94 final Contact managedContact = this.createManaged(branchOffice.getBranchOwnerEmployee());
97 branchOffice.setBranchOwnerEmployee(managedContact);
100 // Is user instance set?
101 if (branchOffice.getBranchCompany() instanceof BasicData) {
102 // Get managed instance back
103 final BasicData managedBasicData = this.createManaged(branchOffice.getBranchCompany());
105 // Set it back in branch office
106 branchOffice.setBranchCompany(managedBasicData);
109 // Is user instance set?
110 if (branchOffice.getBranchUserOwner() instanceof User) {
111 // Get managed instance back
112 final User managedUser = this.createManaged(branchOffice.getBranchUserOwner());
114 // Set it back in branch office
115 branchOffice.setBranchUserOwner(managedUser);
118 // Is user instance set?
119 if (branchOffice.getBranchCountry() instanceof Country) {
120 // Get managed instance back
121 final Country managedCountry = this.createManaged(branchOffice.getBranchCountry());
123 // Set it back in branch office
124 branchOffice.setBranchCountry(managedCountry);
127 // Set "created" timestamp on any number assigned
128 this.setAllPhoneEntriesCreated(branchOffice);
131 final List<OpeningTime> openingTimes = branchOffice.getBranchOpeningTimes();
134 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchOfficeOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes)); //NOI18N
136 // Is opening times set and not empty?
137 if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) {
138 // Add created timestamp for all times
139 this.setAllOpeningTimesCreated(openingTimes);
142 branchOffice.setBranchOpeningTimes(null);
146 this.getEntityManager().persist(branchOffice);
149 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
151 // Return updated instance
156 public BranchOffice updateBranchOffice (final BranchOffice branchOffice) throws BranchOfficeNotFoundException {
158 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
160 // Is parameter valid?
161 if (null == branchOffice) {
163 throw new NullPointerException("branchOffice is null"); //NOI18N
164 } else if (branchOffice.getBranchId() == null) {
166 throw new NullPointerException("branchOffice.branchId is null"); //NOI18N
167 } else if (branchOffice.getBranchId() < 1) {
169 throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchId={0} is invalid", branchOffice.getBranchId())); //NOI18N
170 } else if (!this.isBranchOfficeFound(branchOffice)) {
172 throw new BranchOfficeNotFoundException(branchOffice.getBranchId());
176 final BranchOffice updatedBranchOffice = this.mergeBranchOfficeData(branchOffice);
179 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): updatedBranchOffice={1} - EXIT!", this.getClass().getSimpleName(), updatedBranchOffice));
181 // Return updated instance
182 return updatedBranchOffice;
186 * Checks if given branch office's address is already persisted. The whole
187 * (persisted) list is being loaded and each address is being matched
188 * against the given branch office's address.
190 * @param branchOffice Branch office being checked
192 * @return Whether it has been found
194 private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
196 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
199 final List<BranchOffice> branchOffices = this.branchOfficeBean.fetchAllBranchOffices();
201 // Default is not found
202 boolean isFound = false;
204 // Check all single addresses
205 for (final BranchOffice bo : branchOffices) {
206 // Is the same address found?
207 if (Objects.equals(bo.getBranchId(), branchOffice.getBranchId()) || BranchOfficeUtils.isSameAddress(bo, branchOffice)) {
215 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N