]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jcontactsbusiness/model/branchoffice/PizzaBranchOfficeSessionBean.java
Please cherry-pick:
[pizzaservice-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / branchoffice / PizzaBranchOfficeSessionBean.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.branchoffice;
18
19 import java.text.MessageFormat;
20 import java.util.List;
21 import javax.ejb.Stateless;
22 import javax.persistence.NoResultException;
23 import javax.persistence.Query;
24 import org.mxchange.pizzaaplication.database.BasePizzaEnterpriseBean;
25 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
26
27 /**
28  * A stateless session bean for general branch office purposes
29  * <p>
30  * @author Roland Häder<roland@mxchange.org>
31  */
32 @Stateless (name = "branchOffice", description = "A general statless bean for handling branch office data (all)")
33 public class PizzaBranchOfficeSessionBean extends BasePizzaEnterpriseBean implements BranchOfficeSessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 58_467_386_571_701L;
39
40         @Override
41         @SuppressWarnings ("unchecked")
42         public List<BranchOffice> allBranchOffices () {
43                 // Trace message
44                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBranchOffices: CALLED!", this.getClass().getSimpleName())); //NOI18N
45
46                 // Get query
47                 final Query query = this.getEntityManager().createNamedQuery("AllBranchOffices"); //NOI18N
48
49                 // Get list from it
50                 final List<BranchOffice> list = query.getResultList();
51
52                 // Trace message
53                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allBranchOffices: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
54
55                 // Return it
56                 return list;
57         }
58
59         @Override
60         public BranchOffice findBranchOfficeById (final Long branchOfficeId) throws BranchOfficeNotFoundException {
61                 // Trace message
62                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBranchOfficeById: CALLED!", this.getClass().getSimpleName())); //NOI18N
63
64                 // Validate parameter
65                 if (null == branchOfficeId) {
66                         // Throw NPE
67                         throw new NullPointerException("branchOfficeId is null"); //NOI18N
68                 } else if (branchOfficeId < 1) {
69                         // Throw NPE
70                         throw new NullPointerException(MessageFormat.format("branchOfficeId={0}is invalid", branchOfficeId)); //NOI18N
71                 }
72
73                 // Get query
74                 final Query query = this.getEntityManager().createNamedQuery("SearchBranchOfficeById", CompanyBranchOffice.class); //NOI18N
75
76                 // Set parameter
77                 query.setParameter("branchOfficeId", branchOfficeId); //NOI18N
78
79                 // Get single instance
80                 final BranchOffice branchOffice;
81
82                 // Try to find a result
83                 try {
84                         // Find a single result
85                         branchOffice = (BranchOffice) query.getSingleResult();
86
87                         // Log trace message
88                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBranchOfficeById: Found branchOffice={1}", this.getClass().getSimpleName(), branchOffice)); //NOI18N
89                 } catch (final NoResultException ex) {
90                         // No result found
91                         throw new BranchOfficeNotFoundException(branchOfficeId, ex);
92                 }
93
94                 // Trace message
95                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBranchOfficeById: branchOffice={1} - EXIT!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
96
97                 // Return it
98                 return branchOffice;
99         }
100
101 }