1 /*
2  * Copyright 2013-2020 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  *      https://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.jpa.repository.query;
17
18 import java.lang.reflect.Method;
19 import java.util.Date;
20 import java.util.List;
21
22 import javax.persistence.TemporalType;
23
24 import org.springframework.core.MethodParameter;
25 import org.springframework.data.jpa.repository.Temporal;
26 import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
27 import org.springframework.data.repository.query.Parameter;
28 import org.springframework.data.repository.query.Parameters;
29 import org.springframework.lang.Nullable;
30
31 /**
32  * Custom extension of {@link Parameters} discovering additional query parameter annotations.
33  *
34  * @author Thomas Darimont
35  * @author Mark Paluch
36  * @author RĂ©da Housni Alaoui
37  */

38 public class JpaParameters extends Parameters<JpaParameters, JpaParameter> {
39
40     /**
41      * Creates a new {@link JpaParameters} instance from the given {@link Method}.
42      *
43      * @param method must not be {@literal null}.
44      */

45     public JpaParameters(Method method) {
46         super(method);
47     }
48
49     private JpaParameters(List<JpaParameter> parameters) {
50         super(parameters);
51     }
52
53     /*
54      * (non-Javadoc)
55      * @see org.springframework.data.repository.query.Parameters#createParameter(org.springframework.core.MethodParameter)
56      */

57     @Override
58     protected JpaParameter createParameter(MethodParameter parameter) {
59         return new JpaParameter(parameter);
60     }
61
62     /*
63      * (non-Javadoc)
64      * @see org.springframework.data.repository.query.Parameters#createFrom(java.util.List)
65      */

66     @Override
67     protected JpaParameters createFrom(List<JpaParameter> parameters) {
68         return new JpaParameters(parameters);
69     }
70
71     /**
72      * Custom {@link Parameter} implementation adding parameters of type {@link Temporal} to the special ones.
73      *
74      * @author Thomas Darimont
75      * @author Oliver Gierke
76      */

77     public static class JpaParameter extends Parameter {
78
79         private final @Nullable Temporal annotation;
80         private @Nullable TemporalType temporalType;
81
82         /**
83          * Creates a new {@link JpaParameter}.
84          *
85          * @param parameter must not be {@literal null}.
86          */

87         protected JpaParameter(MethodParameter parameter) {
88
89             super(parameter);
90
91             this.annotation = parameter.getParameterAnnotation(Temporal.class);
92             this.temporalType = null;
93
94             if (!isDateParameter() && hasTemporalParamAnnotation()) {
95                 throw new IllegalArgumentException(
96                         Temporal.class.getSimpleName() + " annotation is only allowed on Date parameter!");
97             }
98         }
99
100         /*
101          * (non-Javadoc)
102          * @see org.springframework.data.repository.query.Parameter#isBindable()
103          */

104         @Override
105         public boolean isBindable() {
106             return super.isBindable() || isTemporalParameter();
107         }
108
109         /**
110          * @return {@literal trueif this parameter is of type {@link Date} and has an {@link Temporal} annotation.
111          */

112         boolean isTemporalParameter() {
113             return isDateParameter() && hasTemporalParamAnnotation();
114         }
115
116         /**
117          * @return the {@link TemporalType} on the {@link Temporal} annotation of the given {@link Parameter}.
118          */

119         @Nullable
120         TemporalType getTemporalType() {
121
122             if (temporalType == null) {
123                 this.temporalType = annotation == null ? null : annotation.value();
124             }
125
126             return this.temporalType;
127         }
128
129         /**
130          * @return the required {@link TemporalType} on the {@link Temporal} annotation of the given {@link Parameter}.
131          * @throws IllegalStateException if the parameter does not define a {@link TemporalType}.
132          * @since 2.0
133          */

134         TemporalType getRequiredTemporalType() throws IllegalStateException {
135
136             TemporalType temporalType = getTemporalType();
137
138             if (temporalType != null) {
139                 return temporalType;
140             }
141
142             throw new IllegalStateException(String.format("Required temporal type not found for %s!", getType()));
143         }
144
145         private boolean hasTemporalParamAnnotation() {
146             return annotation != null;
147         }
148
149         private boolean isDateParameter() {
150             return getType().equals(Date.class);
151         }
152     }
153 }
154