comment = reloadComment(comment);
if (comment == null) {
// Original comment has been deleted in the meantime. Don't delete it again.
return 0;
}
DB db = DBFactory.getInstance();
// First deal with all direct replies
DBQuery query = db.createQuery("select comment from UserCommentImpl as comment where parent=:parent");
query.setEntity("parent", comment);
List<UserComment> replies = query.list();
if (deleteReplies) {
// Since we have a many-to-one we first have to recursively delete
// the replies to prevent foreign key constraints
for (UserComment reply : replies) {
counter += deleteComment(reply, true);
}
} else {
// To not delete the replies we have to set the parent to the parent
// of the original comment for each reply
for (UserComment reply : replies) {
reply.setParent(comment.getParent());
db.updateObject(reply);
}
}
// Now delete this comment and finish
db.deleteObject(comment);
return counter+1;
}