1
|
package eu.dnetlib.data.mapreduce.hbase.broker.model;
|
2
|
|
3
|
import java.util.Date;
|
4
|
import java.util.stream.Collectors;
|
5
|
|
6
|
import org.apache.commons.codec.digest.DigestUtils;
|
7
|
|
8
|
public class EventWrapper {
|
9
|
|
10
|
private final EventMessage event;
|
11
|
|
12
|
private final String counterName;
|
13
|
|
14
|
private final String highlightValue;
|
15
|
|
16
|
public static EventWrapper newInstance(final EventMessage event, final String highlightValue, final String counterName) {
|
17
|
return new EventWrapper(event, counterName, highlightValue);
|
18
|
}
|
19
|
|
20
|
public EventWrapper(final EventMessage event, final String highlightValue, final String counterName) {
|
21
|
this.event = event;
|
22
|
this.counterName = counterName;
|
23
|
this.highlightValue = highlightValue;
|
24
|
}
|
25
|
|
26
|
public Event asBrokerEvent() {
|
27
|
|
28
|
final Date now = new Date();
|
29
|
|
30
|
final Event res = new Event();
|
31
|
|
32
|
final String eventId =
|
33
|
calculateEventId(event.getTopic(), event.getMap().get("target_publication_id").toString(), highlightValue);
|
34
|
|
35
|
res.setEventId(eventId);
|
36
|
res.setProducerId(event.getProducerId());
|
37
|
res.setPayload(event.getPayload());
|
38
|
res.setMap(event.getMap()
|
39
|
.entrySet()
|
40
|
.stream()
|
41
|
.filter(e -> e.getValue().asObject() != null)
|
42
|
.collect(Collectors.toMap(
|
43
|
e -> e.getKey(),
|
44
|
e -> e.getValue().asObject())));
|
45
|
res.setTopic(event.getTopic());
|
46
|
res.setCreationDate(now);
|
47
|
res.setExpiryDate(calculateExpiryDate(now, event.getTthDays()));
|
48
|
res.setInstantMessage(event.getTthDays() == 0);
|
49
|
return res;
|
50
|
}
|
51
|
|
52
|
private String calculateEventId(final String topic, final String publicationId, final String value) {
|
53
|
return "event-"
|
54
|
+ DigestUtils.md5Hex(topic).substring(0, 6) + "-"
|
55
|
+ DigestUtils.md5Hex(publicationId).substring(0, 8) + "-"
|
56
|
+ DigestUtils.md5Hex(value).substring(0, 8);
|
57
|
}
|
58
|
|
59
|
private Date calculateExpiryDate(final Date now, final int ttlDays) {
|
60
|
if (ttlDays < 0) {
|
61
|
return null;
|
62
|
} else if (ttlDays == 0) {
|
63
|
return now;
|
64
|
} else {
|
65
|
return new Date(now.getTime() + ttlDays * 24 * 60 * 60 * 1000);
|
66
|
}
|
67
|
}
|
68
|
|
69
|
public EventMessage getEvent() {
|
70
|
return event;
|
71
|
}
|
72
|
|
73
|
public String getCounterName() {
|
74
|
return counterName;
|
75
|
}
|
76
|
}
|