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.Date;
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 Date());
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);
104 // Set "created" timestamp on any number assigned
105 this.setAllPhoneEntriesCreated(branchOffice);
108 this.getEntityManager().persist(branchOffice);
111 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
113 // Return updated instance
118 * Checks if given branch office's address is already persisted. The whole
119 * (persisted) list is being loaded and each address is being matched
120 * against the given branch office's address.
122 * @param branchOffice Branch office being checked
124 * @return Whether it has been found
126 private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
128 final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
130 // Default is not found
131 boolean isFound = false;
133 // Check all single addresses
134 for (final BranchOffice bo : branchOffices) {
135 // Is the same address found?
136 if (BranchOffices.isSameAddress(bo, branchOffice)) {