1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@see |
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
|
|
| 79.5% |
Uncovered Elements: 18 (88) |
Complexity: 26 |
Complexity Density: 0.48 |
|
55 |
|
public class TableTimeTableXYDatasetBuilder implements TableDatasetBuilder |
56 |
|
{ |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
public static final String TIMEPERIOD_TYPE_PARAM = "time_period"; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private boolean transpose; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private Date[] dates; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private String[] seriesNames; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
private TimeTableXYDataset dataset; |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private TimePeriodType timePeriodType = TimePeriodType.SIMPLE; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
private final Map<TimePeriodType, Class<? extends RegularTimePeriod>> timePeriodClasses |
91 |
|
= new EnumMap<TimePeriodType, Class<? extends RegularTimePeriod>>(TimePeriodType.class) { |
92 |
|
|
93 |
|
private static final long serialVersionUID = 1L; |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
95 |
5 |
{... |
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 |
|
|
110 |
|
|
111 |
|
private LocaleConfiguration localeConfiguration; |
112 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
113 |
5 |
@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 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
123 |
5 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
5 |
@Override... |
134 |
|
public void setTranspose(boolean transpose) |
135 |
|
{ |
136 |
5 |
this.transpose = transpose; |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
@param |
143 |
|
@param |
144 |
|
@throws |
145 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
146 |
58 |
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 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
156 |
12 |
@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 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
166 |
58 |
@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 |
|
|
178 |
|
|
179 |
|
@param |
180 |
|
@return |
181 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
182 |
120 |
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 |
|
|
|
|
| 69.2% |
Uncovered Elements: 4 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
206 |
120 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
228 |
5 |
@Override... |
229 |
|
public Dataset getDataset() |
230 |
|
{ |
231 |
5 |
return dataset; |
232 |
|
} |
233 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
234 |
5 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
251 |
2 |
@Override... |
252 |
|
public boolean forceColumnHeadings() |
253 |
|
{ |
254 |
2 |
return transpose; |
255 |
|
} |
256 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
257 |
0 |
@Override... |
258 |
|
public boolean forceRowHeadings() |
259 |
|
{ |
260 |
0 |
return !transpose; |
261 |
|
} |
262 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
263 |
5 |
@Override... |
264 |
|
public void setLocaleConfiguration(LocaleConfiguration localeConfiguration) |
265 |
|
{ |
266 |
5 |
this.localeConfiguration = localeConfiguration; |
267 |
|
} |
268 |
|
} |