src/main/java/de/uapcore/lightpit/dao/postgres/PGVersionDao.java

Sun, 17 May 2020 16:23:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 17 May 2020 16:23:39 +0200
changeset 59
c759c60507a2
parent 51
src/main/java/de/uapcore/lightpit/dao/postgres/PGProjectDao.java@dd0a45ae25d7
child 62
833e0385572a
permissions
-rw-r--r--

adds version management

universe@38 1 /*
universe@38 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@38 3 *
universe@38 4 * Copyright 2018 Mike Becker. All rights reserved.
universe@38 5 *
universe@38 6 * Redistribution and use in source and binary forms, with or without
universe@38 7 * modification, are permitted provided that the following conditions are met:
universe@38 8 *
universe@38 9 * 1. Redistributions of source code must retain the above copyright
universe@38 10 * notice, this list of conditions and the following disclaimer.
universe@38 11 *
universe@38 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@38 13 * notice, this list of conditions and the following disclaimer in the
universe@38 14 * documentation and/or other materials provided with the distribution.
universe@38 15 *
universe@38 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@38 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@38 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@38 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@38 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@38 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@38 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@38 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@38 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@38 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@38 26 * POSSIBILITY OF SUCH DAMAGE.
universe@38 27 *
universe@38 28 */
universe@38 29 package de.uapcore.lightpit.dao.postgres;
universe@38 30
universe@59 31 import de.uapcore.lightpit.dao.VersionDao;
universe@38 32 import de.uapcore.lightpit.entities.Project;
universe@59 33 import de.uapcore.lightpit.entities.Version;
universe@59 34 import de.uapcore.lightpit.entities.VersionStatus;
universe@38 35
universe@38 36 import java.sql.Connection;
universe@38 37 import java.sql.PreparedStatement;
universe@38 38 import java.sql.ResultSet;
universe@38 39 import java.sql.SQLException;
universe@47 40 import java.util.ArrayList;
universe@47 41 import java.util.List;
universe@38 42 import java.util.Objects;
universe@38 43
universe@59 44 public final class PGVersionDao implements VersionDao {
universe@38 45
universe@47 46 private final PreparedStatement insert, update, list, find;
universe@38 47
universe@59 48 public PGVersionDao(Connection connection) throws SQLException {
universe@47 49 list = connection.prepareStatement(
universe@59 50 "select id, project, name, ordinal, status " +
universe@59 51 "from lpit_version " +
universe@59 52 "where project = ? " +
universe@59 53 "order by ordinal, name");
universe@47 54
universe@47 55 find = connection.prepareStatement(
universe@59 56 "select id, project, name, ordinal, status " +
universe@59 57 "from lpit_version " +
universe@47 58 "where id = ?");
universe@38 59
universe@38 60 insert = connection.prepareStatement(
universe@59 61 "insert into lpit_version (project, name, ordinal, status) values (?, ?, ?, ?::version_status)"
universe@38 62 );
universe@38 63 update = connection.prepareStatement(
universe@59 64 "update lpit_version set name = ?, ordinal = ?, status = ?::version_status where id = ?"
universe@38 65 );
universe@38 66 }
universe@38 67
universe@59 68 public Version mapColumns(ResultSet result) throws SQLException {
universe@59 69 final var version = new Version(result.getInt("id"), new Project(result.getInt("project")));
universe@59 70 version.setName(result.getString("name"));
universe@59 71 version.setOrdinal(result.getInt("ordinal"));
universe@59 72 version.setStatus(VersionStatus.valueOf(result.getString("status")));
universe@59 73 return version;
universe@38 74 }
universe@38 75
universe@38 76 @Override
universe@59 77 public void save(Version instance) throws SQLException {
universe@38 78 Objects.requireNonNull(instance.getName());
universe@59 79 Objects.requireNonNull(instance.getProject());
universe@59 80 insert.setInt(1, instance.getProject().getId());
universe@59 81 insert.setString(2, instance.getName());
universe@59 82 insert.setInt(3, instance.getOrdinal());
universe@59 83 insert.setString(4, instance.getStatus().name());
universe@38 84 insert.executeUpdate();
universe@38 85 }
universe@38 86
universe@38 87 @Override
universe@59 88 public boolean update(Version instance) throws SQLException {
universe@38 89 Objects.requireNonNull(instance.getName());
universe@38 90 update.setString(1, instance.getName());
universe@59 91 update.setInt(2, instance.getOrdinal());
universe@59 92 update.setString(3, instance.getStatus().name());
universe@59 93 update.setInt(4, instance.getId());
universe@38 94 return update.executeUpdate() > 0;
universe@38 95 }
universe@47 96
universe@47 97 @Override
universe@59 98 public List<Version> list(Project project) throws SQLException {
universe@59 99 list.setInt(1, project.getId());
universe@59 100 List<Version> versions = new ArrayList<>();
universe@47 101 try (var result = list.executeQuery()) {
universe@47 102 while (result.next()) {
universe@59 103 versions.add(mapColumns(result));
universe@47 104 }
universe@47 105 }
universe@59 106 return versions;
universe@47 107 }
universe@47 108
universe@47 109 @Override
universe@59 110 public Version find(int id) throws SQLException {
universe@47 111 find.setInt(1, id);
universe@47 112 try (var result = find.executeQuery()) {
universe@47 113 if (result.next()) {
universe@47 114 return mapColumns(result);
universe@47 115 } else {
universe@47 116 return null;
universe@47 117 }
universe@47 118 }
universe@47 119 }
universe@38 120 }

mercurial