Author: fdesbois Date: 2012-04-18 12:14:00 +0200 (Wed, 18 Apr 2012) New Revision: 3293 Url: http://chorem.org/repositories/revision/pollen/3293 Log: add DateConverter to format dates Added: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/DateConverter.java Modified: trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties trunk/pollen-ui-struts2/src/main/resources/xwork-conversion.properties Added: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/DateConverter.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/DateConverter.java (rev 0) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/DateConverter.java 2012-04-18 10:14:00 UTC (rev 3293) @@ -0,0 +1,51 @@ +package org.chorem.pollen.ui.actions; + +import com.opensymphony.xwork2.ActionContext; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.struts2.util.StrutsTypeConverter; +import org.nuiton.i18n.I18n; +import org.nuiton.util.DateUtil; + +import java.text.ParseException; +import java.util.Date; +import java.util.Locale; +import java.util.Map; + +/** + * Created: 18/04/12 + * + * @author fdesbois <desbois@codelutin.com> + */ +public class DateConverter extends StrutsTypeConverter { + + private static final Log log = LogFactory.getLog(DateConverter.class); + + @Override + public Date convertFromString(Map context, String[] values, Class toClass) { + String value = values[0]; + Date result = null; + if (StringUtils.isNotBlank(value)) { + try { + result = DateUtil.parseDate(value, getDatePattern()); + } catch (ParseException e) { + log.error("Error parsing date '" + value + "'", e); + } + } + return result; + } + + @Override + public String convertToString(Map context, Object o) { + Date date = (Date) o; + String formatedDate = DateUtil.formatDate(date, getDatePattern()); + return formatedDate; + } + + protected String getDatePattern() { + Locale locale = ActionContext.getContext().getLocale(); + String result = I18n.l_(locale, "pollen.common.datePattern"); + return result; + } +} Modified: trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties =================================================================== --- trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties 2012-04-18 10:13:55 UTC (rev 3292) +++ trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_en_GB.properties 2012-04-18 10:14:00 UTC (rev 3293) @@ -60,6 +60,7 @@ pollen.common.commentText=Comment pollen.common.comments=Comments about this poll pollen.common.csvImport=CSV import +pollen.common.datePattern=MM/dd/yyyy HH\:mm a pollen.common.description=Description pollen.common.displayType-group=Results by groups pollen.common.displayType-normal=Results Modified: trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties =================================================================== --- trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties 2012-04-18 10:13:55 UTC (rev 3292) +++ trunk/pollen-ui-struts2/src/main/resources/i18n/pollen-ui-struts2_fr_FR.properties 2012-04-18 10:14:00 UTC (rev 3293) @@ -65,6 +65,7 @@ pollen.common.commentText=Commentaire pollen.common.comments=Commentaire à propos du sondage pollen.common.csvImport=Import CSV +pollen.common.datePattern=dd/MM/yyyy HH\:mm pollen.common.description=Description pollen.common.displayType-group=Résultats par groupes pollen.common.displayType-normal=Résultats Modified: trunk/pollen-ui-struts2/src/main/resources/xwork-conversion.properties =================================================================== --- trunk/pollen-ui-struts2/src/main/resources/xwork-conversion.properties 2012-04-18 10:13:55 UTC (rev 3292) +++ trunk/pollen-ui-struts2/src/main/resources/xwork-conversion.properties 2012-04-18 10:14:00 UTC (rev 3293) @@ -21,4 +21,5 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # #L% ### -org.chorem.pollen.bean.PollUri=org.chorem.pollen.ui.actions.PollUriConverter \ No newline at end of file +org.chorem.pollen.bean.PollUri=org.chorem.pollen.ui.actions.PollUriConverter +java.util.Date=org.chorem.pollen.ui.actions.DateConverter \ No newline at end of file