7f5a681b50d543a1bed9f2bb18c6d0db1253509f
[JavaForFun] /
1 package de.spring.example.services.impl;
2
3 import javax.inject.Inject;
4 import javax.inject.Named;
5
6 import org.resthub.common.service.CrudServiceImpl;
7 import org.springframework.data.domain.Page;
8 import org.springframework.data.domain.Pageable;
9
10 import com.querydsl.core.types.dsl.BooleanExpression;
11
12 import de.spring.example.persistence.domain.AdDescription;
13 import de.spring.example.persistence.domain.QAdDescription;
14 import de.spring.example.persistence.repository.AdDescriptionRepository;
15 import de.spring.example.services.AdDescriptionService;
16
17 @Named("adDescriptionService")
18 public class AdDescriptionServiceImpl
19         extends CrudServiceImpl<AdDescription, Long, AdDescriptionRepository>
20         implements AdDescriptionService {
21
22         @Override
23         @Inject
24     public void setRepository(AdDescriptionRepository repository) {
25         this.repository = repository;
26     }
27
28         // Extending CrudServiceImpl when we need some business logic. Otherwise we would be using
29         // the JPA repositories and nothing else :)
30         
31         // In this case there is any business logic, but this is just an example.
32         
33         
34         /**
35          * Using Querydsl. Giving some business logic to this service :)
36          * 
37          * Querydsl: fluent interface done easy. There is no effort because it is already implemented.
38          * 
39          * Criteria using Specifications requires some effort.
40          * 
41          * See: de.spring.example.services.impl.AdServiceImpl
42          */
43         @Override
44         public Page<AdDescription> queryDslExample(Pageable pageRequest) {
45                 final QAdDescription adDescription = QAdDescription.adDescription1;
46                 final BooleanExpression adDescriptionHasAdLink = adDescription.adLink.contains("gumartinm");
47                 final BooleanExpression adDescriptionHasDescription = adDescription.adDescription.contains("babucha");
48                 
49                 return repository.findAll(adDescriptionHasAdLink.and(adDescriptionHasDescription), pageRequest);
50         }
51 }