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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations