use of org.openrefine.wikidata.schema.exceptions.NewEntityNotCreatedYetException in project OpenRefine by OpenRefine.
the class ReconEntityRewriter method rewrite.
/**
* Rewrite an edit, replacing references to all entities already
* created by their fresh identifiers. The subject id might not have been
* created already, in which case it will be left untouched. All the other
* entities need to have been created already.
*
* @param edit
* the edit to rewrite
* @return
* the rewritten update
* @throws NewEntityNotCreatedYetException
* if any non-subject entity had not been created yet
*/
public TermedStatementEntityEdit rewrite(TermedStatementEntityEdit edit) throws NewEntityNotCreatedYetException {
try {
EntityIdValue subject = (EntityIdValue) copyValue(edit.getEntityId());
Set<MonolingualTextValue> labels = edit.getLabels().stream().map(l -> copy(l)).collect(Collectors.toSet());
Set<MonolingualTextValue> labelsIfNew = edit.getLabelsIfNew().stream().map(l -> copy(l)).collect(Collectors.toSet());
Set<MonolingualTextValue> descriptions = edit.getDescriptions().stream().map(l -> copy(l)).collect(Collectors.toSet());
Set<MonolingualTextValue> descriptionsIfNew = edit.getDescriptionsIfNew().stream().map(l -> copy(l)).collect(Collectors.toSet());
Set<MonolingualTextValue> aliases = edit.getAliases().stream().map(l -> copy(l)).collect(Collectors.toSet());
List<StatementEdit> addedStatements = edit.getStatementEdits().stream().map(l -> copy(l)).collect(Collectors.toList());
return new TermedStatementEntityEdit(subject, addedStatements, labels, labelsIfNew, descriptions, descriptionsIfNew, aliases);
} catch (MissingEntityIdFound e) {
throw new NewEntityNotCreatedYetException(e.value);
}
}
use of org.openrefine.wikidata.schema.exceptions.NewEntityNotCreatedYetException in project OpenRefine by OpenRefine.
the class EditBatchProcessor method performEdit.
/**
* Performs the next edit in the batch.
*
* @throws InterruptedException
*/
public void performEdit() throws InterruptedException {
if (remainingEdits() == 0) {
return;
}
if (batchCursor == currentBatch.size()) {
prepareNewBatch();
}
TermedStatementEntityEdit update = currentBatch.get(batchCursor);
// Rewrite mentions to new entities
ReconEntityRewriter rewriter = new ReconEntityRewriter(library, update.getEntityId());
try {
update = rewriter.rewrite(update);
} catch (NewEntityNotCreatedYetException e) {
logger.warn("Failed to rewrite update on entity " + update.getEntityId() + ". Missing entity: " + e.getMissingEntity() + ". Skipping update.");
batchCursor++;
return;
}
try {
// New entities
if (update.isNew()) {
ReconEntityIdValue newCell = (ReconEntityIdValue) update.getEntityId();
// TODO Antonin, 2022-02-11: remove this casting once we have https://github.com/Wikidata/Wikidata-Toolkit/issues/651
if (newCell instanceof ItemIdValue) {
update = update.normalizeLabelsAndAliases();
ItemDocument itemDocument = (ItemDocument) update.toNewEntity();
ItemDocument createdDoc = editor.createItemDocument(itemDocument, summary, tags);
library.setId(newCell.getReconInternalId(), createdDoc.getEntityId().getId());
} else if (newCell instanceof MediaInfoIdValue) {
update = update.normalizeLabelsAndAliases();
throw new NotImplementedException();
}
} else {
// Existing entities
EntityUpdate entityUpdate = update.toEntityUpdate(currentDocs.get(update.getEntityId().getId()));
editor.editEntityDocument(entityUpdate, false, summary, tags);
}
} catch (MediaWikiApiErrorException e) {
// TODO find a way to report these errors to the user in a nice way
logger.warn("MediaWiki error while editing [" + e.getErrorCode() + "]: " + e.getErrorMessage());
} catch (IOException e) {
logger.warn("IO error while editing: " + e.getMessage());
}
batchCursor++;
}
Aggregations