use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ReferencePersistenceImpl method getProblemReferences.
/**
* <p>
* Gets all problem references to the given problem with specified reference type.
* </p>
*
* @return a list containing all problem references to the given problem with specified reference type
* @param problemId
* the id of the referred problem
* @param referenceType
* the reference type of the returned references
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public List<Reference> getProblemReferences(long problemId, ReferenceType referenceType) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement("SELECT r.reference_id, reference_type_id, name, content_type, " + "content, size, compressed " + "FROM problem_reference pr LEFT JOIN reference r ON pr.reference_id = r.reference_id " + "WHERE pr.problem_id = ? AND r.reference_type_id=?");
ps.setLong(1, problemId);
ps.setLong(2, referenceType.getId());
ResultSet rs = ps.executeQuery();
List<Reference> references = new ArrayList<Reference>();
while (rs.next()) {
Reference reference = this.populateReference(rs);
references.add(reference);
}
return references;
} finally {
Database.dispose(ps);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to get the references", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ReferencePersistenceImpl method getProblemReferenceInfo.
/**
* <p>
* Gets all problem reference without data to the given problem with specified reference type.
* </p>
*
* @return a list containing all problem references to the given problem with specified reference type
* @param problemId
* the id of the referred problem
* @param referenceType
* the reference type of the returned references
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public List<Reference> getProblemReferenceInfo(long problemId, ReferenceType referenceType) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement("SELECT r.reference_id, reference_type_id, name, content_type, size " + "FROM problem_reference pr LEFT JOIN reference r ON pr.reference_id = r.reference_id " + "WHERE pr.problem_id = ? AND r.reference_type_id=?");
ps.setLong(1, problemId);
ps.setLong(2, referenceType.getId());
ResultSet rs = ps.executeQuery();
List<Reference> references = new ArrayList<Reference>();
while (rs.next()) {
Reference reference = this.populateReferenceInfo(rs);
references.add(reference);
}
return references;
} finally {
Database.dispose(ps);
}
} catch (Exception e) {
throw new PersistenceException("Failed to get the reference info", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method updatePost.
/**
* <p>
* Updates the specified post in persistence layer.
* </p>
*
* @param post
* the Post instance to update
* @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 updatePost(Post post, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.UPDATE_POST);
ps.setLong(1, post.getThreadId());
ps.setLong(2, post.getUserProfileId());
ps.setString(3, post.getContent());
ps.setLong(4, user);
ps.setTimestamp(5, new Timestamp(new Date().getTime()));
ps.setLong(6, post.getId());
if (ps.executeUpdate() == 0) {
throw new PersistenceException("no such post");
}
} finally {
Database.dispose(ps);
}
} catch (PersistenceException pe) {
throw pe;
} catch (SQLException e) {
throw new PersistenceException("Failed to update post.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method updateForum.
/**
* <p>
* Updates the specified forum in persistence layer.
* </p>
*
* @param forum
* the Forum instance to update
* @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 updateForum(Forum forum, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.UPDATE_FORUM);
ps.setString(1, forum.getName());
ps.setString(2, forum.getDescription());
ps.setLong(3, user);
ps.setTimestamp(4, new Timestamp(new Date().getTime()));
ps.setLong(5, forum.getId());
if (ps.executeUpdate() == 0) {
throw new PersistenceException("no such forum");
}
} finally {
Database.dispose(ps);
}
} catch (PersistenceException e) {
throw e;
} catch (Exception e) {
throw new PersistenceException("Failed to update forum.", e);
} finally {
Database.dispose(conn);
}
}
use of cn.edu.zju.acm.onlinejudge.persistence.PersistenceException in project zoj by licheng.
the class ForumPersistenceImpl method deleteThread.
/**
* <p>
* Deletes the specified thread in persistence layer.
* </p>
*
* @param id
* the id of the thread to delete
* @param user
* the id of the user who made this modification
* @throws PersistenceException
* wrapping a persistence implementation specific exception
*/
public void deleteThread(long id, long user) throws PersistenceException {
Connection conn = null;
try {
conn = Database.createConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(ForumPersistenceImpl.DELETE_THREAD);
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 thread");
}
} finally {
Database.dispose(ps);
}
} catch (PersistenceException pe) {
throw pe;
} catch (SQLException e) {
throw new PersistenceException("Failed to delete thread.", e);
} finally {
Database.dispose(conn);
}
}
Aggregations