2 * Copyright (C) 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.GregorianCalendar;
21 import java.util.List;
23 import javax.ejb.Stateless;
24 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
25 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
26 import org.mxchange.jcontactsbusiness.model.basicdata.BusinessBasicData;
27 import org.mxchange.jcountry.model.data.Country;
28 import org.mxchange.jusercore.model.user.User;
31 * A stateless session bean for administrative branch office purposes
33 * @author Roland Häder<roland@mxchange.org>
35 @Stateless (name = "adminBranchOffice", description = "An administrative statless bean for handling branch office data (all)")
36 public class AddressbookAdminBranchOfficeSessionBean extends BaseAddressbookDatabaseBean implements AdminBranchOfficeSessionBeanRemote {
41 private static final long serialVersionUID = 58_467_386_571_701L;
44 * General branch office bean
47 private BranchOfficeSessionBeanRemote branchOfficeBean;
52 public AddressbookAdminBranchOfficeSessionBean () {
53 // Call super constructor
58 public BranchOffice addBranchOffice (final BranchOffice branchOffice) throws BranchOfficeAlreadyAddedException {
60 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
63 if (null == branchOffice) {
65 throw new NullPointerException("branchOffice is null"); //NOI18N
66 } else if (branchOffice.getBranchId() instanceof Long) {
68 throw new IllegalArgumentException("branchOffice.branchId should not be set."); //NOI18N
69 } else if (this.isBranchOfficeFound(branchOffice)) {
70 // Already added, abort here
71 throw new BranchOfficeAlreadyAddedException(branchOffice);
74 // Add created timestamp
75 branchOffice.setBranchCreated(new GregorianCalendar());
77 // Is user instance set?
78 if (branchOffice.getBranchCompany() instanceof BusinessBasicData) {
79 // Get managed instance back
80 final BusinessBasicData managedBasicData = this.createManaged(branchOffice.getBranchCompany());
82 // Set it back in branch office
83 branchOffice.setBranchCompany(managedBasicData);
86 // Is user instance set?
87 if (branchOffice.getBranchUserOwner() instanceof User) {
88 // Get managed instance back
89 final User managedUser = this.createManaged(branchOffice.getBranchUserOwner());
91 // Set it back in branch office
92 branchOffice.setBranchUserOwner(managedUser);
95 // Is user instance set?
96 if (branchOffice.getBranchCountry() instanceof Country) {
97 // Get managed instance back
98 final Country managedCountry = this.createManaged(branchOffice.getBranchCountry());
100 // Set it back in branch office
101 branchOffice.setBranchCountry(managedCountry);
105 this.getEntityManager().persist(branchOffice);
108 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
110 // Return updated instance
115 * Checks if given branch office's address is already persisted. The whole
116 * (persisted) list is being loaded and each address is being matched
117 * against the given branch office's address.
119 * @param branchOffice Branch office being checked
121 * @return Whether it has been found
123 private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
125 final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
127 // Default is not found
128 boolean isFound = false;
130 // Check all single addresses
131 for (final BranchOffice bo : branchOffices) {
132 // Is the same address found?
133 if (BranchOffices.isSameAddress(bo, branchOffice)) {