-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gradebook.java
298 lines (257 loc) · 12.3 KB
/
Gradebook.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import at.favre.lib.bytes.Bytes;
import java.io.*;
import java.nio.file.*;
import java.security.*;
import java.sql.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import org.sqlite.*;
public class Gradebook {
private final String name;
private SecretKey key;
private Connection db;
private Gradebook(String name) {
this.name = name;
this.key = null;
this.db = null;
}
public static class AppError extends Exception {
}
public static class BackendError extends Exception {
}
public static String create(String name) throws AppError, BackendError {
if (new File(name).exists()) {
throw new AppError();
}
try {
Gradebook gbook = new Gradebook(name);
gbook.db = DriverManager.getConnection("jdbc:sqlite:" + name);
gbook.db.prepareStatement(
"CREATE TABLE assignments(assignmentID INTEGER PRIMARY KEY AUTOINCREMENT, assignmentName TEXT NOT NULL UNIQUE, points INTEGER NOT NULL, weight REAL NOT NULL);")
.execute();
gbook.db.prepareStatement(
"CREATE TRIGGER validate_total_assignment_weights BEFORE INSERT ON assignments BEGIN SELECT CASE WHEN (SELECT SUM(weight) FROM assignments) + NEW.weight > 1.0 THEN RAISE (ABORT, \"Assignment weights above 1.0\") END; END;")
.execute();
gbook.db.prepareStatement(
"CREATE TABLE students(studentID INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT NOT NULL, lastName TEXT NOT NULL, UNIQUE(firstName, lastName));")
.execute();
gbook.db.prepareStatement(
"CREATE TABLE grades(gradeID INTEGER PRIMARY KEY AUTOINCREMENT, studentID INTEGER NOT NULL, assignmentID INTEGER NOT NULL, grade INTEGER NOT NULL,"
+ "FOREIGN KEY (studentID) REFERENCES students (studentID) ON DELETE CASCADE, FOREIGN KEY (assignmentID) REFERENCES assignments (assignmentID) ON DELETE CASCADE,"
+ "UNIQUE(studentID, assignmentID));")
.execute();
gbook.db.prepareStatement(
"CREATE TRIGGER validate_grade BEFORE UPDATE ON grades BEGIN SELECT CASE WHEN NEW.grade > (SELECT points FROM assignments WHERE assignmentID = NEW.assignmentID) THEN RAISE (ABORT, \"Grade above assignment points\") END; END;")
.execute();
KeyGenerator keyGen = KeyGenerator.getInstance("ChaCha20");
keyGen.init(256, SecureRandom.getInstanceStrong());
byte[] bkey = keyGen.generateKey().getEncoded();
gbook.key = new SecretKeySpec(bkey, "ChaCha20");
gbook.save_and_encrypt();
return Bytes.wrap(bkey).encodeHex();
} catch (NoSuchAlgorithmException | SQLException ignored) {
throw new BackendError();
}
}
public void save_and_encrypt() throws BackendError {
try {
this.db.close();
Path file = Paths.get(this.name);
byte[] nonce = new byte[12];
SecureRandom.getInstanceStrong().nextBytes(nonce);
Cipher cipher = Cipher.getInstance("ChaCha20-Poly1305");
IvParameterSpec iv = new IvParameterSpec(nonce);
cipher.init(Cipher.ENCRYPT_MODE, this.key, iv);
byte[] data = Files.readAllBytes(file);
byte[] out = cipher.doFinal(data);
Files.write(file, nonce);
Files.write(file, out, StandardOpenOption.APPEND);
} catch (IOException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException
| SQLException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException ignored) {
throw new BackendError();
}
}
public static Gradebook load_and_decrypt(String name, String key) throws AppError, BackendError {
if (!new File(name).exists()) {
throw new AppError();
}
try {
Gradebook gbook = new Gradebook(name);
byte[] bkey = Bytes.parseHex(key).array();
gbook.key = new SecretKeySpec(bkey, "ChaCha20");
Path file = Paths.get(name);
byte[] data = Files.readAllBytes(file);
if (data.length < 12) {
throw new AppError();
}
Cipher cipher = Cipher.getInstance("ChaCha20-Poly1305");
IvParameterSpec iv = new IvParameterSpec(data, 0, 12);
cipher.init(Cipher.DECRYPT_MODE, gbook.key, iv);
byte[] out = cipher.doFinal(data, 12, data.length - 12);
Files.write(file, out);
gbook.db = DriverManager.getConnection("jdbc:sqlite:" + name);
return gbook;
} catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
| InvalidAlgorithmParameterException | IllegalBlockSizeException ignored) {
throw new BackendError();
} catch (BadPaddingException | SQLException ignored) {
throw new AppError();
}
}
public void addAssignment(String name, int points, double weight) throws AppError, BackendError {
try {
PreparedStatement sql = this.db
.prepareStatement("INSERT INTO assignments (assignmentName, points, weight) VALUES (?1, ?2, ?3);");
sql.setString(1, name);
sql.setInt(2, points);
sql.setDouble(3, weight);
sql.execute();
sql = this.db.prepareStatement(
"INSERT INTO grades (studentID, assignmentID, grade) SELECT studentID, (SELECT assignmentID from assignments WHERE assignmentName = ?1), 0 FROM students;");
sql.setString(1, name);
sql.execute();
} catch (SQLiteException e) {
if (e.getResultCode() == SQLiteErrorCode.SQLITE_CONSTRAINT_UNIQUE
|| e.getResultCode() == SQLiteErrorCode.SQLITE_CONSTRAINT_TRIGGER) {
throw new AppError();
} else {
throw new BackendError();
}
} catch (SQLException ignored) {
throw new BackendError();
}
}
public void deleteAssignment(String name) throws AppError, BackendError {
try {
PreparedStatement sql = this.db.prepareStatement("DELETE FROM assignments WHERE assignmentName = ?1;");
sql.setString(1, name);
if (sql.executeUpdate() == 0) {
throw new AppError();
}
} catch (SQLException ignored) {
throw new BackendError();
}
}
public void addStudent(String firstName, String lastName) throws AppError, BackendError {
try {
PreparedStatement sql = this.db
.prepareStatement("INSERT INTO students (firstName, lastName) VALUES (?1, ?2);");
sql.setString(1, firstName);
sql.setString(2, lastName);
sql.execute();
sql = this.db.prepareStatement(
"INSERT INTO grades (studentID, assignmentID, grade) SELECT (SELECT studentID from students WHERE firstName = ?1 AND lastName = ?2), assignmentID, 0 FROM assignments;");
sql.setString(1, firstName);
sql.setString(2, lastName);
sql.execute();
} catch (SQLiteException e) {
if (e.getResultCode() == SQLiteErrorCode.SQLITE_CONSTRAINT_UNIQUE) {
throw new AppError();
} else {
throw new BackendError();
}
} catch (SQLException ignored) {
throw new BackendError();
}
}
public void deleteStudent(String firstName, String lastName) throws AppError, BackendError {
try {
PreparedStatement sql = this.db
.prepareStatement("DELETE FROM students WHERE firstName = ?1 AND lastName = ?2;");
sql.setString(1, firstName);
sql.setString(2, lastName);
if (sql.executeUpdate() == 0) {
throw new AppError();
}
} catch (SQLException ignored) {
throw new BackendError();
}
}
public void addGrade(String studentFirstName, String studentLastName, String assignmentName, int grade)
throws AppError, BackendError {
try {
PreparedStatement sql = this.db.prepareStatement(
"UPDATE grades SET grade = ?1 WHERE studentID = (SELECT studentID FROM students WHERE firstName = ?2 AND lastName = ?3) AND assignmentID = (SELECT assignmentID FROM assignments WHERE assignmentName = ?4);");
sql.setInt(1, grade);
sql.setString(2, studentFirstName);
sql.setString(3, studentLastName);
sql.setString(4, assignmentName);
if (sql.executeUpdate() == 0) {
throw new AppError();
}
} catch (SQLiteException e) {
if (e.getResultCode() == SQLiteErrorCode.SQLITE_CONSTRAINT_TRIGGER) {
throw new AppError();
} else {
throw new BackendError();
}
} catch (SQLException ignored) {
throw new BackendError();
}
}
public void printAssignmentGrades(String name, boolean alphabetical_or_grade_order) throws AppError, BackendError {
try {
PreparedStatement sql = this.db
.prepareStatement("SELECT assignmentID FROM assignments WHERE assignmentName = ?1;");
sql.setString(1, name);
ResultSet row = sql.executeQuery();
if (!row.isBeforeFirst()) {
throw new AppError();
}
int assignmentID = row.getInt(1);
String order = "lastName, firstName";
if (!alphabetical_or_grade_order) {
order = "grade DESC";
}
sql = this.db.prepareStatement("SELECT lastName, firstName, grade FROM grades "
+ "INNER JOIN students ON grades.studentID = students.studentID "
+ "WHERE assignmentID = ?1 ORDER BY " + order + ";");
sql.setInt(1, assignmentID);
row = sql.executeQuery();
while (row.next()) {
System.out.println("(" + row.getString(1) + ", " + row.getString(2) + ", " + row.getInt(3) + ")");
}
} catch (SQLException ignored) {
throw new BackendError();
}
}
public void printStudentGrades(String firstName, String lastName) throws AppError, BackendError {
try {
PreparedStatement sql = this.db
.prepareStatement("SELECT studentID FROM students WHERE firstName = ?1 AND lastName = ?2;");
sql.setString(1, firstName);
sql.setString(2, lastName);
ResultSet row = sql.executeQuery();
if (!row.isBeforeFirst()) {
throw new AppError();
}
int studentID = row.getInt(1);
sql = this.db.prepareStatement(
"SELECT assignmentName, grade FROM grades INNER JOIN assignments ON grades.assignmentID = assignments.assignmentID WHERE studentID = ?1;");
sql.setInt(1, studentID);
row = sql.executeQuery();
while (row.next()) {
System.out.println("(" + row.getString(1) + ", " + row.getInt(2) + ")");
}
} catch (SQLException ignored) {
throw new BackendError();
}
}
public void printFinalGrades(boolean alphabetical_or_grade_order) throws BackendError {
try {
String order = "lastName, firstName";
if (!alphabetical_or_grade_order) {
order = "totalGrade DESC";
}
PreparedStatement sql = this.db.prepareStatement(
"SELECT lastname, firstName, (SELECT SUM((CAST(grade AS REAL) / points) * weight) FROM grades INNER JOIN assignments ON grades.assignmentID = assignments.assignmentID WHERE grades.studentID = students.studentID) AS totalGrade FROM students ORDER BY "
+ order + ";");
ResultSet row = sql.executeQuery();
while (row.next()) {
System.out.println("(" + row.getString(1) + ", " + row.getString(2) + ", " + row.getDouble(3) + ")");
}
} catch (SQLException ignored) {
throw new BackendError();
}
}
}