Search in sources :

Example 1 with UiAnnotationException

use of org.molgenis.data.annotation.core.exception.UiAnnotationException in project molgenis by molgenis.

the class CrudRepositoryAnnotator method annotate.

private void annotate(RepositoryAnnotator annotator, Repository<Entity> repository, DatabaseAction action) {
    if (!repository.getCapabilities().contains(WRITABLE)) {
        throw new UnsupportedOperationException("Currently only writable repositories can be annotated");
    }
    try {
        EntityType entityType = dataService.getMeta().getEntityType(repository.getName());
        if (annotator instanceof EffectCreatingAnnotator) {
            targetMetaData = ((EffectCreatingAnnotator) annotator).getTargetEntityType(entityType);
            if (!dataService.hasRepository(targetMetaData.getId())) {
                // add new entities to new repo
                Repository externalRepository = dataService.getMeta().createRepository(targetMetaData);
                permissionSystemService.giveUserWriteMetaPermissions(targetMetaData);
                runAsSystem(() -> dataService.getMeta().updateEntityType(externalRepository.getEntityType()));
                iterateOverEntitiesAndAnnotate(repository, annotator, DatabaseAction.ADD);
            } else {
                throw new UnsupportedOperationException("This entity has already been annotated with " + annotator.getSimpleName());
            }
        } else {
            runAsSystem(() -> dataService.getMeta().updateEntityType(addAnnotatorMetaDataToRepositories(entityType, attributeFactory, annotator)));
            iterateOverEntitiesAndAnnotate(dataService.getRepository(repository.getName()), annotator, action);
        }
    } catch (AnnotationException ae) {
        deleteResultEntity(annotator, targetMetaData);
        throw new UiAnnotationException(ae);
    } catch (Exception e) {
        deleteResultEntity(annotator, targetMetaData);
        throw new RuntimeException(e);
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Repository(org.molgenis.data.Repository) EffectCreatingAnnotator(org.molgenis.data.annotation.core.EffectCreatingAnnotator) UiAnnotationException(org.molgenis.data.annotation.core.exception.UiAnnotationException) UiAnnotationException(org.molgenis.data.annotation.core.exception.UiAnnotationException) AnnotationException(org.molgenis.data.annotation.core.exception.AnnotationException) UiAnnotationException(org.molgenis.data.annotation.core.exception.UiAnnotationException) AnnotationException(org.molgenis.data.annotation.core.exception.AnnotationException)

Aggregations

Repository (org.molgenis.data.Repository)1 EffectCreatingAnnotator (org.molgenis.data.annotation.core.EffectCreatingAnnotator)1 AnnotationException (org.molgenis.data.annotation.core.exception.AnnotationException)1 UiAnnotationException (org.molgenis.data.annotation.core.exception.UiAnnotationException)1 EntityType (org.molgenis.data.meta.model.EntityType)1