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

File TableTimeTableXYDatasetBuilder.java

 

Coverage histogram

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

Code metrics

20
54
14
1
268
170
26
0.48
3.86
14
1.86

Classes

Class Line # Actions
TableTimeTableXYDatasetBuilder 55 54 0% 26 18
0.7954545679.5%
 

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.rendering.internal.macro.chart.source.table;
21   
22    import java.lang.reflect.Constructor;
23    import java.text.ParseException;
24    import java.util.Date;
25    import java.util.EnumMap;
26    import java.util.Locale;
27    import java.util.Map;
28    import java.util.TimeZone;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.jfree.data.general.Dataset;
32    import org.jfree.data.time.Day;
33    import org.jfree.data.time.Hour;
34    import org.jfree.data.time.Millisecond;
35    import org.jfree.data.time.Minute;
36    import org.jfree.data.time.Month;
37    import org.jfree.data.time.Quarter;
38    import org.jfree.data.time.RegularTimePeriod;
39    import org.jfree.data.time.Second;
40    import org.jfree.data.time.SimpleTimePeriod;
41    import org.jfree.data.time.TimePeriod;
42    import org.jfree.data.time.TimeTableXYDataset;
43    import org.jfree.data.time.Week;
44    import org.jfree.data.time.Year;
45    import org.xwiki.chart.time.TimePeriodType;
46    import org.xwiki.rendering.internal.macro.chart.source.LocaleConfiguration;
47    import org.xwiki.rendering.macro.MacroExecutionException;
48   
49    /**
50    * A builder of time table xy datasets ({@see TimeTableXYDataset}) from table data sources.
51    *
52    * @version $Id: a9a4f0bb97f8b57dbafe09652eb76685482df9bd $
53    * @since 4.2M1
54    */
 
