1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro.chart.source

File AbstractDataSource.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

16
33
11
1
231
96
19
0.58
3
11
1.73

Classes

Class Line # Actions
AbstractDataSource 38 33 0% 19 8
0.866666786.7%
 

Contributing tests

This file is covered by 14 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.rendering.internal.macro.chart.source;
21   
22    import java.util.Map;
23   
24    import org.jfree.data.general.Dataset;
25    import org.xwiki.chart.dataset.DatasetType;
26    import org.xwiki.chart.model.ChartModel;
27    import org.xwiki.chart.plot.PlotType;
28    import org.xwiki.rendering.macro.MacroExecutionException;
29   
30    /**
31    * A data source is able to provide a data set for chart generation.
32    *
33    * This super class provides basic parameter validation.
34    *
35    * @version $Id: 87c75e9b5229a4605f01e2581ecb233432d655b9 $
36    * @since 4.2M1
37    */
 
38    public abstract class AbstractDataSource implements DataSource
39    {
40    /**
41    * The name of the dataset parameter.
42    */
43    public static final String DATASET_PARAM = "dataset";
44   
45    /**
46    * The name of the plot type parameter.
47    */
48    public static final String PLOT_TYPE_PARAM = "type";
49   
50    /**
51    * The dataset type.
52    */
53    private DatasetType datasetType;
54   
55    /**
56    * The plot type.
57    */
58    private PlotType plotType;
59   
60    /**
61    * The built chart model.
62    */
63    private SimpleChartModel chartModel;
64   
65    /**
66    * A configuration object for time zone and locale.
67    */
68    private LocaleConfiguration localeConfiguration = new LocaleConfiguration();
69   
70    /**
71    * An axis configurator.
72    */
73    private AxisConfigurator axisConfigurator = new AxisConfigurator(localeConfiguration);
74   
75    /**
76    * Validate and set parameters for the data source.
77    *
78    * @param parameters The parameters.
79    * @throws MacroExecutionException if the parameters are invalid for this data source.
80    */
 
81  21 toggle protected void validateParameters(Map<String, String> parameters) throws MacroExecutionException
82    {
83  21 for (String key : parameters.keySet()) {
84  170 if (DATASET_PARAM.equals(key)) {
85  5 datasetType = DatasetType.forName(parameters.get(key));
86  5 if (datasetType == null) {
87  0 invalidParameterValue(DATASET_PARAM, parameters.get(key));
88    }
89  5 continue;
90    }
91  165 if (PLOT_TYPE_PARAM.equals(key)) {
92  21 plotType = PlotType.forName(parameters.get(key));
93  21 if (plotType == null) {
94  0 invalidParameterValue(PLOT_TYPE_PARAM, parameters.get(key));
95    }
96  21 continue;
97    }
98  144 if (localeConfiguration.setParameter(key, parameters.get(key))) {
99  7 continue;
100    }
101  137 if (axisConfigurator.setParameter(key, parameters.get(key))) {
102  14 continue;
103    }
104  123 setParameter(key, parameters.get(key));
105    }
106   
107  21 localeConfiguration.validateParameters();
108   
109  21 axisConfigurator.validateParameters();
110   
111  21 validatePlotType();
112   
113  21 validateDatasetType();
114   
115  21 validateParameters();
116    }
117   
118    /**
119    * Let an implementation set a parameter.
120    *
121    * This method should set the value of the parameter, if the parameter is supported by the data source.
122    *
123    * @param key The key of the parameter.
124    * @param value The value of the parameter.
125    * @return {@code true} if the parameter was claimed.
126    * @throws MacroExecutionException if the parameter is invalid in some way.
127    */
128    protected abstract boolean setParameter(String key, String value) throws MacroExecutionException;
129   
130    /**
131    * Let an implementation validate the value of the previously set parameters, and set default values.
132    *
133    * @throws MacroExecutionException if the previously set value is invalid.
134    */
135    protected abstract void validateParameters() throws MacroExecutionException;
136   
137    /**
138    * Validate the dataset parameter. If no dataset was given as a parameter, set a default value.
139    *
140    * @throws MacroExecutionException if the previously set value is invalid.
141    */
 
142  21 toggle protected void validateDatasetType() throws MacroExecutionException
143    {
144  21 if (getDatasetType() == null) {
145  16 setDatasetType(plotType.getDefaultDatasetType());
146    }
147    }
148   
149    /**
150    * Validate the plot type parameter.
151    *
152    * @throws MacroExecutionException if the previously set value is invalid.
153    */
 
154  21 toggle protected void validatePlotType() throws MacroExecutionException
155    {
156  21 if (plotType == null) {
157  0 throw new MacroExecutionException(String.format("The parameter [%s] is mandatory!", PLOT_TYPE_PARAM));
158    }
159    }
160   
161    /**
162    * Set the axes from the axis configuration.
163    *
164    * @throws MacroExecutionException if the axes are incorrectly specified.
165    */
 
166  21 toggle protected void setAxes() throws MacroExecutionException
167    {
168  21 axisConfigurator.setAxes(plotType, chartModel);
169    }
170   
171    /**
172    * @return the configured dataset type.
173    */
 
174  73 toggle public DatasetType getDatasetType()
175    {
176  73 return this.datasetType;
177    }
178   
179    /**
180    * @param datasetType the dataset type to configure.
181    */
 
182  16 toggle public void setDatasetType(DatasetType datasetType)
183    {
184  16 this.datasetType = datasetType;
185    }
186   
 
187  21 toggle @Override
188    public ChartModel getChartModel()
189    {
190  21 return chartModel;
191    }
192   
193    /**
194    * Set the chart model.
195    *
196    * @param chartModel The chart model.
197    */
 
198  21 toggle protected void setChartModel(SimpleChartModel chartModel)
199    {
200  21 this.chartModel = chartModel;
201    }
202   
203    /**
204    * @param dataset the dataset.
205    */
 
206  21 toggle protected void setDataset(Dataset dataset)
207    {
208  21 chartModel.setDataset(dataset);
209    }
210   
211    /**
212    * Indicate that an invalid parameter value was found.
213    *
214    * @param parameterName The name of the parameter.
215    * @param value The value.
216    * @throws MacroExecutionException always.
217    */
 
218  0 toggle protected void invalidParameterValue(String parameterName, String value) throws MacroExecutionException
219    {
220  0 throw new MacroExecutionException(String.format("Invalid value for parameter [%s]: [%s]",
221    parameterName, value));
222    }
223   
224    /**
225    * @return The locale configuration.
226    */
 
227  21 toggle protected LocaleConfiguration getLocaleConfiguration()
228    {
229  21 return localeConfiguration;
230    }
231    }