EMMA Coverage Report (generated Tue Mar 05 16:36:55 GMT 2013)
[all classes][org.springframework.data.elasticsearch.repository.query]

COVERAGE SUMMARY FOR SOURCE FILE [ElasticsearchStringQuery.java]

nameclass, %method, %block, %line, %
ElasticsearchStringQuery.java100% (1/1)100% (6/6)91%  (134/148)91%  (30/33)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ElasticsearchStringQuery100% (1/1)100% (6/6)91%  (134/148)91%  (30/33)
execute (Object []): Object 100% (1/1)74%  (25/34)80%  (4/5)
getParameterWithIndex (ParametersParameterAccessor, int): String 100% (1/1)80%  (20/25)67%  (4/6)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
ElasticsearchStringQuery (ElasticsearchQueryMethod, ElasticsearchOperations, ... 100% (1/1)100% (46/46)100% (11/11)
createQuery (ParametersParameterAccessor): StringQuery 100% (1/1)100% (11/11)100% (2/2)
replacePlaceholders (String, ParametersParameterAccessor): String 100% (1/1)100% (28/28)100% (8/8)

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 */
16package org.springframework.data.elasticsearch.repository.query;
17 
18 
19import org.springframework.core.convert.support.GenericConversionService;
20import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
21import org.springframework.data.elasticsearch.core.convert.DateTimeConverters;
22import org.springframework.data.elasticsearch.core.query.StringQuery;
23import org.springframework.data.repository.query.ParametersParameterAccessor;
24import org.springframework.util.Assert;
25 
26import java.util.regex.Matcher;
27import java.util.regex.Pattern;
28 
29/**
30 * ElasticsearchStringQuery
31 *
32 * @author Rizwan Idrees
33 * @author Mohsin Husen
34 */
35public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQuery{
36 
37    private static final Pattern PARAMETER_PLACEHOLDER = Pattern.compile("\\?(\\d+)");
38    private String query;
39 
40    private final GenericConversionService conversionService = new GenericConversionService();
41 
42    {
43        if (!conversionService.canConvert(java.util.Date.class, String.class)) {
44            conversionService.addConverter(DateTimeConverters.JavaDateConverter.INSTANCE);
45        }
46        if (!conversionService.canConvert(org.joda.time.ReadableInstant.class, String.class)) {
47            conversionService.addConverter(DateTimeConverters.JodaDateTimeConverter.INSTANCE);
48        }
49        if (!conversionService.canConvert(org.joda.time.LocalDateTime.class, String.class)) {
50            conversionService.addConverter(DateTimeConverters.JodaLocalDateTimeConverter.INSTANCE);
51        }
52 
53    }
54 
55    public ElasticsearchStringQuery(ElasticsearchQueryMethod queryMethod, ElasticsearchOperations elasticsearchOperations, String query) {
56        super(queryMethod, elasticsearchOperations);
57        Assert.notNull(query, "Query cannot be empty");
58        this.query = query;
59    }
60 
61    @Override
62    public Object execute(Object[] parameters) {
63        ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
64        StringQuery stringQuery = createQuery(accessor);
65        if(queryMethod.isPageQuery()){
66            return  elasticsearchOperations.queryForPage(stringQuery, queryMethod.getEntityInformation().getJavaType());
67        }
68        return elasticsearchOperations.queryForObject(stringQuery, queryMethod.getEntityInformation().getJavaType());
69    }
70 
71 
72    protected StringQuery createQuery(ParametersParameterAccessor parameterAccessor) {
73        String queryString = replacePlaceholders(this.query, parameterAccessor);
74        return new StringQuery(queryString);
75    }
76 
77    private String replacePlaceholders(String input, ParametersParameterAccessor accessor) {
78        Matcher matcher = PARAMETER_PLACEHOLDER.matcher(input);
79        String result = input;
80        while (matcher.find()) {
81            String group = matcher.group();
82            int index = Integer.parseInt(matcher.group(1));
83            result = result.replace(group, getParameterWithIndex(accessor, index));
84        }
85        return result;
86    }
87 
88    private String getParameterWithIndex(ParametersParameterAccessor accessor, int index) {
89        Object parameter = accessor.getBindableValue(index);
90        if (parameter == null) {
91            return "null";
92        }
93        if (conversionService.canConvert(parameter.getClass(), String.class)) {
94            return conversionService.convert(parameter, String.class);
95        }
96        return parameter.toString();
97    }
98}

[all classes][org.springframework.data.elasticsearch.repository.query]
EMMA 2.0.5312 (C) Vladimir Roubtsov