]> git.mxchange.org Git - addressbook-ejb.git/blob
225881c32429a9a4b8848be48a73601f12d9408c
[addressbook-ejb.git] /
1 /*
2  * Copyright (C) 2017 Roland Häder
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.GregorianCalendar;
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.BusinessDataAlreadyAddedException;
27 import org.mxchange.jcontactsbusiness.model.employee.Employee;
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 BaseAddressbookDatabaseBean 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
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 BusinessBasicData addCompanyBasicData (final BusinessBasicData basicData) throws BusinessDataAlreadyAddedException {
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 (this.isSameCompanyNameAdded(basicData)) {
70                         // Throw exception
71                         throw new BusinessDataAlreadyAddedException(basicData);
72                 }
73
74                 // Now add current date
75                 basicData.setCompanyCreated(new GregorianCalendar());
76
77                 // Is there a owner set?
78                 if (basicData.getCompanyUserOwner() instanceof User) {
79                         // Get managed instance
80                         final User managedUser = this.createManaged(basicData.getCompanyUserOwner());
81
82                         // Set it back
83                         basicData.setCompanyUserOwner(managedUser);
84                 }
85
86                 // Is a founder set?
87                 if (basicData.getCompanyFounder() instanceof Employee) {
88                         // Get managed instance
89                         final Employee managedEmployee = this.createManaged(basicData.getCompanyFounder());
90
91                         // Set it back
92                         basicData.setCompanyFounder(managedEmployee);
93                 }
94
95                 // Is a contact person set?
96                 if (basicData.getCompanyContactEmployee() instanceof Employee) {
97                         // Get managed instance
98                         final Employee managedEmployee = this.createManaged(basicData.getCompanyContactEmployee());
99
100                         // Set it back
101                         basicData.setCompanyContactEmployee(managedEmployee);
102                 }
103
104                 // Persist it
105                 this.getEntityManager().persist(basicData);
106
107                 // Trace message
108                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBusinessBasicData: basicData.basicDataId={1} - EXIT!", this.getClass().getSimpleName(), basicData.getBasicDataId())); //NOI18N
109
110                 // Return updated instance
111                 return basicData;
112         }
113
114         /**
115          * Checks if given basic data is already added by it's company name
116          * <p>
117          * @param basicData Basic data to be checked
118          *
119          * @return Whether same company name has been used
120          */
121         private boolean isSameCompanyNameAdded (final BusinessBasicData basicData) {
122                 // Get all available entries
123                 final List<BusinessBasicData> list = this.businessDataBean.allCompanyBasicData();
124
125                 // Default is not found
126                 boolean isFound = false;
127
128                 // Then check each entry
129                 for (final BusinessBasicData entry : list) {
130                         // Is the company name matching?
131                         if (Objects.equals(entry.getCompanyName(), basicData.getCompanyName())) {
132                                 // Found match
133                                 isFound = true;
134                         }
135                 }
136
137                 // Return flag
138                 return isFound;
139         }
140
141 }