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 @Override
public void put(final String key, final String value) throws SQLException { public void put(final String key, final String value) throws SQLException {
//
Element element = getElement(key); Element element = getElement(key);
//
if (element == null) { if (element == null) {
// //
element = new Element(); element = new Element();
@ -1699,54 +1702,54 @@ public class SQLSikevaDB implements SikevaDB {
element.update(value); element.update(value);
element.setArchiveDate(null); element.setArchiveDate(null);
//
put(element);
} else { } else {
// //
element.update(value); element.update(value);
}
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
// //
Connection connection = null; connection = getConnection();
PreparedStatement statement = null; connection.setAutoCommit(false);
ResultSet resultSet = null;
try {
//
connection = getConnection();
connection.setAutoCommit(false);
// Archive existing element. // 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.setTimestamp(1, toTimestamp(element.getEditionDate()));
statement.setString(2, element.getKey()); statement.setString(2, element.getKey());
statement.executeUpdate(); statement.executeUpdate();
statement.clearParameters(); 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(?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
connection.commit();
} catch (Exception exception) {
connection.rollback();
} finally {
close(connection, statement, resultSet);
} }
// Insert new version of the element.
statement = connection
.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
connection.commit();
} catch (Exception exception) {
connection.rollback();
} finally {
close(connection, statement, resultSet);
} }
} }
@ -1768,23 +1771,22 @@ public class SQLSikevaDB implements SikevaDB {
element.update(value); element.update(value);
element.setArchiveDate(null); element.setArchiveDate(null);
//
put(element);
} else { } else {
// //
element.update(value); element.update(value);
}
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
// //
Connection connection = null; connection = getConnection();
PreparedStatement statement = null; connection.setAutoCommit(false);
ResultSet resultSet = null;
try {
//
connection = getConnection();
connection.setAutoCommit(false);
// Archive existing element. // 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 = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL");
statement.setTimestamp(1, toTimestamp(element.getEditionDate())); statement.setTimestamp(1, toTimestamp(element.getEditionDate()));
@ -1793,30 +1795,30 @@ public class SQLSikevaDB implements SikevaDB {
statement.executeUpdate(); statement.executeUpdate();
statement.clearParameters(); 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(?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
connection.commit();
} catch (Exception exception) {
connection.rollback();
} finally {
close(connection, statement, resultSet);
} }
// Insert new version of the element.
statement = connection
.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
connection.commit();
} catch (Exception exception) {
connection.rollback();
} finally {
close(connection, statement, resultSet);
} }
} }