BrBETA_dnet-hadoop/dhp-workflows/dhp-broker-events/src/main/java/eu/dnetlib/dhp/broker/model/EventFactory.java

126 lines
3.5 KiB
Java
Raw Normal View History

2020-05-08 16:49:47 +02:00
package eu.dnetlib.dhp.broker.model;
import java.text.ParseException;
import java.util.List;
2020-06-25 15:45:50 +02:00
import java.util.stream.Collectors;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
2021-08-11 12:13:22 +02:00
import eu.dnetlib.broker.objects.OaBrokerAuthor;
2020-06-22 08:51:31 +02:00
import eu.dnetlib.broker.objects.OaBrokerMainEntity;
2020-07-15 09:18:40 +02:00
import eu.dnetlib.broker.objects.OaBrokerRelatedDatasource;
2021-08-11 12:13:22 +02:00
import eu.dnetlib.broker.objects.OaBrokerTypedValue;
2020-07-15 09:18:40 +02:00
import eu.dnetlib.dhp.broker.oa.util.BrokerConstants;
import eu.dnetlib.dhp.broker.oa.util.UpdateInfo;
public class EventFactory {
2021-08-11 12:13:22 +02:00
private static final String PRODUCER_ID = "OpenAIRE";
2021-08-11 12:13:22 +02:00
private static final String[] DATE_PATTERNS = {
"yyyy-MM-dd"
};
2021-08-11 12:13:22 +02:00
private EventFactory() {
}
2020-05-13 12:00:27 +02:00
public static Event newBrokerEvent(final UpdateInfo<?> updateInfo) {
final Event res = new Event();
2020-06-25 15:45:50 +02:00
final MappedFields map = createMapFromResult(updateInfo);
2020-06-08 08:32:22 +02:00
final String eventId = calculateEventId(
2020-07-18 09:40:36 +02:00
updateInfo.getTopicPath(), updateInfo.getTargetDs().getOpenaireId(), updateInfo
.getTarget()
.getOpenaireId(),
updateInfo.getHighlightValueAsString());
res.setEventId(eventId);
res.setProducerId(PRODUCER_ID);
res.setPayload(updateInfo.asBrokerPayload().toJSON());
res.setMap(map);
2020-05-13 12:00:27 +02:00
res.setTopic(updateInfo.getTopicPath());
2020-08-19 12:39:22 +02:00
res.setCreationDate(0l);
res.setExpiryDate(Long.MAX_VALUE);
res.setInstantMessage(false);
2020-07-02 12:43:03 +02:00
return res;
}
2020-06-25 15:45:50 +02:00
private static MappedFields createMapFromResult(final UpdateInfo<?> updateInfo) {
final MappedFields map = new MappedFields();
2020-06-22 08:51:31 +02:00
final OaBrokerMainEntity source = updateInfo.getSource();
final OaBrokerMainEntity target = updateInfo.getTarget();
2020-05-13 12:00:27 +02:00
2020-07-15 09:18:40 +02:00
final OaBrokerRelatedDatasource targetDs = updateInfo.getTargetDs();
map.setTargetDatasourceId(targetDs.getOpenaireId());
map.setTargetDatasourceName(targetDs.getName());
map.setTargetDatasourceType(targetDs.getType());
2020-06-25 15:45:50 +02:00
map.setTargetResultId(target.getOpenaireId());
2020-06-16 12:34:13 +02:00
final List<String> titles = target.getTitles();
2021-08-11 12:13:22 +02:00
if (!titles.isEmpty()) {
2020-06-25 15:45:50 +02:00
map.setTargetResultTitle(titles.get(0));
}
2020-06-16 12:34:13 +02:00
final long date = parseDateTolong(target.getPublicationdate());
if (date > 0) {
2020-06-25 15:45:50 +02:00
map.setTargetDateofacceptance(date);
}
2021-08-11 12:13:22 +02:00
map
.setTargetSubjects(
target.getSubjects().stream().map(OaBrokerTypedValue::getValue).collect(Collectors.toList()));
map
.setTargetAuthors(
target.getCreators().stream().map(OaBrokerAuthor::getFullname).collect(Collectors.toList()));
// PROVENANCE INFO
2020-06-25 15:45:50 +02:00
map.setTrust(updateInfo.getTrust());
map.setProvenanceResultId(source.getOpenaireId());
2020-07-15 09:18:40 +02:00
source
.getDatasources()
.stream()
.filter(ds -> ds.getRelType().equals(BrokerConstants.COLLECTED_FROM_REL))
.findFirst()
.ifPresent(ds -> {
map.setProvenanceDatasourceId(ds.getOpenaireId());
map.setProvenanceDatasourceName(ds.getName());
map.setProvenanceDatasourceType(ds.getType());
});
return map;
}
2020-08-19 12:39:22 +02:00
private static String calculateEventId(final String topic,
final String dsId,
final String publicationId,
2020-07-18 09:40:36 +02:00
final String value) {
return "event-"
2020-07-18 09:40:36 +02:00
+ DigestUtils.md5Hex(topic).substring(0, 4) + "-"
+ DigestUtils.md5Hex(dsId).substring(0, 4) + "-"
+ DigestUtils.md5Hex(publicationId).substring(0, 7) + "-"
+ DigestUtils.md5Hex(value).substring(0, 5);
}
private static long parseDateTolong(final String date) {
2020-06-08 08:32:22 +02:00
if (StringUtils.isBlank(date)) {
return -1;
}
try {
return DateUtils.parseDate(date, DATE_PATTERNS).getTime();
} catch (final ParseException e) {
return -1;
}
}
}