Search in sources :

Example 31 with PersistenceException

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

the class ProblemPersistenceImpl method updateProblem.

/**
     * <p>
     * Updates the specified problem in persistence layer.
     * </p>
     * 
     * @param problem
     *            the Problem instance to update
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void updateProblem(Problem problem, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        PreparedStatement ps = null;
        long contestLimitId = ProblemPersistenceImpl.DEFAULT_LIMIT_ID;
        try {
            ps = conn.prepareStatement(ProblemPersistenceImpl.GET_CONTEST_LIMIT_ID);
            ps.setLong(1, problem.getContestId());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                contestLimitId = rs.getLong(1);
            }
        } finally {
            Database.dispose(ps);
        }
        // update a new limit
        Limit limit = problem.getLimit();
        if (limit.getId() != contestLimitId) {
            try {
                ps = conn.prepareStatement(ProblemPersistenceImpl.INSERT_LIMIT);
                ps.setInt(1, limit.getTimeLimit());
                ps.setInt(2, limit.getMemoryLimit());
                ps.setInt(3, limit.getOutputLimit());
                ps.setInt(4, limit.getSubmissionLimit());
                ps.executeUpdate();
                limit.setId(Database.getLastId(conn));
            } finally {
                Database.dispose(ps);
            }
        }
        try {
            // update the problem
            ps = conn.prepareStatement(ProblemPersistenceImpl.UPDATE_PROBLEM);
            ps.setLong(1, problem.getContestId());
            ps.setString(2, problem.getTitle());
            ps.setString(3, problem.getCode());
            ps.setLong(4, limit.getId());
            ps.setString(5, problem.getAuthor());
            ps.setString(6, problem.getSource());
            ps.setString(7, problem.getContest());
            ps.setBoolean(8, problem.isChecker());
            ps.setLong(9, user);
            ps.setTimestamp(10, new Timestamp(new Date().getTime()));
            ps.setString(11, problem.getColor());
            ps.setInt(12, problem.getScore());
            ps.setLong(13, problem.getId());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        conn.commit();
    } catch (Exception e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to create problem.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) Limit(cn.edu.zju.acm.onlinejudge.bean.Limit) Timestamp(java.sql.Timestamp) Date(java.util.Date) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException)

Example 32 with PersistenceException

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

the class UserPersistenceImpl method getUserProfileByCode.

/**
     * <p>
     * Gets the user profile with given code in persistence layer.
     * </p>
     * 
     * @param code
     *            the code of the user profile
     * @return the user profile with given code in persistence layer
     * @throws NullPointerException
     *             if argument is null
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public UserProfile getUserProfileByCode(String code) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.GET_USER_BY_CODE);
            ps.setString(1, code);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return this.populateUserProfile(rs);
            } else {
                return null;
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the user profile with code " + code, 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)

Example 33 with PersistenceException

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

the class UserPersistenceImpl method deleteUserProfile.

/**
     * <p>
     * Deletes the specified user profile in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the user profile to delete
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void deleteUserProfile(long id, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.DELETE_USER);
            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 user profile.");
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (PersistenceException e) {
        throw e;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to delete user profile.", 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 34 with PersistenceException

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

the class UserPersistenceImpl method searchUserProfiles.

/**
     * <p>
     * Searches all user profiles according with the given criteria in persistence layer.
     * </p>
     * 
     * @return a list of user profiles according with the given criteria
     * @param criteria
     *            the user profile search criteria
     * @param offset
     *            the offset of the start position to search
     * @param count
     *            the maximum number of user profiles in returned list
     * @throws NullPointerException
     *             if argument is null
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public List<UserProfile> searchUserProfiles(UserCriteria criteria, int offset, int count) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = this.getUserSearchSql(conn, criteria, false, offset, count);
            ResultSet rs = ps.executeQuery();
            List<UserProfile> users = new ArrayList<UserProfile>();
            while (rs.next()) {
                users.add(this.populateUserProfile(rs));
            }
            return users;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to search user.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) SQLException(java.sql.SQLException) 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 35 with PersistenceException

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

the class ReferencePersistenceImpl method getReference.

/**
     * <p>
     * Gets the reference with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the reference
     * @return the reference with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public Reference getReference(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("SELECT reference_id, reference_type_id, name, content_type, " + "content, size, compressed FROM reference WHERE reference_id=?");
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                return null;
            }
            return this.populateReference(rs);
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the reference 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)

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