2 * Copyright (C) 2017 - 2020 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.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
25 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
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.jfinancials.enterprise.BaseFinancialsEnterpriseBean;
30 import org.mxchange.jusercore.model.user.User;
33 * A stateless session bean for administrative branch office purposes
35 * @author Roland Häder<roland@mxchange.org>
37 @Stateless (name = "adminBranchOffice", description = "An administrative statless bean for handling branch office data (all)")
38 public class FinancialsAdminBranchOfficeSessionBean extends BaseFinancialsEnterpriseBean implements AdminBranchOfficeSessionBeanRemote {
43 private static final long serialVersionUID = 58_467_386_571_701L;
46 * General branch office bean
48 @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
49 private BranchOfficeSessionBeanRemote branchOfficeBean;
54 public FinancialsAdminBranchOfficeSessionBean () {
55 // Call super constructor
60 public BranchOffice addBranchOffice (final BranchOffice branchOffice) throws BranchOfficeAlreadyAddedException {
62 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
65 if (null == branchOffice) {
67 throw new NullPointerException("branchOffice is null"); //NOI18N
68 } else if (branchOffice.getBranchId() instanceof Long) {
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);
76 // Add created timestamp
77 branchOffice.setBranchEntryCreated(new Date());
79 // Is user instance set?
80 if (branchOffice.getBranchCompany() instanceof BasicData) {
81 // Get managed instance back
82 final BasicData managedBasicData = this.createManaged(branchOffice.getBranchCompany());
84 // Set it back in branch office
85 branchOffice.setBranchCompany(managedBasicData);
88 // Is user instance set?
89 if (branchOffice.getBranchUserOwner() instanceof User) {
90 // Get managed instance back
91 final User managedUser = this.createManaged(branchOffice.getBranchUserOwner());
93 // Set it back in branch office
94 branchOffice.setBranchUserOwner(managedUser);
97 // Is user instance set?
98 if (branchOffice.getBranchCountry() instanceof Country) {
99 // Get managed instance back
100 final Country managedCountry = this.createManaged(branchOffice.getBranchCountry());
102 // Set it back in branch office
103 branchOffice.setBranchCountry(managedCountry);
106 // Set "created" timestamp on any number assigned
107 this.setAllPhoneEntriesCreated(branchOffice);
110 final List<OpeningTime> openingTimes = branchOffice.getBranchOpeningTimes();
113 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchOfficeOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes)); //NOI18N
115 // Is opening times set and not empty?
116 if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) {
117 // Add created timestamp for all times
118 this.setAllOpeningTimesCreated(openingTimes);
121 branchOffice.setBranchOpeningTimes(null);
125 this.getEntityManager().persist(branchOffice);
128 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
130 // Return updated instance
135 public BranchOffice updateBranchOffice (final BranchOffice branchOffice) throws BranchOfficeNotFoundException {
137 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
139 // Is parameter valid?
140 if (null == branchOffice) {
142 throw new NullPointerException("branchOffice is null"); //NOI18N
143 } else if (branchOffice.getBranchId() == null) {
145 throw new NullPointerException("branchOffice.branchId is null"); //NOI18N
146 } else if (branchOffice.getBranchId() < 1) {
148 throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchId={0} is invalid", branchOffice.getBranchId())); //NOI18N
149 } else if (!this.isBranchOfficeFound(branchOffice)) {
151 throw new BranchOfficeNotFoundException(branchOffice.getBranchId());
155 final BranchOffice updatedBranchOffice = this.mergeBranchOfficeData(branchOffice);
158 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): updatedBranchOffice={1} - EXIT!", this.getClass().getSimpleName(), updatedBranchOffice));
160 // Return updated instance
161 return updatedBranchOffice;
165 * Checks if given branch office's address is already persisted. The whole
166 * (persisted) list is being loaded and each address is being matched
167 * against the given branch office's address.
169 * @param branchOffice Branch office being checked
171 * @return Whether it has been found
173 private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
175 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
178 final List<BranchOffice> branchOffices = this.branchOfficeBean.fetchAllBranchOffices();
180 // Default is not found
181 boolean isFound = false;
183 // Check all single addresses
184 for (final BranchOffice bo : branchOffices) {
185 // Is the same address found?
186 if (BranchOffices.isSameAddress(bo, branchOffice)) {
194 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N