Search in sources :

Example 36 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class ReferencePersistenceImpl method getProblemReferences.

/**
     * <p>
     * Gets all problem references to the given problem with specified reference type.
     * </p>
     * 
     * @return a list containing all problem references to the given problem with specified reference type
     * @param problemId
     *            the id of the referred problem
     * @param referenceType
     *            the reference type of the returned references
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public List<Reference> getProblemReferences(long problemId, ReferenceType referenceType) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("SELECT r.reference_id, reference_type_id, name, content_type, " + "content, size, compressed " + "FROM problem_reference pr LEFT JOIN reference r ON pr.reference_id = r.reference_id " + "WHERE pr.problem_id = ? AND r.reference_type_id=?");
            ps.setLong(1, problemId);
            ps.setLong(2, referenceType.getId());
            ResultSet rs = ps.executeQuery();
            List<Reference> references = new ArrayList<Reference>();
            while (rs.next()) {
                Reference reference = this.populateReference(rs);
                references.add(reference);
            }
            return references;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the references", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement)

Example 37 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class ReferencePersistenceImpl method getProblemReferenceInfo.

/**
     * <p>
     * Gets all problem reference without data to the given problem with specified reference type.
     * </p>
     * 
     * @return a list containing all problem references to the given problem with specified reference type
     * @param problemId
     *            the id of the referred problem
     * @param referenceType
     *            the reference type of the returned references
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public List<Reference> getProblemReferenceInfo(long problemId, ReferenceType referenceType) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("SELECT r.reference_id, reference_type_id, name, content_type, size " + "FROM problem_reference pr LEFT JOIN reference r ON pr.reference_id = r.reference_id " + "WHERE pr.problem_id = ? AND r.reference_type_id=?");
            ps.setLong(1, problemId);
            ps.setLong(2, referenceType.getId());
            ResultSet rs = ps.executeQuery();
            List<Reference> references = new ArrayList<Reference>();
            while (rs.next()) {
                Reference reference = this.populateReferenceInfo(rs);
                references.add(reference);
            }
            return references;
        } finally {
            Database.dispose(ps);
        }
    } catch (Exception e) {
        throw new PersistenceException("Failed to get the reference info", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Example 38 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class ForumPersistenceImpl method updatePost.

/**
     * <p>
     * Updates the specified post in persistence layer.
     * </p>
     * 
     * @param post
     *            the Post instance to update
     * @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 updatePost(Post post, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ForumPersistenceImpl.UPDATE_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, post.getId());
            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 update post.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 39 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class ForumPersistenceImpl method updateForum.

/**
     * <p>
     * Updates the specified forum in persistence layer.
     * </p>
     * 
     * @param forum
     *            the Forum instance to update
     * @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 updateForum(Forum forum, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ForumPersistenceImpl.UPDATE_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, forum.getId());
            if (ps.executeUpdate() == 0) {
                throw new PersistenceException("no such forum");
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (PersistenceException e) {
        throw e;
    } catch (Exception e) {
        throw new PersistenceException("Failed to update forum.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Example 40 with PersistenceException

use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.

the class ForumPersistenceImpl method deleteThread.

/**
     * <p>
     * Deletes the specified thread in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the thread to delete
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void deleteThread(long id, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(ForumPersistenceImpl.DELETE_THREAD);
            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 thread");
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (PersistenceException pe) {
        throw pe;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to delete thread.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Aggregations

PersistenceException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceException)90 Connection (java.sql.Connection)87 PreparedStatement (java.sql.PreparedStatement)87 SQLException (java.sql.SQLException)87 ResultSet (java.sql.ResultSet)55 Timestamp (java.sql.Timestamp)29 Date (java.util.Date)28 ArrayList (java.util.ArrayList)25 Limit (cn.edu.zju.acm.onlinejudge.bean.Limit)7 Language (cn.edu.zju.acm.onlinejudge.bean.enumeration.Language)6 Problem (cn.edu.zju.acm.onlinejudge.bean.Problem)5 Forum (cn.edu.zju.acm.onlinejudge.bean.Forum)4 Submission (cn.edu.zju.acm.onlinejudge.bean.Submission)4 UserProfile (cn.edu.zju.acm.onlinejudge.bean.UserProfile)4 RoleSecurity (cn.edu.zju.acm.onlinejudge.security.RoleSecurity)4 HashMap (java.util.HashMap)4 PersistenceCreationException (cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException)3 AbstractContest (cn.edu.zju.acm.onlinejudge.bean.AbstractContest)2 Configuration (cn.edu.zju.acm.onlinejudge.bean.Configuration)2 Post (cn.edu.zju.acm.onlinejudge.bean.Post)2