]> git.mxchange.org Git - addressbook-ejb.git/blob
b682509764e33becb5f549214877a8516fb4b5a6
[addressbook-ejb.git] /
1 /*
2  * Copyright (C) 2020 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcontactsbusiness.model.basicdata;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.ejb.EJB;
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;
30
31 /**
32  * An administrative stateless session bean for business data
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @Stateless (name = "adminBasicCompanyData", description = "An administrative statless bean for handling business data (all)")
37 public class AddressbookAdminBusinessDataSessionBean extends BaseAddressbookDatabaseBean implements AdminBasicCompanyDataSessionBeanRemote {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 56_389_504_892_184_572L;
43
44         /**
45          * Administrative EJB
46          */
47         @EJB
48         private BasicCompanyDataSessionBeanRemote businessDataBean;
49
50         /**
51          * Default constructor
52          */
53         public AddressbookAdminBusinessDataSessionBean () {
54                 // Call super constructor
55                 super();
56         }
57
58         @Override
59         public BasicData addBusinessBasicData (final BasicData basicData) throws BasicDataAlreadyAddedException {
60                 // Trace message
61                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBusinessBasicData: basicData={1} - CALLED!", this.getClass().getSimpleName(), basicData)); //NOI18N
62
63                 // Is the instance set?
64                 if (null == basicData) {
65                         // Throw NPE
66                         throw new NullPointerException("basicData is null"); //NOI18N
67                 } else if (basicData.getBasicDataId() != null) {
68                         // Should be null
69                         throw new IllegalArgumentException(MessageFormat.format("basicData.basicDataId={0} - is not null", basicData.getBasicDataId())); //NOI18N
70                 } else if (basicData.getCompanyShortName() == null) {
71                         // Should not be null
72                         throw new NullPointerException("basicData.companyShortName is null"); //NOI18N
73                 } else if (basicData.getCompanyShortName().isEmpty()) {
74                         // Should not be empty string
75                         throw new IllegalArgumentException("basicData.companyShortName is empty"); //NOI18N
76                 } else if (this.isSameCompanyShortNameAdded(basicData)) {
77                         // Throw exception
78                         throw new BasicDataAlreadyAddedException(basicData);
79                 } else if ((basicData.getCompanyName() != null) && (basicData.getCompanyName().isEmpty())) {
80                         // Should not be empty string when set
81                         throw new IllegalArgumentException("basicData.companyName is empty"); //NOI18N
82                 } else if ((basicData.getCompanyName() != null) && (this.isSameCompanyNameAdded(basicData))) {
83                         // Throw exception
84                         throw new BasicDataAlreadyAddedException(basicData);
85                 }
86
87                 // Now add current date
88                 basicData.setCompanyCreated(new Date());
89
90                 // Is there a owner set?
91                 if (basicData.getCompanyUserOwner() instanceof User) {
92                         // Get managed instance
93                         final User managedUser = this.createManaged(basicData.getCompanyUserOwner());
94
95                         // Set it back
96                         basicData.setCompanyUserOwner(managedUser);
97                 }
98
99                 // Is a founder set?
100                 if (basicData.getCompanyFounder() instanceof Employable) {
101                         // Get managed instance
102                         final Employable managedEmployee = this.createManaged(basicData.getCompanyFounder());
103
104                         // Set it back
105                         basicData.setCompanyFounder(managedEmployee);
106                 }
107
108                 // Is a contact person set?
109                 if (basicData.getCompanyContactEmployee() instanceof Employable) {
110                         // Get managed instance
111                         final Employable managedEmployee = this.createManaged(basicData.getCompanyContactEmployee());
112
113                         // Set it back
114                         basicData.setCompanyContactEmployee(managedEmployee);
115                 }
116
117                 // Is a headquarter set?
118                 if (basicData.getCompanyHeadquarterData() instanceof Headquarter) {
119                         // Get managed instance
120                         final Headquarter managedHeadquarter = this.createManaged(basicData.getCompanyHeadquarterData());
121
122                         // Set it back
123                         basicData.setCompanyHeadquarterData(managedHeadquarter);
124                 }
125
126                 // Set created timestamps for any assigned numbers
127                 this.setAllPhoneEntriesCreated(basicData);
128
129                 // Persist it
130                 this.getEntityManager().persist(basicData);
131
132                 // Trace message
133                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBusinessBasicData: basicData.basicDataId={1} - EXIT!", this.getClass().getSimpleName(), basicData.getBasicDataId())); //NOI18N
134
135                 // Return updated instance
136                 return basicData;
137         }
138
139         /**
140          * Checks if given basic data is already added by it's company name
141          * <p>
142          * @param basicData Basic data to be checked
143          *
144          * @return Whether same company name has been used
145          */
146         private boolean isSameCompanyNameAdded (final BasicData basicData) {
147                 // Get all available entries
148                 final List<BasicData> list = this.businessDataBean.allBusinessBasicData();
149
150                 // Default is not found
151                 boolean isFound = false;
152
153                 // Then check each entry
154                 for (final BasicData entry : list) {
155                         // Is the company name matching?
156                         if (Objects.equals(entry.getCompanyName(), basicData.getCompanyName())) {
157                                 // Found match
158                                 isFound = true;
159                         }
160                 }
161
162                 // Return flag
163                 return isFound;
164         }
165
166         /**
167          * Checks if given basic data is already added by it's company short name
168          * <p>
169          * @param basicData Basic data to be checked
170          *
171          * @return Whether same company short name has been used
172          */
173         private boolean isSameCompanyShortNameAdded (final BasicData basicData) {
174                 // Get all available entries
175                 final List<BasicData> list = this.businessDataBean.allBusinessBasicData();
176
177                 // Default is not found
178                 boolean isFound = false;
179
180                 // Then check each entry
181                 for (final BasicData entry : list) {
182                         // Is the company name matching?
183                         if (Objects.equals(entry.getCompanyShortName(), basicData.getCompanyShortName())) {
184                                 // Found match
185                                 isFound = true;
186                         }
187                 }
188
189                 // Return flag
190                 return isFound;
191         }
192
193 }