-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ContentGenerator.java
103 lines (91 loc) · 4.04 KB
/
ContentGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.subscriptions;
import static java.nio.charset.StandardCharsets.UTF_8;
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.Objects;
import java.util.Optional;
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;
/**
* 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> {
private final Logger log = LogManager.getLogger(ContentGenerator.class);
@SuppressWarnings("unchecked")
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) {
try {
if (Objects.nonNull(ePerson)) {
Locale supportedLocale = I18nUtil.getEPersonLocale(ePerson);
Email email = Email.getEmail(I18nUtil.getEmailFilename(supportedLocale, "subscriptions_content"));
email.addRecipient(ePerson.getEmail());
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) {
log.error(e.getMessage(), e);
log.warn("Cannot email user eperson_id: {} eperson_email: {}", ePerson::getID, ePerson::getEmail);
}
}
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));
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) {
log.error(e.getMessage(), e);
}
return EMPTY;
}
public void setEntityType2Disseminator(Map<String, StreamDisseminationCrosswalk> entityType2Disseminator) {
this.entityType2Disseminator = entityType2Disseminator;
}
}