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.jcontactsbusiness.basicdata.BusinessDataSessionBeanRemote;
26 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BusinessDataNotFoundException;
27 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
30 * A stateless session bean for business data
32 * @author Roland Häder<roland@mxchange.org>
34 @Stateless (name = "businessData", description = "A general statless bean for handling business data (all)")
35 public class JobsBusinessDataSessionBean extends BaseJobsDatabaseBean implements BusinessDataSessionBeanRemote {
40 private static final long serialVersionUID = 56_389_504_892_184_571L;
45 public JobsBusinessDataSessionBean () {
46 // Call super constructor
51 @SuppressWarnings ("unchecked")
52 public List<BusinessBasicData> allCompanyBasicData () {
54 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: CALLED!", this.getClass().getSimpleName())); //NOI18N
57 final Query query = this.getEntityManager().createNamedQuery("AllBusinessData"); //NOI18N
60 final List<BusinessBasicData> list = query.getResultList();
63 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
70 public BusinessBasicData findBasicDataById (final Long basicDataId) throws BusinessDataNotFoundException {
72 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: CALLED!", this.getClass().getSimpleName())); //NOI18N
75 final Query query = this.getEntityManager().createNamedQuery("SearchBusinessDataById", CompanyBasicData.class); //NOI18N
78 query.setParameter("basicDataId", basicDataId); //NOI18N
80 // Get single instance
81 final BusinessBasicData basicData;
83 // Try to find a result
85 // Find a single result
86 basicData = (BusinessBasicData) query.getSingleResult();
89 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: Found basicData={1}", this.getClass().getSimpleName(), basicData)); //NOI18N
90 } catch (final NoResultException ex) {
92 throw new BusinessDataNotFoundException(basicDataId, ex);
96 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: basicData={1} - EXIT!", this.getClass().getSimpleName(), basicData)); //NOI18N
103 public Boolean isCompanyNameUsed (final String companyName) {
105 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: companyName={1} - CALLED!", this.getClass().getSimpleName(), companyName)); //NOI18N
107 // Is the parameter valid?
108 if (null == companyName) {
110 throw new NullPointerException("companyName is null"); //NOI18N
111 } else if (companyName.isEmpty()) {
113 throw new IllegalArgumentException("companyName is empty"); //NOI18N
116 // Default is not found
117 Boolean isCompanyNameUsed = Boolean.FALSE;
120 final List<BusinessBasicData> list = this.allCompanyBasicData();
123 for (final BusinessBasicData basicData : list) {
125 if (Objects.equals(basicData.getCompanyName(), companyName)) {
127 isCompanyNameUsed = Boolean.TRUE;
135 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isCompanyNameUsed: isCompanyNameUsed={1} - EXIT!", this.getClass().getSimpleName(), isCompanyNameUsed)); //NOI18N
138 return isCompanyNameUsed;