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.headquarter;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
23 import javax.ejb.Stateless;
24 import org.mxchange.jcontacts.model.contact.Contact;
25 import org.mxchange.jcontactsbusiness.exceptions.headquarter.HeadquarterAlreadyAddedException;
26 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
27 import org.mxchange.jcountry.model.data.Country;
28 import org.mxchange.jfinancials.enterprise.BaseFinancialsEnterpriseBean;
29 import org.mxchange.jusercore.model.user.User;
32 * A stateless session bean for administrative branch office purposes
34 * @author Roland Häder<roland@mxchange.org>
36 @Stateless (name = "adminHeadquarter", description = "An administrative statless bean for handling branch office data (all)")
37 public class FinancialsAdminHeadquarterSessionBean extends BaseFinancialsEnterpriseBean implements AdminHeadquarterSessionBeanRemote {
42 private static final long serialVersionUID = 58_467_386_571_706L;
45 * General branch office bean
47 @EJB (lookup = "java:global/jfinancials-ejb/headquarter!org.mxchange.jcontactsbusiness.model.headquarter.HeadquarterSessionBeanRemote")
48 private HeadquarterSessionBeanRemote headquarterBean;
53 public FinancialsAdminHeadquarterSessionBean () {
54 // Call super constructor
59 public Headquarter addHeadquarter (final Headquarter headquarter) throws HeadquarterAlreadyAddedException {
61 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addHeadquarter(): headquarter={1} - CALLED!", this.getClass().getSimpleName(), headquarter)); //NOI18N
64 if (null == headquarter) {
66 throw new NullPointerException("headquarter is null"); //NOI18N
67 } else if (headquarter.getHeadquarterId() instanceof Long) {
69 throw new IllegalArgumentException("headquarter.branchId should not be set."); //NOI18N
70 } else if (this.isHeadquarterFound(headquarter)) {
71 // Already added, abort here
72 throw new HeadquarterAlreadyAddedException(headquarter);
75 // Add created timestamp
76 headquarter.setHeadquarterEntryCreated(new Date());
78 // Is contact employee set?
79 if (headquarter.getHeadquarterContactEmployee() instanceof Contact) {
80 // Get managed lead contact
81 final Contact managedContact = this.createManaged(headquarter.getHeadquarterContactEmployee());
84 headquarter.setHeadquarterContactEmployee(managedContact);
87 // Is user instance set?
88 if (headquarter.getHeadquarterUserOwner() instanceof User) {
89 // Get managed instance back
90 final User managedUser = this.createManaged(headquarter.getHeadquarterUserOwner());
92 // Set it back in branch office
93 headquarter.setHeadquarterUserOwner(managedUser);
96 // Is user instance set?
97 if (headquarter.getHeadquarterCountry() instanceof Country) {
98 // Get managed instance back
99 final Country managedCountry = this.createManaged(headquarter.getHeadquarterCountry());
101 // Set it back in branch office
102 headquarter.setHeadquarterCountry(managedCountry);
105 // Set "created" timestamp on any number assigned
106 this.setAllPhoneEntriesCreated(headquarter);
109 final List<OpeningTime> openingTimes = headquarter.getHeadquarterOpeningTimes();
112 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addHeadquarter(): headquarter.headquarterOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes));
114 // Is opening times set and not empty?
115 if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) {
116 // Add created timestamp for all times
117 this.setAllOpeningTimesCreated(openingTimes);
120 headquarter.setHeadquarterOpeningTimes(null);
124 this.getEntityManager().persist(headquarter);
127 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addHeadquarter(): headquarter.branchId={1} - EXIT!", this.getClass().getSimpleName(), headquarter.getHeadquarterId())); //NOI18N
129 // Return updated instance
134 * Checks if given branch office's address is already persisted. The whole
135 * (persisted) list is being loaded and each address is being matched
136 * against the given branch office's address.
138 * @param headquarter Headquarter office being checked
140 * @return Whether it has been found
142 private boolean isHeadquarterFound (final Headquarter headquarter) {
144 final List<Headquarter> headquarters = this.headquarterBean.fetchAllHeadquarters();
146 // Default is not found
147 boolean isFound = false;
149 // Check all single addresses
150 for (final Headquarter hq : headquarters) {
151 // Is the same address found?
152 if (Headquarters.isSameAddress(hq, headquarter)) {