Skip to content

Commit

Permalink
Merge pull request #325 from softwareconstruction240/use-checked-exce…
Browse files Browse the repository at this point in the history
…ption-for-dataaccessexception

Use checked exception for dataaccessexception
  • Loading branch information
pawlh authored Apr 24, 2024
2 parents e4b7822 + b1f02b8 commit c8b12d9
Show file tree
Hide file tree
Showing 29 changed files with 281 additions and 148 deletions.
3 changes: 2 additions & 1 deletion src/main/java/edu/byu/cs/analytics/CommitAnalytics.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.byu.cs.canvas.CanvasIntegrationImpl;
import edu.byu.cs.canvas.CanvasService;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.dataAccess.SubmissionDao;
import edu.byu.cs.model.Phase;
import edu.byu.cs.model.Submission;
Expand Down Expand Up @@ -70,7 +71,7 @@ private record CommitDatum(
*
* @return a serialized version of the data
*/
public static String generateCSV() {
public static String generateCSV() throws DataAccessException {

SubmissionDao submissionDao = DaoService.getSubmissionDao();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.byu.cs.analytics;

import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.util.DateTimeUtils;
import edu.byu.cs.util.FileUtils;

Expand All @@ -18,7 +19,7 @@ public class CommitAnalyticsRouter {
*
* @return the CSV data
*/
public static String update() {
public static String update() throws DataAccessException {
long ts = Instant.now().getEpochSecond();
String data = CommitAnalytics.generateCSV();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/byu/cs/autograder/TrafficController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.byu.cs.controller.WebSocketController;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.model.QueueItem;
import org.eclipse.jetty.websocket.api.Session;

Expand Down Expand Up @@ -38,7 +39,7 @@ public static TrafficController getInstance() {
* Broadcasts the current queue status to all connected clients.
* Each client will be notified of their specific position in the queue.
*/
public static void broadcastQueueStatus() {
public static void broadcastQueueStatus() throws DataAccessException {

List<QueueItem> usersWaitingInQueue = new ArrayList<>();
for (QueueItem item : DaoService.getQueueDao().getAll())
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/byu/cs/autograder/git/GitHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.byu.cs.autograder.GradingContext;
import edu.byu.cs.autograder.GradingException;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.model.Submission;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
Expand Down Expand Up @@ -70,7 +71,7 @@ private int verifyRegularCommits() throws GradingException {
// }

return numCommits;
} catch (IOException | GitAPIException e) {
} catch (IOException | GitAPIException | DataAccessException e) {
gradingContext.observer().notifyError("Failed to count commits: " + e.getMessage());
LOGGER.error("Failed to count commits", e);
throw new GradingException("Failed to count commits: ", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.byu.cs.autograder.GradingContext;
import edu.byu.cs.autograder.GradingException;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.model.Rubric;
import edu.byu.cs.model.RubricConfig;

Expand All @@ -16,7 +17,7 @@ public class QualityGrader {
*
* @return the results of the quality checks as a CanvasIntegration.RubricItem
*/
public Rubric.Results runQualityChecks() throws GradingException {
public Rubric.Results runQualityChecks() throws GradingException, DataAccessException {
RubricConfig rubricConfig = DaoService.getRubricConfigDao().getRubricConfig(gradingContext.phase());
if(rubricConfig.quality() == null) return null;
gradingContext.observer().update("Running code quality...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import edu.byu.cs.canvas.CanvasException;
import edu.byu.cs.canvas.CanvasService;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.model.Phase;
import edu.byu.cs.util.PhaseUtils;
import org.eclipse.jgit.annotations.NonNull;
Expand Down Expand Up @@ -39,7 +40,7 @@ public LateDayCalculator() {
initializePublicHolidays(getEncodedPublicHolidays());
}

public int calculateLateDays(Phase phase, String netId) throws GradingException {
public int calculateLateDays(Phase phase, String netId) throws GradingException, DataAccessException {
int assignmentNum = PhaseUtils.getPhaseAssignmentNumber(phase);

int canvasUserId = DaoService.getUserDao().getUser(netId).canvasUserId();
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/edu/byu/cs/autograder/score/Scorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.byu.cs.canvas.CanvasService;
import edu.byu.cs.canvas.CanvasUtils;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.dataAccess.SubmissionDao;
import edu.byu.cs.dataAccess.UserDao;
import edu.byu.cs.model.*;
Expand Down Expand Up @@ -34,7 +35,7 @@ public Scorer(GradingContext gradingContext) {
this.gradingContext = gradingContext;
}

public Submission score(Rubric rubric, int numCommits) throws GradingException {
public Submission score(Rubric rubric, int numCommits) throws GradingException, DataAccessException {
gradingContext.observer().update("Grading...");

rubric = CanvasUtils.decimalScoreToPoints(gradingContext.phase(), rubric);
Expand Down Expand Up @@ -137,7 +138,7 @@ private float totalPoints(CanvasIntegration.RubricAssessment assessment) {
return points;
}

private float calculateScoreWithLatePenalty(Rubric rubric, int numDaysLate) throws GradingException {
private float calculateScoreWithLatePenalty(Rubric rubric, int numDaysLate) throws GradingException, DataAccessException {
float score = getScore(rubric);
score *= 1 - (numDaysLate * PER_DAY_LATE_PENALTY);
if (score < 0) score = 0;
Expand All @@ -149,7 +150,7 @@ private float calculateScoreWithLatePenalty(Rubric rubric, int numDaysLate) thro
*
* @return the score
*/
private float getScore(Rubric rubric) throws GradingException {
private float getScore(Rubric rubric) throws GradingException, DataAccessException {
int totalPossiblePoints = DaoService.getRubricConfigDao().getPhaseTotalPossiblePoints(gradingContext.phase());

if (totalPossiblePoints == 0)
Expand All @@ -174,7 +175,7 @@ private float getScore(Rubric rubric) throws GradingException {
* @param rubric the rubric for the phase
*/
private Submission saveResults(Rubric rubric, int numCommits, int numDaysLate, float score, String notes)
throws GradingException {
throws GradingException, DataAccessException {
String headHash = getHeadHash();
String netId = gradingContext.netId();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/byu/cs/autograder/test/TestGrader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.byu.cs.autograder.GradingContext;
import edu.byu.cs.autograder.GradingException;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.model.Rubric;
import edu.byu.cs.model.RubricConfig;
import edu.byu.cs.util.PhaseUtils;
Expand Down Expand Up @@ -42,7 +43,7 @@ public TestGrader(GradingContext gradingContext) {
}


public Rubric.Results runTests() throws GradingException {
public Rubric.Results runTests() throws GradingException, DataAccessException {
compileTests();
gradingContext.observer().update("Running " + name() + " tests...");

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/byu/cs/canvas/CanvasUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.byu.cs.autograder.GradingException;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.model.Phase;
import edu.byu.cs.model.Rubric;
import edu.byu.cs.model.RubricConfig;
Expand All @@ -21,7 +22,7 @@ public class CanvasUtils {
* @param rubric the rubric to convert
* @return the rubric with the score converted to points
*/
public static Rubric decimalScoreToPoints(Phase phase, Rubric rubric) throws GradingException {
public static Rubric decimalScoreToPoints(Phase phase, Rubric rubric) throws GradingException, DataAccessException {
RubricConfig rubricConfig = DaoService.getRubricConfigDao().getRubricConfig(phase);

Rubric.RubricItem convertedPassoffTests = null;
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/edu/byu/cs/canvas/FakeCanvasIntegration.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package edu.byu.cs.canvas;

import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.model.User;

import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.HashSet;

public class FakeCanvasIntegration implements CanvasIntegration{
public class FakeCanvasIntegration implements CanvasIntegration {
@Override
public User getUser(String netId) {
User user = DaoService.getUserDao().getUser(netId);
public User getUser(String netId) throws CanvasException {
User user = null;
try {
user = DaoService.getUserDao().getUser(netId);
} catch (DataAccessException e) {
throw new CanvasException("Error getting user from database", e);
}
if(user == null) {
user = new User(netId, 0, "FirstName", "LastName", null, User.Role.ADMIN);
}
Expand Down
108 changes: 78 additions & 30 deletions src/main/java/edu/byu/cs/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import edu.byu.cs.analytics.CommitAnalyticsRouter;
import edu.byu.cs.canvas.CanvasService;
import edu.byu.cs.dataAccess.DaoService;
import edu.byu.cs.dataAccess.DataAccessException;
import edu.byu.cs.dataAccess.UserDao;
import edu.byu.cs.honorChecker.HonorCheckerCompiler;
import edu.byu.cs.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.Route;

Expand All @@ -19,10 +21,20 @@
import static spark.Spark.halt;

public class AdminController {

private static final Logger LOGGER = LoggerFactory.getLogger(AdminController.class);

public static final Route usersGet = (req, res) -> {
UserDao userDao = DaoService.getUserDao();

Collection<User> users = userDao.getUsers();
Collection<User> users = null;
try {
users = userDao.getUsers();
} catch (DataAccessException e) {
LOGGER.error("Error getting users", e);
halt(500);
return null;
}

res.type("application/json");
res.status(200);
Expand All @@ -35,32 +47,46 @@ public class AdminController {
String netId = req.params(":netId");

UserDao userDao = DaoService.getUserDao();
User user = userDao.getUser(netId);
User user;
try {
user = userDao.getUser(netId);
} catch (DataAccessException e) {
LOGGER.error("Error getting user", e);
halt(500);
return null;
}

if (user == null) {
halt(404, "user not found");
return null;
}

String firstName = req.queryParams("firstName");
if (firstName != null)
userDao.setFirstName(user.netId(), firstName);

String lastName = req.queryParams("lastName");
if (lastName != null)
userDao.setLastName(user.netId(), lastName);

String repoUrl = req.queryParams("repoUrl");
if (repoUrl != null)
userDao.setRepoUrl(user.netId(), repoUrl);

String role = req.queryParams("role");
if (role != null) {
try {
userDao.setRole(user.netId(), User.Role.valueOf(role.toUpperCase()));
} catch (IllegalArgumentException e) {
halt(400, "invalid role. must be one of: STUDENT, ADMIN");
return null;
try {
String firstName = req.queryParams("firstName");
if (firstName != null)
userDao.setFirstName(user.netId(), firstName);

String lastName = req.queryParams("lastName");
if (lastName != null)
userDao.setLastName(user.netId(), lastName);

String repoUrl = req.queryParams("repoUrl");
if (repoUrl != null)
userDao.setRepoUrl(user.netId(), repoUrl);

String role = req.queryParams("role");
if (role != null) {
try {
userDao.setRole(user.netId(), User.Role.valueOf(role.toUpperCase()));
} catch (IllegalArgumentException e) {
halt(400, "invalid role. must be one of: STUDENT, ADMIN");
return null;
}
}
} catch (DataAccessException e) {
LOGGER.error("Error updating user", e);
halt(500);
return null;
}

res.status(204);
Expand All @@ -72,17 +98,38 @@ public class AdminController {
User latestTestStudent = CanvasService.getCanvasIntegration().getTestStudent();

UserDao userDao = DaoService.getUserDao();
User user = userDao.getUser("test");
User user;
try {
user = userDao.getUser("test");
} catch (DataAccessException e) {
LOGGER.error("Error getting user", e);
halt(500);
return null;
}

if (user == null) {
user = latestTestStudent;
userDao.insertUser(latestTestStudent);
} else {
userDao.setRepoUrl(user.netId(), latestTestStudent.repoUrl());
userDao.setCanvasUserId(user.netId(), latestTestStudent.canvasUserId());
try {

if (user == null) {
user = latestTestStudent;
userDao.insertUser(latestTestStudent);
} else {
userDao.setRepoUrl(user.netId(), latestTestStudent.repoUrl());
userDao.setCanvasUserId(user.netId(), latestTestStudent.canvasUserId());
}

} catch (DataAccessException e) {
LOGGER.error("Error updating user", e);
halt(500);
return null;
}

DaoService.getSubmissionDao().removeSubmissionsByNetId(user.netId());
try {
DaoService.getSubmissionDao().removeSubmissionsByNetId(user.netId());
} catch (DataAccessException e) {
LOGGER.error("Error removing submissions", e);
halt(500);
return null;
}

res.cookie("/", "token", generateToken(user.netId()), 14400, false, false);

Expand All @@ -103,7 +150,7 @@ public class AdminController {
default -> throw new IllegalStateException("Not found (invalid option: " + option + ")");
};
} catch (Exception e) {
LoggerFactory.getLogger(AdminController.class).error(e.getMessage());
LOGGER.error(e.getMessage());
if (e instanceof IllegalStateException) res.status(404);
else res.status(500);
return e.getMessage();
Expand Down Expand Up @@ -139,6 +186,7 @@ public class AdminController {
return res.raw();
}
} catch (Exception e) {
LOGGER.error("Error compiling honor checker", e);
res.status(500);
return e.getMessage();
}
Expand Down
Loading

0 comments on commit c8b12d9

Please sign in to comment.