mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-16 22:43:12 +00:00
subscription email: do not send email if nothing has changed (#8981)
* improved subscriptions email template * do not send emails without content * fixed coding style violations * removed unnecessary isEmpty check as suggested by reviewer * moved null check on indexableObjects in generateBodyMail * fixed unhandled IOException * fixed typo in bodyCommunities * do not use != to compare strings * fixed improper handling of empty list
This commit is contained in:
@@ -56,8 +56,16 @@ public class ContentGenerator implements SubscriptionGenerator<IndexableObject>
|
||||
Locale supportedLocale = I18nUtil.getEPersonLocale(ePerson);
|
||||
Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "subscriptions_content"));
|
||||
email.addRecipient(ePerson.getEmail());
|
||||
email.addArgument(generateBodyMail(context, indexableComm));
|
||||
email.addArgument(generateBodyMail(context, indexableColl));
|
||||
|
||||
String bodyCommunities = generateBodyMail(context, indexableComm);
|
||||
String bodyCollections = generateBodyMail(context, indexableColl);
|
||||
if (bodyCommunities.equals(EMPTY) && bodyCollections.equals(EMPTY)) {
|
||||
log.debug("subscription(s) of eperson {} do(es) not match any new items: nothing to send" +
|
||||
" - exit silently", ePerson::getID);
|
||||
return;
|
||||
}
|
||||
email.addArgument(bodyCommunities);
|
||||
email.addArgument(bodyCollections);
|
||||
email.send();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -67,21 +75,19 @@ public class ContentGenerator implements SubscriptionGenerator<IndexableObject>
|
||||
}
|
||||
|
||||
private String generateBodyMail(Context context, List<IndexableObject> indexableObjects) {
|
||||
if (indexableObjects == null || indexableObjects.isEmpty()) {
|
||||
return EMPTY;
|
||||
}
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write("\n".getBytes(UTF_8));
|
||||
if (indexableObjects.size() > 0) {
|
||||
for (IndexableObject indexableObject : indexableObjects) {
|
||||
out.write("\n".getBytes(UTF_8));
|
||||
Item item = (Item) indexableObject.getIndexedObject();
|
||||
String entityType = itemService.getEntityTypeLabel(item);
|
||||
Optional.ofNullable(entityType2Disseminator.get(entityType))
|
||||
.orElseGet(() -> entityType2Disseminator.get("Item"))
|
||||
.disseminate(context, item, out);
|
||||
}
|
||||
return out.toString();
|
||||
} else {
|
||||
out.write("No items".getBytes(UTF_8));
|
||||
for (IndexableObject indexableObject : indexableObjects) {
|
||||
out.write("\n".getBytes(UTF_8));
|
||||
Item item = (Item) indexableObject.getIndexedObject();
|
||||
String entityType = itemService.getEntityTypeLabel(item);
|
||||
Optional.ofNullable(entityType2Disseminator.get(entityType))
|
||||
.orElseGet(() -> entityType2Disseminator.get("Item"))
|
||||
.disseminate(context, item, out);
|
||||
}
|
||||
return out.toString();
|
||||
} catch (Exception e) {
|
||||
|
Reference in New Issue
Block a user