Search in sources :

Example 71 with PersistenceException

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

the class SubmissionPersistenceImpl method deleteSubmission.

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

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

the class UserPersistenceImpl method createTeacher.

/**
     * <p>
     * Creates the specified user profile in persistence layer.
     * </p>
     * 
     * @param profile
     *            the UserProfile 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 createTeacher(UserProfile profile, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.INSERT_TEACHER);
            ps.setString(1, profile.getHandle());
            ps.setString(2, profile.getPassword());
            ps.setString(3, profile.getEmail());
            ps.setTimestamp(4, new Timestamp(new Date().getTime()));
            ps.setString(5, profile.getFirstName());
            ps.setString(6, profile.getLastName());
            ps.setString(7, profile.getAddressLine1());
            ps.setString(8, profile.getAddressLine2());
            ps.setString(9, profile.getCity());
            ps.setString(10, profile.getState());
            ps.setLong(11, profile.getCountry().getId());
            ps.setString(12, profile.getZipCode());
            ps.setString(13, profile.getPhoneNumber());
            ps.setTimestamp(14, new Timestamp(profile.getBirthDate().getTime()));
            ps.setString(15, "" + profile.getGender());
            ps.setString(16, profile.getSchool());
            ps.setString(17, profile.getMajor());
            ps.setBoolean(18, profile.isGraduateStudent());
            ps.setInt(19, profile.getGraduationYear());
            ps.setString(20, profile.getStudentNumber());
            ps.setBoolean(21, profile.isConfirmed());
            ps.setLong(22, user);
            ps.setTimestamp(23, new Timestamp(new Date().getTime()));
            ps.setLong(24, user);
            ps.setTimestamp(25, new Timestamp(new Date().getTime()));
            ps.setString(26, profile.getNickName());
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.GET_LAST_ID);
            ResultSet rs = ps.executeQuery();
            rs.next();
            profile.setId(rs.getLong(1));
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to create user.", 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) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 73 with PersistenceException

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

the class UserPersistenceImpl method deleteConfirmCode.

/**
     * <p>
     * Deletes the confirm code of given user in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the user
     * @param user
     *            the id of the user who made this modification
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void deleteConfirmCode(long id, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.DELETE_CODE);
            ps.setLong(1, id);
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to create code.", 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)

Example 74 with PersistenceException

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

the class UserPersistenceImpl method getConfirmCode.

/**
     * <p>
     * Gets the confirm code with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the user
     * @return the confirm code of given user
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public String getConfirmCode(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.GET_CODE);
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getString(DatabaseConstants.CONFIRMATION_CODE);
            } 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)

Example 75 with PersistenceException

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

the class UserPersistenceImpl method login.

/**
     * <p>
     * Gets the user profile with given handle in persistence layer.
     * </p>
     * 
     * @param handle
     *            the handle of the user profile
     * @return the user profile with given handle in persistence layer
     * @throws NullPointerException
     *             if argument is null
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public UserProfile login(String handle, String password) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.LOGIN);
            ps.setString(1, handle);
            ps.setString(2, password);
            ps.setString(3, password);
            ps.setString(4, handle.length() > 1 ? handle.substring(0, 2) : handle + handle);
            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 login the user" + handle + " " + password, 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