1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.activeinstalls.internal.server

File DefaultDataManager.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

6
16
3
1
108
65
6
0.38
5.33
3
2

Classes

Class Line # Actions
DefaultDataManager 49 16 0% 6 5
0.880%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.activeinstalls.internal.server;
21   
22    import java.util.Map;
23   
24    import javax.inject.Inject;
25    import javax.inject.Singleton;
26   
27    import org.xwiki.activeinstalls.internal.JestClientManager;
28    import org.xwiki.activeinstalls.server.DataManager;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.properties.ConverterManager;
31   
32    import com.google.gson.JsonObject;
33   
34    import io.searchbox.action.Action;
35    import io.searchbox.client.JestResult;
36    import io.searchbox.core.Count;
37    import io.searchbox.core.Search;
38    import io.searchbox.params.Parameters;
39    import io.searchbox.params.SearchType;
40   
41    /**
42    * Get stored ping data from a remote Elastic Search instance.
43    *
44    * @version $Id: 92953ec024f57b52ae8685f6593dbf721b924c28 $
45    * @since 5.2M2
46    */
47    @Component
48    @Singleton
 
49    public class DefaultDataManager implements DataManager
50    {
51    @Inject
52    private JestClientManager jestClientManager;
53   
54    @Inject
55    private ConverterManager converterManager;
56   
 
57  1 toggle @Override
58    public JsonObject countInstalls(String indexType, String fullQuery, Map<String, Object> parameters)
59    throws Exception
60    {
61  1 Count.Builder countBuilder = new Count.Builder()
62    .query(fullQuery)
63    .addIndex(JestClientManager.INDEX)
64    .addType(indexType);
65   
66    // Add parameters.
67  1 for (Map.Entry<String, Object> parameterEntry : parameters.entrySet()) {
68  0 countBuilder.setParameter(parameterEntry.getKey(), parameterEntry.getValue());
69    }
70   
71  1 return executeActionQuery(countBuilder.build(), fullQuery).getJsonObject();
72    }
73   
 
74  30 toggle @Override
75    public JsonObject searchInstalls(String indexType, String fullQuery, Map<String, Object> parameters)
76    throws Exception
77    {
78  30 Search.Builder searchBuilder = new Search.Builder(fullQuery)
79    .addIndex(JestClientManager.INDEX)
80    .addType(indexType);
81   
82    // Add parameters and handle specifically the Search Type.
83  30 if (parameters.containsKey(Parameters.SEARCH_TYPE)) {
84  27 SearchType searchType = this.converterManager.convert(SearchType.class,
85    parameters.get(Parameters.SEARCH_TYPE));
86  27 searchBuilder.setSearchType(searchType);
87    }
88  30 for (Map.Entry<String, Object> parameterEntry : parameters.entrySet()) {
89  27 if (!parameterEntry.getKey().equals(Parameters.SEARCH_TYPE)) {
90  0 searchBuilder.setParameter(parameterEntry.getKey(), parameterEntry.getValue());
91    }
92    }
93   
94  30 return executeActionQuery(searchBuilder.build(), fullQuery).getJsonObject();
95    }
96   
 
97  31 toggle private JestResult executeActionQuery(Action action, String fullQuery) throws Exception
98    {
99  31 JestResult result = this.jestClientManager.getClient().execute(action);
100   
101  31 if (!result.isSucceeded()) {
102  0 throw new Exception(String.format("Error while executing [%s] query [%s]: [%s], Reason: [%s]",
103    action.getClass().getSimpleName(), fullQuery, result.getErrorMessage(), result.getJsonString()));
104    }
105   
106  31 return result;
107    }
108    }