Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
Limit limit;
try {
long contestLimitId = ProblemPersistenceImpl.DEFAULT_LIMIT_ID;
ps = conn.prepareStatement(ProblemPersistenceImpl.GET_CONTEST_LIMIT_ID);
ps.setLong(1, problem.getContestId());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
contestLimitId = rs.getLong(1);
}
limit = problem.getLimit();
if (limit == null) {
limit = new Limit();
limit.setId(contestLimitId);
problem.setLimit(limit);
}
if (limit.getId() != contestLimitId) {
ps = conn.prepareStatement(ProblemPersistenceImpl.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 problem
ps = conn.prepareStatement(ProblemPersistenceImpl.INSERT_PROBLEM);
ps.setLong(1, problem.getContestId());
ps.setString(2, problem.getTitle());
ps.setString(3, problem.getCode());
ps.setLong(4, limit.getId());
ps.setString(5, problem.getAuthor());
ps.setString(6, problem.getSource());
ps.setString(7, problem.getContest());
ps.setBoolean(8, problem.isChecker());
ps.setInt(9, 0);