1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.objects.classes

File DateClass.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

10
50
16
1
267
142
25
0.5
3.12
16
1.56

Classes

Class Line # Actions
DateClass 45 50 0% 25 26
0.6578947365.8%
 

Contributing tests

This file is covered by 3 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 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    * Defines an XClass property type whose value is a Date.
42    *
43    * @version $Id: d13e0550e686c20ef74d3a67430a5c126d77102b $
44    */
 
45    public class DateClass extends PropertyClass
46    {
47    /** Logging helper object. */
48    private static final Logger LOGGER = LoggerFactory.getLogger(DateClass.class);
49   
50    /**
51    * The meta property that specifies if a date picker should be used in edit mode to choose the date.
52    */
53    private static final String META_PROPERTY_PICKER = "picker";
54   
55    /**
56    * The meta property that controls the size of the date input in edit mode.
57    */
58    private static final String META_PROPERTY_SIZE = "size";
59   
60    /**
61    * The meta property that controls is an empty date value represents the current date.
62    */
63    private static final String META_PROPERTY_EMPTY_IS_TODAY = "emptyIsToday";
64   
65    /**
66    * The meta property that specifies the date format.
67    */
68    private static final String META_PROPERTY_DATE_FORMAT = "dateFormat";
69   
70    /**
71    * Used to get the current locale.
72    */
73    private LocalizationContext localizationContext;
74   
75    /**
76    * Creates a new Date property that is described by the given meta class.
77    *
78    * @param metaClass the meta class that defines the list of meta properties associated with this property type
79    */
 
80  141 toggle 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    * Default constructor.
92    */
 
93  141 toggle public DateClass()
94    {
95  141 this(null);
96    }
97   
98    /**
99    * @return {@code 1} if a date picker should be used to choose the date, {@code 0} otherwise
100    */
 
101  0 toggle public int getPicker()
102    {
103  0 return getIntValue(META_PROPERTY_PICKER);
104    }
105   
106    /**
107    * Sets whether to use a date picker or not to select the date in edit mode.
108    *
109    * @param picker {@code 1} to use a date picker, {@code 0} otherwise
110    */
 
111  141 toggle public void setPicker(int picker)
112    {
113  141 setIntValue(META_PROPERTY_PICKER, picker);
114    }
115   
116    /**
117    * @return the size of the date input in edit mode
118    */
 
119  0 toggle public int getSize()
120    {
121  0 return getIntValue(META_PROPERTY_SIZE);
122    }
123   
124    /**
125    * Sets the size of the date input in edit mode.
126    *
127    * @param size the size of the date input in edit mode
128    */
 
129  141 toggle public void setSize(int size)
130    {
131  141 setIntValue(META_PROPERTY_SIZE, size);
132    }
133   
134    /**
135    * @return {@code 1} if an empty date value represents the current date, {@code 0} otherwise
136    */
 
137  2 toggle public int getEmptyIsToday()
138    {
139  2 return getIntValue(META_PROPERTY_EMPTY_IS_TODAY);
140    }
141   
142    /**
143    * Sets whether an empty date value represents the current date or not.
144    *
145    * @param emptyIsToday {@code 1} if an empty date value should represent the current date, {@code 0} otherwise
146    */
 
147  196 toggle public void setEmptyIsToday(int emptyIsToday)
148    {
149  196 setIntValue(META_PROPERTY_EMPTY_IS_TODAY, emptyIsToday);
150    }
151   
152    /**
153    * @return the date format
154    */
 
155  4 toggle public String getDateFormat()
156    {
157  4 return getStringValue(META_PROPERTY_DATE_FORMAT);
158    }
159   
160    /**
161    * Sets the date format.
162    *
163    * @param format the new date format
164    */
 
165  146 toggle public void setDateFormat(String format)
166    {
167  146 setStringValue(META_PROPERTY_DATE_FORMAT, format);
168    }
169   
 
170  5 toggle @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   
 
191  15 toggle @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 property a date property
201    * @return the value of the given date property serialized using the {@link #getDateFormat()} format
202    */
 
203  1 toggle 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    * <p>
212    * We have to overwrite this method because the value of a date property is not serialized using the date format
213    * specified in the XClass nor the time stamp but a custom hard-coded date format.. Changing this now will break
214    * existing XARs..
215    * </p>
216    */
 
217  0 toggle @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    // FIXME: The value of a date property should be serialized using the date timestamp or the date format
228    // specified in the XClass the date property belongs to.
229  0 SimpleDateFormat sdf = DateXarObjectPropertySerializer.DEFAULT_FORMAT;
230  0 try {
231  0 property.setValue(sdf.parse(value));
232    } catch (ParseException e) {
233    // I suppose this is a date format used a long time ago. DateProperty is using the above date format now.
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   
 
248  4 toggle 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   
 
256  4 toggle 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    }