Play! framework bootstrap(Fixtures.loadModels("initial-data.yaml)), injecting "random" String value in model's setter
I开发者_StackOverflow中文版 have a model class with a persisted DateTime field that is only interacted via getters/setters for Transient properties, String date;
and String time;
. The do some very specific formatting to create the DateTime object that will be persisted or retrieved when need be.
The problem is that when my model's loaded from the yaml file, the setter for the time
field receives a String value that doesn't correspond at all to anything in my project/code.
Here's the class with only relevant members:
package models;
import javax.persistence.*;
import org.hibernate.annotations.*;
import org.joda.time.*;
import org.joda.time.format.*;
import play.db.jpa.*;
@javax.persistence.Entity
public class Booking extends Model {
@Column
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
public DateTime datetime;
public Integer duration;
@Transient
public String date;
@Transient
public String time;
//default constructor called by play's model loader that sets default values that are required for the getters and setters to work.
public Booking() {
DateTimeFormatter fmt = DateTimeFormat.forPattern("'ISO8601':yyyy-MM-dd'T'HH:mm:ssZ");
this.datetime = fmt.parseDateTime("ISO8601:1970-01-01T00:00:00+0200");
//this.datetime = fmt.parseDateTime(this.date+"T"+this.time);
}
public void setDate(String dateStr) {
this.date = dateStr;
if (dateStr.contains("ISO")) {
DateTimeFormatter dt = DateTimeFormat.forPattern("'ISO8601':yyyy-MM-dd'T'HH:mm:ssZ");
DateTime tmp = dt.parseDateTime(dateStr);
this.datetime = toDateTime(tmp.toString("yyyy-MM-dd"), getTime());
} else {
this.datetime = toDateTime(dateStr, getTime());
}
}
public void setTime(String timeStr) {
this.time = timeStr; //timeStr = "780" for some reason?!
if (timeStr.contains("ISO")) {
DateTimeFormatter dt = DateTimeFormat.forPattern("'ISO8601':yyyy-MM-dd'T'HH:mm:ssZ");
DateTime tmp = dt.parseDateTime(timeStr);
this.datetime = toDateTime(getDate(), tmp.toString("HH:mm"));
}
this.datetime = toDateTime(getDate(), timeStr);
}
public String getDate() {
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
return this.datetime.toString(format);
}
public String getTime() {
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm");
return this.datetime.toString(format);
}
private DateTime toDateTime(String dateStr, String timeStr) {
DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinute();
DateTime dt = fmt.parseDateTime(dateStr + "T" + timeStr);
return dt;
}
When I run through the debugger, the timeStr
parameter that setTime receives when it's first called is "780"
. There is no such value in my yaml file as the model is injected like this:
Booking(bobBooking):
date: 2011-09-16
time: 13:00
duration: 30
headcount: 10
room: b
user: bob
description: Bob's Booking.
The additional fields are omitted.
Try using quotes for time value in yaml file. There could be some issue in parsing colon fields using SnakeYAML parser (which is the default in Play)
YAML 1.1 defines 13:00
as a sexagesimal value (which is not what you expect)
http://yaml.org/type/int.html
Use single or double quotes to specify a string value. ('13:00', "13:00")
精彩评论