55    public class TableTimeTableXYDatasetBuilder implements TableDatasetBuilder
56    {
57    /**
58    * The name of the time period type parameter.
59    */
60    public static final String TIMEPERIOD_TYPE_PARAM = "time_period";
61   
62    /**
63    * Indicates if the transpose of the table should be used.
64    */
65    private boolean transpose;
66   
67    /**
68    * The dates.
69    */
70    private Date[] dates;
71   
72    /**
73    * The series names.
74    */
75    private String[] seriesNames;
76   
77    /**
78    * The dataset.
79    */
80    private TimeTableXYDataset dataset;
81   
82    /**
83    * The time period type.
84    */
85    private TimePeriodType timePeriodType = TimePeriodType.SIMPLE;
86   
87    /**
88    * Map of time period classes.
89    */
90    private final Map<TimePeriodType, Class<? extends RegularTimePeriod>> timePeriodClasses
91    = new EnumMap<TimePeriodType, Class<? extends RegularTimePeriod>>(TimePeriodType.class) {
92    /** Serial version uid. */
93    private static final long serialVersionUID = 1L;
94   
 
95  5 toggle {
96  5 put(TimePeriodType.MILLISECOND, Millisecond.class);
97  5 put(TimePeriodType.SECOND, Second.class);
98  5 put(TimePeriodType.MINUTE, Minute.class);
99  5 put(TimePeriodType.HOUR, Hour.class);
100  5 put(TimePeriodType.DAY, Day.class);
101  5 put(TimePeriodType.WEEK, Week.class);
102  5 put(TimePeriodType.MONTH, Month.class);
103  5 put(TimePeriodType.QUARTER, Quarter.class);
104  5 put(TimePeriodType.YEAR, Year.class);
105    }
106    };
107   
108    /**
109    * The locale configuration.
110    */
111    private LocaleConfiguration localeConfiguration;
112   
 
113  5 toggle @Override
114    public void setNumberOfRows(int numberOfRows)
115    {
116  5 if (transpose) {
117  5 dates = new Date[numberOfRows];
118    } else {
119  0 seriesNames = new String[numberOfRows];
120    }
121    }
122   
 
123  5 toggle @Override
124    public void setNumberOfColumns(int numberOfColumns)
125    {
126  5 if (transpose) {
127  5 seriesNames = new String[numberOfColumns];
128    } else {
129  0 dates = new Date[numberOfColumns];
130    }
131    }
132   
 
133  5 toggle @Override
134    public void setTranspose(boolean transpose)
135    {
136  5 this.transpose = transpose;
137    }
138   
139    /**
140    * Set a date in the date series.
141    *
142    * @param index The index of the date.
143    * @param dateText The text form of the date.
144    * @throws MacroExecutionException if the date could not be parsed.
145    */
 
146  58 toggle private void setDate(int index, String dateText) throws MacroExecutionException
147    {
148  58 try {
149  58 dates[index] = localeConfiguration.getDateFormat().parse(StringUtils.trim(dateText));
150    } catch (ParseException e) {
151  0 throw new MacroExecutionException(
152    String.format("Failed to parse date [%s] in time table.", dateText), e);
153    }
154    }
155   
 
156  12 toggle @Override
157    public void setColumnHeading(int columnIndex, String heading) throws MacroExecutionException
158    {
159  12 if (transpose) {
160  12 seriesNames[columnIndex] = heading;
161    } else {
162  0 setDate(columnIndex, heading);
163    }
164    }
165   
 
166  58 toggle @Override
167    public void setRowHeading(int rowIndex, String heading) throws MacroExecutionException
168    {
169  58 if (transpose) {
170  58 setDate(rowIndex, heading);
171    } else {
172  0 seriesNames[rowIndex] = heading;
173    }
174    }
175   
176    /**
177    * Get a time period in the series.
178    *
179    * @param index The index of the time period.
180    * @return The corresponding time period.
181    */
 
182  120 toggle private TimePeriod getTimePeriod(int index)
183    {
184   
185  120 Date time = dates[index];
186   
187  120 Class<? extends RegularTimePeriod> timePeriodClass = timePeriodClasses.get(timePeriodType);
188   
189  120 if (timePeriodClass == null) {
190  6 Date start = index == 0 ? new Date(0) : dates[index - 1];
191  6 Date end = time;
192  6 return new SimpleTimePeriod(start, end);
193    }
194   
195  114 try {
196  114 Constructor<? extends RegularTimePeriod> constructor
197    = timePeriodClass.getConstructor(Date.class, TimeZone.class, Locale.class);
198   
199  114 return constructor.newInstance(time, localeConfiguration.getTimeZone(),
200    localeConfiguration.getLocale());
201    } catch (Exception e) {
202  0 throw new RuntimeException(e);
203    }
204    }
205   
 
206  120 toggle @Override
207    public void setValue(int rowIndex, int columnIndex, Number value) throws MacroExecutionException
208    {
209  120 TimePeriod period;
210   
211  120 if (transpose) {
212  120 period = getTimePeriod(rowIndex);
213    } else {
214  0 period = getTimePeriod(columnIndex);
215    }
216   
217  120 String series;
218   
219  120 if (transpose) {
220  120 series = seriesNames[columnIndex];
221    } else {
222  0 series = seriesNames[rowIndex];
223    }
224   
225  120 dataset.add(period, value, series, false);
226    }
227   
 
228  5 toggle @Override
229    public Dataset getDataset()
230    {
231  5 return dataset;
232    }
233   
 
234  5 toggle @Override
235    public void setParameters(Map<String, String> parameters) throws MacroExecutionException
236    {
237  5 String timePeriodTypeString = parameters.get(TIMEPERIOD_TYPE_PARAM);
238   
239  5 if (timePeriodTypeString != null) {
240  4 this.timePeriodType = TimePeriodType.forName(timePeriodTypeString);
241  4 if (this.timePeriodType == null) {
242  0 throw new MacroExecutionException(String.format("Invalid time period type [%s].",
243    timePeriodTypeString));
244    }
245    }
246   
247  5 dataset = new TimeTableXYDataset(localeConfiguration.getTimeZone(),
248    localeConfiguration.getLocale());
249    }
250   
 
251  2 toggle @Override
252    public boolean forceColumnHeadings()
253    {
254  2 return transpose;
255    }
256   
 
257  0 toggle @Override
258    public boolean forceRowHeadings()
259    {
260  0 return !transpose;
261    }
262   
 
263  5 toggle @Override
264    public void setLocaleConfiguration(LocaleConfiguration localeConfiguration)
265    {
266  5 this.localeConfiguration = localeConfiguration;
267    }
268    }