You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gc...@apache.org on 2010/10/12 19:15:11 UTC
svn commit: r1021846 - in /myfaces/trinidad/trunk/trinidad-impl/src/main:
java/org/apache/myfaces/trinidadinternal/validator/DateTimeRangeValidator.java
javascript/META-INF/adf/jsLibs/CoreFormat.js
Author: gcrawford
Date: Tue Oct 12 17:15:11 2010
New Revision: 1021846
URL: http://svn.apache.org/viewvc?rev=1021846&view=rev
Log:
TRINIDAD-1920 DateTimeRangevalidator fails across multiple timezones
Modified:
myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/validator/DateTimeRangeValidator.java
myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/CoreFormat.js
Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/validator/DateTimeRangeValidator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/validator/DateTimeRangeValidator.java?rev=1021846&r1=1021845&r2=1021846&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/validator/DateTimeRangeValidator.java (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/validator/DateTimeRangeValidator.java Tue Oct 12 17:15:11 2010
@@ -19,6 +19,9 @@
package org.apache.myfaces.trinidadinternal.validator;
import java.io.IOException;
+
+import java.text.SimpleDateFormat;
+
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
@@ -108,8 +111,8 @@ public class DateTimeRangeValidator exte
String hintRange = this.getHintNotInRange();
Map<String, String> cMessages = null;
- if (messageDetailMax != null || messageDetailMin != null || messageDetailRange != null ||
- hintMax != null || hintMin != null || hintRange != null)
+ if(messageDetailMax != null || messageDetailMin != null || messageDetailRange != null ||
+ hintMax != null || hintMin != null|| hintRange != null)
{
cMessages = new HashMap<String, String>();
cMessages.put("max", messageDetailMax);
@@ -120,10 +123,13 @@ public class DateTimeRangeValidator exte
cMessages.put("hintRange", hintRange);
}
- return _getTrDateTimeRangeValidator(context, component, maxStr, minStr,
- max != null ? Long.toString(max.getTime()) : "null",
- min != null ? Long.toString(min.getTime()) : "null",
- cMessages);
+ // Trinidad-1818: Send min/max in two formats: one parseable by the converter (for hints),
+ // one in an ISO-like format that doesn't lose information if the converter has
+ // a pattern that loses information.
+ String maxISOStr = (max == null) ? "null" : "'" + _ISO_FORMAT.format(max) + "'" ;
+ String minISOStr = (min == null) ? "null" : "'" + _ISO_FORMAT.format(min) + "'" ;
+ return _getTrDateTimeRangeValidator(context, component, maxStr, maxISOStr,
+ minStr, minISOStr, cMessages);
}
public String getClientLibrarySource(
@@ -136,9 +142,9 @@ public class DateTimeRangeValidator exte
FacesContext context,
UIComponent component,
String max,
+ String maxISOStr,
String min,
- String maxMilli,
- String minMilli,
+ String minISOStr,
Map<String, String> messages)
{
StringBuilder outBuffer = new StringBuilder(31 + min.length() + max.length());
@@ -163,11 +169,11 @@ public class DateTimeRangeValidator exte
}
}
outBuffer.append(',');
- outBuffer.append(maxMilli);
+ outBuffer.append(maxISOStr);
outBuffer.append(',');
- outBuffer.append(minMilli);
+ outBuffer.append(minISOStr);
+
outBuffer.append(')');
-
return outBuffer.toString();
}
@@ -175,6 +181,8 @@ public class DateTimeRangeValidator exte
private static final TrinidadLogger _LOG = TrinidadLogger
.createTrinidadLogger(DateTimeRangeValidator.class);
private static final Collection<String> _IMPORT_NAMES = Collections.singletonList( "TrNumberConverter()" );
+
+ private static final SimpleDateFormat _ISO_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/CoreFormat.js
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/CoreFormat.js?rev=1021846&r1=1021845&r2=1021846&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/CoreFormat.js (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/CoreFormat.js Tue Oct 12 17:15:11 2010
@@ -567,18 +567,24 @@ TrLengthValidator.prototype.validate =
}
}
+// Trinidad-1818: When min/max is specified, use two pieces of information
+// min/maxValue : The date as a string parseable by the converter (for hints)
+// minISODate/maxISODate: The date as an ISO-like string to correctly recreate the Date object.
+// This is because the converter pattern could lose information, e.g. with a
+// Date of 4712-12-31 and converter with YY, min/maxValue would be "12-12-31"
+// and the converter would parse that into 2012-12-31. See TRINIDAD-1920
function TrDateTimeRangeValidator(
- maxDateString,
- minDateString,
+ maxValue,
+ minValue,
messages,
- maxDateMilliseconds,
- minDateMilliseconds
- )
+ maxISODate,
+ minISODate
+)
{
- this._maxDateString = maxDateString;
- this._minDateString = minDateString;
- this._maxDateMilliseconds = maxDateMilliseconds;
- this._minDateMilliseconds = minDateMilliseconds;
+ this._maxValue = maxValue;
+ this._maxISODate = maxISODate;
+ this._minValue = minValue;
+ this._minISODate = minISODate;
this._messages = messages;
// for debugging
this._class = "TrDateTimeRangeValidator";
@@ -589,14 +595,14 @@ TrDateTimeRangeValidator.prototype.getHi
converter
)
{
- if (this._maxDateString == null && this._maxDateMilliseconds != null)
- this._maxDateString = converter.getAsString(new Date(this._maxDateMilliseconds));
-
- if (this._minDateString == null && this._minDateMilliseconds != null)
- this._minDateString = converter.getAsString(new Date(this._minDateMilliseconds));
-
- var max = this._maxDateString ? this._maxDateString : null;
- var min = this._minDateString ? this._minDateString : null;
+ var max = null;
+ var min = null;
+
+ if (this._maxValue)
+ max = this._maxValue;
+
+ if (this._minValue)
+ min = this._minValue;
return _returnRangeHints(
this._messages,
@@ -617,24 +623,24 @@ TrDateTimeRangeValidator.prototype.valid
converter
)
{
- if (this._maxDateString == null && this._maxDateMilliseconds != null)
- this._maxDateString = converter.getAsString(new Date(this._maxDateMilliseconds));
-
- if (this._minDateString == null && this._minDateMilliseconds != null)
- this._minDateString = converter.getAsString(new Date(this._minDateMilliseconds));
-
dateTime = value.getTime();
var facesMessage;
+ var isoConverter = this._getISOConverter ();
//range
- if (this._minDateString && this._maxDateString)
+ if(this._minValue && this._maxValue)
{
try
{
- minDate = (this._minDateMilliseconds != null) ? this._minDateMilliseconds :
- converter.getAsObject(this._minDateString).getTime();
-
- maxDate = (this._maxDateMilliseconds != null) ? this._maxDateMilliseconds :
- converter.getAsObject(this._maxDateString).getTime();
+ // min/maxISODate were introduced in TRINIDAD-1920, pre-existing callers may have them null.
+ // If so, revert to previous behavior where we just parse the min/maxValue string, though
+ // that may have less information than the ISO version.
+ minDate = (this._minISODate == null) ?
+ converter.getAsObject (this._minValue):
+ isoConverter.getAsObject (this._minISODate);
+
+ maxDate = (this._maxISODate == null) ?
+ converter.getAsObject (this._maxValue):
+ isoConverter.getAsObject (this._maxISODate);
}
catch (e)
{
@@ -643,41 +649,42 @@ TrDateTimeRangeValidator.prototype.valid
return value;
}
- if (dateTime >= minDate && dateTime <= maxDate)
+ if(dateTime >= minDate && dateTime <= maxDate)
{
return value;
}
else
{
var key = "org.apache.myfaces.trinidad.validator.DateTimeRangeValidator.NOT_IN_RANGE";
- if (this._messages && this._messages["range"])
- {
+ if(this._messages && this._messages["range"])
+ {
facesMessage = _createCustomFacesMessage(TrMessageFactory.getSummaryString(key),
this._messages["range"],
label,
""+converter.getAsString(value),
- ""+this._minDateString,
- ""+this._maxDateString);
- }
+ ""+this._minValue,
+ ""+this._maxValue);
+ }
else
{
facesMessage = _createFacesMessage(key,
label,
""+converter.getAsString(value),
- ""+this._minDateString,
- ""+this._maxDateString);
+ ""+this._minValue,
+ ""+this._maxValue);
}
}
}
else
{
//only min
- if (this._minDateString)
+ if(this._minValue)
{
try
{
- minDate = (this._minDateMilliseconds != null) ? this._minDateMilliseconds :
- converter.getAsObject(this._minDateString).getTime();
+ minDate = (this._minISODate == null) ?
+ converter.getAsObject (this._minValue):
+ isoConverter.getAsObject (this._minISODate);
}
catch (e)
{
@@ -686,37 +693,38 @@ TrDateTimeRangeValidator.prototype.valid
return value;
}
- if (dateTime >= minDate)
+ if(dateTime >= minDate)
{
return value;
}
else
{
var key = "org.apache.myfaces.trinidad.validator.DateTimeRangeValidator.MINIMUM";
- if (this._messages && this._messages["min"])
+ if(this._messages && this._messages["min"])
{
facesMessage = _createCustomFacesMessage(TrMessageFactory.getSummaryString(key),
this._messages["min"],
label,
""+converter.getAsString(value),
- ""+this._minDateString);
+ ""+this._minValue);
}
- else
- {
+ else
+ {
facesMessage = _createFacesMessage(key,
label,
""+converter.getAsString(value),
- ""+this._minDateString);
- }
+ ""+this._minValue);
+ }
}
}
//max only
- else if (this._maxDateString)
+ else if(this._maxValue)
{
try
{
- maxDate = (this._maxDateMilliseconds != null) ? this._maxDateMilliseconds :
- converter.getAsObject(this._maxDateString).getTime();
+ maxDate = (this._maxISODate == null) ?
+ converter.getAsObject (this._maxValue):
+ isoConverter.getAsObject (this._maxISODate);
}
catch (e)
{
@@ -724,28 +732,27 @@ TrDateTimeRangeValidator.prototype.valid
// client conversion fails
return value;
}
-
- if (dateTime <= maxDate)
+ if(dateTime <= maxDate)
{
return value;
}
else
{
var key = "org.apache.myfaces.trinidad.validator.DateTimeRangeValidator.MAXIMUM";
- if (this._messages && this._messages["max"])
+ if(this._messages && this._messages["max"])
{
facesMessage = _createCustomFacesMessage(TrMessageFactory.getSummaryString(key),
this._messages["max"],
label,
""+converter.getAsString(value),
- ""+this._maxDateString);
+ ""+this._maxValue);
}
else
{
facesMessage = _createFacesMessage(key,
label,
""+converter.getAsString(value),
- ""+this._maxDateString);
+ ""+this._maxValue);
}
}
}
@@ -758,6 +765,14 @@ TrDateTimeRangeValidator.prototype.valid
throw new TrConverterException(facesMessage);
}
+TrDateTimeRangeValidator.prototype._getISOConverter = function ()
+{
+ // This pattern must be kept in sync with DateTimeRangeValidator#_ISO_FORMAT
+ if (this._ISO_CONVERTER == null)
+ this._ISO_CONVERTER = new TrDateTimeConverter("yyyy-MM-dd HH:mm:ss", null, null, null, null);
+ return this._ISO_CONVERTER;
+}
+
function TrDateRestrictionValidator(
weekdaysValue,
monthValue,