Search in sources :

Example 21 with PersistenceException

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

the class LanguagePersistenceImpl method createLanguage.

/*
     * (non-Javadoc)
     * 
     * @see
     * cn.edu.zju.acm.onlinejudge.persistence.sql.LanguagePersistence#createLanguage(cn.edu.zju.acm.onlinejudge.bean
     * .enumeration.Language, long)
     */
public void createLanguage(Language language, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        synchronized (this.allLanguages) {
            try {
                ps = conn.prepareStatement(LanguagePersistenceImpl.INSERT_LANGUAGE);
                ps.setLong(1, language.getId());
                ps.setString(2, language.getName());
                ps.setString(3, language.getDescription());
                ps.setString(4, language.getOptions());
                ps.setString(5, language.getCompiler());
                ps.setLong(6, user);
                ps.setTimestamp(7, new Timestamp(new Date().getTime()));
                ps.setLong(8, user);
                ps.setTimestamp(9, new Timestamp(new Date().getTime()));
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
            this.allLanguages.put(language.getId(), language);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to create language.", 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 22 with PersistenceException

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

the class LanguagePersistenceImpl method deleteLanguage.

/*
     * (non-Javadoc)
     * 
     * @see cn.edu.zju.acm.onlinejudge.persistence.sql.LanguagePersistence#deleteLanguage(long, long)
     */
// TODO(xuchuan): this is a very dangerous operation. Check if necessary.
public void deleteLanguage(long id, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        conn.setAutoCommit(false);
        synchronized (this.allLanguages) {
            PreparedStatement ps = null;
            ps = conn.prepareStatement(LanguagePersistenceImpl.DELETE_SUBMISSION);
            try {
                ps.setLong(1, id);
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
            try {
                ps = conn.prepareStatement(LanguagePersistenceImpl.DELETE_LANGUAGE_CONTEST);
                ps.setLong(1, id);
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
            try {
                ps = conn.prepareStatement(LanguagePersistenceImpl.DELETE_LANGUAGE);
                ps.setLong(1, id);
                ps.executeUpdate();
            } finally {
                Database.dispose(ps);
            }
            conn.commit();
            this.allLanguages.remove(id);
        }
    } catch (Exception e) {
        Database.rollback(conn);
        throw new PersistenceException("Failed to delete language.", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Connection(java.sql.Connection) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) PreparedStatement(java.sql.PreparedStatement) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) SQLException(java.sql.SQLException) PersistenceCreationException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceCreationException)

Example 23 with PersistenceException

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

the class SubmissionPersistenceImpl method changeQQStatus.

public void changeQQStatus(long pid, long uid, String status) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("UPDATE submission_status SET status=? WHERE problem_id=? AND user_profile_id=?");
            ps.setString(1, status);
            ps.setLong(2, pid);
            ps.setLong(3, uid);
            int changes = ps.executeUpdate();
            if (changes == 0) {
                ps = conn.prepareStatement("INSERT INTO submission_status (problem_id, user_profile_id, status) VALUES (?,?,?)");
                ps.setLong(1, pid);
                ps.setLong(2, uid);
                ps.setString(3, status);
                ps.executeUpdate();
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to update the QQs", 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 24 with PersistenceException

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

the class UserPersistenceImpl method searchUserProfilesCount.

public int searchUserProfilesCount(UserCriteria criteria) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = this.getUserSearchSql(conn, criteria, true, 0, 0);
            ResultSet rs = ps.executeQuery();
            rs.next();
            return rs.getInt(1);
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to search 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)

Example 25 with PersistenceException

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

the class UserPersistenceImpl method createUserProfile.

/**
     * <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 createUserProfile(UserProfile profile, long user) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.INSERT_USER);
            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)

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