]> 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.jjobs.database.BaseJobsDatabaseBean;
26 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BasicCompanyDataNotFoundException;
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 BasicCompanyDataNotFoundException {
70                 // Trace message
71                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: CALLED!", this.getClass().getSimpleName())); //NOI18N
72
73                 // Validate parameter
74                 if (null == basicDataId) {
75                         // Throw NPE
76                         throw new NullPointerException("basicDataId is null"); //NOI18N
77                 } else if (basicDataId < 1) {
78                         // Throw NPE
79                         throw new NullPointerException(MessageFormat.format("basicDataId={0}is invalid", basicDataId)); //NOI18N
80                 }
81
82                 // Get query
83                 final Query query = this.getEntityManager().createNamedQuery("SearchBusinessDataById", CompanyBasicData.class); //NOI18N
84
85                 // Set parameter
86                 query.setParameter("basicDataId", basicDataId); //NOI18N
87
88                 // Get single instance
89                 final BusinessBasicData basicData;
90
91                 // Try to find a result
92                 try {
93                         // Find a single result
94                         basicData = (BusinessBasicData) query.getSingleResult();
95
96                         // Log trace message
97                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: Found basicData={1}", this.getClass().getSimpleName(), basicData)); //NOI18N
98                 } catch (final NoResultException ex) {
99                         // No result found
100                         throw new BasicCompanyDataNotFoundException(basicDataId, ex);
101                 }
102
103                 // Trace message
104                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: basicData={1} - EXIT!", this.getClass().getSimpleName(), basicData)); //NOI18N
105
106                 // Return it
107                 return basicData;
108         }
109
110         @Override
111         public Boolean isCompanyNameUsed (final String companyName) {
112                 // Trace message
113                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: companyName={1} - CALLED!", this.getClass().getSimpleName(), companyName)); //NOI18N
114
115                 // Is the parameter valid?
116                 if (null == companyName) {
117                         // Throw NPE
118                         throw new NullPointerException("companyName is null"); //NOI18N
119                 } else if (companyName.isEmpty()) {
120                         // Throw IAE
121                         throw new IllegalArgumentException("companyName is empty"); //NOI18N
122                 }
123
124                 // Default is not found
125                 Boolean isCompanyNameUsed = Boolean.FALSE;
126
127                 // Get whole list
128                 final List<BusinessBasicData> list = this.allCompanyBasicData();
129
130                 // Check whole list
131                 for (final BusinessBasicData basicData : list) {
132                         // Is name used?
133                         if (Objects.equals(basicData.getCompanyName(), companyName)) {
134                                 // Found one
135                                 isCompanyNameUsed = Boolean.TRUE;
136
137                                 // Abort search
138                                 break;
139                         }
140                 }
141
142                 // Trace message
143                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: isCompanyNameUsed={1} - EXIT!", this.getClass().getSimpleName(), isCompanyNameUsed)); //NOI18N
144
145                 // Return it
146                 return isCompanyNameUsed;
147         }
148
149 }