From: Roland Haeder Date: Sun, 17 Apr 2016 10:38:39 +0000 (+0200) Subject: Continued a bit: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=adf7db30a8054a66c931a61e8d5a42fea43029f7;p=pizzaservice-mailer-ejb.git Continued a bit: - added session bean for handling phone number lookups, maybe it will be used for user-depending purposes later on. So maybe session-scoped is not a bad idea after all. - updated jar(s) Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jphone/phonenumbers/phone/PizzaPhoneSessionBean.java b/src/java/org/mxchange/jphone/phonenumbers/phone/PizzaPhoneSessionBean.java new file mode 100644 index 0000000..b903705 --- /dev/null +++ b/src/java/org/mxchange/jphone/phonenumbers/phone/PizzaPhoneSessionBean.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2016 Roland Haeder + * + * 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.jphone.phonenumbers.phone; + +import java.text.MessageFormat; +import javax.ejb.Stateless; +import javax.persistence.NoResultException; +import javax.persistence.Query; +import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException; +import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber; +import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; + +/** + * A general phone EJB + *

+ * @author Roland Haeder + */ +@Stateless (name = "phone", mappedName = "ejb/stateless-pizza-phone", description = "A bean handling phone data") +public class PizzaPhoneSessionBean extends BaseDatabaseBean implements PhoneSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 134_945_698_127_601L; + + /** + * Default constructor + */ + public PizzaPhoneSessionBean () { + } + + @Override + public DialableCellphoneNumber findCellphoneById (final Long cellphoneId) throws PhoneEntityNotFoundException { + // The id number should be valid + if (null == cellphoneId) { + // Throw NPE + throw new NullPointerException("cellphoneId is null"); //NOI18N + } else if (cellphoneId < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("cellphoneId={0} is not valid.", cellphoneId)); //NOI18N + } + + // Now find it + Query query = this.getEntityManager().createNamedQuery("SearchCellphoneId", CellphoneNumber.class); //NOI18N + + // Set parameter + query.setParameter("cellphoneId", cellphoneId); //NOI18N + + // Init instance + DialableCellphoneNumber cellphone = null; + + // Try to get a result + try { + // Get a single result + cellphone = (DialableCellphoneNumber) query.getSingleResult(); + } catch (NoResultException ex) { + // The entry was not found, so throw it again + throw new PhoneEntityNotFoundException(cellphoneId, ex); + } + + // Return found instance + return cellphone; + } + +}