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.jcountry.model.data.Country;
31 import org.mxchange.jfinancials.enterprise.BaseFinancialsEnterpriseBean;
32 import org.mxchange.jusercore.model.user.User;
35 * A stateless session bean for administrative branch office purposes
37 * @author Roland Häder<roland@mxchange.org>
39 @Stateless (name = "adminBranchOffice", description = "An administrative statless bean for handling branch office data (all)")
40 public class FinancialsAdminBranchOfficeSessionBean extends BaseFinancialsEnterpriseBean implements AdminBranchOfficeSessionBeanRemote {
45 private static final long serialVersionUID = 58_467_386_571_701L;
48 * General branch office bean
50 @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
51 private BranchOfficeSessionBeanRemote branchOfficeBean;
56 public FinancialsAdminBranchOfficeSessionBean () {
57 // Call super constructor
62 public BranchOffice addBranchOffice (final BranchOffice branchOffice) throws BranchOfficeAlreadyAddedException {
64 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
67 if (null == branchOffice) {
69 throw new NullPointerException("branchOffice is null"); //NOI18N
70 } else if (branchOffice.getBranchId() instanceof Long) {
72 throw new IllegalArgumentException("branchOffice.branchId should not be set."); //NOI18N
73 } else if (this.isBranchOfficeFound(branchOffice)) {
74 // Already added, abort here
75 throw new BranchOfficeAlreadyAddedException(branchOffice);
78 // Add created timestamp
79 branchOffice.setBranchEntryCreated(new Date());
81 // Is contact employee set?
82 if (branchOffice.getBranchContactEmployee() instanceof Contact) {
83 // Get managed lead contact
84 final Contact managedContact = this.createManaged(branchOffice.getBranchContactEmployee());
87 branchOffice.setBranchContactEmployee(managedContact);
90 // Is owner employee set?
91 if (branchOffice.getBranchOwnerEmployee() instanceof Contact) {
92 // Get managed lead contact
93 final Contact managedContact = this.createManaged(branchOffice.getBranchOwnerEmployee());
96 branchOffice.setBranchOwnerEmployee(managedContact);
99 // Is user instance set?
100 if (branchOffice.getBranchCompany() instanceof BasicData) {
101 // Get managed instance back
102 final BasicData managedBasicData = this.createManaged(branchOffice.getBranchCompany());
104 // Set it back in branch office
105 branchOffice.setBranchCompany(managedBasicData);
108 // Is user instance set?
109 if (branchOffice.getBranchUserOwner() instanceof User) {
110 // Get managed instance back
111 final User managedUser = this.createManaged(branchOffice.getBranchUserOwner());
113 // Set it back in branch office
114 branchOffice.setBranchUserOwner(managedUser);
117 // Is user instance set?
118 if (branchOffice.getBranchCountry() instanceof Country) {
119 // Get managed instance back
120 final Country managedCountry = this.createManaged(branchOffice.getBranchCountry());
122 // Set it back in branch office
123 branchOffice.setBranchCountry(managedCountry);
126 // Set "created" timestamp on any number assigned
127 this.setAllPhoneEntriesCreated(branchOffice);
130 final List<OpeningTime> openingTimes = branchOffice.getBranchOpeningTimes();
133 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchOfficeOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes)); //NOI18N
135 // Is opening times set and not empty?
136 if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) {
137 // Add created timestamp for all times
138 this.setAllOpeningTimesCreated(openingTimes);
141 branchOffice.setBranchOpeningTimes(null);
145 this.getEntityManager().persist(branchOffice);
148 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
150 // Return updated instance
155 public BranchOffice updateBranchOffice (final BranchOffice branchOffice) throws BranchOfficeNotFoundException {
157 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
159 // Is parameter valid?
160 if (null == branchOffice) {
162 throw new NullPointerException("branchOffice is null"); //NOI18N
163 } else if (branchOffice.getBranchId() == null) {
165 throw new NullPointerException("branchOffice.branchId is null"); //NOI18N
166 } else if (branchOffice.getBranchId() < 1) {
168 throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchId={0} is invalid", branchOffice.getBranchId())); //NOI18N
169 } else if (!this.isBranchOfficeFound(branchOffice)) {
171 throw new BranchOfficeNotFoundException(branchOffice.getBranchId());
175 final BranchOffice updatedBranchOffice = this.mergeBranchOfficeData(branchOffice);
178 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): updatedBranchOffice={1} - EXIT!", this.getClass().getSimpleName(), updatedBranchOffice));
180 // Return updated instance
181 return updatedBranchOffice;
185 * Checks if given branch office's address is already persisted. The whole
186 * (persisted) list is being loaded and each address is being matched
187 * against the given branch office's address.
189 * @param branchOffice Branch office being checked
191 * @return Whether it has been found
193 private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
195 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
198 final List<BranchOffice> branchOffices = this.branchOfficeBean.fetchAllBranchOffices();
200 // Default is not found
201 boolean isFound = false;
203 // Check all single addresses
204 for (final BranchOffice bo : branchOffices) {
205 // Is the same address found?
206 if (Objects.equals(bo.getBranchId(), branchOffice.getBranchId()) || BranchOffices.isSameAddress(bo, branchOffice)) {
214 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N