Search in sources :

Example 86 with PersistenceException

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

the class UserPersistenceImpl method getAllCountries.

/**
     * <p>
     * Gets all countries from the persistence layer.
     * </p>
     * 
     * @return a list containing all country names
     */
public List<Country> getAllCountries() throws PersistenceException {
    if (this.allCountries == null) {
        synchronized (this) {
            if (this.allCountries == null) {
                Connection conn = null;
                try {
                    conn = Database.createConnection();
                    PreparedStatement ps = null;
                    try {
                        ps = conn.prepareStatement(UserPersistenceImpl.GET_ALL_COUNTRIES);
                        ResultSet rs = ps.executeQuery();
                        List<Country> countries = new ArrayList<Country>();
                        while (rs.next()) {
                            countries.add(new Country(rs.getLong(DatabaseConstants.COUNTRY_COUNTRY_ID), rs.getString(DatabaseConstants.COUNTRY_NAME)));
                        }
                        this.allCountries = Collections.unmodifiableList(countries);
                    } finally {
                        Database.dispose(ps);
                    }
                } catch (Exception e) {
                    throw new PersistenceCreationException("Failed to get all countries", e);
                } finally {
                    Database.dispose(conn);
                }
            }
        }
    }
    return this.allCountries;
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) Country(cn.edu.zju.acm.onlinejudge.bean.enumeration.Country) PreparedStatement(java.sql.PreparedStatement) PersistenceCreationException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException) PersistenceCreationException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException)

Example 87 with PersistenceException

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

the class UserPersistenceImpl method updateUserProfile.

/**
     * <p>
     * Updates the specified user profile in persistence layer.
     * </p>
     * 
     * @param profile
     *            the UserProfile 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 updateUserProfile(UserProfile profile, long user) throws PersistenceException {
    // TODO(xuchuan): refactor this function
    if (profile.getPassword() == null || profile.getPassword().trim().length() == 0) {
        Connection conn = null;
        try {
            conn = Database.createConnection();
            PreparedStatement ps = null;
            try {
                ps = conn.prepareStatement(UserPersistenceImpl.UPDATE_USER_1);
                ps.setString(1, profile.getHandle());
                ps.setString(2, profile.getEmail());
                ps.setString(3, profile.getFirstName());
                ps.setString(4, profile.getLastName());
                ps.setString(5, profile.getAddressLine1());
                ps.setString(6, profile.getAddressLine2());
                ps.setString(7, profile.getCity());
                ps.setString(8, profile.getState());
                ps.setLong(9, profile.getCountry().getId());
                ps.setString(10, profile.getZipCode());
                ps.setString(11, profile.getPhoneNumber());
                ps.setTimestamp(12, new Timestamp(profile.getBirthDate().getTime()));
                ps.setString(13, "" + profile.getGender());
                ps.setString(14, profile.getSchool());
                ps.setString(15, profile.getMajor());
                ps.setBoolean(16, profile.isGraduateStudent());
                ps.setInt(17, profile.getGraduationYear());
                ps.setString(18, profile.getStudentNumber());
                ps.setBoolean(19, profile.isConfirmed());
                ps.setLong(20, user);
                ps.setTimestamp(21, new Timestamp(new Date().getTime()));
                ps.setString(22, profile.getNickName());
                ps.setLong(23, profile.getId());
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
        } catch (SQLException e) {
            throw new PersistenceException("Failed to update user.", e);
        } finally {
            Database.dispose(conn);
        }
    } else {
        Connection conn = null;
        try {
            conn = Database.createConnection();
            PreparedStatement ps = null;
            try {
                ps = conn.prepareStatement(UserPersistenceImpl.UPDATE_USER);
                ps.setString(1, profile.getHandle());
                ps.setString(2, profile.getPassword());
                ps.setString(3, profile.getEmail());
                ps.setString(4, profile.getFirstName());
                ps.setString(5, profile.getLastName());
                ps.setString(6, profile.getAddressLine1());
                ps.setString(7, profile.getAddressLine2());
                ps.setString(8, profile.getCity());
                ps.setString(9, profile.getState());
                ps.setLong(10, profile.getCountry().getId());
                ps.setString(11, profile.getZipCode());
                ps.setString(12, profile.getPhoneNumber());
                ps.setTimestamp(13, new Timestamp(profile.getBirthDate().getTime()));
                ps.setString(14, "" + profile.getGender());
                ps.setString(15, profile.getSchool());
                ps.setString(16, profile.getMajor());
                ps.setBoolean(17, profile.isGraduateStudent());
                ps.setInt(18, profile.getGraduationYear());
                ps.setString(19, profile.getStudentNumber());
                ps.setBoolean(20, profile.isConfirmed());
                ps.setLong(21, user);
                ps.setTimestamp(22, new Timestamp(new Date().getTime()));
                ps.setString(23, profile.getNickName());
                ps.setLong(24, profile.getId());
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
        } catch (SQLException e) {
            throw new PersistenceException("Failed to update user.", 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 88 with PersistenceException

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

the class UserPersistenceImpl method createUserPreference.

/**
     * <p>
     * Creates the specified user preference in persistence layer.
     * </p>
     * 
     * @param preference
     *            the UserPreference 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 createUserPreference(UserPreference preference, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.INSERT_USER_PREFERENCE);
            ps.setLong(1, preference.getId());
            ps.setString(2, preference.getPlan());
            ps.setInt(3, preference.getProblemPaging());
            ps.setInt(4, preference.getSubmissionPaging());
            ps.setInt(5, preference.getStatusPaging());
            ps.setInt(6, preference.getUserPaging());
            ps.setInt(7, preference.getThreadPaging());
            ps.setInt(8, preference.getPostPaging());
            ps.setLong(9, user);
            ps.setTimestamp(10, new Timestamp(new Date().getTime()));
            ps.setLong(11, user);
            ps.setTimestamp(12, new Timestamp(new Date().getTime()));
            ps.executeUpdate();
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to create preference.", 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 89 with PersistenceException

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

the class UserPersistenceImpl method getUserProfileByEmail.

/**
     * <p>
     * Gets the user profile with given email in persistence layer.
     * </p>
     * 
     * @param email
     *            the email of the user profile
     * @return the user profile with given email in persistence layer
     * @throws NullPointerException
     *             if argument is null
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public UserProfile getUserProfileByEmail(String email) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.GET_USER_BY_EMAIL);
            ps.setString(1, email);
            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 email " + email, 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 90 with PersistenceException

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

the class UserPersistenceImpl method createConfirmCode.

/**
     * <p>
     * Creates a confirm code for given user in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the user
     * @param code
     *            the confirm code
     * @param user
     *            the id of the user who made this modification
     * @throws NullPointerException
     *             if argument is null
     * @throws IllegalArgumentException
     *             if argument is empty
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public void createConfirmCode(long id, String code, long user) throws PersistenceException {
    if (code.trim().length() == 0) {
        throw new IllegalArgumentException("code is empty");
    }
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.UPDATE_CODE);
            ps.setString(1, code);
            ps.setLong(2, id);
            int row = ps.executeUpdate();
            if (row == 0) {
                ps = conn.prepareStatement(UserPersistenceImpl.INSERT_CODE);
                ps.setLong(1, id);
                ps.setString(2, code);
                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)

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