use of cn.edu.zju.acm.onlinejudge.bean.Problemset in project zoj by licheng.
the class ContestPersistenceImpl method createContest.
/**
* <p>
* Creates the specified contest in persistence layer.
* </p>
*
* @param contest
* the AbstractContest instance to create
* @param user
* the id of the user who made this modification
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void createContest(AbstractContest contest, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
Limit limit = contest.getLimit();
try {
// create a new limit
if (limit != null && limit.getId() != ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_LIMIT);
ps.setInt(1, limit.getTimeLimit());
ps.setInt(2, limit.getMemoryLimit());
ps.setInt(3, limit.getOutputLimit());
ps.setInt(4, limit.getSubmissionLimit());
ps.executeUpdate();
limit.setId(Database.getLastId(conn));
}
} finally {
Database.dispose(ps);
}
try {
// create the contest
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST);
ps.setString(1, contest.getTitle());
ps.setString(2, contest.getDescription());
if (contest.getStartTime() != null) {
ps.setTimestamp(3, new Timestamp(contest.getStartTime().getTime()));
} else {
ps.setTimestamp(3, null);
}
if (contest.getEndTime() != null) {
ps.setTimestamp(4, new Timestamp(contest.getEndTime().getTime()));
} else {
ps.setTimestamp(4, null);
}
ps.setLong(5, contest.getForumId());
if (limit == null || limit.getId() == ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
ps.setLong(6, ContestPersistenceImpl.DEFAULT_LIMIT_ID);
} else {
ps.setLong(6, limit.getId());
}
int contesttype = 0;
if (contest instanceof Problemset) {
contesttype = 1;
}
if (contest instanceof Course) {
contesttype = 2;
}
ps.setInt(7, contesttype);
ps.setLong(8, user);
ps.setTimestamp(9, new Timestamp(new Date().getTime()));
ps.setLong(10, user);
ps.setTimestamp(11, new Timestamp(new Date().getTime()));
ps.setBoolean(12, contest.isCheckIp());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
contest.setId(Database.getLastId(conn));
// create languages
if (contest.getLanguages() != null) {
for (Language language : contest.getLanguages()) {
try {
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST_LANGUAGE);
ps.setLong(1, contest.getId());
ps.setLong(2, language.getId());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
}
}
conn.commit();
} catch (Exception e) {
Database.rollback(conn);
throw new PersistenceException("Failed to create contest.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.bean.Problemset in project zoj by licheng.
the class ContestPersistenceImpl method updateContest.
/**
* <p>
* Updates the specified contest in persistence layer.
* </p>
*
* @param contest
* the AbstractContest instance to update
* @param user
* the id of the user who made this modification
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void updateContest(AbstractContest contest, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
long contestLimitId = ContestPersistenceImpl.DEFAULT_LIMIT_ID;
try {
ps = conn.prepareStatement(ContestPersistenceImpl.GET_CONTEST_LIMIT_ID);
ps.setLong(1, contest.getId());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
contestLimitId = rs.getLong(1);
}
} finally {
Database.dispose(ps);
}
// update the limit
Limit limit = contest.getLimit();
if (limit.getId() != ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
try {
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_LIMIT);
ps.setInt(1, limit.getTimeLimit());
ps.setInt(2, limit.getMemoryLimit());
ps.setInt(3, limit.getOutputLimit());
ps.setInt(4, limit.getSubmissionLimit());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
limit.setId(Database.getLastId(conn));
}
if (contestLimitId != limit.getId()) {
// TODO(xuchuan) I don't understand what's that.
try {
ps = conn.prepareStatement(ContestPersistenceImpl.UPDATE_PROBLEM_LIMIT);
ps.setLong(1, limit.getId());
ps.setLong(2, contest.getId());
ps.setLong(3, contestLimitId);
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
}
try {
// update the contest
ps = conn.prepareStatement(ContestPersistenceImpl.UPDATE_CONTEST);
ps.setString(1, contest.getTitle());
ps.setString(2, contest.getDescription());
if (contest.getStartTime() != null) {
ps.setTimestamp(3, new Timestamp(contest.getStartTime().getTime()));
} else {
ps.setTimestamp(3, null);
}
if (contest.getEndTime() != null) {
ps.setTimestamp(4, new Timestamp(contest.getEndTime().getTime()));
} else {
ps.setTimestamp(4, null);
}
ps.setLong(5, contest.getForumId());
if (limit == null || limit.getId() == ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
ps.setLong(6, ContestPersistenceImpl.DEFAULT_LIMIT_ID);
} else {
ps.setLong(6, limit.getId());
}
ps.setBoolean(7, contest instanceof Problemset);
ps.setLong(8, user);
ps.setTimestamp(9, new Timestamp(new Date().getTime()));
ps.setBoolean(10, contest.isCheckIp());
ps.setLong(11, contest.getId());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
try {
// delete languages
ps = conn.prepareStatement(ContestPersistenceImpl.DELETE_CONTEST_LANGUAGE);
ps.setLong(1, contest.getId());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
// insert languages
if (contest.getLanguages() != null) {
for (Language language : contest.getLanguages()) {
try {
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST_LANGUAGE);
ps.setLong(1, contest.getId());
ps.setLong(2, language.getId());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
}
}
conn.commit();
} catch (Exception e) {
Database.rollback(conn);
throw new PersistenceException("Failed to create contest.", e);
} finally {
Database.dispose(conn);
}
}
Aggregations