org.apache.myfaces.trinidadinternal.convert.DateTimeConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trinidad-impl Show documentation
Show all versions of trinidad-impl Show documentation
Private implementation of the Apache MyFaces Trinidad project
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.myfaces.trinidadinternal.convert;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.ValueHolder;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.el.ValueExpression;
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFConverter;
import org.apache.myfaces.trinidad.component.UIXEditableValue;
import org.apache.myfaces.trinidad.context.RenderingContext;
import org.apache.myfaces.trinidad.convert.ClientConverter;
import org.apache.myfaces.trinidad.logging.TrinidadLogger;
import org.apache.myfaces.trinidad.util.MessageFactory;
import org.apache.myfaces.trinidadinternal.renderkit.core.xhtml.XhtmlUtils;
import org.apache.myfaces.trinidadinternal.ui.laf.base.xhtml.XhtmlLafUtils;
import org.apache.myfaces.trinidadinternal.util.JsonUtils;
/**
*
* This class implements client side equivalent of DateTimeConverter. This class
* pushes all relevant information to the client side so that conversion can be
* enabled at the client side.
*
*
* @version $Name: $ ($Revision:
* adfrt/faces/adf-faces-impl/src/main/java/oracle/adfinternal/view/faces/convert/DateTimeConverter.java#0 $)
* $Date: 10-nov-2005.19:06:22 $
*/
@JSFConverter(
name="tr:convertDateTime",
bodyContent="empty",
id="org.apache.myfaces.trinidad.DateTime",
tagClass="org.apache.myfaces.trinidadinternal.taglib.convert.ConvertDateTimeTag")
public class DateTimeConverter extends
org.apache.myfaces.trinidad.convert.DateTimeConverter implements
ClientConverter
{
public DateTimeConverter()
{
super();
}
public DateTimeConverter(String pattern)
{
super(pattern);
}
public DateTimeConverter(String pattern, String secondaryPattern)
{
super(pattern, secondaryPattern);
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value)
{
if (value == null)
return null;
GenericConverterFactory fac = GenericConverterFactory.getCurrentInstance();
// we support other types of dates, like oracle.jbo.domain.Date:
if (!(value instanceof String) && (!(value instanceof Date)) && fac.isConvertible(value, Date.class))
{
value = fac.convert(value, Date.class);
}
return super.getAsString(context, component, value);
}
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value)
{
Object date = super.getAsObject(context, component, value);
if (date == null)
return null;
date = __typeConvert(context, this, component, value, date);
return date;
}
/**
* Super class only returns instances of java.util.Date. However, the expected
* type of the attribute might be java.sql.Timestamp. Therefore, we must get
* the expected type and convert if necessary: bug 4549630:
*/
static Object __typeConvert(FacesContext context, Converter converter,
UIComponent component, String strValue, Object value) throws ConverterException
{
assert value != null;
ValueExpression expression = component.getValueExpression("value");
if (expression != null)
{
Class> expectedType = expression.getExpectedType();
// If the expectedType is Object ask for the type which may be more specific
if(expectedType == null || expectedType == Object.class)
{
expectedType = expression.getType(context.getELContext());
}
// Sometimes the type might be null, if it cannot be determined:
if ((expectedType != null)
&& (!expectedType.isAssignableFrom(value.getClass())))
{
// sometimes we might have to return the date/number as a string.
// see bug 4602629:
if (expectedType == String.class)
{
ValueHolder holder = (ValueHolder) component;
// if the submitted string is identical to the existing string
// then there is no need to convert: bug 4620622:
if (strValue.equals(holder.getValue()))
return strValue;
return converter.getAsString(context, component, value);
} else
{
GenericConverterFactory fac = GenericConverterFactory
.getCurrentInstance();
try
{
value = fac.convert(value, expectedType);
}
catch(TypeConversionException e)
{
// Use underlying exception's message if TypeConversionException
// wrapped exception raised by the converter
Throwable cause = e.getCause();
if (cause == null)
cause = e;
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,
MessageFactory.getString(context, UIXEditableValue.CONVERSION_MESSAGE_ID),
cause.getLocalizedMessage());
throw new ConverterException(msg, e);
}
}
}
}
return value;
}
@SuppressWarnings("unchecked")
public String getClientScript(FacesContext context, UIComponent component)
{
if (component == null)
{
_LOG
.severe("The component is null, but it is needed for the client id, so no script written");
return null;
}
// Add a JavaScript Object to store the datefield formats
// on the client-side. We currently store the format string
// for each and every field. It'd be more efficient to have
// an array of formats, then store for each field the
// index of the format, especially if we could delay outputting
// these objects to when the