use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class UserPersistenceImpl method getUserPreference.
/**
* <p>
* Gets the user preference with given id in persistence layer.
* </p>
*
* @param id
* the id of the user preference
* @return the user preference with given id in persistence layer
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public UserPreference getUserPreference(long id) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(UserPersistenceImpl.GET_USER_PREFERENCE);
ps.setLong(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
UserPreference preference = new UserPreference();
preference.setId(rs.getLong(DatabaseConstants.USER_PREFERENCE_USER_PROFILE_ID));
preference.setPlan(rs.getString(DatabaseConstants.USER_PREFERENCE_PLAN));
preference.setPostPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_POST_PAGING));
preference.setProblemPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_PROBLEM_PAGING));
preference.setStatusPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_STATUS_PAGING));
preference.setSubmissionPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_SUBMISSION_PAGING));
preference.setThreadPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_THREAD_PAGING));
preference.setUserPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_USER_PAGING));
return preference;
} else {
return null;
}
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the user preference with id " + id, e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class SubmissionPersistenceImpl method getProblemsetRankList.
public ProblemsetRankList getProblemsetRankList(long contestId, int offset, int count, String sort) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
String sql = null;
if (sort.equalsIgnoreCase("submit")) {
sql = "SELECT u.user_profile_id, u.handle, u.nickname, up.plan, ua.solved, ua.tiebreak " + "FROM user_ac ua " + "LEFT JOIN user_profile u ON ua.user_profile_id = u.user_profile_id " + "LEFT JOIN user_preference up ON ua.user_profile_id = up.user_profile_id " + "WHERE contest_id=? ORDER BY ua.tiebreak DESC, ua.solved DESC " + "LIMIT " + offset + "," + count;
} else {
sql = "SELECT u.user_profile_id, u.handle, u.nickname, up.plan, ua.solved, ua.tiebreak " + "FROM user_ac ua " + "LEFT JOIN user_profile u ON ua.user_profile_id = u.user_profile_id " + "LEFT JOIN user_preference up ON ua.user_profile_id = up.user_profile_id " + "WHERE contest_id=? ORDER BY ua.solved DESC, ua.tiebreak ASC " + "LIMIT " + offset + "," + count;
}
List<UserProfile> users = new ArrayList<UserProfile>();
List<Integer> solved = new ArrayList<Integer>();
List<Integer> total = new ArrayList<Integer>();
try {
ps = conn.prepareStatement(sql);
ps.setLong(1, contestId);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
UserProfile user = new UserProfile();
user.setId(rs.getLong(1));
user.setHandle(rs.getString(2));
user.setNickName(rs.getString(3));
user.setDeclaration(rs.getString(4));
users.add(user);
solved.add(rs.getInt(5));
total.add(rs.getInt(6));
}
} finally {
Database.dispose(ps);
}
int[] solvedArray = new int[solved.size()];
int[] totalArray = new int[solved.size()];
for (int i = 0; i < solvedArray.length; ++i) {
solvedArray[i] = solved.get(i);
totalArray[i] = total.get(i);
}
ProblemsetRankList r = new ProblemsetRankList(offset, count);
r.setUsers(users.toArray(new UserProfile[0]));
r.setSolved(solvedArray);
r.setTotal(totalArray);
return r;
} 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 getUserStatistics.
public UserStatistics getUserStatistics(long contestId, long userId) throws PersistenceException {
Connection conn = null;
try {
UserStatistics statistics = new UserStatistics(userId, contestId);
conn = Database.createConnection();
PreparedStatement ps = null;
/*String sql =
"SELECT DISTINCT p.problem_id, p.code, p.title, s.judge_comment " +
SubmissionPersistenceImpl.GET_SUBMISSION_FROM_PART +
" AND s.user_profile_id=? AND s.judge_reply_id=? AND s.contest_id=?";*/
String sql = "SELECT p.problem_id, p.code, p.title, s.judge_comment " + SubmissionPersistenceImpl.GET_SUBMISSION_FROM_PART + " AND s.user_profile_id=? AND s.judge_reply_id=? AND s.contest_id=?";
sql = sql.replace("FORCE_INDEX", "USE INDEX (index_submission_user_reply_contest)");
HashSet<Long> solvedid = new HashSet<Long>();
HashSet<Long> confirmedid = new HashSet<Long>();
List<Problem> solved = new ArrayList<Problem>();
List<Problem> confirmed = new ArrayList<Problem>();
try {
ps = conn.prepareStatement(sql);
ps.setLong(1, userId);
ps.setLong(2, JudgeReply.ACCEPTED.getId());
ps.setLong(3, contestId);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Long probemid = new Long(rs.getLong("problem_id"));
if (!solvedid.contains(probemid)) {
Problem p = new Problem();
p.setContestId(contestId);
p.setId(rs.getLong("problem_id"));
p.setCode(rs.getString("code"));
p.setTitle(rs.getString("title"));
solved.add(p);
solvedid.add(probemid);
}
String comment = rs.getString("judge_comment");
if (!confirmed.contains(probemid) && "Yes".equals(comment)) {
Problem p = new Problem();
p.setContestId(contestId);
p.setId(rs.getLong("problem_id"));
p.setCode(rs.getString("code"));
p.setTitle(rs.getString("title"));
confirmed.add(p);
confirmedid.add(probemid);
}
}
} finally {
Database.dispose(ps);
}
statistics.setSolved(new TreeSet<Problem>(solved));
statistics.setConfirmed(new TreeSet<Problem>(confirmed));
try {
ps = conn.prepareStatement("SELECT judge_reply_id, count(*) FROM submission WHERE contest_id=? AND user_profile_id=? GROUP BY judge_reply_id");
ps.setLong(1, contestId);
ps.setLong(2, userId);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
long jid = rs.getLong(1);
int count = rs.getInt(2);
statistics.setCount(jid, count);
}
return statistics;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the user statistics", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class SubmissionPersistenceImpl method getRankListEntry.
public RankListEntry getRankListEntry(long contestId, long userId) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement("SELECT ac_number, submission_number FROM user_stat " + "WHERE contest_id=? AND user_id=?");
ps.setLong(1, contestId);
ps.setLong(2, userId);
ResultSet rs = ps.executeQuery();
RankListEntry re = null;
if (rs.next()) {
re = new RankListEntry(1);
re.setSolved(rs.getLong(1));
re.setSubmitted(rs.getLong(2));
}
return re;
} finally {
Database.dispose(ps);
}
} 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 getSubmission.
/**
* <p>
* Gets the submission with given id in persistence layer.
* </p>
*
* @param id
* the id of the submission
* @return the submission with given id in persistence layer
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public Submission getSubmission(long id) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(SubmissionPersistenceImpl.GET_SUBMISSION.replace("FORCE_INDEX", ""));
ps.setLong(1, id);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
return null;
}
Map<Long, Language> languageMap = PersistenceManager.getInstance().getLanguagePersistence().getLanguageMap();
Submission submission = this.populateSubmission(rs, true, languageMap);
return submission;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the submission with id " + id, e);
} finally {
Database.dispose(conn);
}
}
Aggregations