diff --git a/src/main/java/org/olat/notifications/NotificationsManagerImpl.java b/src/main/java/org/olat/notifications/NotificationsManagerImpl.java index bf513b721f56adb94114f3046acf21a3d48c536c..c67196161c65cd1846a45b641af83313adaf1e5c 100644 --- a/src/main/java/org/olat/notifications/NotificationsManagerImpl.java +++ b/src/main/java/org/olat/notifications/NotificationsManagerImpl.java @@ -594,42 +594,45 @@ public class NotificationsManagerImpl extends NotificationsManager implements Us */ @Override public Publisher getPublisher(SubscriptionContext subsContext) { - return getPublisher(subsContext.getResName(), subsContext.getResId(), subsContext.getSubidentifier(), false); - } - - private Publisher getPublisherForUpdate(SubscriptionContext subsContext) { - return getPublisher(subsContext.getResName(), subsContext.getResId(), subsContext.getSubidentifier(), true); - } - - @Override - public List<Publisher> getAllPublisher() { - String q = "select pub from org.olat.notifications.PublisherImpl pub"; - return DBFactory.getInstance().getCurrentEntityManager().createQuery(q, Publisher.class) - .getResultList(); - } - - /** - * return the publisher for the given composite primary key ores + - * subidentifier. - */ - private Publisher getPublisher(String resName, Long resId, String subidentifier, boolean forUpdate) { StringBuilder q = new StringBuilder(); q.append("select pub from ").append(PublisherImpl.class.getName()).append(" pub ") - .append(" where pub.resName=:resName and pub.resId = :resId and pub.subidentifier = :subidentifier"); + .append(" where pub.resName=:resName and pub.resId = :resId"); + if(StringHelper.containsNonWhitespace(subsContext.getSubidentifier())) { + q.append(" and pub.subidentifier=:subidentifier"); + } else { + q.append(" and (pub.subidentifier='' or pub.subidentifier is null)"); + } TypedQuery<Publisher> query = DBFactory.getInstance().getCurrentEntityManager() .createQuery(q.toString(), Publisher.class) - .setParameter("resName", resName) - .setParameter("resId", resId.longValue()) - .setParameter("subidentifier", subidentifier); - if(forUpdate) { - query.setLockMode(LockModeType.PESSIMISTIC_WRITE); + .setParameter("resName", subsContext.getResName()) + .setParameter("resId", subsContext.getResId()); + + if(StringHelper.containsNonWhitespace(subsContext.getSubidentifier())) { + query.setParameter("subidentifier", subsContext.getSubidentifier()); } List<Publisher> res = query.getResultList(); if (res.isEmpty()) return null; if (res.size() != 1) throw new AssertException("only one subscriber per person and publisher!!"); return res.get(0); } + + private Publisher getPublisherForUpdate(SubscriptionContext subsContext) { + Publisher pub = getPublisher(subsContext); + if(pub != null && pub.getKey() != null) { + //prevent optimistic lock issue + DBFactory.getInstance().getCurrentEntityManager().detach(pub); + pub = DBFactory.getInstance().getCurrentEntityManager().find(PublisherImpl.class, pub.getKey(), LockModeType.PESSIMISTIC_WRITE); + } + return pub; + } + + @Override + public List<Publisher> getAllPublisher() { + String q = "select pub from org.olat.notifications.PublisherImpl pub"; + return DBFactory.getInstance().getCurrentEntityManager().createQuery(q, Publisher.class) + .getResultList(); + } /** * @param resName @@ -838,7 +841,7 @@ public class NotificationsManagerImpl extends NotificationsManager implements Us // to make sure: ignore if no subscriptionContext if (subscriptionContext == null) return; - Publisher toUpdate = getPublisher(subscriptionContext.getResName(), subscriptionContext.getResId(), subscriptionContext.getSubidentifier(), true); + Publisher toUpdate = getPublisherForUpdate(subscriptionContext); if(toUpdate == null) { return; }