]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jcontactsbusiness/model/basicdata/JobsBusinessDataSessionBean.java
Please cherry-pick:
[jjobs-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / basicdata / JobsBusinessDataSessionBean.java
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.List;
21 import java.util.Objects;
22 import javax.ejb.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.Query;
25 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BusinessDataNotFoundException;
26 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
27
28 /**
29  * A stateless session bean for business data
30  * <p>
31  * @author Roland Häder<roland@mxchange.org>
32  */
33 @Stateless (name = "basicCompanyData", description = "A general statless bean for handling business data (all)")
34 public class JobsBusinessDataSessionBean extends BaseJobsDatabaseBean implements BasicCompanyDataSessionBeanRemote {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 56_389_504_892_184_571L;
40
41         /**
42          * Default constructor
43          */
44         public JobsBusinessDataSessionBean () {
45                 // Call super constructor
46                 super();
47         }
48
49         @Override
50         @SuppressWarnings ("unchecked")
51         public List<BusinessBasicData> allCompanyBasicData () {
52                 // Trace message
53                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: CALLED!", this.getClass().getSimpleName())); //NOI18N
54
55                 // Get query
56                 final Query query = this.getEntityManager().createNamedQuery("AllBusinessData"); //NOI18N
57
58                 // Get list from it
59                 final List<BusinessBasicData> list = query.getResultList();
60
61                 // Trace message
62                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
63
64                 // Return it
65                 return list;
66         }
67
68         @Override
69         public BusinessBasicData findBasicDataById (final Long basicDataId) throws BusinessDataNotFoundException {
70                 // Trace message
71                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: CALLED!", this.getClass().getSimpleName())); //NOI18N
72
73                 // Get query
74                 final Query query = this.getEntityManager().createNamedQuery("SearchBusinessDataById", CompanyBasicData.class); //NOI18N
75
76                 // Set parameter
77                 query.setParameter("basicDataId", basicDataId); //NOI18N
78
79                 // Get single instance
80                 final BusinessBasicData basicData;
81
82                 // Try to find a result
83                 try {
84                         // Find a single result
85                         basicData = (BusinessBasicData) query.getSingleResult();
86
87                         // Log trace message
88                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: Found basicData={1}", this.getClass().getSimpleName(), basicData)); //NOI18N
89                 } catch (final NoResultException ex) {
90                         // No result found
91                         throw new BusinessDataNotFoundException(basicDataId, ex);
92                 }
93
94                 // Trace message
95                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: basicData={1} - EXIT!", this.getClass().getSimpleName(), basicData)); //NOI18N
96
97                 // Return it
98                 return basicData;
99         }
100
101         @Override
102         public Boolean isCompanyNameUsed (final String companyName) {
103                 // Trace message
104                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: companyName={1} - CALLED!", this.getClass().getSimpleName(), companyName)); //NOI18N
105
106                 // Is the parameter valid?
107                 if (null == companyName) {
108                         // Throw NPE
109                         throw new NullPointerException("companyName is null"); //NOI18N
110                 } else if (companyName.isEmpty()) {
111                         // Throw IAE
112                         throw new IllegalArgumentException("companyName is empty"); //NOI18N
113                 }
114
115                 // Default is not found
116                 Boolean isCompanyNameUsed = Boolean.FALSE;
117
118                 // Get whole list
119                 final List<BusinessBasicData> list = this.allCompanyBasicData();
120
121                 // Check whole list
122                 for (final BusinessBasicData basicData : list) {
123                         // Is name used?
124                         if (Objects.equals(basicData.getCompanyName(), companyName)) {
125                                 // Found one
126                                 isCompanyNameUsed = Boolean.TRUE;
127
128                                 // Abort search
129                                 break;
130                         }
131                 }
132
133                 // Trace message
134                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: isCompanyNameUsed={1} - EXIT!", this.getClass().getSimpleName(), isCompanyNameUsed)); //NOI18N
135
136                 // Return it
137                 return isCompanyNameUsed;
138         }
139
140 }