From: Roland Häder Date: Thu, 27 Jul 2017 21:12:48 +0000 (+0200) Subject: Please cherry-pick (when needed): X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e46f27da696415ffd36f5e5ae4cc98e1a1bd9b57;p=jjobs-ejb.git Please cherry-pick (when needed): - duplicated BusinessDataSessionBean as (correctly) AdminBusinessDataSessionBean - implemented generic business data EJB with first method which returns an entity for given id number or throws a proper exception if not found Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jcontactsbusiness/FinancialsAdminBusinessDataSessionBean.java b/src/java/org/mxchange/jcontactsbusiness/FinancialsAdminBusinessDataSessionBean.java new file mode 100644 index 0000000..e579e78 --- /dev/null +++ b/src/java/org/mxchange/jcontactsbusiness/FinancialsAdminBusinessDataSessionBean.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcontactsbusiness; + +import java.text.MessageFormat; +import java.util.List; +import javax.ejb.Stateless; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; + +/** + * An administrative stateless session bean for business data + *

+ * @author Roland Häder + */ +@Stateless (name = "adminBusinessData", description = "An administrative statless bean for handling business data (all)") +public class FinancialsAdminBusinessDataSessionBean extends BaseFinancialsDatabaseBean implements BusinessDataAdminSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 56_389_504_892_184_572L; + + /** + * Default constructor + */ + public FinancialsAdminBusinessDataSessionBean () { + // Call super constructor + super(); + } + + @Override + @SuppressWarnings ("unchecked") + public List allBusinessContacts () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + Query query = this.getEntityManager().createNamedQuery("AllBusinessData"); //NOI18N + + // Get list from it + List list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + +} diff --git a/src/java/org/mxchange/jcontactsbusiness/FinancialsBusinessDataSessionBean.java b/src/java/org/mxchange/jcontactsbusiness/FinancialsBusinessDataSessionBean.java index be569fe..320e573 100644 --- a/src/java/org/mxchange/jcontactsbusiness/FinancialsBusinessDataSessionBean.java +++ b/src/java/org/mxchange/jcontactsbusiness/FinancialsBusinessDataSessionBean.java @@ -17,9 +17,10 @@ package org.mxchange.jcontactsbusiness; import java.text.MessageFormat; -import java.util.List; import javax.ejb.Stateless; +import javax.persistence.NoResultException; import javax.persistence.Query; +import org.mxchange.jcontactsbusiness.exceptions.BusinessDataNotFoundException; import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; /** @@ -27,8 +28,8 @@ import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; *

* @author Roland Häder */ -@Stateless (name = "adminBusinessData", description = "An administrative statless bean for handling business data (all)") -public class FinancialsBusinessDataSessionBean extends BaseFinancialsDatabaseBean implements BusinessDataAdminSessionBeanRemote { +@Stateless (name = "businessData", description = "A general statless bean for handling business data (all)") +public class FinancialsBusinessDataSessionBean extends BaseFinancialsDatabaseBean implements BusinessDataSessionBeanRemote { /** * Serial number @@ -44,22 +45,36 @@ public class FinancialsBusinessDataSessionBean extends BaseFinancialsDatabaseBea } @Override - @SuppressWarnings ("unchecked") - public List allBusinessContacts () { + public BusinessBasicData findBusinessDataById (final Long businessDataId) throws BusinessDataNotFoundException { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: CALLED!", this.getClass().getSimpleName())); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBusinessDataById: CALLED!", this.getClass().getSimpleName())); //NOI18N // Get query - Query query = this.getEntityManager().createNamedQuery("AllBusinessData"); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchBusinessDataById", CompanyBasicData.class); //NOI18N - // Get list from it - List list = query.getResultList(); + // Set parameter + query.setParameter("businessDataId", businessDataId); //NOI18N + + // Get single instance + BusinessBasicData businessData = null; + + // Try to find a result + try { + // Find a single result + businessData = (BusinessBasicData) query.getSingleResult(); + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBusinessDataById: Found contact={1}", this.getClass().getSimpleName(), businessData)); //NOI18N + } catch (final NoResultException ex) { + // No result found + throw new BusinessDataNotFoundException(businessDataId, ex); + } // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBusinessContacts: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBusinessDataById: businessData={1} - EXIT!", this.getClass().getSimpleName(), businessData)); //NOI18N // Return it - return list; + return businessData; } }