use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class SubmissionPersistenceImpl method getContestStatistics.
public ContestStatistics getContestStatistics(List<Problem> problems) throws PersistenceException {
Connection conn = null;
ContestStatistics statistics = new ContestStatistics(problems);
if (problems.size() == 0) {
return statistics;
}
try {
conn = Database.createConnection();
List<Long> problemIds = new ArrayList<Long>();
for (Problem problem : problems) {
problemIds.add(new Long(((Problem) problem).getId()));
}
String inProblemIds = Database.createNumberValues(problemIds);
String query = "SELECT problem_id, judge_reply_id, count(*) FROM submission " + "WHERE problem_id IN " + inProblemIds + " GROUP BY problem_id, judge_reply_id";
/*
* String query = "SELECT problem_id, judge_reply_id, count FROM problem_statistics " +
* "WHERE problem_id IN " + inProblemIds;
*/
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
long problemId = rs.getLong(1);
long judgeReplyId = rs.getLong(2);
int value = rs.getInt(3);
statistics.setCount(problemId, judgeReplyId, value);
}
return statistics;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the statistics", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class SubmissionPersistenceImpl method searchQQs.
public List<QQ> searchQQs(long contestId) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement("SELECT s.submission_id, s.submission_date, " + "u.user_profile_id, u.handle, u.nickname, " + "p.problem_id, p.code, p.color, ss.status " + "FROM submission s " + "LEFT JOIN user_profile u ON s.user_profile_id=u.user_profile_id " + "LEFT JOIN problem p ON s.problem_id=p.problem_id " + "LEFT JOIN submission_status ss ON u.user_profile_id=ss.user_profile_id AND p.problem_id=ss.problem_id " + "WHERE p.contest_id=? AND s.judge_reply_id=? AND p.active=1 AND (ss.status IS NULL OR ss.status<>?) " + "ORDER BY s.submission_date");
ps.setLong(1, contestId);
ps.setLong(2, JudgeReply.ACCEPTED.getId());
ps.setString(3, QQ.QQ_FINISHED);
ResultSet rs = ps.executeQuery();
List<QQ> qqs = new ArrayList<QQ>();
while (rs.next()) {
QQ qq = new QQ();
qq.setCode(rs.getString("code"));
qq.setColor(rs.getString("color"));
qq.setNickName(rs.getString("nickname"));
qq.setHandle(rs.getString("handle"));
qq.setProblemId(rs.getLong("problem_id"));
qq.setUserProfileId(rs.getLong("user_profile_id"));
qq.setSubmissionId(rs.getLong("submission_id"));
qq.setSubmissionDate(Database.getDate(rs, "submission_date"));
qq.setStatus(rs.getString("status"));
if (qq.getStatus() == null) {
qq.setStatus(QQ.QQ_NEW);
}
qqs.add(qq);
}
return qqs;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the QQs", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class SubmissionPersistenceImpl method createSubmission.
/**
* <p>
* Creates the specified submission in persistence layer.
* </p>
*
* @param submission
* the Submission instance to create
* @param user
* the id of the user who made this modification
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void createSubmission(Submission submission, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
String maxOrder = null;
try {
ps = conn.prepareStatement("select max(contest_order) from submission where contest_id=" + submission.getContestId());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
maxOrder = rs.getString(1);
}
} finally {
Database.dispose(ps);
}
long count = maxOrder == null ? 0 : Long.parseLong(maxOrder) + 1;
submission.setContestOrder(count);
try {
// create the submission
ps = conn.prepareStatement(SubmissionPersistenceImpl.INSERT_SUBMISSION);
ps.setLong(1, submission.getProblemId());
ps.setLong(2, submission.getLanguage().getId());
ps.setLong(3, submission.getJudgeReply().getId());
ps.setLong(4, submission.getUserProfileId());
ps.setString(5, submission.getContent());
ps.setString(10, submission.getJudgeComment());
ps.setInt(6, submission.getTimeConsumption());
ps.setInt(7, submission.getMemoryConsumption());
ps.setTimestamp(8, Database.toTimestamp(submission.getSubmitDate()));
ps.setTimestamp(9, Database.toTimestamp(submission.getJudgeDate()));
ps.setLong(11, user);
ps.setTimestamp(12, new Timestamp(new Date().getTime()));
ps.setLong(13, user);
ps.setTimestamp(14, new Timestamp(new Date().getTime()));
ps.setLong(15, submission.getContestId());
ps.setLong(16, submission.getContestOrder());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
submission.setId(Database.getLastId(conn));
conn.commit();
} catch (Exception e) {
Database.rollback(conn);
throw new PersistenceException("Failed to insert submission.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class SubmissionPersistenceImpl method getRankList.
public List<RankListEntry> getRankList(List<Problem> problems, long contestStartDate, long roleId) throws PersistenceException {
Connection conn = null;
if (problems.size() == 0) {
return new ArrayList<RankListEntry>();
}
try {
conn = Database.createConnection();
PreparedStatement ps = null;
List<Long> problemIds = new ArrayList<Long>();
Map<Long, Integer> problemIndexes = new HashMap<Long, Integer>();
int index = 0;
for (Problem problem2 : problems) {
Problem problem = (Problem) problem2;
problemIds.add(new Long(problem.getId()));
problemIndexes.put(new Long(problem.getId()), new Integer(index));
index++;
}
String userIdsCon = "";
if (roleId >= 0) {
// TODO performance issue!!
List<Long> ids = new ArrayList<Long>();
try {
ps = conn.prepareStatement("SELECT user_profile_id FROM user_role WHERE role_id=?");
ps.setLong(1, roleId);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
ids.add(rs.getLong(1));
}
if (ids.size() == 0) {
return new ArrayList<RankListEntry>();
}
} finally {
Database.dispose(ps);
}
userIdsCon = " AND user_profile_id IN " + Database.createNumberValues(ids);
}
String inProblemIds = Database.createNumberValues(problemIds);
Map<Long, RankListEntry> entries = new HashMap<Long, RankListEntry>();
try {
ps = conn.prepareStatement("SELECT user_profile_id, problem_id, judge_reply_id, submission_date FROM submission " + "WHERE problem_id IN " + inProblemIds + userIdsCon + " ORDER BY submission_date");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
long userId = rs.getLong(1);
RankListEntry entry = (RankListEntry) entries.get(new Long(userId));
if (entry == null) {
entry = new RankListEntry(problems.size());
entries.put(new Long(userId), entry);
UserProfile profile = new UserProfile();
profile.setId(userId);
entry.setUserProfile(profile);
}
long problemId = rs.getLong(2);
long judgeReplyId = rs.getLong(3);
int time = (int) ((rs.getTimestamp(4).getTime() - contestStartDate) / 1000 / 60);
entry.update(((Integer) problemIndexes.get(new Long(problemId))).intValue(), time, judgeReplyId == JudgeReply.ACCEPTED.getId());
}
} finally {
Database.dispose(ps);
}
List<RankListEntry> entryList = new ArrayList<RankListEntry>(entries.values());
Collections.sort(entryList);
return entryList;
} catch (SQLException e) {
throw new PersistenceException("Failed to get the rank list", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class SubmissionPersistenceImpl method searchSubmissions.
/**
* <p>
* Searches all submissions according with the given criteria in persistence layer.
* </p>
*
* @return a list of submissions according with the given criteria
* @param criteria
* the submission search criteria
* @param lastId
* the last id
* @param count
* the maximum number of submissions in returned list
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public List<Submission> searchSubmissions(SubmissionCriteria criteria, long firstId, long lastId, int count, boolean withContent) throws PersistenceException {
if (lastId < 0) {
throw new IllegalArgumentException("offset is negative");
}
if (count < 0) {
throw new IllegalArgumentException("count is negative");
}
Connection conn = null;
Map<Long, Language> languageMap = PersistenceManager.getInstance().getLanguagePersistence().getLanguageMap();
try {
conn = Database.createConnection();
PreparedStatement ps = null;
if (criteria.getUserId() == null && criteria.getHandle() != null) {
try {
ps = conn.prepareStatement("select user_profile_id from user_profile where handle=? AND active=1");
ps.setString(1, criteria.getHandle());
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
return new ArrayList<Submission>();
}
long userId = rs.getLong(1);
criteria.setUserId(userId);
} finally {
Database.dispose(ps);
}
}
if (criteria.getProblemId() == null && criteria.getProblemCode() != null) {
try {
ps = conn.prepareStatement("select problem_id from problem where code=? AND contest_id=? AND active=1");
ps.setString(1, criteria.getProblemCode());
ps.setLong(2, criteria.getContestId());
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
return new ArrayList<Submission>();
}
long problemId = rs.getLong(1);
criteria.setProblemId(problemId);
} finally {
Database.dispose(ps);
}
}
try {
ps = this.buildQuery(withContent ? SubmissionPersistenceImpl.GET_SUBMISSIONS_WITH_CONTENT : SubmissionPersistenceImpl.GET_SUBMISSIONS, criteria, firstId, lastId, count, conn);
if (ps == null) {
return new ArrayList<Submission>();
}
ResultSet rs = ps.executeQuery();
List<Submission> submissions = new ArrayList<Submission>();
while (rs.next()) {
Submission submission = this.populateSubmission(rs, withContent, languageMap);
submissions.add(submission);
}
return submissions;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the submissions", e);
} finally {
Database.dispose(conn);
}
}
Aggregations