]> git.mxchange.org Git - addressbook-ejb.git/blob - src/java/org/mxchange/jcontactsbusiness/model/employee/AddressbookCompanyEmployeeSessionBean.java
8d8673a51f3533a7d23759034d1462c621e5790b
[addressbook-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / employee / AddressbookCompanyEmployeeSessionBean.java
1 /*
2  * Copyright (C) 2020 Free Software Foundation
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.employee;
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.addressbook.database.BaseAddressbookDatabaseBean;
25 import org.mxchange.jcontactsbusiness.exceptions.employee.CompanyEmployeeNotFoundException;
26
27 /**
28  * A stateless bean for general purposes for company employees.
29  * <p>
30  * @author Roland Häder<roland@mxchange.org>
31  */
32 @Stateless (name = "companyEmployee", description = "A general statless bean for handling company employees")
33 public class AddressbookCompanyEmployeeSessionBean extends BaseAddressbookDatabaseBean implements CompanyEmployeeSessionBeanRemote {
34
35         /**
36          * Serial number
37          */
38         private static final long serialVersionUID = 26_458_796_703_761L;
39
40         /**
41          * Default constructor
42          */
43         public AddressbookCompanyEmployeeSessionBean () {
44                 super();
45         }
46
47         @Override
48         @SuppressWarnings ("unchecked")
49         public List<Employee> allCompanyEmployees () {
50                 // Trace message
51                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCompanyEmployees(): CALLED!", this.getClass().getSimpleName())); //NOI18N
52
53                 // Get named query
54                 final Query query = this.getEntityManager().createNamedQuery("AllCompanyEmployees"); //NOI18N
55
56                 // Get list form it
57                 final List<Employee> employees = query.getResultList();
58
59                 // Trace message
60                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCompanyEmployees(): employees.size()={1} - EXIT!", this.getClass().getSimpleName(), employees.size())); //NOI18N
61
62                 // Return it
63                 return employees;
64         }
65
66         @Override
67         public Employee findCompanyEmployeeById (final Long employeeId) throws CompanyEmployeeNotFoundException {
68                 // Trace message
69                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findCompanyEmployeeById(): employeeId={1} - CALLED!", this.getClass().getSimpleName(), employeeId)); //NOI18N
70
71                 // Is the employee id valid?
72                 if (null == employeeId) {
73                         // Throw NPE
74                         throw new NullPointerException("employeeId is null"); //NOI18N
75                 } else if (employeeId < 1) {
76                         // Not valid
77                         throw new IllegalArgumentException(MessageFormat.format("employeeId={0} is not valid", employeeId)); //NOI18N
78                 }
79
80                 // Now get named query
81                 final Query query = this.getEntityManager().createNamedQuery("SearchCompanyEmployeeById"); //NOI18N
82
83                 // Set parameter
84                 query.setParameter("employeeId", employeeId); //NOI18N
85
86                 // Declare instance
87                 final Employee employee;
88
89                 // Try to find a result
90                 try {
91                         // Find a single result
92                         employee = (Employee) query.getSingleResult();
93
94                         // Log trace message
95                         this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findCompanyEmployeeById: Found employee={1}", this.getClass().getSimpleName(), employee)); //NOI18N
96                 } catch (final NoResultException ex) {
97                         // No result found
98                         throw new CompanyEmployeeNotFoundException(employeeId, ex);
99                 }
100
101                 // Log trace message
102                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findCompanyEmployeeById: employee={1} - EXIT!", this.getClass().getSimpleName(), employee)); //NOI18N
103
104                 // Return found instance
105                 return employee;
106         }
107
108 }