dnet-hadoop/dhp-workflows/dhp-broker-events/src/main/java/eu/dnetlib/dhp/broker/oa/util/UpdateInfo.java

122 lines
3.0 KiB
Java
Raw Normal View History

2020-05-08 16:49:47 +02:00
package eu.dnetlib.dhp.broker.oa.util;
2020-05-13 12:00:27 +02:00
import java.util.function.BiConsumer;
import java.util.function.Function;
2020-06-22 08:51:31 +02:00
import eu.dnetlib.broker.objects.OaBrokerEventPayload;
import eu.dnetlib.broker.objects.OaBrokerInstance;
import eu.dnetlib.broker.objects.OaBrokerMainEntity;
import eu.dnetlib.broker.objects.OaBrokerProvenance;
2020-07-15 09:18:40 +02:00
import eu.dnetlib.broker.objects.OaBrokerRelatedDatasource;
2020-05-13 12:00:27 +02:00
import eu.dnetlib.dhp.broker.model.Topic;
2020-05-13 12:00:27 +02:00
public final class UpdateInfo<T> {
2020-05-13 12:00:27 +02:00
private final Topic topic;
private final T highlightValue;
2020-06-22 08:51:31 +02:00
private final OaBrokerMainEntity source;
2020-05-13 12:00:27 +02:00
2020-06-22 08:51:31 +02:00
private final OaBrokerMainEntity target;
2020-05-13 12:00:27 +02:00
2020-07-15 09:18:40 +02:00
private final OaBrokerRelatedDatasource targetDs;
2020-06-22 08:51:31 +02:00
private final BiConsumer<OaBrokerMainEntity, T> compileHighlight;
2020-05-13 12:00:27 +02:00
private final Function<T, String> highlightToString;
private final float trust;
2020-06-22 08:51:31 +02:00
public UpdateInfo(final Topic topic, final T highlightValue, final OaBrokerMainEntity source,
final OaBrokerMainEntity target,
2020-07-15 09:18:40 +02:00
final OaBrokerRelatedDatasource targetDs,
2020-06-22 08:51:31 +02:00
final BiConsumer<OaBrokerMainEntity, T> compileHighlight,
2020-07-09 12:53:46 +02:00
final Function<T, String> highlightToString) {
this.topic = topic;
this.highlightValue = highlightValue;
2020-05-13 12:00:27 +02:00
this.source = source;
this.target = target;
2020-07-15 09:18:40 +02:00
this.targetDs = targetDs;
2020-05-13 12:00:27 +02:00
this.compileHighlight = compileHighlight;
this.highlightToString = highlightToString;
2020-07-09 12:53:46 +02:00
this.trust = TrustUtils.calculateTrust(source, target);
}
public T getHighlightValue() {
return highlightValue;
}
2020-06-22 08:51:31 +02:00
public OaBrokerMainEntity getSource() {
2020-05-13 12:00:27 +02:00
return source;
}
2020-06-22 08:51:31 +02:00
public OaBrokerMainEntity getTarget() {
2020-05-13 12:00:27 +02:00
return target;
}
2020-07-15 09:18:40 +02:00
public OaBrokerRelatedDatasource getTargetDs() {
return targetDs;
}
2020-05-13 12:00:27 +02:00
protected Topic getTopic() {
return topic;
}
2020-05-13 12:00:27 +02:00
public String getTopicPath() {
return topic.getPath();
}
2020-05-13 12:00:27 +02:00
public float getTrust() {
return trust;
}
public String getHighlightValueAsString() {
return highlightToString.apply(getHighlightValue());
}
2020-06-22 08:51:31 +02:00
public OaBrokerEventPayload asBrokerPayload() {
2020-06-16 12:34:13 +02:00
compileHighlight.accept(target, getHighlightValue());
2020-06-22 08:51:31 +02:00
final OaBrokerMainEntity hl = new OaBrokerMainEntity();
compileHighlight.accept(hl, getHighlightValue());
2020-06-25 13:01:09 +02:00
final String provId = getSource().getOpenaireId();
2020-07-15 09:18:40 +02:00
final String provRepo = getSource()
.getDatasources()
.stream()
.filter(ds -> ds.getRelType().equals(BrokerConstants.COLLECTED_FROM_REL))
.map(ds -> ds.getName())
.findFirst()
.orElse("");
final String provType = getSource()
.getDatasources()
.stream()
.filter(ds -> ds.getRelType().equals(BrokerConstants.COLLECTED_FROM_REL))
.map(ds -> ds.getType())
.findFirst()
.orElse("");
2020-06-16 12:34:13 +02:00
2020-06-08 08:32:22 +02:00
final String provUrl = getSource()
2020-06-16 12:34:13 +02:00
.getInstances()
2020-06-08 08:32:22 +02:00
.stream()
2020-06-22 08:51:31 +02:00
.map(OaBrokerInstance::getUrl)
2020-06-08 08:32:22 +02:00
.findFirst()
2020-06-10 12:11:16 +02:00
.orElse(null);
;
2020-07-02 12:43:03 +02:00
final OaBrokerProvenance provenance = new OaBrokerProvenance(provId, provRepo, provType, provUrl);
2020-06-22 08:51:31 +02:00
final OaBrokerEventPayload res = new OaBrokerEventPayload();
2020-06-19 12:33:29 +02:00
res.setResult(target);
res.setHighlight(hl);
res.setTrust(trust);
res.setProvenance(provenance);
return res;
}
}