Search in sources :

Example 1 with AnnotationException

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

the class AnnotationJobTest method testFirstAnnotatorFails.

@Test
public void testFirstAnnotatorFails() throws IOException {
    when(exac.getSimpleName()).thenReturn("exac");
    when(cadd.getSimpleName()).thenReturn("cadd");
    AnnotationException exception = new AnnotationException(mock(AnnotationException.class));
    doThrow(exception).when(crudRepositoryAnnotator).annotate(exac, repository);
    try {
        annotationJob.call();
        fail("Should throw exception");
    } catch (JobExecutionException actual) {
        assertEquals(actual.getCause(), exception);
    }
    Mockito.verify(progress).start();
    Mockito.verify(progress).setProgressMax(2);
    Mockito.verify(progress).progress(0, "Annotating \"My repo\" with exac (annotator 1 of 2, started by \"fdlk\")");
    Mockito.verify(progress).progress(1, "Annotating \"My repo\" with cadd (annotator 2 of 2, started by \"fdlk\")");
    Mockito.verify(progress).status("Failed annotators: exac. Successful annotators: cadd");
    Mockito.verify(progress).failed(exception);
}
Also used : JobExecutionException(org.molgenis.jobs.JobExecutionException) AnnotationException(org.molgenis.data.annotation.core.exception.AnnotationException) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 2 with AnnotationException

use of org.molgenis.data.annotation.core.exception.AnnotationException 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)

Example 3 with AnnotationException

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

the class AbstractRepositoryEntityAnnotator method annotate.

@Override
@Transactional
@RunAsSystem
public Iterator<Entity> annotate(final Iterable<Entity> sourceIterable, boolean updateMode) {
    Iterator<Entity> source = sourceIterable.iterator();
    return new Iterator<Entity>() {

        int current = 0;

        int size = 0;

        List<Entity> results;

        Entity result;

        @Override
        public boolean hasNext() {
            return current < size || source.hasNext();
        }

        @Override
        public Entity next() {
            Entity sourceEntity = null;
            if (current >= size) {
                if (source.hasNext()) {
                    try {
                        sourceEntity = source.next();
                        results = annotateEntity(sourceEntity, updateMode);
                    } catch (Exception e) {
                        throw new AnnotationException(sourceEntity, current + 1, getRequiredAttributes(), getSimpleName(), e);
                    }
                    size = results.size();
                }
                current = 0;
            }
            if (!results.isEmpty()) {
                result = results.get(current);
            } else {
                result = sourceEntity;
            }
            ++current;
            return result;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : Entity(org.molgenis.data.Entity) Iterator(java.util.Iterator) AnnotationException(org.molgenis.data.annotation.core.exception.AnnotationException) List(java.util.List) AnnotationException(org.molgenis.data.annotation.core.exception.AnnotationException) IOException(java.io.IOException) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AnnotationException (org.molgenis.data.annotation.core.exception.AnnotationException)3 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Entity (org.molgenis.data.Entity)1 Repository (org.molgenis.data.Repository)1 EffectCreatingAnnotator (org.molgenis.data.annotation.core.EffectCreatingAnnotator)1 UiAnnotationException (org.molgenis.data.annotation.core.exception.UiAnnotationException)1 EntityType (org.molgenis.data.meta.model.EntityType)1 JobExecutionException (org.molgenis.jobs.JobExecutionException)1 RunAsSystem (org.molgenis.security.core.runas.RunAsSystem)1 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)1 Transactional (org.springframework.transaction.annotation.Transactional)1 Test (org.testng.annotations.Test)1