use of cn.edu.zju.acm.onlinejudge.bean.Problem 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.bean.Problem 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.bean.Problem in project zoj by licheng.
the class ProblemPersistenceImpl method getProblem.
/**
* <p>
* Gets the problem with given id in persistence layer.
* </p>
*
* @param id
* the id of the problem
* @return the problem with given id in persistence layer
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public Problem getProblem(long id) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ProblemPersistenceImpl.GET_PROBLEM);
ps.setLong(1, id);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
return null;
}
Problem problem = this.populateProblem(rs);
long limitId = rs.getLong(DatabaseConstants.PROBLEM_LIMITS_ID);
ps = conn.prepareStatement(ProblemPersistenceImpl.GET_LIMIT);
ps.setLong(1, limitId);
rs = ps.executeQuery();
if (rs.next()) {
Limit limit = this.populateLimit(rs);
problem.setLimit(limit);
}
return problem;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the contest with id " + id, e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Problem in project zoj by licheng.
the class ProblemPersistenceImpl method searchProblems.
/**
* <p>
* Searches all problems according with the given criteria in persistence layer.
* </p>
*
* @return a list of problems according with the given criteria
* @param criteria
* the problem search criteria
* @param offset
* the offset of the start position to search
* @param count
* the maximum number of problems in returned list
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public List<Problem> searchProblems(ProblemCriteria criteria, int offset, int count) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(this.buildSearchQuery(criteria, offset, count));
ResultSet rs = ps.executeQuery();
List<Problem> problems = new ArrayList<Problem>();
while (rs.next()) {
Problem problem = this.populateProblem(rs);
Limit limit = this.populateLimit(rs);
problem.setLimit(limit);
problems.add(problem);
}
return problems;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to search the problems", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Problem in project zoj by licheng.
the class IntegrationTest method init.
@BeforeClass
public static void init() throws Exception {
ReflectionUtil.setFieldValue(DAOFactory.class, "languageDAO", new MockLanguageDAO());
ReflectionUtil.setFieldValue(DAOFactory.class, "problemDAO", new MockProblemDAO());
ReflectionUtil.setFieldValue(DAOFactory.class, "submissionDAO", new MockSubmissionDAO());
ReflectionUtil.setFieldValue(DAOFactory.class, "referenceDAO", new MockReferenceDAO());
Problem problem = new Problem();
problem.setId(0);
problem.setRevision(0);
Limit limit = new Limit();
limit.setTimeLimit(1);
limit.setMemoryLimit(1024);
limit.setOutputLimit(1);
problem.setLimit(limit);
Reference reference = new Reference();
reference.setReferenceType(ReferenceType.INPUT);
reference.setContent("0 0\n1 2\n2 3\n".getBytes("ASCII"));
DAOFactory.getReferenceDAO().save(reference, 0);
DAOFactory.getReferenceDAO().save(reference, 1);
reference = new Reference();
reference.setReferenceType(ReferenceType.OUTPUT);
reference.setContent("0\n3\n5\n".getBytes("ASCII"));
DAOFactory.getReferenceDAO().save(reference, 0);
DAOFactory.getProblemDAO().update(problem);
}
Aggregations