[LIB-9] Implementation SPI for services, managers and adapters
[chesshog.git] / chesshog-db-etude / src / main / java / org / hedgecode / chess / service / jpa / JpaAuthorService.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.service.jpa;
18
19 import java.util.Date;
20 import java.util.List;
21
22 import javax.persistence.Query;
23
24 import org.hedgecode.chess.domain.Author;
25 import org.hedgecode.chess.service.AuthorService;
26
27 /**
28  *
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public class JpaAuthorService extends JpaDomainService<Author> implements AuthorService {
33
34     @Override
35     @SuppressWarnings(
36             "unchecked"
37     )
38     public List<Author> findAll() {
39         return getEntityManager().createNamedQuery(
40                 Author.FIND_ALL
41         ).getResultList();
42     }
43
44     @Override
45     @SuppressWarnings(
46             "unchecked"
47     )
48     public Author findById(Long id) {
49         Query query = getEntityManager().createNamedQuery(
50                 Author.FIND_BY_ID
51         );
52         query.setParameter(Author.ID_PARAMETER, id);
53         return (Author) query.getSingleResult();
54     }
55
56     @Override
57     @SuppressWarnings(
58             "unchecked"
59     )
60     public List<Author> findByName(String name) {
61         Query query = getEntityManager().createNamedQuery(
62                 Author.FIND_BY_NAME
63         );
64         query.setParameter(Author.NAME_PARAMETER, name);
65         return query.getResultList();
66     }
67
68     @Override
69     @SuppressWarnings(
70             "unchecked"
71     )
72     public List<Author> findByBirthDate(Date birthdate) {
73         Query query = getEntityManager().createNamedQuery(
74                 Author.FIND_BY_BIRTHDATE
75         );
76         query.setParameter(Author.BIRTHDATE_PARAMETER, birthdate);
77         return query.getResultList();
78     }
79
80 }