1 /*
2 * Copyright 2008-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.repository.query;
17
18 import java.lang.reflect.Method;
19 import java.util.Locale;
20
21 import org.springframework.data.projection.ProjectionFactory;
22 import org.springframework.data.repository.core.NamedQueries;
23 import org.springframework.data.repository.core.RepositoryMetadata;
24 import org.springframework.lang.Nullable;
25 import org.springframework.util.StringUtils;
26
27 /**
28 * Strategy interface for which way to lookup {@link RepositoryQuery}s.
29 *
30 * @author Oliver Gierke
31 */
32 public interface QueryLookupStrategy {
33
34 public static enum Key {
35
36 CREATE, USE_DECLARED_QUERY, CREATE_IF_NOT_FOUND;
37
38 /**
39 * Returns a strategy key from the given XML value.
40 *
41 * @param xml
42 * @return a strategy key from the given XML value
43 */
44 @Nullable
45 public static Key create(String xml) {
46
47 if (!StringUtils.hasText(xml)) {
48 return null;
49 }
50
51 return valueOf(xml.toUpperCase(Locale.US).replace("-", "_"));
52 }
53 }
54
55 /**
56 * Resolves a {@link RepositoryQuery} from the given {@link QueryMethod} that can be executed afterwards.
57 *
58 * @param method will never be {@literal null}.
59 * @param metadata will never be {@literal null}.
60 * @param factory will never be {@literal null}.
61 * @param namedQueries will never be {@literal null}.
62 * @return
63 */
64 RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
65 NamedQueries namedQueries);
66 }
67