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