use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method deletePost.
/**
* <p>
* Deletes the specified post in persistence layer.
* </p>
*
* @param id
* the id of the post to delete
* @param user
* the id of the user who made this modification
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void deletePost(long id, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.DELETE_POST);
ps.setLong(1, user);
ps.setTimestamp(2, new Timestamp(new Date().getTime()));
ps.setLong(3, id);
if (ps.executeUpdate() == 0) {
throw new PersistenceException("no such post");
}
} finally {
Database.dispose(ps);
}
} catch (PersistenceException pe) {
throw pe;
} catch (SQLException e) {
throw new PersistenceException("Failed to delete post.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method createPost.
/**
* <p>
* Creates the specified post in persistence layer.
* </p>
*
* @param post
* the Post 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 createPost(Post post, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.INSERT_POST);
ps.setLong(1, post.getThreadId());
ps.setLong(2, post.getUserProfileId());
ps.setString(3, post.getContent());
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();
ps = conn.prepareStatement(ForumPersistenceImpl.GET_LAST_ID);
ResultSet rs = ps.executeQuery();
rs.next();
post.setId(rs.getLong(1));
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to create post.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method getPosts.
/**
* <p>
* Gets all posts in the specified thread from persistence layer.
* </p>
*
* @param threadId
* the id of the thread
* @param offset
* the offset of the start position to get the posts
* @param count
* the maximum number of posts in returned list
* @return a list of Post instances containing all posts in the specified thread
* @throws IllegalArgumentException
* if offset or count is negative
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public List<Post> getPosts(long threadId, int offset, int count) throws PersistenceException {
if (offset < 0) {
throw new IllegalArgumentException("offset should not be negative");
}
if (count < 0) {
throw new IllegalArgumentException("count should not be negative");
}
if (count == 0) {
return new ArrayList<Post>();
}
Connection conn = null;
ResultSet rs = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.GET_POSTS);
ps.setLong(1, threadId);
rs = ps.executeQuery();
List<Post> posts = new ArrayList<Post>();
int index = 0;
while (rs.next() && index - offset < count) {
++index;
if (index > offset) {
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));
posts.add(post);
}
}
return posts;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the posts", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method getThread.
/**
* <p>
* Get the thread with given id in persistence layer.
* </p>
*
* @param id
* the id of the thread
* @return the thread with given id in persistence layer
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public Thread getThread(long id) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.GET_THREAD);
ps.setLong(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Thread thread = new Thread();
thread.setId(rs.getLong(DatabaseConstants.THREAD_THREAD_ID));
thread.setForumId(rs.getLong(DatabaseConstants.THREAD_FORUM_ID));
thread.setUserProfileId(rs.getLong(DatabaseConstants.THREAD_USER_PROFILE_ID));
thread.setTitle(rs.getString(DatabaseConstants.THREAD_TITLE));
return thread;
} else {
return null;
}
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the thread with id " + id, e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method getAllForums.
/**
* <p>
* Get all forums in persistence layer.
* </p>
*
* @return a list of Forum instances containing all forums in persistence layer
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public List<Forum> getAllForums() throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.GET_ALL_FORUMS);
ResultSet rs = ps.executeQuery();
List<Forum> forums = new ArrayList<Forum>();
while (rs.next()) {
Forum forum = new Forum();
forum.setId(rs.getLong(DatabaseConstants.FORUM_FORUM_ID));
forum.setName(rs.getString(DatabaseConstants.FORUM_NAME));
forum.setDescription(rs.getString(DatabaseConstants.FORUM_DESCRIPTION));
forums.add(forum);
}
return forums;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get all forums", e);
} finally {
Database.dispose(conn);
}
}
Aggregations