Search in sources :

Example 41 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class EmxMetaDataParser method parseEntitiesSheet.

/**
 * Load all entities (optional)
 *
 * @param entitiesRepo        the Repository for the entities
 * @param intermediateResults {@link IntermediateParseResults} containing the attributes already parsed
 */
private void parseEntitiesSheet(Repository<Entity> entitiesRepo, IntermediateParseResults intermediateResults) {
    if (entitiesRepo != null) {
        for (Attribute attr : entitiesRepo.getEntityType().getAtomicAttributes()) {
            if (!EMX_ENTITIES_ALLOWED_ATTRS.contains(attr.getName().toLowerCase()) && !(isI18n(attr.getName()) && (attr.getName().startsWith(EMX_ENTITIES_DESCRIPTION) || attr.getName().startsWith(EMX_ENTITIES_LABEL)))) {
                throw new IllegalArgumentException("Unsupported entity metadata: entities." + attr.getName());
            }
        }
        int i = 1;
        for (Entity entity : entitiesRepo) {
            i++;
            String emxEntityName = entity.getString(EMX_ENTITIES_NAME);
            String emxEntityPackage = entity.getString(EMX_ENTITIES_PACKAGE);
            String emxEntityLabel = entity.getString(EMX_ENTITIES_LABEL);
            String emxEntityDescription = entity.getString(EMX_ENTITIES_DESCRIPTION);
            String emxEntityAbstract = entity.getString(EMX_ENTITIES_ABSTRACT);
            String emxEntityExtends = entity.getString(EMX_ENTITIES_EXTENDS);
            String emxEntityBackend = entity.getString(EMX_ENTITIES_BACKEND);
            String emxEntityTags = entity.getString(EMX_ENTITIES_TAGS);
            // required
            if (emxEntityName == null) {
                throw new IllegalArgumentException("entity.name is missing on line " + i);
            }
            String entityTypeId;
            if (emxEntityPackage != null) {
                entityTypeId = emxEntityPackage + PACKAGE_SEPARATOR + emxEntityName;
            } else {
                entityTypeId = emxEntityName;
            }
            EntityType entityType = intermediateResults.getEntityType(entityTypeId);
            if (entityType == null) {
                entityType = intermediateResults.addEntityType(entityTypeId);
            }
            if (dataService != null) {
                if (emxEntityBackend != null) {
                    if (dataService.getMeta().getBackend(emxEntityBackend) == null) {
                        throw new MolgenisDataException("Unknown backend '" + emxEntityBackend + '\'');
                    }
                } else {
                    emxEntityBackend = dataService.getMeta().getDefaultBackend().getName();
                }
                entityType.setBackend(emxEntityBackend);
            }
            if (emxEntityPackage != null) {
                Package p = intermediateResults.getPackage(emxEntityPackage);
                if (p == null) {
                    throw new MolgenisDataException("Unknown package: '" + emxEntityPackage + "' for entity '" + emxEntityName + "'. Please specify the package on the " + EMX_PACKAGES + " sheet and use the fully qualified package and entity names.");
                }
                entityType.setPackage(p);
            }
            if (emxEntityLabel == null) {
                emxEntityLabel = emxEntityName;
            }
            entityType.setLabel(emxEntityLabel);
            entityType.setDescription(emxEntityDescription);
            for (String attributeName : entity.getAttributeNames()) {
                if (isI18n(attributeName)) {
                    if (attributeName.startsWith(EMX_ENTITIES_DESCRIPTION)) {
                        String description = entity.getString(attributeName);
                        if (description != null) {
                            String languageCode = getLanguageCode(attributeName);
                            entityType.setDescription(languageCode, description);
                        }
                    } else if (attributeName.startsWith(EMX_ENTITIES_LABEL)) {
                        String label = entity.getString(attributeName);
                        if (label != null) {
                            String languageCode = getLanguageCode(attributeName);
                            entityType.setLabel(languageCode, label);
                        }
                    }
                }
            }
            if (emxEntityAbstract != null) {
                entityType.setAbstract(parseBoolean(emxEntityAbstract, i, EMX_ENTITIES_ABSTRACT));
            }
            if (emxEntityExtends != null) {
                EntityType extendsEntityType = null;
                if (intermediateResults.knowsEntity(emxEntityExtends)) {
                    extendsEntityType = intermediateResults.getEntityType(emxEntityExtends);
                } else {
                    if (dataService != null) {
                        extendsEntityType = dataService.getMeta().getEntityType(emxEntityExtends);
                    }
                }
                if (extendsEntityType == null) {
                    throw new MolgenisDataException("Missing super entity " + emxEntityExtends + " for entity " + emxEntityName + " on line " + i);
                }
                entityType.setExtends(extendsEntityType);
            }
            List<String> tagIdentifiers = toList(emxEntityTags);
            if (tagIdentifiers != null && !tagIdentifiers.isEmpty()) {
                entityType.setTags(toTags(intermediateResults, tagIdentifiers));
            }
        }
    }
}
Also used : SystemEntityType(org.molgenis.data.meta.SystemEntityType) L10nString(org.molgenis.data.i18n.model.L10nString) DefaultPackage(org.molgenis.data.meta.DefaultPackage) Package(org.molgenis.data.meta.model.Package)

Example 42 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class EmxMetaDataParser method putEntitiesInDefaultPackage.

