Search in sources :

Example 41 with PersistenceException

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

the class ForumPersistenceImpl method deleteForum.

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

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

the class PerformanceManager method saveAccessLog.

public synchronized void saveAccessLog(List<AccessLog> logs) throws PersistenceException {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = Database.createConnection();
        ps = conn.prepareStatement("INSERT INTO access_log (user_profile_id, handle, action, url, ip, timestamp, access_time) " + "VALUES(?,?,?,?,?,?,?)");
        for (AccessLog log : logs) {
            if (log.getHandle() == null) {
                ps.setNull(1, Types.BIGINT);
                ps.setNull(2, Types.VARCHAR);
            } else {
                ps.setLong(1, log.getUserId());
                ps.setString(2, log.getHandle());
            }
            ps.setString(3, log.getAction());
            ps.setString(4, log.getUrl());
            ps.setString(5, log.getIp());
            ps.setTimestamp(6, new Timestamp(log.getTimestamp().getTime()));
            ps.setLong(7, log.getAccessTime());
            ps.addBatch();
        }
        ps.executeBatch();
    } catch (SQLException e) {
        throw new PersistenceException("Failed to save logs.", e);
    } finally {
        Database.dispose(ps);
        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)

Example 43 with PersistenceException

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

the class PerformanceManager method getActionDashboard.

public synchronized List<ActionLog> getActionDashboard(LogCriteria criteria, String orderBy) throws PersistenceException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = Database.createConnection();
        ps = buildQuery(criteria, orderBy, conn);
        rs = ps.executeQuery();
        List<ActionLog> logs = new ArrayList<ActionLog>();
        while (rs.next()) {
            ActionLog log = new ActionLog();
            log.setAction(rs.getString("action"));
            log.setAvgAccessTime(rs.getLong("avg"));
            log.setMinAccessTime(rs.getLong("min"));
            log.setMaxAccessTime(rs.getLong("max"));
            log.setCount(rs.getLong("count"));
            logs.add(log);
        }
        return logs;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to search logs.", e);
    } finally {
        Database.dispose(ps);
        Database.dispose(conn);
    }
}
Also used : 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 44 with PersistenceException

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

the class UserPersistenceImpl method getStudents.

public List getStudents(long userId) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.GET_USER_BY_CREATE_USER);
            ps.setLong(1, userId);
            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 get the user profile with create user " + userId, 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 45 with PersistenceException

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

the class UserPersistenceImpl method getUserProfileByHandle.

/**
     * <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 getUserProfileByHandle(String handle) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.GET_USER_BY_HANDLE);
            ps.setString(1, 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 get the user profile with handle " + handle, 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