Improve code design.

This commit is contained in:
Christian P. MOMON 2013-12-11 12:14:14 +01:00
parent 4b211d8190
commit bdc7359b79

View file

@ -1690,8 +1690,11 @@ public class SQLSikevaDB implements SikevaDB {
*/
@Override
public void put(final String key, final String value) throws SQLException {
//
Element element = getElement(key);
//
if (element == null) {
//
element = new Element();
@ -1699,12 +1702,10 @@ public class SQLSikevaDB implements SikevaDB {
element.update(value);
element.setArchiveDate(null);
//
put(element);
} else {
//
element.update(value);
}
//
Connection connection = null;
@ -1716,16 +1717,19 @@ public class SQLSikevaDB implements SikevaDB {
connection.setAutoCommit(false);
// Archive existing element.
statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL");
if (element.getCreationDate() != null) {
statement = connection
.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL");
statement.setTimestamp(1, toTimestamp(element.getEditionDate()));
statement.setString(2, element.getKey());
statement.executeUpdate();
statement.clearParameters();
statement.close();
}
// Insert new version of the element.
statement.clearParameters();
statement = connection
.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
@ -1748,7 +1752,6 @@ public class SQLSikevaDB implements SikevaDB {
close(connection, statement, resultSet);
}
}
}
/**
* {@inheritDoc}
@ -1768,12 +1771,10 @@ public class SQLSikevaDB implements SikevaDB {
element.update(value);
element.setArchiveDate(null);
//
put(element);
} else {
//
element.update(value);
}
//
Connection connection = null;
@ -1785,6 +1786,7 @@ public class SQLSikevaDB implements SikevaDB {
connection.setAutoCommit(false);
// Archive existing element.
if (element.getCreationDate() != null) {
statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL");
statement.setTimestamp(1, toTimestamp(element.getEditionDate()));
@ -1793,9 +1795,10 @@ public class SQLSikevaDB implements SikevaDB {
statement.executeUpdate();
statement.clearParameters();
statement.close();
}
// Insert new version of the element.
statement.clearParameters();
statement = connection
.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
@ -1818,7 +1821,6 @@ public class SQLSikevaDB implements SikevaDB {
close(connection, statement, resultSet);
}
}
}
/**
* {@inheritDoc}