Search in sources :

Example 1 with Repository

use of org.molgenis.data.Repository 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 2 with Repository

use of org.molgenis.data.Repository in project molgenis by molgenis.

the class StyleServiceTest method addStylesWithExistingId.

@Test(expectedExceptions = MolgenisStyleException.class)
public void addStylesWithExistingId() throws IOException, MolgenisStyleException {
    String styleId = "style";
    String bs3FileName = "any";
    String bs4FileName = "any";
    InputStream bs3Data = IOUtils.toInputStream("any", "UTF-8");
    InputStream bs4Data = IOUtils.toInputStream("any", "UTF-8");
    StyleSheet styleSheet = mock(StyleSheet.class);
    Repository styleSheetRepository = mock(Repository.class);
    when(dataService.getRepository(STYLE_SHEET)).thenReturn(styleSheetRepository);
    when(styleSheetRepository.findOneById(styleId)).thenReturn(styleSheet);
    styleService.addStyle(styleId, bs3FileName, bs3Data, bs4FileName, bs4Data);
}
Also used : Repository(org.molgenis.data.Repository) InputStream(java.io.InputStream) Test(org.testng.annotations.Test)

Example 3 with Repository

use of org.molgenis.data.Repository in project molgenis by molgenis.

the class IndexBootstrapperTest method testStartupNoIndex.

@Test
public void testStartupNoIndex() {
    @SuppressWarnings("unchecked") Repository<Entity> repo1 = mock(Repository.class);
    EntityType entityType1 = mock(EntityType.class);
    when(repo1.getEntityType()).thenReturn(entityType1);
    @SuppressWarnings("unchecked") Repository<Entity> repo2 = mock(Repository.class);
    EntityType entityType2 = mock(EntityType.class);
    when(repo2.getEntityType()).thenReturn(entityType2);
    @SuppressWarnings("unchecked") Repository<Entity> repo3 = mock(Repository.class);
    EntityType entityType3 = mock(EntityType.class);
    when(repo3.getEntityType()).thenReturn(entityType3);
    List<Repository<Entity>> repos = Arrays.asList(repo1, repo2, repo3);
    when(indexService.hasIndex(attributeMetadata)).thenReturn(false);
    when(metaDataService.getRepositories()).thenReturn(repos.stream());
    indexBootstrapper.bootstrap();
    // verify that new jobs are registered for all repos
    verify(indexActionRegisterService).register(entityType1, null);
    verify(indexActionRegisterService).register(entityType2, null);
    verify(indexActionRegisterService).register(entityType3, null);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) Repository(org.molgenis.data.Repository) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 4 with Repository

use of org.molgenis.data.Repository in project molgenis by molgenis.

the class StyleServiceTest method addBootstrap3StyleOnly.

@Test
public void addBootstrap3StyleOnly() throws IOException, MolgenisStyleException {
    // setup
    String styleId = "my-style.min.css";
    String bs3FileName = "bs3FileName";
    InputStream bs3Data = IOUtils.toInputStream("bs 3 data", "UTF-8");
    Repository styleSheetRepository = mock(Repository.class);
    when(dataService.getRepository(STYLE_SHEET)).thenReturn(styleSheetRepository);
    when(styleSheetRepository.findOneById(styleId)).thenReturn(null);
    String generatedId = "my-id";
    when(idGenerator.generateId()).thenReturn(generatedId);
    StyleSheet styleSheet = mock(StyleSheet.class);
    when(styleSheet.getName()).thenReturn(styleId);
    when(styleSheetFactory.create(styleId)).thenReturn(styleSheet);
    FileMeta fileMeta = mock(FileMeta.class);
    when(fileMetaFactory.create(generatedId)).thenReturn(fileMeta);
    File storedFile = mock(File.class);
    when(storedFile.length()).thenReturn(123L);
    when(fileStore.getFile(generatedId)).thenReturn(storedFile);
    // execute
    styleService.addStyle(styleId, bs3FileName, bs3Data, null, null);
    // verify
    verify(styleSheet).setName(styleId);
    verify(dataService, times(1)).add(STYLE_SHEET, styleSheet);
    verify(fileStore, times(1)).store(bs3Data, generatedId);
    verify(dataService, times(1)).add(FileMetaMetaData.FILE_META, fileMeta);
}
Also used : Repository(org.molgenis.data.Repository) InputStream(java.io.InputStream) File(java.io.File) FileMeta(org.molgenis.data.file.model.FileMeta) Test(org.testng.annotations.Test)

Example 5 with Repository

use of org.molgenis.data.Repository in project molgenis by molgenis.

the class StyleServiceTest method addBootstrap3And4Styles.

@Test
public void addBootstrap3And4Styles() throws IOException, MolgenisStyleException {
    // setup
    String styleId = "my-style.min.css";
    String bs3FileName = "bs3FileName";
    String bs4FileName = "bs4FileName";
    InputStream bs3Data = IOUtils.toInputStream("bs 3 data", "UTF-8");
    InputStream bs4Data = IOUtils.toInputStream("bs 4 data", "UTF-8");
    Repository styleSheetRepository = mock(Repository.class);
    when(dataService.getRepository(STYLE_SHEET)).thenReturn(styleSheetRepository);
    when(styleSheetRepository.findOneById(styleId)).thenReturn(null);
    String generatedId = "my-id";
    when(idGenerator.generateId()).thenReturn(generatedId);
    StyleSheet styleSheet = mock(StyleSheet.class);
    when(styleSheet.getName()).thenReturn(styleId);
    when(styleSheetFactory.create(styleId)).thenReturn(styleSheet);
    FileMeta fileMeta = mock(FileMeta.class);
    when(fileMetaFactory.create(generatedId)).thenReturn(fileMeta);
    File storedFile = mock(File.class);
    when(storedFile.length()).thenReturn(123L);
    when(fileStore.getFile(generatedId)).thenReturn(storedFile);
    // execute
    styleService.addStyle(styleId, bs3FileName, bs3Data, bs4FileName, bs4Data);
    // verify
    verify(styleSheet).setName(styleId);
    verify(dataService, times(1)).add(STYLE_SHEET, styleSheet);
    verify(fileStore, times(1)).store(bs3Data, generatedId);
    verify(fileStore, times(1)).store(bs4Data, generatedId);
    // two times, once for each style file
    verify(dataService, times(2)).add(FileMetaMetaData.FILE_META, fileMeta);
}
Also used : Repository(org.molgenis.data.Repository) InputStream(java.io.InputStream) File(java.io.File) FileMeta(org.molgenis.data.file.model.FileMeta) Test(org.testng.annotations.Test)

Aggregations

Repository (org.molgenis.data.Repository)6 Test (org.testng.annotations.Test)4 InputStream (java.io.InputStream)3 EntityType (org.molgenis.data.meta.model.EntityType)3 File (java.io.File)2 Entity (org.molgenis.data.Entity)2 FileMeta (org.molgenis.data.file.model.FileMeta)2 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 CaffeinatedGuava (com.github.benmanes.caffeine.guava.CaffeinatedGuava)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 Maps.newConcurrentMap (com.google.common.collect.Maps.newConcurrentMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 MINUTES (java.util.concurrent.TimeUnit.MINUTES)1