2 * Copyright (C) 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jcontactsbusiness.model.basicdata;
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;
29 * A stateless session bean for business data
31 * @author Roland Häder<roland@mxchange.org>
33 @Stateless (name = "basicCompanyData", description = "A general statless bean for handling business data (all)")
34 public class JobsBusinessDataSessionBean extends BaseJobsDatabaseBean implements BasicCompanyDataSessionBeanRemote {
39 private static final long serialVersionUID = 56_389_504_892_184_571L;
44 public JobsBusinessDataSessionBean () {
45 // Call super constructor
50 @SuppressWarnings ("unchecked")
51 public List<BusinessBasicData> allCompanyBasicData () {
53 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: CALLED!", this.getClass().getSimpleName())); //NOI18N
56 final Query query = this.getEntityManager().createNamedQuery("AllBusinessData"); //NOI18N
59 final List<BusinessBasicData> list = query.getResultList();
62 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
69 public BusinessBasicData findBasicDataById (final Long basicDataId) throws BasicCompanyDataNotFoundException {
71 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: CALLED!", this.getClass().getSimpleName())); //NOI18N
74 if (null == basicDataId) {
76 throw new NullPointerException("basicDataId is null"); //NOI18N
77 } else if (basicDataId < 1) {
79 throw new NullPointerException(MessageFormat.format("basicDataId={0}is invalid", basicDataId)); //NOI18N
83 final Query query = this.getEntityManager().createNamedQuery("SearchBusinessDataById", CompanyBasicData.class); //NOI18N
86 query.setParameter("basicDataId", basicDataId); //NOI18N
88 // Get single instance
89 final BusinessBasicData basicData;
91 // Try to find a result
93 // Find a single result
94 basicData = (BusinessBasicData) query.getSingleResult();
97 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: Found basicData={1}", this.getClass().getSimpleName(), basicData)); //NOI18N
98 } catch (final NoResultException ex) {
100 throw new BasicCompanyDataNotFoundException(basicDataId, ex);
104 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: basicData={1} - EXIT!", this.getClass().getSimpleName(), basicData)); //NOI18N
111 public Boolean isCompanyNameUsed (final String companyName) {
113 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: companyName={1} - CALLED!", this.getClass().getSimpleName(), companyName)); //NOI18N
115 // Is the parameter valid?
116 if (null == companyName) {
118 throw new NullPointerException("companyName is null"); //NOI18N
119 } else if (companyName.isEmpty()) {
121 throw new IllegalArgumentException("companyName is empty"); //NOI18N
124 // Default is not found
125 Boolean isCompanyNameUsed = Boolean.FALSE;
128 final List<BusinessBasicData> list = this.allCompanyBasicData();
131 for (final BusinessBasicData basicData : list) {
133 if (Objects.equals(basicData.getCompanyName(), companyName)) {
135 isCompanyNameUsed = Boolean.TRUE;
143 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: isCompanyNameUsed={1} - EXIT!", this.getClass().getSimpleName(), isCompanyNameUsed)); //NOI18N
146 return isCompanyNameUsed;