Search in sources :

Example 1 with NewEntityNotCreatedYetException

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);
    }
}
Also used : NewEntityNotCreatedYetException(org.openrefine.wikidata.schema.exceptions.NewEntityNotCreatedYetException) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) DataObjectFactoryImpl(org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl) ReconMediaInfoIdValue(org.openrefine.wikidata.schema.entityvalues.ReconMediaInfoIdValue) PropertyIdValue(org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue) Set(java.util.Set) ReconPropertyIdValue(org.openrefine.wikidata.schema.entityvalues.ReconPropertyIdValue) Statement(org.wikidata.wdtk.datamodel.interfaces.Statement) Collectors(java.util.stream.Collectors) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) StatementUpdate(org.wikidata.wdtk.datamodel.interfaces.StatementUpdate) List(java.util.List) StatementEdit(org.openrefine.wikidata.updates.StatementEdit) ReconItemIdValue(org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue) ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) Datamodel(org.wikidata.wdtk.datamodel.helpers.Datamodel) DatamodelConverter(org.wikidata.wdtk.datamodel.helpers.DatamodelConverter) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) NewEntityNotCreatedYetException(org.openrefine.wikidata.schema.exceptions.NewEntityNotCreatedYetException) EntityIdValue(org.wikidata.wdtk.datamodel.interfaces.EntityIdValue) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) MonolingualTextValue(org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue) StatementEdit(org.openrefine.wikidata.updates.StatementEdit)

Example 2 with NewEntityNotCreatedYetException

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++;
}
Also used : ItemIdValue(org.wikidata.wdtk.datamodel.interfaces.ItemIdValue) EntityUpdate(org.wikidata.wdtk.datamodel.interfaces.EntityUpdate) ItemDocument(org.wikidata.wdtk.datamodel.interfaces.ItemDocument) NewEntityNotCreatedYetException(org.openrefine.wikidata.schema.exceptions.NewEntityNotCreatedYetException) MediaInfoIdValue(org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue) ReconEntityIdValue(org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue) NotImplementedException(org.apache.commons.lang.NotImplementedException) TermedStatementEntityEdit(org.openrefine.wikidata.updates.TermedStatementEntityEdit) IOException(java.io.IOException) MediaWikiApiErrorException(org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException)

Aggregations

NewEntityNotCreatedYetException (org.openrefine.wikidata.schema.exceptions.NewEntityNotCreatedYetException)2 TermedStatementEntityEdit (org.openrefine.wikidata.updates.TermedStatementEntityEdit)2 ItemIdValue (org.wikidata.wdtk.datamodel.interfaces.ItemIdValue)2 MediaInfoIdValue (org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue)2 IOException (java.io.IOException)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 NotImplementedException (org.apache.commons.lang.NotImplementedException)1 ReconEntityIdValue (org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue)1 ReconItemIdValue (org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue)1 ReconMediaInfoIdValue (org.openrefine.wikidata.schema.entityvalues.ReconMediaInfoIdValue)1 ReconPropertyIdValue (org.openrefine.wikidata.schema.entityvalues.ReconPropertyIdValue)1 StatementEdit (org.openrefine.wikidata.updates.StatementEdit)1 Datamodel (org.wikidata.wdtk.datamodel.helpers.Datamodel)1 DatamodelConverter (org.wikidata.wdtk.datamodel.helpers.DatamodelConverter)1 DataObjectFactoryImpl (org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl)1 EntityIdValue (org.wikidata.wdtk.datamodel.interfaces.EntityIdValue)1 EntityUpdate (org.wikidata.wdtk.datamodel.interfaces.EntityUpdate)1 ItemDocument (org.wikidata.wdtk.datamodel.interfaces.ItemDocument)1