1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.objects.classes; |
21 |
|
|
22 |
|
import java.text.ParseException; |
23 |
|
import java.text.SimpleDateFormat; |
24 |
|
import java.util.Date; |
25 |
|
import java.util.Locale; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.StringUtils; |
28 |
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
29 |
|
import org.dom4j.Element; |
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.slf4j.LoggerFactory; |
32 |
|
import org.xwiki.localization.LocalizationContext; |
33 |
|
import org.xwiki.xar.internal.property.DateXarObjectPropertySerializer; |
34 |
|
|
35 |
|
import com.xpn.xwiki.objects.BaseProperty; |
36 |
|
import com.xpn.xwiki.objects.DateProperty; |
37 |
|
import com.xpn.xwiki.objects.meta.PropertyMetaClass; |
38 |
|
import com.xpn.xwiki.web.Utils; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
|
|
|
| 65.8% |
Uncovered Elements: 26 (76) |
Complexity: 25 |
Complexity Density: 0.5 |
|
45 |
|
public class DateClass extends PropertyClass |
46 |
|
{ |
47 |
|
|
48 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(DateClass.class); |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
private static final String META_PROPERTY_PICKER = "picker"; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
private static final String META_PROPERTY_SIZE = "size"; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private static final String META_PROPERTY_EMPTY_IS_TODAY = "emptyIsToday"; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private static final String META_PROPERTY_DATE_FORMAT = "dateFormat"; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
private LocalizationContext localizationContext; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@param |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
80 |
141 |
public DateClass(PropertyMetaClass metaClass)... |
81 |
|
{ |
82 |
141 |
super("date", "Date", metaClass); |
83 |
|
|
84 |
141 |
setSize(20); |
85 |
141 |
setDateFormat("dd/MM/yyyy HH:mm:ss"); |
86 |
141 |
setEmptyIsToday(1); |
87 |
141 |
setPicker(1); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
93 |
141 |
public DateClass()... |
94 |
|
{ |
95 |
141 |
this(null); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
@return |
100 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
101 |
0 |
public int getPicker()... |
102 |
|
{ |
103 |
0 |
return getIntValue(META_PROPERTY_PICKER); |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
@param |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
111 |
141 |
public void setPicker(int picker)... |
112 |
|
{ |
113 |
141 |
setIntValue(META_PROPERTY_PICKER, picker); |
114 |
|
} |
115 |
|
|
116 |
|
|
117 |
|
@return |
118 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
0 |
public int getSize()... |
120 |
|
{ |
121 |
0 |
return getIntValue(META_PROPERTY_SIZE); |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
@param |
128 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
129 |
141 |
public void setSize(int size)... |
130 |
|
{ |
131 |
141 |
setIntValue(META_PROPERTY_SIZE, size); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
@return |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
2 |
public int getEmptyIsToday()... |
138 |
|
{ |
139 |
2 |
return getIntValue(META_PROPERTY_EMPTY_IS_TODAY); |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@param |
146 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
147 |
196 |
public void setEmptyIsToday(int emptyIsToday)... |
148 |
|
{ |
149 |
196 |
setIntValue(META_PROPERTY_EMPTY_IS_TODAY, emptyIsToday); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
@return |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
155 |
4 |
public String getDateFormat()... |
156 |
|
{ |
157 |
4 |
return getStringValue(META_PROPERTY_DATE_FORMAT); |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
@param |
164 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
165 |
146 |
public void setDateFormat(String format)... |
166 |
|
{ |
167 |
146 |
setStringValue(META_PROPERTY_DATE_FORMAT, format); |
168 |
|
} |
169 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
170 |
5 |
@Override... |
171 |
|
public BaseProperty fromString(String value) |
172 |
|
{ |
173 |
5 |
BaseProperty property = newProperty(); |
174 |
|
|
175 |
5 |
if (StringUtils.isEmpty(value)) { |
176 |
2 |
if (getEmptyIsToday() == 1) { |
177 |
1 |
property.setValue(new Date()); |
178 |
|
} |
179 |
2 |
return property; |
180 |
|
} |
181 |
|
|
182 |
3 |
try { |
183 |
3 |
SimpleDateFormat sdf = new SimpleDateFormat(getDateFormat(), getCurrentLocale()); |
184 |
3 |
property.setValue(sdf.parse(value)); |
185 |
3 |
return property; |
186 |
|
} catch (ParseException e) { |
187 |
0 |
return null; |
188 |
|
} |
189 |
|
} |
190 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
191 |
15 |
@Override... |
192 |
|
public BaseProperty newProperty() |
193 |
|
{ |
194 |
15 |
BaseProperty property = new DateProperty(); |
195 |
15 |
property.setName(getName()); |
196 |
15 |
return property; |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
@param |
201 |
|
@return@link |
202 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
203 |
1 |
public String toFormString(BaseProperty property)... |
204 |
|
{ |
205 |
1 |
return property.getValue() == null ? "" : new SimpleDateFormat(getDateFormat(), getCurrentLocale()) |
206 |
|
.format(property.getValue()); |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
|
@inheritDoc |
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 4 |
Complexity Density: 0.29 |
|
217 |
0 |
@Override... |
218 |
|
public BaseProperty newPropertyfromXML(Element element) |
219 |
|
{ |
220 |
0 |
String value = element.getText(); |
221 |
0 |
BaseProperty property = newProperty(); |
222 |
|
|
223 |
0 |
if (StringUtils.isEmpty(value)) { |
224 |
0 |
return property; |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
|
|
229 |
0 |
SimpleDateFormat sdf = DateXarObjectPropertySerializer.DEFAULT_FORMAT; |
230 |
0 |
try { |
231 |
0 |
property.setValue(sdf.parse(value)); |
232 |
|
} catch (ParseException e) { |
233 |
|
|
234 |
0 |
SimpleDateFormat sdfOld = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US); |
235 |
0 |
LOGGER.warn("Failed to parse date [{}] using format [{}]. Trying again with format [{}].", value, |
236 |
|
sdf.toString(), sdfOld.toString()); |
237 |
0 |
try { |
238 |
0 |
property.setValue(sdfOld.parse(value)); |
239 |
|
} catch (ParseException exception) { |
240 |
0 |
LOGGER.warn("Failed to parse date [{}] using format [{}]. Defaulting to the current date.", value, |
241 |
|
sdfOld.toString()); |
242 |
0 |
property.setValue(new Date()); |
243 |
|
} |
244 |
|
} |
245 |
0 |
return property; |
246 |
|
} |
247 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
248 |
4 |
private LocalizationContext getLocalizationContext()... |
249 |
|
{ |
250 |
4 |
if (this.localizationContext == null) { |
251 |
3 |
this.localizationContext = Utils.getComponent(LocalizationContext.class); |
252 |
|
} |
253 |
4 |
return this.localizationContext; |
254 |
|
} |
255 |
|
|
|
|
| 40% |
Uncovered Elements: 3 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
256 |
4 |
private Locale getCurrentLocale()... |
257 |
|
{ |
258 |
4 |
try { |
259 |
4 |
return getLocalizationContext().getCurrentLocale(); |
260 |
|
} catch (Exception e) { |
261 |
0 |
Locale defaultLocale = Locale.getDefault(); |
262 |
0 |
LOGGER.warn("Failed to get the context locale: [{}]. Continue using the default JVM locale [{}].", |
263 |
|
ExceptionUtils.getRootCauseMessage(e), defaultLocale); |
264 |
0 |
return defaultLocale; |
265 |
|
} |
266 |
|
} |
267 |
|
} |