|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 High Tech Kids. All rights reserved |
| 3 | + * HighTechKids is on the web at: http://www.hightechkids.org |
| 4 | + * This code is released under GPL; see LICENSE.txt for details. |
| 5 | + */ |
| 6 | + |
| 7 | +package fll.db; |
| 8 | + |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.PreparedStatement; |
| 11 | +import java.sql.ResultSet; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.sql.Statement; |
| 14 | +import java.util.LinkedList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import static org.checkerframework.checker.nullness.util.NullnessUtil.castNonNull; |
| 18 | + |
| 19 | +import fll.web.report.awards.AwardCategory; |
| 20 | +import fll.web.report.awards.ChampionshipCategory; |
| 21 | +import fll.web.report.awards.HeadToHeadCategory; |
| 22 | +import fll.xml.ChallengeDescription; |
| 23 | +import fll.xml.NonNumericCategory; |
| 24 | +import fll.xml.PerformanceScoreCategory; |
| 25 | +import fll.xml.SubjectiveScoreCategory; |
| 26 | +import fll.xml.VirtualSubjectiveScoreCategory; |
| 27 | + |
| 28 | +/** |
| 29 | + * Load and save the award determination order in the database. |
| 30 | + */ |
| 31 | +public final class AwardDeterminationOrder { |
| 32 | + |
| 33 | + private AwardDeterminationOrder() { |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param connection the database |
| 38 | + * @param description used to ensure that all awards are returned, even if not |
| 39 | + * stored in the database |
| 40 | + * @return the order that awards are determined |
| 41 | + * @throws SQLException on a database error |
| 42 | + */ |
| 43 | + public static List<AwardCategory> get(final Connection connection, |
| 44 | + final ChallengeDescription description) |
| 45 | + throws SQLException { |
| 46 | + final List<AwardCategory> order = new LinkedList<>(); |
| 47 | + |
| 48 | + try (Statement stmt = connection.createStatement()) { |
| 49 | + try ( |
| 50 | + ResultSet rs = stmt.executeQuery("SELECT award FROM award_determination_order ORDER BY determined_order ASC")) { |
| 51 | + while (rs.next()) { |
| 52 | + final String award = castNonNull(rs.getString(1)); |
| 53 | + order.add(description.getCategoryByTitle(award)); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // ensure that all categories are added. |
| 59 | + if (!order.contains(ChampionshipCategory.INSTANCE)) { |
| 60 | + order.add(ChampionshipCategory.INSTANCE); |
| 61 | + } |
| 62 | + final PerformanceScoreCategory performance = description.getPerformance(); |
| 63 | + if (!order.contains(performance)) { |
| 64 | + order.add(performance); |
| 65 | + } |
| 66 | + for (final VirtualSubjectiveScoreCategory cat : description.getVirtualSubjectiveCategories()) { |
| 67 | + if (!order.contains(cat)) { |
| 68 | + order.add(cat); |
| 69 | + } |
| 70 | + } |
| 71 | + for (final SubjectiveScoreCategory cat : description.getSubjectiveCategories()) { |
| 72 | + if (!order.contains(cat)) { |
| 73 | + order.add(cat); |
| 74 | + } |
| 75 | + } |
| 76 | + for (final NonNumericCategory cat : description.getNonNumericCategories()) { |
| 77 | + // add all categories, handle ignored categories on output |
| 78 | + if (!order.contains(cat)) { |
| 79 | + order.add(cat); |
| 80 | + } |
| 81 | + } |
| 82 | + if (!order.contains(HeadToHeadCategory.INSTANCE)) { |
| 83 | + order.add(HeadToHeadCategory.INSTANCE); |
| 84 | + } |
| 85 | + return order; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param connection where to store the data |
| 90 | + * @param awardDeterminationOrder the data to store |
| 91 | + * @throws SQLException on a database error |
| 92 | + */ |
| 93 | + public static void save(final Connection connection, |
| 94 | + final List<AwardCategory> awardDeterminationOrder) |
| 95 | + throws SQLException { |
| 96 | + final boolean autoCommit = connection.getAutoCommit(); |
| 97 | + try { |
| 98 | + connection.setAutoCommit(false); |
| 99 | + try (Statement stmt = connection.createStatement()) { |
| 100 | + stmt.executeUpdate("DELETE FROM award_determination_order"); |
| 101 | + } |
| 102 | + |
| 103 | + try ( |
| 104 | + PreparedStatement prep = connection.prepareStatement("INSERT INTO award_determination_order (award, determined_order) VALUES(?, ?)")) { |
| 105 | + int index = 0; |
| 106 | + for (final AwardCategory award : awardDeterminationOrder) { |
| 107 | + ++index; |
| 108 | + prep.setString(1, award.getTitle()); |
| 109 | + prep.setInt(2, index); |
| 110 | + prep.executeUpdate(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + connection.commit(); |
| 115 | + } finally { |
| 116 | + connection.setAutoCommit(autoCommit); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param connection the database to check |
| 122 | + * @return if there is data stored in the database |
| 123 | + * @throws SQLException on a database error |
| 124 | + */ |
| 125 | + public static boolean dataExists(final Connection connection) throws SQLException { |
| 126 | + try (Statement stmt = connection.createStatement()) { |
| 127 | + try ( |
| 128 | + ResultSet rs = stmt.executeQuery("SELECT award FROM award_determination_order ORDER BY determined_order ASC")) { |
| 129 | + return rs.next(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments