Skip to content

Commit

Permalink
SAK-41139 No alert for draft announcements (sakaiproject#6832)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfish authored and Miguel Pellicer committed Apr 19, 2019
1 parent 6556c9a commit a69cc72
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@

var message = '';

if (alert.event === 'annc.new') {
if (alert.event === 'annc.new' || alert.event === 'annc.revise.availability') {
message
= data.i18n.announcement.replace('{0}', title).replace('{1}', siteTitle);
} else if (alert.event === 'asn.new.assignment') {
Expand Down
4 changes: 4 additions & 0 deletions portal/portal-api/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
*/
public interface BullhornHandler {

public String getHandledEvent();
public List<String> getHandledEvents();
public Optional<List<BullhornData>> handleEvent(Event e, Cache<String, Map> countCache);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.sakaiproject.portal.beans.bullhornhandlers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -56,8 +57,8 @@ public class AddAssignmentBullhornHandler extends AbstractBullhornHandler {
private SiteService siteService;

@Override
public String getHandledEvent() {
return AssignmentConstants.EVENT_ADD_ASSIGNMENT;
public List<String> getHandledEvents() {
return Arrays.asList(AssignmentConstants.EVENT_ADD_ASSIGNMENT);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package org.sakaiproject.portal.beans.bullhornhandlers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.Resource;
import javax.inject.Inject;

import org.sakaiproject.announcement.api.AnnouncementMessage;
Expand All @@ -31,10 +33,18 @@
import org.sakaiproject.event.api.Event;
import org.sakaiproject.memory.api.Cache;
import org.sakaiproject.portal.api.BullhornData;
import org.sakaiproject.portal.beans.BullhornAlert;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.api.SiteService;

import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -51,12 +61,18 @@ public class AnnouncementsBullhornHandler extends AbstractBullhornHandler {
@Inject
private ServerConfigurationService serverConfigurationService;

@Resource(name = "org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory")
private SessionFactory sessionFactory;

@Inject
private SiteService siteService;

@Resource(name = "org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager")
private PlatformTransactionManager transactionManager;

@Override
public String getHandledEvent() {
return AnnouncementService.SECURE_ANNC_ADD;
public List<String> getHandledEvents() {
return Arrays.asList(AnnouncementService.SECURE_ANNC_ADD, AnnouncementService.EVENT_ANNC_UPDATE_AVAILABILITY);
}

@Override
Expand All @@ -69,13 +85,41 @@ public Optional<List<BullhornData>> handleEvent(Event e, Cache<String, Map> coun

String siteId = pathParts[3];

SecurityAdvisor sa = unlock(new String[] {AnnouncementService.SECURE_ANNC_READ});
SecurityAdvisor sa = unlock(new String[] {AnnouncementService.SECURE_ANNC_READ, AnnouncementService.SECURE_ANNC_READ_DRAFT});
try {
AnnouncementMessage message
= (AnnouncementMessage) announcementService.getMessage(
entityManager.newReference(ref));

if (announcementService.isMessageViewable(message)) {
// If the announcement has just been hidden, remove any existing alerts for it
if (e.getEvent().equals(AnnouncementService.EVENT_ANNC_UPDATE_AVAILABILITY) && message.getHeader().getDraft()) {
try {
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);

transactionTemplate.execute(new TransactionCallbackWithoutResult() {

protected void doInTransactionWithoutResult(TransactionStatus status) {

// Grab the alerts we'll be deleting. We'll need to clear the count caches
// for the recipients
final List<BullhornAlert> alerts
= sessionFactory.getCurrentSession().createCriteria(BullhornAlert.class)
.add(Restrictions.eq("event", AnnouncementService.SECURE_ANNC_ADD))
.add(Restrictions.eq("ref", ref)).list();

alerts.forEach(a -> countCache.remove(a.getToUser()));

sessionFactory.getCurrentSession().createQuery("delete BullhornAlert where event = :event and ref = :ref")
.setString("event", AnnouncementService.SECURE_ANNC_ADD)
.setString("ref", ref).executeUpdate();
}
});
} catch (Exception e1) {
log.error("Failed to delete bullhorn add announcement event", e1);
}
}

if (!message.getHeader().getDraft() && announcementService.isMessageViewable(message)) {
Site site = siteService.getSite(siteId);
String toolId = site.getToolForCommonId("sakai.announcements").getId();
String url = serverConfigurationService.getPortalUrl() + "/directtool/" + toolId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.sakaiproject.portal.beans.bullhornhandlers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -52,8 +53,8 @@ public class CommonsCommentBullhornHandler extends AbstractBullhornHandler {
private SiteService siteService;

@Override
public String getHandledEvent() {
return CommonsEvents.COMMENT_CREATED;
public List<String> getHandledEvents() {
return Arrays.asList(CommonsEvents.COMMENT_CREATED);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,28 @@
*/
package org.sakaiproject.portal.beans.bullhornhandlers;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;

import org.sakaiproject.event.api.Event;
import org.sakaiproject.memory.api.Cache;
import org.sakaiproject.portal.api.BullhornData;
import org.sakaiproject.profile2.logic.ProfileLinkLogic;
import org.sakaiproject.profile2.util.ProfileConstants;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -44,12 +47,15 @@ public class FriendConfirmBullhornHandler extends AbstractBullhornHandler {
@Inject
private ProfileLinkLogic profileLinkLogic;

@Inject @Named("org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory")
@Resource(name = "org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory")
private SessionFactory sessionFactory;

@Resource(name = "org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager")
private PlatformTransactionManager transactionManager;

@Override
public String getHandledEvent() {
return ProfileConstants.EVENT_FRIEND_CONFIRM;
public List<String> getHandledEvents() {
return Arrays.asList(ProfileConstants.EVENT_FRIEND_CONFIRM);
}

public boolean isAcademic() {
Expand All @@ -65,18 +71,20 @@ public Optional<List<BullhornData>> handleEvent(Event e, Cache<String, Map> coun
String[] pathParts = ref.split("/");

String to = pathParts[2];
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
session.createQuery("delete BullhornAlert where event = :event and fromUser = :fromUser")
.setString("event", ProfileConstants.EVENT_FRIEND_REQUEST)
.setString("fromUser", to).executeUpdate();
tx.commit();
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);

transactionTemplate.execute(new TransactionCallbackWithoutResult() {

protected void doInTransactionWithoutResult(TransactionStatus status) {

sessionFactory.getCurrentSession().createQuery("delete BullhornAlert where event = :event and fromUser = :fromUser")
.setString("event", ProfileConstants.EVENT_FRIEND_REQUEST)
.setString("fromUser", to).executeUpdate();
}
});
} catch (Exception e1) {
log.error("Failed to delete bullhorn request event", e1);
tx.rollback();
} finally {
session.close();
}
String url = profileLinkLogic.getInternalDirectUrlToUserConnections(to);
countCache.remove(to);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,41 @@
*/
package org.sakaiproject.portal.beans.bullhornhandlers;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.inject.Inject;
import javax.inject.Named;
import javax.annotation.Resource;

import org.sakaiproject.event.api.Event;
import org.sakaiproject.memory.api.Cache;
import org.sakaiproject.portal.api.BullhornData;
import org.sakaiproject.profile2.util.ProfileConstants;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class FriendIgnoreBullhornHandler extends AbstractBullhornHandler {

@Inject @Named("org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory")
@Resource(name = "org.sakaiproject.springframework.orm.hibernate.GlobalSessionFactory")
private SessionFactory sessionFactory;

@Resource(name = "org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager")
private PlatformTransactionManager transactionManager;

@Override
public String getHandledEvent() {
return ProfileConstants.EVENT_FRIEND_IGNORE;
public List<String> getHandledEvents() {
return Arrays.asList(ProfileConstants.EVENT_FRIEND_IGNORE);
}

@Override
Expand All @@ -56,18 +61,20 @@ public Optional<List<BullhornData>> handleEvent(Event e, Cache<String, Map> coun
String[] pathParts = ref.split("/");

String to = pathParts[2];
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
try {
session.createQuery("delete BullhornAlert where event = :event and fromUser = :fromUser")
.setString("event", ProfileConstants.EVENT_FRIEND_REQUEST)
.setString("fromUser", to).executeUpdate();
tx.commit();
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);

transactionTemplate.execute(new TransactionCallbackWithoutResult() {

protected void doInTransactionWithoutResult(TransactionStatus status) {

sessionFactory.getCurrentSession().createQuery("delete BullhornAlert where event = :event and fromUser = :fromUser")
.setString("event", ProfileConstants.EVENT_FRIEND_REQUEST)
.setString("fromUser", to).executeUpdate();
}
});
} catch (Exception e1) {
log.error("Failed to delete bullhorn request event", e1);
tx.rollback();
} finally {
session.close();
}
countCache.remove(from);
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.sakaiproject.portal.beans.bullhornhandlers;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -46,8 +47,8 @@ public class FriendMessageBullhornHandler extends AbstractBullhornHandler {
private SiteService siteService;

@Override
public String getHandledEvent() {
return ProfileConstants.EVENT_MESSAGE_SENT;
public List<String> getHandledEvents() {
return Arrays.asList(ProfileConstants.EVENT_MESSAGE_SENT);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.sakaiproject.portal.beans.bullhornhandlers;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -46,8 +47,8 @@ public class FriendRequestBullhornHandler extends AbstractBullhornHandler {
private SiteService siteService;

@Override
public String getHandledEvent() {
return ProfileConstants.EVENT_FRIEND_REQUEST;
public List<String> getHandledEvents() {
return Arrays.asList(ProfileConstants.EVENT_FRIEND_REQUEST);
}

public boolean isAcademic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.sakaiproject.portal.beans.bullhornhandlers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -45,8 +46,8 @@ public class FriendStatusBullhornHandler extends AbstractBullhornHandler {
private ProfileLinkLogic profileLinkLogic;

@Override
public String getHandledEvent() {
return ProfileConstants.EVENT_STATUS_UPDATE;
public List<String> getHandledEvents() {
return Arrays.asList(ProfileConstants.EVENT_STATUS_UPDATE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.sakaiproject.portal.beans.bullhornhandlers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -44,8 +45,8 @@ public class GradeAssignmentBullhornHandler extends AbstractBullhornHandler {
private AssignmentService assignmentService;

@Override
public String getHandledEvent() {
return AssignmentConstants.EVENT_GRADE_ASSIGNMENT_SUBMISSION;
public List<String> getHandledEvents() {
return Arrays.asList(AssignmentConstants.EVENT_GRADE_ASSIGNMENT_SUBMISSION);
}

@Override
Expand Down
Loading

0 comments on commit a69cc72

Please sign in to comment.