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