Search in sources :

Example 16 with Forum

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

the class ForumPersistenceImpl method getForum.

/**
     * <p>
     * Get the forum with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the forum
     * @return the forum with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public Forum getForum(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            // TODO(xuchuan): move all prepareStatement out of try-catch
            ps = conn.prepareStatement(ForumPersistenceImpl.GET_FORUM);
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (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));
                return forum;
            } else {
                return null;
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the forum with id " + id, e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 17 with Forum

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

the class AuthorizationPersistenceImplTest method testGetForum.

/**
	 * Tests getForum method
	 * @throws Exception to JUnit
	 */
public void testGetForum() throws Exception {
    List forums = persistence.getAllForums();
    for (Iterator it = forums.iterator(); it.hasNext(); ) {
        Forum forum = (Forum) it.next();
        Forum forum1 = persistence.getForum(forum.getId());
        checkForum(forum, forum1);
    }
}
Also used : Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 18 with Forum

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

the class AuthorizationPersistenceImplTest method testCreateForum2.

/**
	 * Tests createForum method
	 * @throws Exception to JUnit
	 */
public void testCreateForum2() throws Exception {
    Forum forum1 = new Forum();
    forum1.setName("name1");
    forum1.setDescription("desc1");
    Forum forum2 = new Forum();
    forum2.setName("name2");
    forum2.setDescription("desc2");
    persistence.createForum(forum1, 1);
    persistence.createForum(forum2, 1);
    Forum forum11 = persistence.getForum(forum1.getId());
    checkForum(forum1, forum11);
    Forum forum22 = persistence.getForum(forum2.getId());
    checkForum(forum2, forum22);
}
Also used : Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Example 19 with Forum

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

the class AuthorizationPersistenceImplTest method newForum.

/**
	 * Creates a new forum.
	 * @param id the id
	 * @return a new forum instance
	 */
private Forum newForum(long id) {
    Forum forum = new Forum();
    forum.setId(id);
    forum.setName("forum" + id);
    forum.setDescription("forum" + id + " description");
    return forum;
}
Also used : Forum(cn.edu.zju.acm.onlinejudge.bean.Forum)

Aggregations

Forum (cn.edu.zju.acm.onlinejudge.bean.Forum)19 ArrayList (java.util.ArrayList)5 PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2