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.basicdata;
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.addressbook.database.BaseAddressbookDatabaseBean;
26 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BasicDataAlreadyAddedException;
27 import org.mxchange.jcontactsbusiness.model.employee.Employable;
28 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
29 import org.mxchange.jusercore.model.user.User;
32 * An administrative stateless session bean for business data
34 * @author Roland Häder<roland@mxchange.org>
36 @Stateless (name = "adminBasicCompanyData", description = "An administrative statless bean for handling business data (all)")
37 public class AddressbookAdminBusinessDataSessionBean extends BaseAddressbookDatabaseBean implements AdminBasicCompanyDataSessionBeanRemote {
42 private static final long serialVersionUID = 56_389_504_892_184_572L;
48 private BasicCompanyDataSessionBeanRemote businessDataBean;
53 public AddressbookAdminBusinessDataSessionBean () {
54 // Call super constructor
59 public BasicData addBusinessBasicData (final BasicData basicData) throws BasicDataAlreadyAddedException {
61 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBusinessBasicData: basicData={1} - CALLED!", this.getClass().getSimpleName(), basicData)); //NOI18N
63 // Is the instance set?
64 if (null == basicData) {
66 throw new NullPointerException("basicData is null"); //NOI18N
67 } else if (basicData.getBasicDataId() != null) {
69 throw new IllegalArgumentException(MessageFormat.format("basicData.basicDataId={0} - is not null", basicData.getBasicDataId())); //NOI18N
70 } else if (this.isSameCompanyNameAdded(basicData)) {
72 throw new BasicDataAlreadyAddedException(basicData);
75 // Now add current date
76 basicData.setCompanyCreated(new Date());
78 // Is there a owner set?
79 if (basicData.getCompanyUserOwner() instanceof User) {
80 // Get managed instance
81 final User managedUser = this.createManaged(basicData.getCompanyUserOwner());
84 basicData.setCompanyUserOwner(managedUser);
88 if (basicData.getCompanyFounder() instanceof Employable) {
89 // Get managed instance
90 final Employable managedEmployee = this.createManaged(basicData.getCompanyFounder());
93 basicData.setCompanyFounder(managedEmployee);
96 // Is a contact person set?
97 if (basicData.getCompanyContactEmployee() instanceof Employable) {
98 // Get managed instance
99 final Employable managedEmployee = this.createManaged(basicData.getCompanyContactEmployee());
102 basicData.setCompanyContactEmployee(managedEmployee);
105 // Is a headquarter set?
106 if (basicData.getCompanyHeadquarterData()instanceof Headquarter) {
107 // Get managed instance
108 final Headquarter managedHeadquarter = this.createManaged(basicData.getCompanyHeadquarterData());
111 basicData.setCompanyHeadquarterData(managedHeadquarter);
114 // Set created timestamps for any assigned numbers
115 this.setAllPhoneEntriesCreated(basicData);
118 this.getEntityManager().persist(basicData);
121 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBusinessBasicData: basicData.basicDataId={1} - EXIT!", this.getClass().getSimpleName(), basicData.getBasicDataId())); //NOI18N
123 // Return updated instance
128 * Checks if given basic data is already added by it's company name
130 * @param basicData Basic data to be checked
132 * @return Whether same company name has been used
134 private boolean isSameCompanyNameAdded (final BasicData basicData) {
135 // Get all available entries
136 final List<BasicData> list = this.businessDataBean.allBusinessBasicData();
138 // Default is not found
139 boolean isFound = false;
141 // Then check each entry
142 for (final BasicData entry : list) {
143 // Is the company name matching?
144 if (Objects.equals(entry.getCompanyName(), basicData.getCompanyName())) {