1 | /* |
2 | * Copyright 2013 the original author or authors. |
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 | package org.springframework.data.elasticsearch.core.query; |
17 | |
18 | |
19 | import org.springframework.data.domain.Pageable; |
20 | import org.springframework.util.Assert; |
21 | |
22 | /** |
23 | * CriteriaQuery |
24 | * |
25 | * @author Rizwan Idrees |
26 | * @author Mohsin Husen |
27 | */ |
28 | public class CriteriaQuery extends AbstractQuery implements Query { |
29 | |
30 | private Criteria criteria; |
31 | private CriteriaQuery() { |
32 | } |
33 | |
34 | public CriteriaQuery(Criteria criteria) { |
35 | this(criteria, null); |
36 | } |
37 | |
38 | public CriteriaQuery(Criteria criteria, Pageable pageable) { |
39 | this.criteria = criteria; |
40 | this.pageable = pageable; |
41 | if (pageable != null) { |
42 | this.addSort(pageable.getSort()); |
43 | } |
44 | } |
45 | |
46 | public static final Query fromQuery(CriteriaQuery source) { |
47 | return fromQuery(source, new CriteriaQuery()); |
48 | } |
49 | |
50 | public static <T extends CriteriaQuery> T fromQuery(CriteriaQuery source, T destination) { |
51 | if (source == null || destination == null) { |
52 | return null; |
53 | } |
54 | |
55 | if (source.getCriteria() != null) { |
56 | destination.addCriteria(source.getCriteria()); |
57 | } |
58 | |
59 | if (source.getSort() != null) { |
60 | destination.addSort(source.getSort()); |
61 | } |
62 | |
63 | return destination; |
64 | } |
65 | |
66 | @SuppressWarnings("unchecked") |
67 | public final <T extends CriteriaQuery> T addCriteria(Criteria criteria) { |
68 | Assert.notNull(criteria, "Cannot add null criteria."); |
69 | if (this.criteria == null) { |
70 | this.criteria = criteria; |
71 | } else { |
72 | this.criteria.and(criteria); |
73 | } |
74 | return (T) this; |
75 | } |
76 | |
77 | public Criteria getCriteria() { |
78 | return this.criteria; |
79 | } |
80 | |
81 | } |