Search in sources :

Example 1 with Post

use of cn.edu.zju.acm.onlinejudge.bean.Post 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);
    }
}
Also used : SQLException(java.sql.SQLException) Post(cn.edu.zju.acm.onlinejudge.bean.Post) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement)

Example 2 with Post

use of cn.edu.zju.acm.onlinejudge.bean.Post in project zoj by licheng.

the class AuthorizationPersistenceImplTest method checkPostList.

/**
	 * Checks whether the two lists are same.
	 * @param posts1 the expected profile
	 * @param posts2 the profile to check
	 * @param offset1 the offset in post list 1.
	 * @param offset2 the offset in post list 2.
	 * @param count the count
	 */
private void checkPostList(List posts1, int offset1, List posts2, int offset2, int count) {
    for (int i = 0; i < count; ++i) {
        Post post1 = (Post) posts1.get(offset1 + i);
        Post post2 = (Post) posts2.get(offset2 + i);
        checkPost(post1, post2);
    }
}
Also used : Post(cn.edu.zju.acm.onlinejudge.bean.Post)

Example 3 with Post

use of cn.edu.zju.acm.onlinejudge.bean.Post in project zoj by licheng.

the class AuthorizationPersistenceImplTest method testGetPost.

/**
	 * Tests getPost method
	 * @throws Exception to JUnit
	 */
public void testGetPost() throws Exception {
    Post post = persistence.getPost(post1.getId());
    checkPost(post1, post);
}
Also used : Post(cn.edu.zju.acm.onlinejudge.bean.Post)

Example 4 with Post

use of cn.edu.zju.acm.onlinejudge.bean.Post in project zoj by licheng.

the class AuthorizationPersistenceImplTest method testUpdatePost1.

/**
	 * Tests updatePost method
	 * @throws Exception to JUnit
	 */
public void testUpdatePost1() throws Exception {
    post1.setId(post2.getId());
    persistence.updatePost(post1, 1);
    Post post = persistence.getPost(post1.getId());
    checkPost(post1, post);
}
Also used : Post(cn.edu.zju.acm.onlinejudge.bean.Post)

Example 5 with Post

use of cn.edu.zju.acm.onlinejudge.bean.Post in project zoj by licheng.

the class AuthorizationPersistenceImplTest method newPost.

/**
	 * Creates a new post.
	 * @param id the id
	 * @param threadId the thread id
	 * @param userId the user id
	 * @return a new post instance
	 */
private Post newPost(long id, long threadId, long userId) {
    Post post = new Post();
    post.setId(id);
    post.setThreadId(threadId);
    post.setUserProfileId(userId);
    post.setContent("post content" + id);
    return post;
}
Also used : Post(cn.edu.zju.acm.onlinejudge.bean.Post)

Aggregations

Post (cn.edu.zju.acm.onlinejudge.bean.Post)14 ArrayList (java.util.ArrayList)3 Country (cn.edu.zju.acm.onlinejudge.bean.enumeration.Country)2 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2