[CST-12752] handled automatic pattern/services in the LDNConsumer

This commit is contained in:
mohamed eskander
2023-11-22 15:44:22 +02:00
parent 6fa57f2aef
commit 2dd9575293
6 changed files with 154 additions and 15 deletions

View File

@@ -29,12 +29,14 @@ import org.dspace.app.ldn.factory.NotifyServiceFactory;
import org.dspace.app.ldn.model.Notification;
import org.dspace.app.ldn.service.LDNMessageService;
import org.dspace.app.ldn.service.NotifyPatternToTriggerService;
import org.dspace.app.ldn.service.NotifyServiceInboundPatternService;
import org.dspace.content.Bitstream;
import org.dspace.content.BitstreamFormat;
import org.dspace.content.Bundle;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.logic.LogicalStatement;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
@@ -45,6 +47,7 @@ import org.dspace.event.Consumer;
import org.dspace.event.Event;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.dspace.utils.DSpace;
import org.dspace.web.ContextUtil;
/**
@@ -55,6 +58,7 @@ import org.dspace.web.ContextUtil;
public class LDNMessageConsumer implements Consumer {
private NotifyPatternToTriggerService notifyPatternToTriggerService;
private NotifyServiceInboundPatternService inboundPatternService;
private LDNMessageService ldnMessageService;
private ConfigurationService configurationService;
private ItemService itemService;
@@ -67,6 +71,7 @@ public class LDNMessageConsumer implements Consumer {
configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
itemService = ContentServiceFactory.getInstance().getItemService();
bitstreamService = ContentServiceFactory.getInstance().getBitstreamService();
inboundPatternService = NotifyServiceFactory.getInstance().getNotifyServiceInboundPatternService();
}
@Override
@@ -77,36 +82,53 @@ public class LDNMessageConsumer implements Consumer {
return;
}
createLDNMessages(context, (Item) event.getSubject(context));
Item item = (Item) event.getSubject(context);
createManualLDNMessages(context, item);
createAutomaticLDNMessages(context, item);
}
private void createLDNMessages(Context context, Item item) throws SQLException {
private void createManualLDNMessages(Context context, Item item) throws SQLException, JsonProcessingException {
List<NotifyPatternToTrigger> patternsToTrigger =
notifyPatternToTriggerService.findByItem(context, item);
patternsToTrigger.forEach(patternToTrigger -> {
try {
createLDNMessage(context, patternToTrigger);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
for (NotifyPatternToTrigger patternToTrigger : patternsToTrigger) {
createLDNMessage(context,patternToTrigger.getItem(),
patternToTrigger.getNotifyService(), patternToTrigger.getPattern());
}
}
private void createLDNMessage(Context context, NotifyPatternToTrigger patternToTrigger)
private void createAutomaticLDNMessages(Context context, Item item) throws SQLException, JsonProcessingException {
List<NotifyServiceInboundPattern> inboundPatterns = inboundPatternService.findAutomaticPatterns(context);
for (NotifyServiceInboundPattern inboundPattern : inboundPatterns) {
if (inboundPattern.getConstraint() == null ||
evaluateFilter(context, item, inboundPattern.getConstraint())) {
createLDNMessage(context, item, inboundPattern.getNotifyService(), inboundPattern.getPattern());
}
}
}
private boolean evaluateFilter(Context context, Item item, String constraint) {
LogicalStatement filter =
new DSpace().getServiceManager().getServiceByName(constraint, LogicalStatement.class);
return filter != null && filter.getResult(context, item);
}
private void createLDNMessage(Context context, Item item, NotifyServiceEntity service, String pattern)
throws SQLException, JsonMappingException, JsonProcessingException {
LDN ldn = getLDNMessage(patternToTrigger.getPattern());
LDN ldn = getLDNMessage(pattern);
LDNMessageEntity ldnMessage =
ldnMessageService.create(context, format("urn:uuid:%s", UUID.randomUUID()));
ldnMessage.setObject(patternToTrigger.getItem());
ldnMessage.setTarget(patternToTrigger.getNotifyService());
ldnMessage.setObject(item);
ldnMessage.setTarget(service);
ldnMessage.setQueueStatus(LDNMessageEntity.QUEUE_STATUS_QUEUED);
ldnMessage.setQueueTimeout(new Date());
appendGeneratedMessage(ldn, ldnMessage, patternToTrigger.getPattern());
appendGeneratedMessage(ldn, ldnMessage, pattern);
ObjectMapper mapper = new ObjectMapper();
Notification notification = mapper.readValue(ldnMessage.getMessage(), Notification.class);

View File

@@ -8,6 +8,7 @@
package org.dspace.app.ldn.dao;
import java.sql.SQLException;
import java.util.List;
import org.dspace.app.ldn.NotifyServiceEntity;
import org.dspace.app.ldn.NotifyServiceInboundPattern;
@@ -35,4 +36,12 @@ public interface NotifyServiceInboundPatternDao extends GenericDAO<NotifyService
public NotifyServiceInboundPattern findByServiceAndPattern(Context context,
NotifyServiceEntity notifyServiceEntity,
String pattern) throws SQLException;
/**
* find all automatic notifyServiceInboundPatterns
*
* @param context the context
* @return all automatic notifyServiceInboundPatterns
* @throws SQLException if database error
*/
List<NotifyServiceInboundPattern> findAutomaticPatterns(Context context) throws SQLException;
}

