use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method createThread.
/**
* <p>
* Creates the specified thread in persistence layer.
* </p>
*
* @param thread
* the Thread instance to create
* @param user
* the id of the user who made this modification
* @throws NullPointerException
* if argument is null
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void createThread(Thread thread, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.INSERT_THREAD);
ps.setLong(1, thread.getForumId());
ps.setLong(2, thread.getUserProfileId());
ps.setString(3, thread.getTitle());
ps.setLong(4, user);
ps.setTimestamp(5, new Timestamp(new Date().getTime()));
ps.setLong(6, user);
ps.setTimestamp(7, new Timestamp(new Date().getTime()));
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
try {
ps = conn.prepareStatement(ForumPersistenceImpl.GET_LAST_ID);
ResultSet rs = ps.executeQuery();
rs.next();
thread.setId(rs.getLong(1));
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to create thread.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method createForum.
/**
* <p>
* Creates the specified forum in persistence layer.
* </p>
*
* @param forum
* the Forum instance to create
* @param user
* the id of the user who made this modification
* @throws NullPointerException
* if argument is null
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void createForum(Forum forum, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.INSERT_FORUM);
ps.setString(1, forum.getName());
ps.setString(2, forum.getDescription());
ps.setLong(3, user);
ps.setTimestamp(4, new Timestamp(new Date().getTime()));
ps.setLong(5, user);
ps.setTimestamp(6, new Timestamp(new Date().getTime()));
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
try {
// TODO(xuchuan): check all get last id. They should be put in a transaction. Or think about using a id
// generator.
ps = conn.prepareStatement(ForumPersistenceImpl.GET_LAST_ID);
ResultSet rs = ps.executeQuery();
rs.next();
forum.setId(rs.getLong(1));
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to create forum.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method getPost.
/**
* <p>
* Gets the post with given id in persistence layer.
* </p>
*
* @param id
* the id of the post
* @return the post with given id in persistence layer
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public Post getPost(long id) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.GET_POST);
ps.setLong(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Post post = new Post();
post.setId(rs.getLong(DatabaseConstants.POST_POST_ID));
post.setThreadId(rs.getLong(DatabaseConstants.POST_THREAD_ID));
post.setUserProfileId(rs.getLong(DatabaseConstants.POST_USER_PROFILE_ID));
post.setContent(rs.getString(DatabaseConstants.POST_CONTENT));
return post;
} else {
return null;
}
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the post with id " + id, e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ProblemPersistenceImpl method getProblemsCount.
public int getProblemsCount(long contestId) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement("select count(*) from problem where contest_id=? and active=1");
ps.setLong(1, contestId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
return rs.getInt(1);
} else {
return 0;
}
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to search the problems count", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class SubmissionPersistenceImpl method getSubmissionSource.
public String getSubmissionSource(long id) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement("SELECT content FROM submission WHERE submission_id=?");
ps.setLong(1, id);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
throw new PersistenceException("Submission id " + id + " not found");
}
String content = rs.getString("content");
if (content == null) {
return "";
} else {
return content;
}
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the submission with id " + id, e);
} finally {
Database.dispose(conn);
}
}
Aggregations