}
@Override
public AbstractMetadataValue createValue(Object o) throws Descriptor.FormException {
boolean detailsCheck = false;
DateMetadataValue dateMetadataValue;
Calendar cal = Calendar.getInstance();
MetadataChecks checks = new MetadataChecks();
if (o instanceof JSONObject) {
String day = "";
String month = "";
String year = "";
JSONObject jsonObject = (JSONObject)o;
if (jsonObject.has("day")) {
day = jsonObject.getString("day");
}
if (jsonObject.has("month")) {
month = jsonObject.getString("month");
}
if (jsonObject.has("year")) {
year = jsonObject.getString("year");
}
FormValidation dateValidation = checks.doCheckDateValue(year, month, day);
if (!dateValidation.equals(FormValidation.ok())) {
throw new Descriptor.FormException("Wrong date format " + dateValidation.getMessage(), "");
}
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
cal.set(Calendar.MONTH, Integer.parseInt(month) - DEFAULT_MONTH_ADJUSTMENT);
cal.set(Calendar.YEAR, Integer.parseInt(year));
if (jsonObject.has("details")) {
String hour = "";
String minute = "";
String second = "";
detailsCheck = true;
JSONObject details = jsonObject.getJSONObject("details");
if (details.has("hour")) {
hour = details.getString("hour");
}
if (details.has("minute")) {
minute = details.getString("minute");
}
if (details.has("second")) {
second = details.getString("second");
}
FormValidation timeValidation = checks.doCheckTimeValue(hour, minute, second);
if (!timeValidation.equals(FormValidation.ok())) {
throw new Descriptor.FormException("Wrong time format: " + timeValidation.getMessage(), "");
}
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
cal.set(Calendar.MINUTE, Integer.parseInt(minute));
cal.set(Calendar.SECOND, Integer.parseInt(second));
} else {
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
}
cal.set(Calendar.MILLISECOND, 0);
dateMetadataValue = new DateMetadataValue(getName(), getDescription(),
cal, detailsCheck, isExposedToEnvironment());
} else {
//maybe we should throw an exception here instead of going with default.
dateMetadataValue = new DateMetadataValue(getName(), getDescription(),
getDefaultValue(), detailsCheck, isExposedToEnvironment());
}
return dateMetadataValue;
}