use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class AuthorizationPersistenceImpl method getRole.
/**
* <p>
* Gets a role with given id from persistence layer.
* </p>
*
* @return a role with given id from persistence layer
* @param id
* the id of the role
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public RoleSecurity getRole(long id) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
ResultSet rs = null;
RoleSecurity role;
try {
ps = conn.prepareStatement("SELECT role_id, name, description FROM role WHERE role_id=?");
ps.setLong(1, id);
rs = ps.executeQuery();
if (!rs.next()) {
return null;
}
role = new RoleSecurity(rs.getLong(1), rs.getString(2), rs.getString(3));
} finally {
Database.dispose(ps);
}
try {
// select the contest permissions
ps = conn.prepareStatement("SELECT role_id, contest_id, permission_level_id FROM contest_permission WHERE role_id=?");
ps.setLong(1, id);
rs = ps.executeQuery();
while (rs.next()) {
role.getContestPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
}
} finally {
Database.dispose(ps);
}
try {
// select the forum permissions
ps = conn.prepareStatement("SELECT role_id, forum_id, permission_level_id FROM forum_permission WHERE role_id=?");
ps.setLong(1, id);
rs = ps.executeQuery();
while (rs.next()) {
role.getForumPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
}
} finally {
Database.dispose(ps);
}
return role;
} catch (SQLException e) {
throw new PersistenceException("Failed to get the role with id " + id, e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class AuthorizationPersistenceImpl method getAllRoles.
/**
* <p>
* Gets all roles from persistence layer.
* </p>
*
* @return a list of RoleSecurity instances
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public List<RoleSecurity> getAllRoles() throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
ResultSet rs = null;
List<RoleSecurity> roles = new ArrayList<RoleSecurity>();
Map<Long, RoleSecurity> roleIds = new HashMap<Long, RoleSecurity>();
try {
// select the roles;
ps = conn.prepareStatement("SELECT role_id, name, description FROM role");
rs = ps.executeQuery();
while (rs.next()) {
RoleSecurity role = new RoleSecurity(rs.getLong(1), rs.getString(2), rs.getString(3));
roles.add(role);
roleIds.put(role.getId(), role);
}
} finally {
Database.dispose(ps);
}
try {
// select the contests permissions
ps = conn.prepareStatement("SELECT role_id, contest_id, permission_level_id FROM contest_permission");
rs = ps.executeQuery();
while (rs.next()) {
RoleSecurity role = roleIds.get(rs.getLong(1));
role.getContestPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
}
} finally {
Database.dispose(ps);
}
try {
// select the forum permissions
ps = conn.prepareStatement("SELECT role_id, forum_id, permission_level_id FROM forum_permission");
rs = ps.executeQuery();
while (rs.next()) {
RoleSecurity role = roleIds.get(rs.getLong(1));
role.getForumPermission().addPermission(rs.getLong(2), PermissionLevel.findById(rs.getLong(3)));
}
} finally {
Database.dispose(ps);
}
return roles;
} catch (SQLException e) {
throw new PersistenceException("Failed to get all roles", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ConfigurationPersistenceImpl method setConfigurations.
/**
* <p>
* Stores the given list of Configuration instances to persistence layer.
* </p>
*
* @param configurations
* a list of Configuration instances to store
* @param user
* the id of the user who made this modification.
* @throws PersistenceException
* wrapping a persistence implementation specific exception
* @throws NullPointerException
* if configurations is null
* @throws IllegalArgumentException
* if configurations contains invalid or duplicate element
*/
public void setConfigurations(List<Configuration> configurations, long user) throws PersistenceException {
if (configurations.size() == 0) {
return;
}
Set<String> nameSet = new HashSet<String>();
for (Configuration configuration : configurations) {
String name = configuration.getName();
if (!nameSet.add(name)) {
throw new IllegalArgumentException("configurations contains duplicate element");
}
}
Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
Set<String> existingConfigurations = new HashSet<String>();
try {
// get existing configurations
ps = conn.prepareStatement(ConfigurationPersistenceImpl.GET_CONFIGURATION_NAMES + Database.createValues(nameSet));
ResultSet rs = ps.executeQuery();
while (rs.next()) {
existingConfigurations.add(rs.getString(DatabaseConstants.CONFIGURATION_NAME));
}
} finally {
Database.dispose(ps);
}
// update
for (Configuration configuration : configurations) {
try {
if (existingConfigurations.contains(configuration.getName())) {
ps = conn.prepareStatement(ConfigurationPersistenceImpl.UPDATE_CONFIGURATION);
ps.setString(5, configuration.getName());
ps.setString(1, configuration.getValue());
ps.setString(2, configuration.getDescription());
ps.setLong(3, user);
ps.setTimestamp(4, new Timestamp(new Date().getTime()));
} else {
ps = conn.prepareStatement(ConfigurationPersistenceImpl.INSERT_CONFIGURATION);
ps.setString(1, configuration.getName());
ps.setString(2, configuration.getValue());
ps.setString(3, configuration.getDescription());
ps.setLong(4, user);
ps.setTimestamp(5, new Timestamp(new Date().getTime()));
ps.setLong(6, user);
ps.setTimestamp(7, new Timestamp(new Date().getTime()));
}
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
}
conn.commit();
} catch (SQLException e) {
Database.rollback(conn);
throw new PersistenceException("Error.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ConfigurationPersistenceImpl method getConfigurations.
/**
* <p>
* Returns a list of Configuration instances retrieved from persistence layer.
* </p>
*
* @return a list of Configuration instances retrieved from persistence layer.
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public List<Configuration> getConfigurations() throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ConfigurationPersistenceImpl.GET_ALL_CONFIGURATIONS);
ResultSet rs = ps.executeQuery();
List<Configuration> configurations = new ArrayList<Configuration>();
while (rs.next()) {
Configuration configuration = new Configuration();
configuration.setName(rs.getString(DatabaseConstants.CONFIGURATION_NAME));
configuration.setValue(rs.getString(DatabaseConstants.CONFIGURATION_VALUE));
configuration.setDescription(rs.getString(DatabaseConstants.CONFIGURATION_DESCRIPTION));
configurations.add(configuration);
}
return configurations;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Error.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ContestPersistenceImpl method createContest.
/**
* <p>
* Creates the specified contest in persistence layer.
* </p>
*
* @param contest
* the AbstractContest instance to create
* @param user
* the id of the user who made this modification
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void createContest(AbstractContest contest, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
conn.setAutoCommit(false);
PreparedStatement ps = null;
Limit limit = contest.getLimit();
try {
// create a new limit
if (limit != null && limit.getId() != ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_LIMIT);
ps.setInt(1, limit.getTimeLimit());
ps.setInt(2, limit.getMemoryLimit());
ps.setInt(3, limit.getOutputLimit());
ps.setInt(4, limit.getSubmissionLimit());
ps.executeUpdate();
limit.setId(Database.getLastId(conn));
}
} finally {
Database.dispose(ps);
}
try {
// create the contest
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST);
ps.setString(1, contest.getTitle());
ps.setString(2, contest.getDescription());
if (contest.getStartTime() != null) {
ps.setTimestamp(3, new Timestamp(contest.getStartTime().getTime()));
} else {
ps.setTimestamp(3, null);
}
if (contest.getEndTime() != null) {
ps.setTimestamp(4, new Timestamp(contest.getEndTime().getTime()));
} else {
ps.setTimestamp(4, null);
}
ps.setLong(5, contest.getForumId());
if (limit == null || limit.getId() == ContestPersistenceImpl.DEFAULT_LIMIT_ID) {
ps.setLong(6, ContestPersistenceImpl.DEFAULT_LIMIT_ID);
} else {
ps.setLong(6, limit.getId());
}
int contesttype = 0;
if (contest instanceof Problemset) {
contesttype = 1;
}
if (contest instanceof Course) {
contesttype = 2;
}
ps.setInt(7, contesttype);
ps.setLong(8, user);
ps.setTimestamp(9, new Timestamp(new Date().getTime()));
ps.setLong(10, user);
ps.setTimestamp(11, new Timestamp(new Date().getTime()));
ps.setBoolean(12, contest.isCheckIp());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
contest.setId(Database.getLastId(conn));
// create languages
if (contest.getLanguages() != null) {
for (Language language : contest.getLanguages()) {
try {
ps = conn.prepareStatement(ContestPersistenceImpl.INSERT_CONTEST_LANGUAGE);
ps.setLong(1, contest.getId());
ps.setLong(2, language.getId());
ps.executeUpdate();
} finally {
Database.dispose(ps);
}
}
}
conn.commit();
} catch (Exception e) {
Database.rollback(conn);
throw new PersistenceException("Failed to create contest.", e);
} finally {
Database.dispose(conn);
}
}
Aggregations