/**
 * Put the entities that are not in a package in the selected package
 */
private List<EntityType> putEntitiesInDefaultPackage(IntermediateParseResults intermediateResults, String defaultPackageId) {
    Package p = getPackage(intermediateResults, defaultPackageId);
    if (p == null && dataService != null) {
        throw new IllegalArgumentException(format("Unknown package [%s]", defaultPackageId));
    }
    List<EntityType> entities = newArrayList();
    for (EntityType entityType : intermediateResults.getEntities()) {
        if (entityType.getPackage() == null) {
            entityType.setPackage(p);
        }
        entities.add(entityType);
    }
    return entities;
}
Also used : SystemEntityType(org.molgenis.data.meta.SystemEntityType) DefaultPackage(org.molgenis.data.meta.DefaultPackage) Package(org.molgenis.data.meta.model.Package)

Example 43 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class SystemEntityTypePersisterTest method removeNonExistingSystemEntities.

@Test
public void removeNonExistingSystemEntities() throws Exception {
    Package systemPackage = mock(Package.class);
    when(systemPackage.getId()).thenReturn(PACKAGE_SYSTEM);
    EntityType refRemovedMeta = when(mock(EntityType.class).getId()).thenReturn("refRemoved").getMock();
    when(refRemovedMeta.getPackage()).thenReturn(systemPackage);
    when(refRemovedMeta.toString()).thenReturn("refRemoved");
    when(refRemovedMeta.getAtomicAttributes()).thenReturn(emptyList());
    EntityType removedMeta = when(mock(EntityType.class).getId()).thenReturn("removed").getMock();
    when(removedMeta.getPackage()).thenReturn(systemPackage);
    when(removedMeta.toString()).thenReturn("removed");
    Attribute refAttr = when(mock(Attribute.class).getRefEntity()).thenReturn(refRemovedMeta).getMock();
    when(removedMeta.getAtomicAttributes()).thenReturn(singletonList(refAttr));
    EntityType refEntityType = when(mock(EntityType.class).getId()).thenReturn("refEntity").getMock();
    when(refEntityType.getPackage()).thenReturn(systemPackage);
    when(refEntityType.toString()).thenReturn("refEntity");
    when(refEntityType.getAtomicAttributes()).thenReturn(emptyList());
    EntityType entityType = when(mock(EntityType.class).getId()).thenReturn("entity").getMock();
    when(entityType.getPackage()).thenReturn(systemPackage);
    when(entityType.toString()).thenReturn("entity");
    when(entityType.getAtomicAttributes()).thenReturn(emptyList());
    when(systemEntityTypeRegistry.hasSystemEntityType("removed")).thenReturn(false);
    when(systemEntityTypeRegistry.hasSystemEntityType("refRemoved")).thenReturn(false);
    when(systemEntityTypeRegistry.hasSystemEntityType("entity")).thenReturn(true);
    when(systemEntityTypeRegistry.hasSystemEntityType("refEntity")).thenReturn(true);
    when(dataService.findAll(ENTITY_TYPE_META_DATA, EntityType.class)).thenReturn(Stream.of(refEntityType, entityType, refRemovedMeta, removedMeta));
    systemEntityTypePersister.removeNonExistingSystemEntityTypes();
    verify(metaDataService).deleteEntityType(newArrayList(refRemovedMeta, removedMeta));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Attribute(org.molgenis.data.meta.model.Attribute) SystemPackage(org.molgenis.data.meta.SystemPackage) Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 44 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class PackageRepositoryValidationDecoratorTest method testDeleteInvalid.

@Test(expectedExceptions = MolgenisValidationException.class)
public void testDeleteInvalid() throws Exception {
    Package package_ = mock(Package.class);
    doThrow(mock(MolgenisValidationException.class)).when(packageValidator).validate(package_);
    packageRepositoryValidationDecorator.delete(package_);
}
Also used : Package(org.molgenis.data.meta.model.Package) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 45 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class PackageRepositoryValidationDecoratorTest method testAddInvalid.

@Test(expectedExceptions = MolgenisValidationException.class)
public void testAddInvalid() throws Exception {
    Package package_ = mock(Package.class);
    doThrow(mock(MolgenisValidationException.class)).when(packageValidator).validate(package_);
    packageRepositoryValidationDecorator.add(package_);
}
Also used : Package(org.molgenis.data.meta.model.Package) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Aggregations

Package (org.molgenis.data.meta.model.Package)98 Test (org.testng.annotations.Test)70 EntityType (org.molgenis.data.meta.model.EntityType)22 Stream (java.util.stream.Stream)20 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)13 Attribute (org.molgenis.data.meta.model.Attribute)11 DefaultPackage (org.molgenis.data.meta.DefaultPackage)10 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)10 Objects.requireNonNull (java.util.Objects.requireNonNull)6 L10nString (org.molgenis.data.i18n.model.L10nString)6 PackageIdentity (org.molgenis.data.security.PackageIdentity)6 Collectors.toList (java.util.stream.Collectors.toList)5 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)5 DataService (org.molgenis.data.DataService)5 QueryImpl (org.molgenis.data.support.QueryImpl)5 RepositoryCollection (org.molgenis.data.RepositoryCollection)4 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)3 java.util (java.util)3 Collections.singletonList (java.util.Collections.singletonList)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3