Skip to content
Snippets Groups Projects
Commit 405251ce authored by srosse's avatar srosse
Browse files

OO-573: delete every single comment sorted by creation date to prevent constraint errors

parent 5fce03dd
No related branches found
No related tags found
No related merge requests found
...@@ -25,6 +25,9 @@ import java.util.HashSet; ...@@ -25,6 +25,9 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.olat.core.commons.persistence.DB; import org.olat.core.commons.persistence.DB;
import org.olat.core.commons.persistence.DBFactory; import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.commons.persistence.DBQuery; import org.olat.core.commons.persistence.DBQuery;
...@@ -285,22 +288,37 @@ public class UserCommentsManagerImpl extends UserCommentsManager { ...@@ -285,22 +288,37 @@ public class UserCommentsManagerImpl extends UserCommentsManager {
public int deleteAllComments() { public int deleteAllComments() {
DB db = DBFactory.getInstance(); DB db = DBFactory.getInstance();
// special query when sub path is null // special query when sub path is null
List<UserCommentImpl> comments;
if (getOLATResourceableSubPath() == null) { if (getOLATResourceableSubPath() == null) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("delete from ").append(UserCommentImpl.class.getName()).append(" where resName=:resName and resId=:resId and resSubPath is null"); sb.append("select comment from ").append(UserCommentImpl.class.getName()).append(" comment")
return db.getCurrentEntityManager().createQuery(sb.toString()) .append(" where resName=:resName and resId=:resId and resSubPath is null")
.append(" order by creationDate desc");
comments = db.getCurrentEntityManager().createQuery(sb.toString(), UserCommentImpl.class)
.setParameter("resName", getOLATResourceable().getResourceableTypeName()) .setParameter("resName", getOLATResourceable().getResourceableTypeName())
.setParameter("resId", getOLATResourceable().getResourceableId()) .setParameter("resId", getOLATResourceable().getResourceableId())
.executeUpdate(); .getResultList();
} else { } else {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("delete from ").append(UserCommentImpl.class.getName()).append(" where resName=:resName and resId=:resId and resSubPath=:resSubPath"); sb.append("select comment from ").append(UserCommentImpl.class.getName()).append(" comment")
return db.getCurrentEntityManager().createQuery(sb.toString()) .append(" where resName=:resName and resId=:resId and resSubPath=:resSubPath")
.append(" order by creationDate desc");
comments = db.getCurrentEntityManager().createQuery(sb.toString(), UserCommentImpl.class)
.setParameter("resName", getOLATResourceable().getResourceableTypeName()) .setParameter("resName", getOLATResourceable().getResourceableTypeName())
.setParameter("resId", getOLATResourceable().getResourceableId()) .setParameter("resId", getOLATResourceable().getResourceableId())
.setParameter("resSubPath", getOLATResourceableSubPath()) .setParameter("resSubPath", getOLATResourceableSubPath())
.executeUpdate(); .getResultList();
} }
if(comments != null && !comments.isEmpty()) {
for(UserCommentImpl comment:comments) {
db.getCurrentEntityManager().remove(comment);
}
}
return comments == null ? 0 : comments.size();
} }
/** /**
...@@ -310,11 +328,18 @@ public class UserCommentsManagerImpl extends UserCommentsManager { ...@@ -310,11 +328,18 @@ public class UserCommentsManagerImpl extends UserCommentsManager {
public int deleteAllCommentsIgnoringSubPath() { public int deleteAllCommentsIgnoringSubPath() {
DB db = DBFactory.getInstance(); DB db = DBFactory.getInstance();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("delete from ").append(UserCommentImpl.class.getName()).append(" where resName=:resName and resId=:resId"); sb.append("select comment from ").append(UserCommentImpl.class.getName()).append(" comment")
return db.getCurrentEntityManager().createQuery(sb.toString()) .append(" where resName=:resName and resId=:resId")
.append(" order by creationDate desc");
List<UserCommentImpl> comments = db.getCurrentEntityManager().createQuery(sb.toString(), UserCommentImpl.class)
.setParameter("resName", getOLATResourceable().getResourceableTypeName()) .setParameter("resName", getOLATResourceable().getResourceableTypeName())
.setParameter("resId", getOLATResourceable().getResourceableId()) .setParameter("resId", getOLATResourceable().getResourceableId())
.executeUpdate(); .getResultList();
for(UserCommentImpl comment:comments) {
db.getCurrentEntityManager().remove(comment);
}
return comments.size();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment