Skip to content

Commit

Permalink
Merged in DSC-1321 (pull request DSpace#1271)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevhenii-Lohatskyi authored and LucaGiamminonni committed Nov 13, 2023
2 parents bf5b19b + a4e942e commit 9940fcd
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,51 @@
import static org.apache.commons.lang.StringUtils.EMPTY;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Resource;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dspace.content.Item;
import org.dspace.content.crosswalk.StreamDisseminationCrosswalk;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.core.Email;
import org.dspace.core.I18nUtil;
import org.dspace.discovery.IndexableObject;
import org.dspace.eperson.EPerson;
import org.dspace.subscriptions.service.SubscriptionGenerator;
import org.springframework.beans.factory.annotation.Autowired;

import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;


/**
* Implementation class of SubscriptionGenerator
* which will handle the logic of sending the emails
* in case of 'content' subscriptionType
*/
@SuppressWarnings("rawtypes")
public class ContentGenerator implements SubscriptionGenerator<IndexableObject> {
public class ContentGenerator {

private final Logger log = LogManager.getLogger(ContentGenerator.class);
private final ConfigurationService configurationService = DSpaceServicesFactory.getInstance()
.getConfigurationService();

@SuppressWarnings("unchecked")
@Resource(name = "entityDissemination")
private Map<String, StreamDisseminationCrosswalk> entityType2Disseminator = new HashMap();

@Autowired
private ItemService itemService;

@Override
public void notifyForSubscriptions(Context context, EPerson ePerson,
List<IndexableObject> indexableComm,
List<IndexableObject> indexableColl,
List<IndexableObject> indexableItems) {
public void notifyForSubscriptions(EPerson ePerson,
List<SubscriptionItem> indexableComm,
List<SubscriptionItem> indexableColl,
Map<String, List<SubscriptionItem>> indexableEntityByType) {
try {
if (Objects.nonNull(ePerson)) {
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));
email.addArgument(generateBodyMail(context, indexableItems));
email.addArgument(configurationService.getProperty("subscription.url"));
email.addArgument(generateBodyMail("Community", indexableComm));
email.addArgument(generateBodyMail("Collection", indexableColl));
email.addArgument(
indexableEntityByType.entrySet().stream()
.map(entry -> generateBodyMail(entry.getKey(), entry.getValue()))
.collect(Collectors.joining("\n\n"))
);
email.send();
}
} catch (Exception e) {
Expand All @@ -72,18 +64,22 @@ public void notifyForSubscriptions(Context context, EPerson ePerson,
}
}

private String generateBodyMail(Context context, List<IndexableObject> indexableObjects) {
private String generateBodyMail(String type, List<SubscriptionItem> subscriptionItems) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write("\n".getBytes(UTF_8));
if (indexableObjects.size() > 0) {
for (IndexableObject indexableObject : indexableObjects) {
if (!subscriptionItems.isEmpty()) {
out.write(("\nYou have " + subscriptionItems.size() + " subscription(s) active to type " + type + "\n")
.getBytes(UTF_8));
for (SubscriptionItem item : subscriptionItems) {
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);
out.write("List of new content for the\n".getBytes(UTF_8));
out.write((type + " " + item.getName() + " - " + item.getUrl() + "\n")
.getBytes(UTF_8));

for (Entry<String, String> entry : item.getItemUrlsByItemName().entrySet()) {
out.write("\n".getBytes(UTF_8));
out.write((entry.getKey() + " - " + entry.getValue()).getBytes(UTF_8));
}
}
return out.toString();
} else {
Expand All @@ -96,8 +92,4 @@ private String generateBodyMail(Context context, List<IndexableObject> indexable
return EMPTY;
}

public void setEntityType2Disseminator(Map<String, StreamDisseminationCrosswalk> entityType2Disseminator) {
this.entityType2Disseminator = entityType2Disseminator;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.dspace.core.Email;
import org.dspace.eperson.EPerson;
import org.dspace.services.ConfigurationService;
import org.dspace.subscriptions.service.SubscriptionGenerator;
import org.springframework.beans.factory.annotation.Autowired;


Expand All @@ -38,19 +37,16 @@
*
* @author Alba Aliu
*/
public class StatisticsGenerator implements SubscriptionGenerator<CrisMetrics> {
public class StatisticsGenerator {
private static final Logger log = LogManager.getLogger(StatisticsGenerator.class);

@Autowired
private ConfigurationService configurationService;

@Override
public void notifyForSubscriptions(Context c, EPerson ePerson, List<CrisMetrics> crisMetricsList,
List<CrisMetrics> crisMetricsList1, List<CrisMetrics> crisMetricsList2) {
// find statistics for all the subscribed objects
public void notifyForSubscriptions(Context c, EPerson ePerson, List<CrisMetrics> crisMetricsList) {
try {
// send the notification to the user
if (Objects.nonNull(ePerson) && crisMetricsList.size() > 0) {
if (Objects.nonNull(ePerson) && !crisMetricsList.isEmpty()) {
Email email = new Email();
String name = configurationService.getProperty("dspace.name");
File attachment = generateExcel(crisMetricsList, c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
package org.dspace.subscriptions;

import java.util.Set;
import java.util.List;

import org.dspace.core.Context;
import org.dspace.scripts.handler.DSpaceRunnableHandler;
Expand All @@ -32,6 +32,6 @@ public interface SubscriptionEmailNotificationService {
/**
* returns a set of supported SubscriptionTypes
*/
public Set<String> getSupportedSubscriptionTypes();
public List<String> getSupportedSubscriptionTypes();

}
Loading

0 comments on commit 9940fcd

Please sign in to comment.