Fix for #240 - Add support for using Joda LocalDateTime and DateTime with @CreatedTimestamp, @UpdatedTimestamp

This commit is contained in:
rbygrave
2015-02-02 20:39:06 +13:00
parent 36dd2bbee7
commit ace45a038e
7 changed files with 254 additions and 1 deletions
@@ -0,0 +1,66 @@
package com.avaje.ebeaninternal.server.deploy.generatedproperty;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import org.joda.time.LocalDateTime;
import org.joda.time.DateTime;
/**
* Support joda time types as GeneratedProperty.
*/
public class GeneratedInsertJodaTime {
public static abstract class Base implements GeneratedProperty {
@Override
public boolean includeInUpdate() {
return false;
}
@Override
public boolean includeInAllUpdates() {
return false;
}
@Override
public boolean includeInInsert() {
return true;
}
@Override
public boolean isDDLNotNullable() {
return true;
}
@Override
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
return prop.getValue(bean);
}
}
/**
* LocalDateTime support.
*/
public static class LocalDT extends Base {
@Override
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
return new LocalDateTime();
}
}
/**
* DateTime support.
*/
public static class DateTimeDT extends Base {
@Override
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
return new DateTime();
}
}
}
@@ -0,0 +1,69 @@
package com.avaje.ebeaninternal.server.deploy.generatedproperty;
import com.avaje.ebean.bean.EntityBean;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
import org.joda.time.LocalDateTime;
import org.joda.time.DateTime;
/**
* Support java.time DateTime types as GeneratedProperty.
*/
public class GeneratedUpdateJodaTime {
public static abstract class Base implements GeneratedProperty {
@Override
public boolean includeInUpdate() {
return true;
}
@Override
public boolean includeInAllUpdates() {
return true;
}
@Override
public boolean includeInInsert() {
return true;
}
@Override
public boolean isDDLNotNullable() {
return true;
}
}
/**
* LocalDateTime support.
*/
public static class LocalDT extends Base {
@Override
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
return new LocalDateTime();
}
@Override
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
return new LocalDateTime();
}
}
/**
* OffsetDateTime support.
*/
public static class DateTimeDT extends Base {
@Override
public Object getInsertValue(BeanProperty prop, EntityBean bean) {
return new DateTime();
}
@Override
public Object getUpdateValue(BeanProperty prop, EntityBean bean) {
return new DateTime();
}
}
}
@@ -32,6 +32,11 @@ public class InsertTimestampFactory {
map.put(OffsetDateTime.class, new GeneratedInsertJavaTime.OffsetDT());
map.put(ZonedDateTime.class, new GeneratedInsertJavaTime.ZonedDT());
}
if (ClassUtil.isPresent("org.joda.time.LocalDateTime", this.getClass())) {
map.put(org.joda.time.LocalDateTime.class, new GeneratedInsertJodaTime.LocalDT());
map.put(org.joda.time.DateTime.class, new GeneratedInsertJodaTime.DateTimeDT());
}
}
public void setInsertTimestamp(DeployBeanProperty property) {
@@ -32,6 +32,10 @@ public class UpdateTimestampFactory {
map.put(OffsetDateTime.class, new GeneratedUpdateJavaTime.OffsetDT());
map.put(ZonedDateTime.class, new GeneratedUpdateJavaTime.ZonedDT());
}
if (ClassUtil.isPresent("org.joda.time.LocalDateTime", this.getClass())) {
map.put(org.joda.time.LocalDateTime.class, new GeneratedUpdateJodaTime.LocalDT());
map.put(org.joda.time.DateTime.class, new GeneratedUpdateJodaTime.DateTimeDT());
}
}
public void setUpdateTimestamp(DeployBeanProperty property) {
@@ -2,7 +2,6 @@ package com.avaje.ebeaninternal.server.type;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;
import com.avaje.ebean.config.JsonConfig;
import org.joda.time.LocalDateTime;
@@ -0,0 +1,68 @@
package com.avaje.tests.model.joda;
import com.avaje.ebean.annotation.CreatedTimestamp;
import com.avaje.ebean.annotation.UpdatedTimestamp;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
@Entity
public class BasicJodaEntity {
@Id
Long id;
String name;
@CreatedTimestamp
LocalDateTime created;
@UpdatedTimestamp
DateTime updated;
@Version
LocalDateTime version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getCreated() {
return created;
}
public void setCreated(LocalDateTime created) {
this.created = created;
}
public DateTime getUpdated() {
return updated;
}
public void setUpdated(DateTime updated) {
this.updated = updated;
}
public LocalDateTime getVersion() {
return version;
}
public void setVersion(LocalDateTime version) {
this.version = version;
}
}
@@ -0,0 +1,42 @@
package com.avaje.tests.model.joda;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
public class TestJodaInsertUpdate extends BaseTestCase {
@Test
public void test() throws InterruptedException {
BasicJodaEntity e0 = new BasicJodaEntity();
e0.setName("foo");
Ebean.save(e0);
LocalDateTime created = e0.getCreated();
DateTime updated = e0.getUpdated();
LocalDateTime version = e0.getVersion();
assertNotNull(created);
assertNotNull(updated);
assertNotNull(version);
Thread.sleep(10);
e0.setName("bar");
Ebean.save(e0);
LocalDateTime created1 = e0.getCreated();
DateTime updated1 = e0.getUpdated();
LocalDateTime version1 = e0.getVersion();
assertSame(created, created1);
assertNotSame(updated, updated1);
assertNotSame(version, version1);
}
}