use of org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue in project OpenRefine by OpenRefine.
the class TermedStatementEntityEdit method toEntityUpdate.
/**
* In case the subject id is not new, returns the corresponding update given
* the current state of the entity.
*/
public EntityUpdate toEntityUpdate(EntityDocument entityDocument) {
Validate.isFalse(isNew(), "Cannot create a corresponding entity update for a creation of a new entity.");
if (id instanceof ItemIdValue) {
ItemDocument itemDocument = (ItemDocument) entityDocument;
// Labels
List<MonolingualTextValue> labels = getLabels().stream().collect(Collectors.toList());
labels.addAll(getLabelsIfNew().stream().filter(label -> !itemDocument.getLabels().containsKey(label.getLanguageCode())).collect(Collectors.toList()));
TermUpdate labelUpdate = Datamodel.makeTermUpdate(labels, Collections.emptyList());
// Descriptions
List<MonolingualTextValue> descriptions = getDescriptions().stream().collect(Collectors.toList());
descriptions.addAll(getDescriptionsIfNew().stream().filter(desc -> !itemDocument.getDescriptions().containsKey(desc.getLanguageCode())).collect(Collectors.toList()));
TermUpdate descriptionUpdate = Datamodel.makeTermUpdate(descriptions, Collections.emptyList());
// Aliases
Set<MonolingualTextValue> aliases = getAliases();
Map<String, List<MonolingualTextValue>> aliasesMap = aliases.stream().collect(Collectors.groupingBy(MonolingualTextValue::getLanguageCode));
Map<String, AliasUpdate> aliasMap = aliasesMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> Datamodel.makeAliasUpdate(e.getValue(), Collections.emptyList())));
// Statements
StatementUpdate statementUpdate = toStatementUpdate(itemDocument);
return Datamodel.makeItemUpdate((ItemIdValue) getEntityId(), entityDocument.getRevisionId(), labelUpdate, descriptionUpdate, aliasMap, statementUpdate, Collections.emptyList(), Collections.emptyList());
} else if (id instanceof MediaInfoIdValue) {
MediaInfoDocument mediaInfoDocument = (MediaInfoDocument) entityDocument;
// Labels (captions)
List<MonolingualTextValue> labels = getLabels().stream().collect(Collectors.toList());
labels.addAll(getLabelsIfNew().stream().filter(label -> !mediaInfoDocument.getLabels().containsKey(label.getLanguageCode())).collect(Collectors.toList()));
TermUpdate labelUpdate = Datamodel.makeTermUpdate(labels, Collections.emptyList());
// Statements
StatementUpdate statementUpdate = toStatementUpdate(mediaInfoDocument);
return Datamodel.makeMediaInfoUpdate((MediaInfoIdValue) id, entityDocument.getRevisionId(), labelUpdate, statementUpdate);
} else {
throw new NotImplementedException("Editing entities of type " + id.getEntityType() + " is not supported yet.");
}
}
use of org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue in project OpenRefine by OpenRefine.
the class TermedStatementEntityEdit method normalizeLabelsAndAliases.
/**
* This should only be used when creating a new entity. This ensures that we never
* add an alias without adding a label in the same language.
*/
public TermedStatementEntityEdit normalizeLabelsAndAliases() {
// Ensure that we are only adding aliases with labels
Set<MonolingualTextValue> filteredAliases = new HashSet<>();
Map<String, MonolingualTextValue> newLabels = new HashMap<>(labelsIfNew);
newLabels.putAll(labels);
for (MonolingualTextValue alias : getAliases()) {
if (!newLabels.containsKey(alias.getLanguageCode())) {
newLabels.put(alias.getLanguageCode(), alias);
} else {
filteredAliases.add(alias);
}
}
Map<String, MonolingualTextValue> newDescriptions = new HashMap<>(descriptionsIfNew);
newDescriptions.putAll(descriptions);
return new TermedStatementEntityEdit(id, statements, newLabels, Collections.emptyMap(), newDescriptions, Collections.emptyMap(), constructTermListMap(filteredAliases));
}
use of org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue in project OpenRefine by OpenRefine.
the class TermedStatementEntityEdit method merge.
/**
* Merges all the changes in other with this instance. Both updates should have
* the same subject. Changes coming from `other` have priority over changes
* from this instance. This instance is not modified, the merged update is returned
* instead.
*
* @param other
* the other change that should be merged
*/
public TermedStatementEntityEdit merge(TermedStatementEntityEdit other) {
Validate.isTrue(id.equals(other.getEntityId()));
List<StatementEdit> newStatements = new ArrayList<>(statements);
for (StatementEdit statement : other.getStatementEdits()) {
if (!newStatements.contains(statement)) {
newStatements.add(statement);
}
}
Map<String, MonolingualTextValue> newLabels = new HashMap<>(labels);
Map<String, MonolingualTextValue> newLabelsIfNew = new HashMap<>(labelsIfNew);
mergeSingleTermMaps(newLabels, newLabelsIfNew, other.getLabels(), other.getLabelsIfNew());
Map<String, MonolingualTextValue> newDescriptions = new HashMap<>(descriptions);
Map<String, MonolingualTextValue> newDescriptionsIfNew = new HashMap<>(descriptionsIfNew);
mergeSingleTermMaps(newDescriptions, newDescriptionsIfNew, other.getDescriptions(), other.getDescriptionsIfNew());
Map<String, List<MonolingualTextValue>> newAliases = new HashMap<>(aliases);
for (MonolingualTextValue alias : other.getAliases()) {
List<MonolingualTextValue> aliases = newAliases.get(alias.getLanguageCode());
if (aliases == null) {
aliases = new LinkedList<>();
newAliases.put(alias.getLanguageCode(), aliases);
}
if (!aliases.contains(alias)) {
aliases.add(alias);
}
}
return new TermedStatementEntityEdit(id, newStatements, newLabels, newLabelsIfNew, newDescriptions, newDescriptionsIfNew, newAliases);
}
Aggregations