View File

@@ -8,6 +8,7 @@
package org.dspace.app.ldn.dao.impl;
import java.sql.SQLException;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
@@ -42,4 +43,17 @@ public class NotifyServiceInboundPatternDaoImpl
));
return uniqueResult(context, criteriaQuery, false, NotifyServiceInboundPattern.class);
}
@Override
public List<NotifyServiceInboundPattern> findAutomaticPatterns(Context context) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, NotifyServiceInboundPattern.class);
Root<NotifyServiceInboundPattern> inboundPatternRoot = criteriaQuery.from(NotifyServiceInboundPattern.class);
criteriaQuery.select(inboundPatternRoot);
criteriaQuery.where(
criteriaBuilder.equal(
inboundPatternRoot.get(NotifyServiceInboundPattern_.automatic), true)
);
return list(context, criteriaQuery, false, NotifyServiceInboundPattern.class, -1, -1);
}
}

View File

@@ -8,6 +8,7 @@
package org.dspace.app.ldn.service;
import java.sql.SQLException;
import java.util.List;
import org.dspace.app.ldn.NotifyServiceEntity;
import org.dspace.app.ldn.NotifyServiceInboundPattern;
@@ -35,6 +36,15 @@ public interface NotifyServiceInboundPatternService {
NotifyServiceEntity notifyServiceEntity,
String pattern) throws SQLException;
/**
* find all automatic notifyServiceInboundPatterns
*
* @param context the context
* @return all automatic notifyServiceInboundPatterns
* @throws SQLException if database error
*/
public List<NotifyServiceInboundPattern> findAutomaticPatterns(Context context) throws SQLException;
/**
* create new notifyServiceInboundPattern
*

View File

@@ -8,6 +8,7 @@
package org.dspace.app.ldn.service.impl;
import java.sql.SQLException;
import java.util.List;
import org.dspace.app.ldn.NotifyServiceEntity;
import org.dspace.app.ldn.NotifyServiceInboundPattern;
@@ -33,6 +34,11 @@ public class NotifyServiceInboundPatternServiceImpl implements NotifyServiceInbo
return inboundPatternDao.findByServiceAndPattern(context, notifyServiceEntity, pattern);
}
@Override
public List<NotifyServiceInboundPattern> findAutomaticPatterns(Context context) throws SQLException {
return inboundPatternDao.findAutomaticPatterns(context);
}
@Override
public NotifyServiceInboundPattern create(Context context, NotifyServiceEntity notifyServiceEntity)
throws SQLException {