#659 - ENH: Add support for Instant with @WhenCreated @WhenModified

This commit is contained in:
Robin Bygrave
2016-04-22 16:51:30 +12:00
parent b79bb892f7
commit e769abf737
9 changed files with 87 additions and 1 deletions
@@ -36,6 +36,17 @@ public class GeneratedInsertJavaTime {
}
}
/**
* Instant support.
*/
public static class InstantDT extends Base {
@Override
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
return JavaTimeUtils.toInstant(now);
}
}
/**
* LocalDateTime support.
*/
@@ -31,6 +31,22 @@ public class GeneratedUpdateJavaTime {
}
}
/**
* Instant support.
*/
public static class InstantDT extends Base {
@Override
public Object getInsertValue(BeanProperty prop, EntityBean bean, long now) {
return JavaTimeUtils.toInstant(now);
}
@Override
public Object getUpdateValue(BeanProperty prop, EntityBean bean, long now) {
return JavaTimeUtils.toInstant(now);
}
}
/**
* LocalDateTime support.
*/
@@ -5,6 +5,7 @@ import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import javax.persistence.PersistenceException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
@@ -27,6 +28,7 @@ public class InsertTimestampFactory {
map.put(long.class, longTime);
if (classLoadConfig.isJavaTimePresent()) {
map.put(Instant.class, new GeneratedInsertJavaTime.InstantDT());
map.put(LocalDateTime.class, new GeneratedInsertJavaTime.LocalDT());
map.put(OffsetDateTime.class, new GeneratedInsertJavaTime.OffsetDT());
map.put(ZonedDateTime.class, new GeneratedInsertJavaTime.ZonedDT());
@@ -11,6 +11,13 @@ import java.time.ZonedDateTime;
*/
public class JavaTimeUtils {
/**
* Return the system millis time as a LocalDateTime.
*/
public static Object toInstant(long systemMillis) {
return Instant.ofEpochMilli(systemMillis);
}
/**
* Return the system millis time as a LocalDateTime.
*/
@@ -5,6 +5,7 @@ import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import javax.persistence.PersistenceException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
@@ -27,6 +28,7 @@ public class UpdateTimestampFactory {
map.put(long.class, longTime);
if (classLoadConfig.isJavaTimePresent()) {
map.put(Instant.class, new GeneratedUpdateJavaTime.InstantDT());
map.put(LocalDateTime.class, new GeneratedUpdateJavaTime.LocalDT());
map.put(OffsetDateTime.class, new GeneratedUpdateJavaTime.OffsetDT());
map.put(ZonedDateTime.class, new GeneratedUpdateJavaTime.ZonedDT());
@@ -5,6 +5,7 @@ import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@@ -14,6 +15,16 @@ public class InsertTimestampFactoryTest {
InsertTimestampFactory factory = new InsertTimestampFactory(new ClassLoadConfig());
@Test
public void test_createdTimestamp_Instant() {
DeployBeanProperty prop = new DeployBeanProperty(null, Instant.class, null, null);
GeneratedProperty insertTimestamp = factory.createInsertTimestamp(prop);
Object value = insertTimestamp.getInsertValue(null, null, System.currentTimeMillis());
assertTrue(value instanceof Instant);
}
@Test
public void test_createdTimestamp_LocalDateTime() {
@@ -5,6 +5,7 @@ import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
@@ -15,6 +16,16 @@ public class UpdateTimestampFactoryTest {
UpdateTimestampFactory factory = new UpdateTimestampFactory(new ClassLoadConfig());
@Test
public void test_createdTimestamp_Instant() {
DeployBeanProperty prop = new DeployBeanProperty(null, Instant.class, null, null);
GeneratedProperty generatedProperty = factory.createUpdateTimestamp(prop);
Object value = generatedProperty.getInsertValue(null, null, System.currentTimeMillis());
assertTrue(value instanceof Instant);
}
@Test
public void test_createdTimestamp_LocalDateTime() {
@@ -31,16 +31,19 @@ public class TestGeneratedProperties extends BaseTestCase {
assertNotNull(bean.getZdtUpdated());
assertNotNull(bean.getLongCreated());
assertNotNull(bean.getLongUpdated());
assertNotNull(bean.getInstantCreated());
assertNotNull(bean.getInstantUpdated());
assertThat(bean.getWhenCreated().toInstant().toEpochMilli()).isEqualTo(bean.getLongCreated());
assertThat(bean.getWhenModified().toInstant().toEpochMilli()).isEqualTo(bean.getLongCreated());
assertThat(bean.getWhenModified().toInstant().toEpochMilli()).isEqualTo(bean.getLongCreated());
bean.setName("updating...");
Ebean.save(bean);
assertThat(bean.getWhenModified()).isNotEqualTo(bean.getLongCreated());
assertThat(bean.getWhenModified().toInstant().toEpochMilli()).isEqualTo(bean.getLongUpdated());
assertThat(bean.getInstantUpdated().toEpochMilli()).isEqualTo(bean.getLongUpdated());
assertThat(bean.getInstantCreated().toEpochMilli()).isEqualTo(bean.getLongCreated());
Ebean.delete(bean);
}
@@ -9,6 +9,7 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
@@ -54,6 +55,12 @@ public class EGenProps {
@UpdatedTimestamp
ZonedDateTime zdtUpdated;
@CreatedTimestamp
Instant instantCreated;
@UpdatedTimestamp
Instant instantUpdated;
@CreatedTimestamp
long longCreated;
@@ -179,4 +186,20 @@ public class EGenProps {
public void setLongUpdated(long longUpdated) {
this.longUpdated = longUpdated;
}
public Instant getInstantCreated() {
return instantCreated;
}
public void setInstantCreated(Instant instantCreated) {
this.instantCreated = instantCreated;
}
public Instant getInstantUpdated() {
return instantUpdated;
}
public void setInstantUpdated(Instant instantUpdated) {
this.instantUpdated = instantUpdated;
}
}