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