Skip to content

Commit 8ff85a9

Browse files
committed
Merge branch 'issue/1206/order-awards-determined'
Fixes #1206
2 parents 76d4fbf + 11c6530 commit 8ff85a9

12 files changed

+727
-207
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* #1206 - Allow one to specify the order that awards are determined
12
* #1207 - Add virtual subjective categories to finalist scheduling and deliberations
23
* #1204 - Allow sorting of virtual subjective categories for awards script
34
* #1205 - Add virtual subjective categories to the awards script
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

src/main/java/fll/db/GenerateDB.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class GenerateDB {
4141
/**
4242
* Version of the database that will be created.
4343
*/
44-
public static final int DATABASE_VERSION = 46;
44+
public static final int DATABASE_VERSION = 47;
4545

4646
private static final org.apache.logging.log4j.Logger LOGGER = org.apache.logging.log4j.LogManager.getLogger();
4747

@@ -348,6 +348,8 @@ public static void generateDB(final ChallengeDescription description,
348348
createDeliberationCategoryOrder(connection, true);
349349

350350
createVirtualSubjectiveCategoryTable(connection, true);
351+
352+
createAwardDeterminationTable(connection, true);
351353

352354
// --------------- create views ---------------
353355

@@ -1457,6 +1459,23 @@ private static void createAwardsScriptRankTable(final Connection connection,
14571459

14581460
}
14591461

1462+
@SuppressFBWarnings(value = { "SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE" }, justification = "Building up SQL based on parameters")
1463+
/* package */ static void createAwardDeterminationTable(final Connection connection,
1464+
final boolean createConstraints)
1465+
throws SQLException {
1466+
try (Statement stmt = connection.createStatement()) {
1467+
final Formatter sql = new Formatter();
1468+
sql.format("CREATE TABLE award_determination_order (");
1469+
sql.format(" award LONGVARCHAR NOT NULL");
1470+
sql.format(" ,determined_order INTEGER NOT NULL");
1471+
if (createConstraints) {
1472+
sql.format(" , CONSTRAINT award_determine_order_pk PRIMARY KEY (award)");
1473+
}
1474+
sql.format(")");
1475+
stmt.executeUpdate(sql.toString());
1476+
}
1477+
}
1478+
14601479
/**
14611480
* Create tables used to store information about the awards script.
14621481
* This also populates the default values.

src/main/java/fll/db/ImportDB.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,11 @@ private static void upgradeDatabase(final Connection connection,
749749
upgrade45to46(connection);
750750
}
751751

752+
dbVersion = Queries.getDatabaseVersion(connection);
753+
if (dbVersion < 47) {
754+
upgrade46to47(connection);
755+
}
756+
752757
// NOTE: when adding new tournament parameters they need to be explicitly set in
753758
// importTournamentParameters
754759

@@ -1555,6 +1560,18 @@ private static void upgrade45to46(final Connection connection) throws SQLExcepti
15551560
setDBVersion(connection, 46);
15561561
}
15571562

1563+
/**
1564+
* Add award determination order table.
1565+
*/
1566+
private static void upgrade46to47(final Connection connection) throws SQLException {
1567+
LOGGER.debug("Upgrading database from 46 to 47");
1568+
1569+
try (Statement stmt = connection.createStatement()) {
1570+
GenerateDB.createAwardDeterminationTable(connection, false);
1571+
}
1572+
setDBVersion(connection, 47);
1573+
}
1574+
15581575
/**
15591576
* Check for a column in a table. This checks table names both upper and lower
15601577
* case.
@@ -1960,6 +1977,7 @@ public static void importDatabase(final Connection sourceConnection,
19601977
throws SQLException {
19611978

19621979
final ChallengeDescription description = GlobalParameters.getChallengeDescription(destinationConnection);
1980+
importGlobalData(sourceConnection, destinationConnection, description);
19631981

19641982
final Tournament sourceTournament = Tournament.findTournamentByName(sourceConnection, tournamentName);
19651983
final int sourceTournamentID = sourceTournament.getTournamentID();
@@ -1993,6 +2011,19 @@ public static void importDatabase(final Connection sourceConnection,
19932011
ScoreStandardization.updateScoreTotals(description, destinationConnection, destTournamentID);
19942012
}
19952013

2014+
private static void importGlobalData(final Connection sourceConnection,
2015+
final Connection destinationConnection,
2016+
final ChallengeDescription description)
2017+
throws SQLException {
2018+
2019+
// import the award determination order if it doesn't already exist at the
2020+
// destination
2021+
if (!AwardDeterminationOrder.dataExists(destinationConnection)) {
2022+
final List<AwardCategory> awardDeterminationOrder = AwardDeterminationOrder.get(sourceConnection, description);
2023+
AwardDeterminationOrder.save(destinationConnection, awardDeterminationOrder);
2024+
}
2025+
}
2026+
19962027
private static void importSubjectiveData(final Connection sourceConnection,
19972028
final Connection destinationConnection,
19982029
final ChallengeDescription description,

0 commit comments

Comments
 (0)