From cac8228f38136cfc41673458c58c25f168b1e1ff Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 20 Nov 2012 20:02:47 +0100 Subject: o Adding BoneCP init to Config. o Starting on an actor-like structure for the running jobs. o Loading Nexus servers and group ids to look for from the database. --- .../io/trygvis/esper/testing/nexus/NexusDao.java | 107 ++++++++++++++++++--- 1 file changed, 93 insertions(+), 14 deletions(-) (limited to 'src/main/java/io/trygvis/esper/testing/nexus/NexusDao.java') diff --git a/src/main/java/io/trygvis/esper/testing/nexus/NexusDao.java b/src/main/java/io/trygvis/esper/testing/nexus/NexusDao.java index 39d4233..00e15fe 100755 --- a/src/main/java/io/trygvis/esper/testing/nexus/NexusDao.java +++ b/src/main/java/io/trygvis/esper/testing/nexus/NexusDao.java @@ -5,21 +5,43 @@ import static fj.data.Option.*; import io.trygvis.esper.testing.*; import org.joda.time.*; +import java.net.*; +import java.sql.Array; import java.sql.*; +import java.util.*; +import java.util.List; public class NexusDao extends Dao { protected NexusDao(Connection c) { super(c); } - public void insertRepository(String repositoryId, LocalDateTime discoveryDate) throws SQLException { - PreparedStatement s = prepareStatement("INSERT INTO nexus_repository(id) VALUES(?)"); - s.setString(1, repositoryId); - s.executeUpdate(); + /* + public void insertRepository(String repositoryId, URI nexusUri, LocalDateTime discoveryDate) throws SQLException { + int i = 1; + try (PreparedStatement s = prepareStatement("INSERT INTO nexus_repository(id, uri, discovered_date) VALUES(?, ?, ?)")) { + s.setString(i++, repositoryId); + s.setString(i++, nexusUri.toASCIIString()); + s.setTimestamp(i, new Timestamp(discoveryDate.toDateTime().getMillis())); + s.executeUpdate(); + } + } + */ + + public List selectServer() throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT url FROM nexus_server")) { + ResultSet rs = s.executeQuery(); + + List servers = new ArrayList<>(); + while(rs.next()) { + servers.add(new NexusServerDto(uri(rs.getString(1)))); + } + return servers; + } } - public Option selectRepository(String repositoryId) throws SQLException { - PreparedStatement s = prepareStatement("SELECT id, discovery_date, last_update, last_successful_update FROM nexus_repository WHERE id=?"); + public Option selectRepository(String repositoryId) throws SQLException { + PreparedStatement s = prepareStatement("SELECT id, nexus_server_url, group_ids, discovery_date, last_update, last_successful_update FROM nexus_repository WHERE id=?"); s.setString(1, repositoryId); try (ResultSet rs = s.executeQuery()) { @@ -27,24 +49,81 @@ public class NexusDao extends Dao { return Option.none(); } - return some(new NexusRepository( - rs.getString(1), - fromNull(rs.getTimestamp(2)).map(timestampToLocalDateTime), - fromNull(rs.getTimestamp(3)).map(timestampToLocalDateTime), - fromNull(rs.getTimestamp(4)).map(timestampToLocalDateTime) - )); + return some(toRepository(rs)); + } + } + + public List findRepositories(URI nexusUrl) throws SQLException { + PreparedStatement s = prepareStatement("SELECT id, nexus_server_url, group_ids, created_date, last_update, last_successful_update FROM nexus_repository WHERE nexus_server_url=?"); + s.setString(1, nexusUrl.toASCIIString()); + + List list = new ArrayList<>(); + try (ResultSet rs = s.executeQuery()) { + while(rs.next()) { + list.add(toRepository(rs)); + } + } + return list; + } + + private NexusRepositoryDto toRepository(ResultSet rs) throws SQLException { + int i = 1; + + return new NexusRepositoryDto( + rs.getString(i++), + uri(rs.getString(i++)), + (String[]) rs.getArray(i++).getArray(), + fromNull(rs.getTimestamp(i++)).map(timestampToLocalDateTime), + fromNull(rs.getTimestamp(i++)).map(timestampToLocalDateTime), + fromNull(rs.getTimestamp(i)).map(timestampToLocalDateTime) + ); + } + + private URI uri(String s) { + try { + return URI.create(s); + } catch (Exception e) { + e.printStackTrace(System.out); + return null; } } } -class NexusRepository { +class NexusServerDto { + public final URI url; + + NexusServerDto(URI url) { + this.url = url; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof NexusServerDto)) return false; + + NexusServerDto that = (NexusServerDto) o; + + if (!url.equals(that.url)) return false; + + return true; + } + + public int hashCode() { + return url.hashCode(); + } +} + +class NexusRepositoryDto { public final String repositoryId; + public final URI nexusUrl; + public final String[] groupIds; public final Option discoveryDate; public final Option lastUpdate; public final Option lastSuccessfulUpdate; - NexusRepository(String repositoryId, Option discoveryDate, Option lastUpdate, Option lastSuccessfulUpdate) { + NexusRepositoryDto(String repositoryId, URI nexusUrl, String[] groupIds, Option discoveryDate, Option lastUpdate, Option lastSuccessfulUpdate) { this.repositoryId = repositoryId; + this.nexusUrl = nexusUrl; + this.groupIds = groupIds; this.discoveryDate = discoveryDate; this.lastUpdate = lastUpdate; this.lastSuccessfulUpdate = lastSuccessfulUpdate; -- cgit v1.2.3