Search in sources :

Example 76 with PersistenceException

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

the class UserPersistenceImpl method getUserPreference.

/**
     * <p>
     * Gets the user preference with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the user preference
     * @return the user preference with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public UserPreference getUserPreference(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(UserPersistenceImpl.GET_USER_PREFERENCE);
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                UserPreference preference = new UserPreference();
                preference.setId(rs.getLong(DatabaseConstants.USER_PREFERENCE_USER_PROFILE_ID));
                preference.setPlan(rs.getString(DatabaseConstants.USER_PREFERENCE_PLAN));
                preference.setPostPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_POST_PAGING));
                preference.setProblemPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_PROBLEM_PAGING));
                preference.setStatusPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_STATUS_PAGING));
                preference.setSubmissionPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_SUBMISSION_PAGING));
                preference.setThreadPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_THREAD_PAGING));
                preference.setUserPaging(rs.getInt(DatabaseConstants.USER_PREFERENCE_USER_PAGING));
                return preference;
            } else {
                return null;
            }
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the user preference 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) UserPreference(cn.edu.zju.acm.onlinejudge.bean.UserPreference)

Example 77 with PersistenceException

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

the class SubmissionPersistenceImpl method getProblemsetRankList.

public ProblemsetRankList getProblemsetRankList(long contestId, int offset, int count, String sort) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        String sql = null;
        if (sort.equalsIgnoreCase("submit")) {
            sql = "SELECT u.user_profile_id, u.handle, u.nickname, up.plan, ua.solved, ua.tiebreak " + "FROM user_ac ua " + "LEFT JOIN user_profile u ON ua.user_profile_id = u.user_profile_id " + "LEFT JOIN user_preference up ON ua.user_profile_id = up.user_profile_id " + "WHERE contest_id=? ORDER BY ua.tiebreak DESC, ua.solved DESC " + "LIMIT " + offset + "," + count;
        } else {
            sql = "SELECT u.user_profile_id, u.handle, u.nickname, up.plan, ua.solved, ua.tiebreak " + "FROM user_ac ua " + "LEFT JOIN user_profile u ON ua.user_profile_id = u.user_profile_id " + "LEFT JOIN user_preference up ON ua.user_profile_id = up.user_profile_id " + "WHERE contest_id=? ORDER BY ua.solved DESC, ua.tiebreak ASC " + "LIMIT " + offset + "," + count;
        }
        List<UserProfile> users = new ArrayList<UserProfile>();
        List<Integer> solved = new ArrayList<Integer>();
        List<Integer> total = new ArrayList<Integer>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setLong(1, contestId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                UserProfile user = new UserProfile();
                user.setId(rs.getLong(1));
                user.setHandle(rs.getString(2));
                user.setNickName(rs.getString(3));
                user.setDeclaration(rs.getString(4));
                users.add(user);
                solved.add(rs.getInt(5));
                total.add(rs.getInt(6));
            }
        } finally {
            Database.dispose(ps);
        }
        int[] solvedArray = new int[solved.size()];
        int[] totalArray = new int[solved.size()];
        for (int i = 0; i < solvedArray.length; ++i) {
            solvedArray[i] = solved.get(i);
            totalArray[i] = total.get(i);
        }
        ProblemsetRankList r = new ProblemsetRankList(offset, count);
        r.setUsers(users.toArray(new UserProfile[0]));
        r.setSolved(solvedArray);
        r.setTotal(totalArray);
        return r;
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the rank list", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : UserProfile(cn.edu.zju.acm.onlinejudge.bean.UserProfile) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) ProblemsetRankList(cn.edu.zju.acm.onlinejudge.util.ProblemsetRankList)

Example 78 with PersistenceException

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

the class SubmissionPersistenceImpl method getUserStatistics.

public UserStatistics getUserStatistics(long contestId, long userId) throws PersistenceException {
    Connection conn = null;
    try {
        UserStatistics statistics = new UserStatistics(userId, contestId);
        conn = Database.createConnection();
        PreparedStatement ps = null;
        /*String sql =
                    "SELECT DISTINCT p.problem_id, p.code, p.title, s.judge_comment " +
                        SubmissionPersistenceImpl.GET_SUBMISSION_FROM_PART +
                        " AND s.user_profile_id=? AND s.judge_reply_id=? AND s.contest_id=?";*/
        String sql = "SELECT p.problem_id, p.code, p.title, s.judge_comment " + SubmissionPersistenceImpl.GET_SUBMISSION_FROM_PART + " AND s.user_profile_id=? AND s.judge_reply_id=? AND s.contest_id=?";
        sql = sql.replace("FORCE_INDEX", "USE INDEX (index_submission_user_reply_contest)");
        HashSet<Long> solvedid = new HashSet<Long>();
        HashSet<Long> confirmedid = new HashSet<Long>();
        List<Problem> solved = new ArrayList<Problem>();
        List<Problem> confirmed = new ArrayList<Problem>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setLong(1, userId);
            ps.setLong(2, JudgeReply.ACCEPTED.getId());
            ps.setLong(3, contestId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Long probemid = new Long(rs.getLong("problem_id"));
                if (!solvedid.contains(probemid)) {
                    Problem p = new Problem();
                    p.setContestId(contestId);
                    p.setId(rs.getLong("problem_id"));
                    p.setCode(rs.getString("code"));
                    p.setTitle(rs.getString("title"));
                    solved.add(p);
                    solvedid.add(probemid);
                }
                String comment = rs.getString("judge_comment");
                if (!confirmed.contains(probemid) && "Yes".equals(comment)) {
                    Problem p = new Problem();
                    p.setContestId(contestId);
                    p.setId(rs.getLong("problem_id"));
                    p.setCode(rs.getString("code"));
                    p.setTitle(rs.getString("title"));
                    confirmed.add(p);
                    confirmedid.add(probemid);
                }
            }
        } finally {
            Database.dispose(ps);
        }
        statistics.setSolved(new TreeSet<Problem>(solved));
        statistics.setConfirmed(new TreeSet<Problem>(confirmed));
        try {
            ps = conn.prepareStatement("SELECT judge_reply_id, count(*) FROM submission WHERE contest_id=? AND user_profile_id=? GROUP BY judge_reply_id");
            ps.setLong(1, contestId);
            ps.setLong(2, userId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                long jid = rs.getLong(1);
                int count = rs.getInt(2);
                statistics.setCount(jid, count);
            }
            return statistics;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the user statistics", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) UserStatistics(cn.edu.zju.acm.onlinejudge.util.UserStatistics) ResultSet(java.sql.ResultSet) PersistenceException(cn.edu.zju.acm.onlinejudge.persistence.PersistenceException) Problem(cn.edu.zju.acm.onlinejudge.bean.Problem) HashSet(java.util.HashSet)

Example 79 with PersistenceException

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

the class SubmissionPersistenceImpl method getRankListEntry.

public RankListEntry getRankListEntry(long contestId, long userId) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("SELECT ac_number, submission_number FROM user_stat " + "WHERE contest_id=? AND user_id=?");
            ps.setLong(1, contestId);
            ps.setLong(2, userId);
            ResultSet rs = ps.executeQuery();
            RankListEntry re = null;
            if (rs.next()) {
                re = new RankListEntry(1);
                re.setSolved(rs.getLong(1));
                re.setSubmitted(rs.getLong(2));
            }
            return re;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the rank list", e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : RankListEntry(cn.edu.zju.acm.onlinejudge.util.RankListEntry) 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 80 with PersistenceException

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

the class SubmissionPersistenceImpl method getSubmission.

/**
     * <p>
     * Gets the submission with given id in persistence layer.
     * </p>
     * 
     * @param id
     *            the id of the submission
     * @return the submission with given id in persistence layer
     * @throws PersistenceException
     *             wrapping a persistence implementation specific exception
     */
public Submission getSubmission(long id) throws PersistenceException {
    Connection conn = null;
    try {
        conn = Database.createConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(SubmissionPersistenceImpl.GET_SUBMISSION.replace("FORCE_INDEX", ""));
            ps.setLong(1, id);
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                return null;
            }
            Map<Long, Language> languageMap = PersistenceManager.getInstance().getLanguagePersistence().getLanguageMap();
            Submission submission = this.populateSubmission(rs, true, languageMap);
            return submission;
        } finally {
            Database.dispose(ps);
        }
    } catch (SQLException e) {
        throw new PersistenceException("Failed to get the submission with id " + id, e);
    } finally {
        Database.dispose(conn);
    }
}
Also used : Language(cn.edu.zju.acm.onlinejudge.bean.enumeration.Language) Submission(cn.edu.zju.acm.onlinejudge.bean.Submission) 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