Java tutorial
/*$Id: DateFieldData.java 15541 2010-04-20 15:45:18Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2009 ABM-utvikling * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.questionnaire.domain.data; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.Transient; import no.abmu.questionnaire.domain.metadata.Field; import no.abmu.util.date.DateUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.annotations.Type; /** * BooleanFieldData. * * @author Aase Mestad * @author $Author: jens $ * @version $Rev: 15541 $ * @date $Date: 2010-04-20 17:45:18 +0200 (Tue, 20 Apr 2010) $ * @copyright ABM-Utvikling */ @SuppressWarnings("serial") @Entity //@Table(name="DATE_FIELD_DATA", schema = DbSchemaNameConst.QUESTIONNAIRE_DB_SCHEMA_NAME) //@PrimaryKeyJoinColumn(name="id") @DiscriminatorValue("Date") public class DateFieldData extends FieldDataImpl { private static final Log logger = (Log) LogFactory.getLog(DateFieldData.class); private Date dateValue; public DateFieldData() { } public DateFieldData(Field field) { super(field); } @Type(type = "no.abmu.util.hibernate3.DateType") public Date getDateValue() { // logger.debug("Executing getDateValue for code=[" + getCode() + "]"); return this.dateValue; } public void setDateValue(Date date) { // logger.debug("Executing setDateValue as Date date=[" + date + "] for code=[" + getCode() + "]"); if (date == null) { if (this.dateValue != null) { this.dateValue = null; } return; } if (date.equals(this.dateValue)) { return; } this.dateValue = date; } // //Only set dirty flag and value if value really changes. // private void setTimeMs(Date dateValue) { // logger.debug("Executing setTimeMs dateValue=[" + dateValue + "]."); // if ((this.dateValue == null && dateValue == null) || ((this.dateValue != null) // && this.dateValue.equals(dateValue))) { // return; // } // this.dateValue = dateValue; // } @Transient public Object getUntypedValue() { // logger.debug("Executing getUntypedValue"); return getValue(); } @Transient public Date getValue() { if (logger.isDebugEnabled()) { logger.debug("Executing getValue for code=[" + getCode() + "]"); } return this.dateValue; } // public void setValue(Date value) { // setValueAsString(value); // } public void setValue(Object value) { if (logger.isDebugEnabled()) { logger.debug("Executing setValue as Object value=[" + value + "] for code=[" + getCode() + "]"); } if (value == null) { if (this.dateValue != null) { this.dateValue = null; } return; } if (value instanceof Date) { setDateValue((Date) value); return; } if (value instanceof String) { String stringValue = (String) value; setValueAsString(stringValue); return; } logger.warn("Unknown dataType for " + this.toString() + " Code=[" + this.getCode() + "] DataType received was " + value.getClass().getName()); } public void setValue(Date value) { if (logger.isDebugEnabled()) { logger.debug("Executing setValue as Date value=[" + value + "] for code=[" + getCode() + "]"); } setDateValue(value); } /** * Assumes that date will be a String in ISO data format. * * @param value */ public void setValueAsString(String value) { // logger.debug("Executing setValueAsString as string=[" + value + "] for code=[" + getCode() + "]"); if (value == null) { if (this.dateValue != null) { this.dateValue = null; } return; } String trimmedStringValue = value.trim(); if (trimmedStringValue.isEmpty()) { if (this.dateValue != null) { this.dateValue = null; } return; } Date newDate = parseDateString(trimmedStringValue); setDateValue(newDate); } @Transient public String getValueAsString() { if (this.dateValue == null) { return ""; } DateFormat outputFormat = DateUtil.EUROPEAN_DATEFORMAT; return outputFormat.format(dateValue); } public boolean acceptValue(Object value) { return (value instanceof Date); } private Date parseDateString(String dateStringIn) { List<DateFormat> validDateFormats = new ArrayList<DateFormat>(); validDateFormats.add(new SimpleDateFormat("yyyy-MM-dd")); validDateFormats.add(new SimpleDateFormat("dd/MM-yyyy")); validDateFormats.add(new SimpleDateFormat("dd/MM-yy")); validDateFormats.add(new SimpleDateFormat("dd.MM.yyyy")); validDateFormats.add(new SimpleDateFormat("dd-MM-yyyy")); validDateFormats.add(new SimpleDateFormat("dd.MM.yy")); String dateString = dateStringIn.trim(); Date date = null; for (DateFormat dateFormat : validDateFormats) { try { date = dateFormat.parse(dateString); } catch (ParseException e) { // ignore, try next format } } if (date == null) { throw new IllegalArgumentException(dateString + " is not a valid date"); } return date; } }