Migrate from java.util.Date to Joda DateTime.

This commit is contained in:
Christian P. MOMON 2013-09-15 07:34:04 +02:00
parent a2e7f31068
commit 220a584c2c
13 changed files with 97 additions and 80 deletions

View file

@ -22,5 +22,6 @@
</classpathentry>
<classpathentry kind="lib" path="lib/mysql-jdbc-5.0.8.jar"/>
<classpathentry kind="lib" path="lib/devinsy-utils-0.2.6.jar"/>
<classpathentry kind="lib" path="lib/joda-time-2.3.jar" sourcepath="lib/joda-time-2.3-sources.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

Binary file not shown.

BIN
lib/joda-time-2.3.jar Normal file

Binary file not shown.

View file

@ -15,9 +15,8 @@
*/
package fr.devinsy.sikevadb;
import java.util.Date;
import org.apache.commons.codec.digest.DigestUtils;
import org.joda.time.DateTime;
/**
*
@ -26,9 +25,9 @@ import org.apache.commons.codec.digest.DigestUtils;
*/
public class Element {
private Date creationDate;
private Date editionDate;
private Date archiveDate;
private DateTime creationDate;
private DateTime editionDate;
private DateTime archiveDate;
private String key;
private String subkey;
private long size;
@ -59,14 +58,14 @@ public class Element {
*
*/
public void archive() {
this.archiveDate = new Date();
this.archiveDate = DateTime.now();
}
public Date getArchiveDate() {
public DateTime getArchiveDate() {
return archiveDate;
}
public Date getCreationDate() {
public DateTime getCreationDate() {
return creationDate;
}
@ -74,7 +73,7 @@ public class Element {
return digest;
}
public Date getEditionDate() {
public DateTime getEditionDate() {
return editionDate;
}
@ -94,24 +93,20 @@ public class Element {
return value;
}
public void setArchiveDate(final Date archiveDate) {
if (archiveDate == null) {
this.archiveDate = null;
} else {
this.archiveDate = (Date) archiveDate.clone();
}
public void setArchiveDate(final DateTime date) {
this.archiveDate = date;
}
public void setCreationDate(final Date creationDate) {
this.creationDate = (Date) creationDate.clone();
public void setCreationDate(final DateTime date) {
this.creationDate = date;
}
public void setDigest(final String digest) {
this.digest = digest;
}
public void setEditionDate(final Date editionDate) {
this.editionDate = (Date) editionDate.clone();
public void setEditionDate(final DateTime date) {
this.editionDate = date;
}
public void setKey(final String key) {
@ -135,7 +130,7 @@ public class Element {
setValue(value);
//
this.editionDate = new Date();
this.editionDate = DateTime.now();
//
if (value == null) {

View file

@ -16,7 +16,8 @@
package fr.devinsy.sikevadb;
import java.io.File;
import java.util.Date;
import org.joda.time.DateTime;
import fr.devinsy.util.StringList;
@ -61,7 +62,7 @@ public class FileSikevaDB implements SikevaDB {
}
@Override
public void clearArchive(final Date beforeDate) throws Exception {
public void clearArchive(final DateTime beforeDate) throws Exception {
// TODO Auto-generated method stub
}

View file

@ -29,6 +29,7 @@ import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -107,7 +108,7 @@ public class SQLSikevaDB implements SikevaDB {
connection.setAutoCommit(true);
statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL");
statement.setTimestamp(1, dateToTimestamp(element.getArchiveDate()));
statement.setTimestamp(1, toTimestamp(element.getArchiveDate()));
statement.setString(2, element.getKey());
statement.executeUpdate();
@ -146,7 +147,7 @@ public class SQLSikevaDB implements SikevaDB {
connection.setAutoCommit(true);
statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=?");
statement.setTimestamp(1, dateToTimestamp(element.getArchiveDate()));
statement.setTimestamp(1, toTimestamp(element.getArchiveDate()));
statement.setString(2, element.getKey());
statement.setString(3, element.getSubkey());
@ -185,7 +186,7 @@ public class SQLSikevaDB implements SikevaDB {
* {@inheritDoc}
*/
@Override
public void clearArchive(final Date beforeDate) throws SQLException {
public void clearArchive(final DateTime beforeDate) throws SQLException {
//
if (beforeDate == null) {
throw new NullPointerException("beforeDate is null.");
@ -198,9 +199,9 @@ public class SQLSikevaDB implements SikevaDB {
//
connection = getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement("DELETE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND TO_SECONDS(ARCHIVE_DATE)<?");
statement = connection.prepareStatement("DELETE FROM elements WHERE ARCHIVE_DATE IS NOT NULL AND UNIX_TIMESTAMP(ARCHIVE_DATE)<?");
statement.setLong(1, beforeDate.getTime() / 1000);
statement.setLong(1, beforeDate.getMillis() / 1000);
statement.executeUpdate();
@ -222,7 +223,7 @@ public class SQLSikevaDB implements SikevaDB {
if (maxDays < 0) {
throw new IndexOutOfBoundsException("maxDays is negative.");
} else {
Date beforeDate = new Date(new Date().getTime() - maxDays * 24 * 60 * 60 * 1000);
DateTime beforeDate = DateTime.now().minusDays(maxDays);
clearArchive(beforeDate);
}
}
@ -603,9 +604,9 @@ public class SQLSikevaDB implements SikevaDB {
element.setValue(resultSet.getString(3));
element.setSize(resultSet.getLong(4));
element.setDigest(resultSet.getString(5));
element.setCreationDate(timestampToDate(resultSet.getTimestamp(6)));
element.setEditionDate(timestampToDate(resultSet.getTimestamp(7)));
element.setArchiveDate(timestampToDate(resultSet.getTimestamp(8)));
element.setCreationDate(toDateTime(resultSet.getTimestamp(6)));
element.setEditionDate(toDateTime(resultSet.getTimestamp(7)));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(8)));
//
result.add(element);
@ -654,9 +655,9 @@ public class SQLSikevaDB implements SikevaDB {
element.setValue(resultSet.getString(3));
element.setSize(resultSet.getLong(4));
element.setDigest(resultSet.getString(5));
element.setCreationDate(timestampToDate(resultSet.getTimestamp(6)));
element.setEditionDate(timestampToDate(resultSet.getTimestamp(7)));
element.setArchiveDate(timestampToDate(resultSet.getTimestamp(8)));
element.setCreationDate(toDateTime(resultSet.getTimestamp(6)));
element.setEditionDate(toDateTime(resultSet.getTimestamp(7)));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(8)));
//
result.add(element);
@ -948,9 +949,9 @@ public class SQLSikevaDB implements SikevaDB {
result.setValue(resultSet.getString(3));
result.setSize(resultSet.getLong(4));
result.setDigest(resultSet.getString(5));
result.setCreationDate(timestampToDate(resultSet.getTimestamp(6)));
result.setEditionDate(timestampToDate(resultSet.getTimestamp(7)));
result.setArchiveDate(timestampToDate(resultSet.getTimestamp(8)));
result.setCreationDate(toDateTime(resultSet.getTimestamp(6)));
result.setEditionDate(toDateTime(resultSet.getTimestamp(7)));
result.setArchiveDate(toDateTime(resultSet.getTimestamp(8)));
} else {
result = null;
@ -1003,9 +1004,9 @@ public class SQLSikevaDB implements SikevaDB {
result.setValue(resultSet.getString(3));
result.setSize(resultSet.getLong(4));
result.setDigest(resultSet.getString(5));
result.setCreationDate(timestampToDate(resultSet.getTimestamp(6)));
result.setEditionDate(timestampToDate(resultSet.getTimestamp(7)));
result.setArchiveDate(timestampToDate(resultSet.getTimestamp(8)));
result.setCreationDate(toDateTime(resultSet.getTimestamp(6)));
result.setEditionDate(toDateTime(resultSet.getTimestamp(7)));
result.setArchiveDate(toDateTime(resultSet.getTimestamp(8)));
} else {
result = null;
}
@ -1054,9 +1055,9 @@ public class SQLSikevaDB implements SikevaDB {
element.setValue(resultSet.getString(3));
element.setSize(resultSet.getLong(4));
element.setDigest(resultSet.getString(5));
element.setCreationDate(timestampToDate(resultSet.getTimestamp(6)));
element.setEditionDate(timestampToDate(resultSet.getTimestamp(7)));
element.setArchiveDate(timestampToDate(resultSet.getTimestamp(8)));
element.setCreationDate(toDateTime(resultSet.getTimestamp(6)));
element.setEditionDate(toDateTime(resultSet.getTimestamp(7)));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(8)));
//
result.add(element);
@ -1105,9 +1106,9 @@ public class SQLSikevaDB implements SikevaDB {
element.setValue(resultSet.getString(3));
element.setSize(resultSet.getLong(4));
element.setDigest(resultSet.getString(5));
element.setCreationDate(timestampToDate(resultSet.getTimestamp(6)));
element.setEditionDate(timestampToDate(resultSet.getTimestamp(7)));
element.setArchiveDate(timestampToDate(resultSet.getTimestamp(8)));
element.setCreationDate(toDateTime(resultSet.getTimestamp(6)));
element.setEditionDate(toDateTime(resultSet.getTimestamp(7)));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(8)));
//
result.add(element);
@ -1619,9 +1620,9 @@ public class SQLSikevaDB implements SikevaDB {
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, new java.sql.Timestamp(element.getCreationDate().getTime()));
statement.setTimestamp(7, dateToTimestamp(element.getEditionDate()));
statement.setTimestamp(8, dateToTimestamp(element.getArchiveDate()));
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
@ -1663,7 +1664,7 @@ public class SQLSikevaDB implements SikevaDB {
// Archive existing element.
statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL");
statement.setTimestamp(1, dateToTimestamp(element.getEditionDate()));
statement.setTimestamp(1, toTimestamp(element.getEditionDate()));
statement.setString(2, element.getKey());
statement.executeUpdate();
@ -1679,9 +1680,9 @@ public class SQLSikevaDB implements SikevaDB {
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, dateToTimestamp(element.getCreationDate()));
statement.setTimestamp(7, dateToTimestamp(element.getEditionDate()));
statement.setTimestamp(8, dateToTimestamp(element.getArchiveDate()));
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
@ -1732,7 +1733,7 @@ public class SQLSikevaDB implements SikevaDB {
// Archive existing element.
statement = connection.prepareStatement("UPDATE elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=?");
statement.setTimestamp(1, dateToTimestamp(element.getEditionDate()));
statement.setTimestamp(1, toTimestamp(element.getEditionDate()));
statement.setString(2, element.getKey());
statement.setString(3, element.getSubkey());
@ -1749,9 +1750,9 @@ public class SQLSikevaDB implements SikevaDB {
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, dateToTimestamp(element.getCreationDate()));
statement.setTimestamp(7, dateToTimestamp(element.getEditionDate()));
statement.setTimestamp(8, dateToTimestamp(element.getArchiveDate()));
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
@ -1888,7 +1889,25 @@ public class SQLSikevaDB implements SikevaDB {
* @param source
* @return
*/
public static java.sql.Timestamp dateToTimestamp(final Date source) {
public static DateTime toDateTime(final java.sql.Timestamp source) {
DateTime result;
if (source == null) {
result = null;
} else {
result = new DateTime(source.getTime());
}
//
return result;
}
/**
*
* @param source
* @return
*/
public static java.sql.Timestamp toTimestamp(final Date source) {
java.sql.Timestamp result;
if (source == null) {
@ -1906,13 +1925,13 @@ public class SQLSikevaDB implements SikevaDB {
* @param source
* @return
*/
public static Date timestampToDate(final java.sql.Timestamp source) {
Date result;
public static java.sql.Timestamp toTimestamp(final DateTime source) {
java.sql.Timestamp result;
if (source == null) {
result = null;
} else {
result = new Date(source.getTime());
result = new java.sql.Timestamp(source.getMillis());
}
//

View file

@ -15,7 +15,7 @@
*/
package fr.devinsy.sikevadb;
import java.util.Date;
import org.joda.time.DateTime;
import fr.devinsy.util.StringList;
@ -34,7 +34,7 @@ public interface SikevaDB {
public void clearAllArchive() throws Exception;
public void clearArchive(final Date beforeDate) throws Exception;
public void clearArchive(final DateTime beforeDate) throws Exception;
public void clearArchive(final int maxDays) throws Exception;

View file

@ -2,11 +2,11 @@ package fr.devinsy.sikevadb;
import java.io.File;
import java.io.OutputStream;
import java.util.Date;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -93,15 +93,15 @@ public class XMLSikevaDB {
String value = StringEscapeUtils.unescapeXml(in.readContentTag("value").getContent());
int size = Integer.parseInt(tag.attributes().getByLabel("size").getValue());
String digest = in.readContentTag("digest").getContent();
Date creationDate = new Date(Long.parseLong(in.readContentTag("creation_date").getContent()));
Date editionDate = new Date(Long.parseLong(in.readContentTag("edition_date").getContent()));
DateTime creationDate = DateTime.parse(in.readContentTag("creation_date").getContent());
DateTime editionDate = DateTime.parse(in.readContentTag("edition_date").getContent());
Date archiveDate;
DateTime archiveDate;
String archiveDateValue = in.readNullableContentTag("archive_date").getContent();
if (archiveDateValue == null) {
archiveDate = null;
} else {
archiveDate = new Date(Long.parseLong(archiveDateValue));
archiveDate = DateTime.parse(archiveDateValue);
}
//
@ -178,12 +178,12 @@ public class XMLSikevaDB {
out.writeTag("value", StringEscapeUtils.escapeXml(source.getValue()));
out.writeTag("size", String.valueOf(source.getSize()));
out.writeTag("digest", source.getDigest());
out.writeTag("creation_date", String.valueOf(source.getCreationDate().getTime()));
out.writeTag("edition_date", String.valueOf(source.getEditionDate().getTime()));
out.writeTag("creation_date", source.getCreationDate().toString());
out.writeTag("edition_date", source.getEditionDate().toString());
if (source.getArchiveDate() == null) {
out.writeTag("archive_date", null);
} else {
out.writeTag("archive_date", String.valueOf(source.getArchiveDate().getTime()));
out.writeTag("archive_date", source.getArchiveDate().toString());
}
}

View file

@ -1,7 +1,6 @@
package fr.devinsy.sikevadb;
import java.sql.SQLException;
import java.util.Date;
import javax.naming.NamingException;
@ -10,6 +9,7 @@ import org.apache.commons.lang3.RandomStringUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@ -70,9 +70,9 @@ public class SQLSikevaDBTest {
element.setSubkey(null);
element.setSize(10);
element.setDigest("qsdkfqskjf");
element.setCreationDate(new Date());
element.setEditionDate(new Date());
element.setArchiveDate(new Date(new Date().getTime() - 10 * 24 * 60 * 60 * 1000));
element.setCreationDate(DateTime.now().minusDays(11));
element.setEditionDate(DateTime.now().minusDays(11));
element.setArchiveDate(DateTime.now().minusDays(10));
element.setValue("bonjour");
database.put(element);
@ -106,16 +106,16 @@ public class SQLSikevaDBTest {
element.setSubkey(null);
element.setSize(10);
element.setDigest("qsdkfqskjf");
element.setCreationDate(new Date());
element.setEditionDate(new Date());
element.setArchiveDate(new Date(new Date().getTime() - 10 * 24 * 60 * 60 * 1000));
element.setCreationDate(DateTime.now());
element.setEditionDate(DateTime.now());
element.setArchiveDate(DateTime.now().minusDays(10));
element.setValue("bonjour");
database.put(element);
database.put("alpha", "bravo", "toto");
database.clearArchive(new Date(new Date().getTime() - 5 * 24 * 60 * 60 * 1000));
database.clearArchive(DateTime.now().minusDays(5));
Assert.assertEquals(1, database.countOfElements());
Assert.assertEquals(0, database.countOfArchivedElements());
@ -216,7 +216,8 @@ public class SQLSikevaDBTest {
database.put("alpha01", "bravo5", "qlskjfmlqj");
Assert.assertEquals(120, database.memorySize());
Assert.assertEquals(10, database.memorySize("alpha01"));
Assert.assertEquals(60, database.memorySize("alpha01"));
Assert.assertEquals(10, database.memorySize("alpha03"));
Assert.assertEquals(10, database.memorySize("alpha01", "bravo1"));
database.clearDatabase();