2020-04-30 11:05:17 +02:00
|
|
|
|
2020-05-11 17:32:06 +02:00
|
|
|
package eu.dnetlib.dhp.bulktag.community;
|
2020-04-21 16:03:51 +02:00
|
|
|
|
2020-05-11 17:38:08 +02:00
|
|
|
import static eu.dnetlib.dhp.bulktag.community.TaggingConstants.*;
|
|
|
|
import static eu.dnetlib.dhp.schema.common.ModelConstants.*;
|
2020-04-21 16:03:51 +02:00
|
|
|
|
|
|
|
import java.io.Serializable;
|
2020-03-03 16:38:50 +01:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Stream;
|
2020-04-30 11:05:17 +02:00
|
|
|
|
2020-05-11 17:38:08 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.jayway.jsonpath.DocumentContext;
|
|
|
|
import com.jayway.jsonpath.JsonPath;
|
|
|
|
|
|
|
|
import eu.dnetlib.dhp.schema.oaf.*;
|
2020-04-30 11:05:17 +02:00
|
|
|
|
2020-04-21 16:03:51 +02:00
|
|
|
/** Created by miriam on 02/08/2018. */
|
|
|
|
public class ResultTagger implements Serializable {
|
2020-03-03 16:38:50 +01:00
|
|
|
|
2020-04-30 11:05:17 +02:00
|
|
|
private boolean clearContext(Result result) {
|
|
|
|
int tmp = result.getContext().size();
|
|
|
|
List<Context> clist = result
|
|
|
|
.getContext()
|
|
|
|
.stream()
|
|
|
|
.filter(c -> (!c.getId().contains(ZENODO_COMMUNITY_INDICATOR)))
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
result.setContext(clist);
|
|
|
|
return (tmp != clist.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, List<String>> getParamMap(final Result result, Map<String, String> params) {
|
|
|
|
Map<String, List<String>> param = new HashMap<>();
|
|
|
|
String json = new Gson().toJson(result, Result.class);
|
|
|
|
DocumentContext jsonContext = JsonPath.parse(json);
|
|
|
|
if (params == null) {
|
|
|
|
params = new HashMap<>();
|
|
|
|
}
|
|
|
|
for (String key : params.keySet()) {
|
|
|
|
try {
|
|
|
|
param.put(key, jsonContext.read(params.get(key)));
|
|
|
|
} catch (com.jayway.jsonpath.PathNotFoundException e) {
|
|
|
|
param.put(key, new ArrayList<>());
|
|
|
|
// throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return param;
|
|
|
|
}
|
|
|
|
|
|
|
|
public <R extends Result> R enrichContextCriteria(
|
2020-05-11 17:38:08 +02:00
|
|
|
final R result, final CommunityConfiguration conf, final Map<String, String> criteria) {
|
2020-04-30 11:05:17 +02:00
|
|
|
|
|
|
|
// }
|
|
|
|
// public Result enrichContextCriteria(final Result result, final CommunityConfiguration
|
|
|
|
// conf, final Map<String,String> criteria) {
|
|
|
|
final Map<String, List<String>> param = getParamMap(result, criteria);
|
|
|
|
|
|
|
|
// Verify if the entity is deletedbyinference. In case verify if to clean the context list
|
|
|
|
// from all the zenodo communities
|
|
|
|
if (result.getDataInfo().getDeletedbyinference()) {
|
|
|
|
clearContext(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// communities contains all the communities to be added as context for the result
|
|
|
|
final Set<String> communities = new HashSet<>();
|
|
|
|
|
|
|
|
// tagging for Subject
|
|
|
|
final Set<String> subjects = new HashSet<>();
|
2020-07-09 14:05:21 +02:00
|
|
|
|
2020-07-17 14:03:23 +02:00
|
|
|
if (Objects.nonNull(result.getSubject())) {
|
|
|
|
result
|
|
|
|
.getSubject()
|
2020-04-30 11:05:17 +02:00
|
|
|
.stream()
|
|
|
|
.map(subject -> subject.getValue())
|
|
|
|
.filter(StringUtils::isNotBlank)
|
|
|
|
.map(String::toLowerCase)
|
|
|
|
.map(String::trim)
|
|
|
|
.collect(Collectors.toCollection(HashSet::new))
|
|
|
|
.forEach(s -> subjects.addAll(conf.getCommunityForSubjectValue(s)));
|
|
|
|
}
|
|
|
|
|
|
|
|
communities.addAll(subjects);
|
|
|
|
|
|
|
|
// Tagging for datasource
|
|
|
|
final Set<String> datasources = new HashSet<>();
|
|
|
|
final Set<String> tmp = new HashSet<>();
|
|
|
|
|
2020-07-09 14:05:21 +02:00
|
|
|
if (Objects.nonNull(result.getInstance())) {
|
|
|
|
for (Instance i : result.getInstance()) {
|
2020-07-17 14:03:23 +02:00
|
|
|
if (Objects.nonNull(i.getCollectedfrom())) {
|
|
|
|
if (Objects.nonNull(i.getCollectedfrom().getKey())) {
|
2020-07-09 14:05:21 +02:00
|
|
|
tmp.add(StringUtils.substringAfter(i.getCollectedfrom().getKey(), "|"));
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 14:03:23 +02:00
|
|
|
if (Objects.nonNull(i.getHostedby())) {
|
|
|
|
if (Objects.nonNull(i.getHostedby().getKey())) {
|
2020-07-09 14:05:21 +02:00
|
|
|
tmp.add(StringUtils.substringAfter(i.getHostedby().getKey(), "|"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 11:05:17 +02:00
|
|
|
}
|
|
|
|
|
2020-07-09 14:05:21 +02:00
|
|
|
result
|
|
|
|
.getInstance()
|
2020-04-30 11:05:17 +02:00
|
|
|
.stream()
|
|
|
|
.map(i -> new Pair<>(i.getCollectedfrom().getKey(), i.getHostedby().getKey()))
|
|
|
|
.flatMap(p -> Stream.of(p.getFst(), p.getSnd()))
|
|
|
|
.map(s -> StringUtils.substringAfter(s, "|"))
|
|
|
|
.collect(Collectors.toCollection(HashSet::new))
|
|
|
|
.forEach(
|
|
|
|
dsId -> datasources
|
|
|
|
.addAll(
|
|
|
|
conf.getCommunityForDatasource(dsId, param)));
|
|
|
|
}
|
|
|
|
|
|
|
|
communities.addAll(datasources);
|
|
|
|
|
|
|
|
/* Tagging for Zenodo Communities */
|
|
|
|
final Set<String> czenodo = new HashSet<>();
|
|
|
|
|
|
|
|
Optional<List<Context>> oresultcontext = Optional.ofNullable(result.getContext());
|
|
|
|
if (oresultcontext.isPresent()) {
|
|
|
|
oresultcontext
|
|
|
|
.get()
|
|
|
|
.stream()
|
|
|
|
.filter(c -> c.getId().contains(ZENODO_COMMUNITY_INDICATOR))
|
|
|
|
.collect(Collectors.toList())
|
|
|
|
.forEach(
|
|
|
|
c -> czenodo
|
|
|
|
.addAll(
|
|
|
|
conf
|
|
|
|
.getCommunityForZenodoCommunityValue(
|
|
|
|
c
|
|
|
|
.getId()
|
|
|
|
.substring(
|
|
|
|
c.getId().lastIndexOf("/") + 1)
|
|
|
|
.trim())));
|
|
|
|
}
|
|
|
|
|
|
|
|
communities.addAll(czenodo);
|
|
|
|
|
|
|
|
clearContext(result);
|
|
|
|
|
|
|
|
/* Verify if there is something to bulktag */
|
|
|
|
if (communities.isEmpty()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
.getContext()
|
|
|
|
.stream()
|
|
|
|
.map(
|
|
|
|
c -> {
|
|
|
|
if (communities.contains(c.getId())) {
|
|
|
|
Optional<List<DataInfo>> opt_dataInfoList = Optional.ofNullable(c.getDataInfo());
|
|
|
|
List<DataInfo> dataInfoList;
|
|
|
|
if (opt_dataInfoList.isPresent())
|
|
|
|
dataInfoList = opt_dataInfoList.get();
|
|
|
|
else {
|
|
|
|
dataInfoList = new ArrayList<>();
|
|
|
|
c.setDataInfo(dataInfoList);
|
|
|
|
}
|
|
|
|
if (subjects.contains(c.getId()))
|
|
|
|
dataInfoList
|
|
|
|
.add(
|
|
|
|
getDataInfo(
|
|
|
|
BULKTAG_DATA_INFO_TYPE,
|
|
|
|
CLASS_ID_SUBJECT,
|
2020-08-03 18:32:17 +02:00
|
|
|
CLASS_NAME_BULKTAG_SUBJECT,
|
2020-08-04 10:13:25 +02:00
|
|
|
TAGGING_TRUST));
|
2020-04-30 11:05:17 +02:00
|
|
|
if (datasources.contains(c.getId()))
|
|
|
|
dataInfoList
|
|
|
|
.add(
|
|
|
|
getDataInfo(
|
|
|
|
BULKTAG_DATA_INFO_TYPE,
|
|
|
|
CLASS_ID_DATASOURCE,
|
2020-08-03 18:32:17 +02:00
|
|
|
CLASS_NAME_BULKTAG_DATASOURCE,
|
2020-08-04 10:13:25 +02:00
|
|
|
TAGGING_TRUST));
|
2020-04-30 11:05:17 +02:00
|
|
|
if (czenodo.contains(c.getId()))
|
|
|
|
dataInfoList
|
|
|
|
.add(
|
|
|
|
getDataInfo(
|
|
|
|
BULKTAG_DATA_INFO_TYPE,
|
|
|
|
CLASS_ID_CZENODO,
|
2020-08-03 18:32:17 +02:00
|
|
|
CLASS_NAME_BULKTAG_ZENODO,
|
2020-08-04 10:13:25 +02:00
|
|
|
TAGGING_TRUST));
|
2020-04-30 11:05:17 +02:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
})
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
communities
|
|
|
|
.removeAll(
|
|
|
|
result.getContext().stream().map(c -> c.getId()).collect(Collectors.toSet()));
|
|
|
|
|
|
|
|
if (communities.isEmpty())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
List<Context> toaddcontext = communities
|
|
|
|
.stream()
|
|
|
|
.map(
|
|
|
|
c -> {
|
|
|
|
Context context = new Context();
|
|
|
|
context.setId(c);
|
|
|
|
List<DataInfo> dataInfoList = new ArrayList<>();
|
|
|
|
if (subjects.contains(c))
|
|
|
|
dataInfoList
|
|
|
|
.add(
|
|
|
|
getDataInfo(
|
|
|
|
BULKTAG_DATA_INFO_TYPE,
|
|
|
|
CLASS_ID_SUBJECT,
|
2020-08-03 18:32:17 +02:00
|
|
|
CLASS_NAME_BULKTAG_SUBJECT,
|
2020-08-04 10:13:25 +02:00
|
|
|
TAGGING_TRUST));
|
2020-04-30 11:05:17 +02:00
|
|
|
if (datasources.contains(c))
|
|
|
|
dataInfoList
|
|
|
|
.add(
|
|
|
|
getDataInfo(
|
|
|
|
BULKTAG_DATA_INFO_TYPE,
|
|
|
|
CLASS_ID_DATASOURCE,
|
2020-08-03 18:32:17 +02:00
|
|
|
CLASS_NAME_BULKTAG_DATASOURCE,
|
2020-08-04 10:13:25 +02:00
|
|
|
TAGGING_TRUST));
|
2020-04-30 11:05:17 +02:00
|
|
|
if (czenodo.contains(c))
|
|
|
|
dataInfoList
|
|
|
|
.add(
|
|
|
|
getDataInfo(
|
|
|
|
BULKTAG_DATA_INFO_TYPE,
|
|
|
|
CLASS_ID_CZENODO,
|
2020-08-03 18:32:17 +02:00
|
|
|
CLASS_NAME_BULKTAG_ZENODO,
|
2020-08-04 10:13:25 +02:00
|
|
|
TAGGING_TRUST));
|
2020-04-30 11:05:17 +02:00
|
|
|
context.setDataInfo(dataInfoList);
|
|
|
|
return context;
|
|
|
|
})
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
result.getContext().addAll(toaddcontext);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DataInfo getDataInfo(
|
2020-08-03 18:32:17 +02:00
|
|
|
String inference_provenance, String inference_class_id, String inference_class_name, String trust) {
|
2020-04-30 11:05:17 +02:00
|
|
|
DataInfo di = new DataInfo();
|
|
|
|
di.setInferred(true);
|
|
|
|
di.setInferenceprovenance(inference_provenance);
|
|
|
|
di.setProvenanceaction(getQualifier(inference_class_id, inference_class_name));
|
2020-08-03 18:32:17 +02:00
|
|
|
di.setTrust(trust);
|
2020-04-30 11:05:17 +02:00
|
|
|
return di;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Qualifier getQualifier(String inference_class_id, String inference_class_name) {
|
|
|
|
Qualifier pa = new Qualifier();
|
|
|
|
pa.setClassid(inference_class_id);
|
|
|
|
pa.setClassname(inference_class_name);
|
2020-05-11 17:32:06 +02:00
|
|
|
pa.setSchemeid(DNET_PROVENANCE_ACTIONS);
|
|
|
|
pa.setSchemename(DNET_PROVENANCE_ACTIONS);
|
2020-04-30 11:05:17 +02:00
|
|
|
return pa;
|
|
|
|
}
|
2020-04-21 16:03:51 +02:00
|
|
|
}
|