use of cn.edu.zju.acm.onlinejudge.util.RankListEntry 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.util.RankListEntry in project zoj by licheng.
the class ShowRankListAction method exportToExcel.
private byte[] exportToExcel(AbstractContest contest, List<Problem> problems, RankList ranklist) throws Exception {
List<RankListEntry> entries = ranklist.getEntries();
long time = this.getTimeEscaped(contest);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue(contest.getTitle());
if (ranklist.getRole() != null) {
row = sheet.createRow(1);
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(ranklist.getRole().getDescription());
}
row = sheet.createRow(2);
cell = row.createCell((short) 0);
cell.setCellValue("Length");
cell = row.createCell((short) 1);
cell.setCellValue(Utility.toTime(contest.getLength() / 1000));
row = sheet.createRow(3);
cell = row.createCell((short) 0);
cell.setCellValue("Time Escaped");
cell = row.createCell((short) 1);
cell.setCellValue(Utility.toTime(time / 1000));
row = sheet.createRow(5);
row.createCell((short) 0).setCellValue("Rank");
row.createCell((short) 1).setCellValue("Handle");
row.createCell((short) 2).setCellValue("Nickname");
row.createCell((short) 3).setCellValue("Solved");
short columnIndex = 4;
for (Problem problem2 : problems) {
Problem problem = problem2;
row.createCell(columnIndex).setCellValue(problem.getCode());
columnIndex++;
}
row.createCell(columnIndex).setCellValue("Penalty");
int rowIndex = 6;
for (RankListEntry rankListEntry : entries) {
RankListEntry entry = rankListEntry;
row = sheet.createRow(rowIndex);
row.createCell((short) 0).setCellValue(rowIndex - 5);
row.createCell((short) 1).setCellValue(entry.getUserProfile().getHandle());
String nick = entry.getUserProfile().getHandle();
if (entry.getUserProfile().getNickName() != null) {
nick = entry.getUserProfile().getNickName();
}
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(nick);
row.createCell((short) 3).setCellValue(entry.getSolved());
for (short i = 0; i < problems.size(); ++i) {
String score = entry.getAcceptTime(i) > 0 ? entry.getAcceptTime(i) + "(" + entry.getSubmitNumber(i) + ")" : "" + entry.getSubmitNumber(i);
row.createCell((short) (4 + i)).setCellValue(score);
}
row.createCell((short) (4 + problems.size())).setCellValue(entry.getPenalty());
rowIndex++;
}
// output to stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
wb.write(out);
return out.toByteArray();
} finally {
out.close();
}
}
use of cn.edu.zju.acm.onlinejudge.util.RankListEntry in project zoj by licheng.
the class ShowRankListAction method exportToText.
private byte[] exportToText(AbstractContest contest, List<Problem> problems, RankList ranklist, boolean windows) throws Exception {
List<RankListEntry> entries = ranklist.getEntries();
String lineHolder = windows ? "\r\n" : "\n";
long time = this.getTimeEscaped(contest);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(contest.getTitle());
writer.write(lineHolder);
writer.write(ranklist.getRole() == null ? "" : ranklist.getRole().getDescription());
writer.write(lineHolder);
writer.write("Length: " + Utility.toTime(contest.getLength() / 1000));
writer.write(lineHolder);
writer.write("Time Escaped: " + Utility.toTime(time / 1000));
writer.write(lineHolder);
writer.write(lineHolder);
writer.write("Rank\tHandle\tNickname\tSolved\t");
for (Problem problem2 : problems) {
Problem problem = problem2;
writer.write(problem.getCode());
writer.write("\t");
}
writer.write("Penalty");
writer.write(lineHolder);
int index = 0;
for (RankListEntry rankListEntry : entries) {
index++;
RankListEntry entry = rankListEntry;
writer.write(index + "\t");
writer.write(entry.getUserProfile().getHandle() + "\t");
writer.write((entry.getUserProfile().getNickName() == null ? entry.getUserProfile().getHandle() : entry.getUserProfile().getNickName()) + "\t");
writer.write(entry.getSolved() + "\t");
for (int i = 0; i < problems.size(); ++i) {
String score = entry.getAcceptTime(i) > 0 ? entry.getAcceptTime(i) + "(" + entry.getSubmitNumber(i) + ")" : "" + entry.getSubmitNumber(i);
writer.write(score + "\t");
}
writer.write(entry.getPenalty() + lineHolder);
}
writer.close();
return out.toByteArray();
}
use of cn.edu.zju.acm.onlinejudge.util.RankListEntry 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);
}
}
Aggregations