Search in sources :

Example 1 with PACKAGE

use of org.molgenis.data.meta.model.PackageMetadata.PACKAGE in project molgenis by molgenis.

the class EntityTypeValidator method validateOwnAttributes.

/**
 * Validates the attributes owned by this entity:
 * 1) validates that the parent entity doesn't have attributes with the same name
 * 2) validates that this entity doesn't have attributes with the same name
 * 3) validates that this entity has attributes defined at all
 *
 * @param entityType entity meta data
 * @throws MolgenisValidationException if an attribute is owned by another entity or a parent attribute has the same name
 */
private static void validateOwnAttributes(EntityType entityType) {
    // Validate that entity has attributes
    if (asStream(entityType.getAllAttributes()).collect(toList()).isEmpty()) {
        throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] does not contain any attributes. Did you use the correct package+entity name combination in both the entities as well as the attributes sheet?", entityType.getId())));
    }
    // Validate that entity does not contain multiple attributes with the same name
    Multimap<String, Attribute> attrMultiMap = asStream(entityType.getAllAttributes()).collect(MultimapCollectors.toArrayListMultimap(Attribute::getName, Function.identity()));
    attrMultiMap.keySet().forEach(attrName -> {
        if (attrMultiMap.get(attrName).size() > 1) {
            throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] contains multiple attributes with name [%s]", entityType.getId(), attrName)));
        }
    });
    // Validate that entity attributes with same name do no exist in parent entity
    EntityType extendsEntityType = entityType.getExtends();
    if (extendsEntityType != null) {
        Map<String, Attribute> extendsAllAttrMap = stream(extendsEntityType.getAllAttributes().spliterator(), false).collect(toMap(Attribute::getName, Function.identity(), (u, v) -> {
            throw new IllegalStateException(String.format("Duplicate key %s", u));
        }, LinkedHashMap::new));
        entityType.getOwnAllAttributes().forEach(attr -> {
            if (extendsAllAttrMap.containsKey(attr.getName())) {
                throw new MolgenisValidationException(new ConstraintViolation(format("An attribute with name [%s] already exists in entity [%s] or one of its parents", attr.getName(), extendsEntityType.getId())));
            }
        });
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) Multimap(com.google.common.collect.Multimap) Attribute(org.molgenis.data.meta.model.Attribute) Function(java.util.function.Function) EntityUtils.asStream(org.molgenis.data.util.EntityUtils.asStream) LinkedHashMap(java.util.LinkedHashMap) MetaUtils(org.molgenis.data.meta.MetaUtils) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) Collectors.toMap(java.util.stream.Collectors.toMap) AttributeUtils(org.molgenis.data.support.AttributeUtils) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SystemEntityTypeRegistry(org.molgenis.data.meta.system.SystemEntityTypeRegistry) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) RepositoryCollection(org.molgenis.data.RepositoryCollection) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) MultimapCollectors(org.molgenis.util.stream.MultimapCollectors) EntityType(org.molgenis.data.meta.model.EntityType) String.format(java.lang.String.format) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) PACKAGE(org.molgenis.data.meta.model.PackageMetadata.PACKAGE) StreamSupport.stream(java.util.stream.StreamSupport.stream) DataService(org.molgenis.data.DataService) Package(org.molgenis.data.meta.model.Package) MolgenisDataException(org.molgenis.data.MolgenisDataException) Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 2 with PACKAGE

use of org.molgenis.data.meta.model.PackageMetadata.PACKAGE in project molgenis by molgenis.

the class SystemEntityTypePersister method injectExistingPackageIdentifiers.

private void injectExistingPackageIdentifiers(List<Package> systemPackages) {
    Map<String, Package> existingPackageMap = dataService.findAll(PACKAGE, Package.class).collect(toMap(Package::getId, pack -> pack));
    systemPackages.forEach(pack -> {
        Package existingPackage = existingPackageMap.get(pack.getId());
        if (existingPackage != null) {
            pack.setId(existingPackage.getId());
        }
    });
}
Also used : EntityUtils(org.molgenis.data.util.EntityUtils) RepositoryCollection(org.molgenis.data.RepositoryCollection) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) Function(java.util.function.Function) MetaDataService(org.molgenis.data.meta.MetaDataService) MutableAclClassService(org.molgenis.security.acl.MutableAclClassService) Component(org.springframework.stereotype.Component) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) PACKAGE(org.molgenis.data.meta.model.PackageMetadata.PACKAGE) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) StreamSupport.stream(java.util.stream.StreamSupport.stream) SystemEntityType(org.molgenis.data.meta.SystemEntityType) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) DataService(org.molgenis.data.DataService) Package(org.molgenis.data.meta.model.Package) EntityIdentityUtils(org.molgenis.data.security.EntityIdentityUtils) EntityTypeDependencyResolver(org.molgenis.data.meta.EntityTypeDependencyResolver) Package(org.molgenis.data.meta.model.Package)

Example 3 with PACKAGE

use of org.molgenis.data.meta.model.PackageMetadata.PACKAGE in project molgenis by molgenis.

the class PlatformIT method testDeletePackage.

@WithMockUser(username = USERNAME)
@Test(singleThreaded = true)
public void testDeletePackage() {
    populateUserPermissions();
    runAsSystem(() -> {
        MetaDataService metadataService = dataService.getMeta();
        Package parentPackage = packageFactory.create("parent").setLabel("parent");
        Package subPackage = packageFactory.create("parent_sub").setLabel("sub").setParent(parentPackage);
        metadataService.upsertPackages(Stream.of(parentPackage, subPackage));
        EntityType entityTypeInSubPackage = testHarness.createDynamicRefEntityType("entityInSub", subPackage);
        EntityType entityTypeInParentPackage = testHarness.createDynamicTestEntityType("entityInParent", parentPackage, entityTypeInSubPackage);
        metadataService.upsertEntityTypes(asList(entityTypeInSubPackage, entityTypeInParentPackage));
        List<Entity> entities = createAndAdd(entityTypeInParentPackage, entityTypeInSubPackage, 5);
        Set<Entity> refEntities = entities.stream().map(e -> e.getEntity(ATTR_XREF)).collect(toSet());
        assertPresent(entityTypeInParentPackage, entities);
        assertPresent(entityTypeInSubPackage, newArrayList(refEntities));
        dataService.deleteById(PACKAGE, "parent");
        assertNull(metadataService.getPackage("parent"));
        assertNull(metadataService.getPackage("parent_sub"));
        entities.forEach(this::assertNotPresent);
        refEntities.forEach(this::assertNotPresent);
    });
}
Also used : AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) MetaDataServiceImpl(org.molgenis.data.meta.MetaDataServiceImpl) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) RunAsSystemAspect.runAsSystem(org.molgenis.security.core.runas.RunAsSystemAspect.runAsSystem) IndexActionRegisterServiceImpl(org.molgenis.data.index.IndexActionRegisterServiceImpl) L10N_STRING(org.molgenis.data.i18n.model.L10nStringMetaData.L10N_STRING) EntityListener(org.molgenis.data.listeners.EntityListener) MetaDataService(org.molgenis.data.meta.MetaDataService) Collections.singletonList(java.util.Collections.singletonList) TestExecutionListeners(org.springframework.test.context.TestExecutionListeners) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) ElasticsearchService(org.molgenis.data.elasticsearch.ElasticsearchService) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Collectors.toSet(java.util.stream.Collectors.toSet) L10nString(org.molgenis.data.i18n.model.L10nString) AbstractTestNGSpringContextTests(org.springframework.test.context.testng.AbstractTestNGSpringContextTests) EntityTestHarness(org.molgenis.data.EntityTestHarness) LanguageService(org.molgenis.i18n.LanguageService) org.molgenis.data(org.molgenis.data) IndexJobScheduler(org.molgenis.data.index.job.IndexJobScheduler) IndexAction(org.molgenis.data.index.meta.IndexAction) READ(org.molgenis.data.security.EntityTypePermission.READ) PACKAGE(org.molgenis.data.meta.model.PackageMetadata.PACKAGE) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) WithMockUser(org.springframework.security.test.context.support.WithMockUser) LANGUAGE(org.molgenis.data.i18n.model.LanguageMetadata.LANGUAGE) java.util(java.util) org.testng.annotations(org.testng.annotations) QueryImpl(org.molgenis.data.support.QueryImpl) EntityListenersService(org.molgenis.data.listeners.EntityListenersService) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) AggregateResult(org.molgenis.data.aggregation.AggregateResult) Assert(org.testng.Assert) WithSecurityContextTestExecutionListener(org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener) Objects.requireNonNull(java.util.Objects.requireNonNull) L10nStringMetaData(org.molgenis.data.i18n.model.L10nStringMetaData) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) AttributeType(org.molgenis.data.meta.AttributeType) Logger(org.slf4j.Logger) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) IndexActionMetaData(org.molgenis.data.index.meta.IndexActionMetaData) L10nStringFactory(org.molgenis.data.i18n.model.L10nStringFactory) LanguageFactory(org.molgenis.data.i18n.model.LanguageFactory) Collectors.toList(java.util.stream.Collectors.toList) org.molgenis.data.meta.model(org.molgenis.data.meta.model) ContextConfiguration(org.springframework.test.context.ContextConfiguration) WRITE(org.molgenis.data.security.EntityTypePermission.WRITE) Package(org.molgenis.data.meta.model.Package) Transactional(org.springframework.transaction.annotation.Transactional) MetaDataService(org.molgenis.data.meta.MetaDataService) Package(org.molgenis.data.meta.model.Package) WithMockUser(org.springframework.security.test.context.support.WithMockUser)

Example 4 with PACKAGE

use of org.molgenis.data.meta.model.PackageMetadata.PACKAGE in project molgenis by molgenis.

the class PlatformIT method testDeletePackageWithOneToMany.

@WithMockUser(username = USERNAME)
@Test(singleThreaded = true)
public void testDeletePackageWithOneToMany() {
    populateUserPermissions();
    runAsSystem(() -> {
        MetaDataService metadataService = dataService.getMeta();
        Package package_ = packageFactory.create("package_onetomany").setLabel("package");
        metadataService.upsertPackages(Stream.of(package_));
        EntityType refEntityType = testHarness.createDynamicRefEntityType("entityType_onetomany", package_);
        EntityType entityType = testHarness.createDynamicTestEntityType("refEntityType_onetomany", package_, refEntityType);
        Attribute oneToManyAttribute = attributeFactory.create("onetomany").setName("onetomany").setDataType(AttributeType.ONE_TO_MANY).setRefEntity(entityType).setMappedBy(entityType.getAttribute(ATTR_XREF));
        refEntityType.addAttribute(oneToManyAttribute);
        metadataService.upsertEntityTypes(asList(refEntityType, entityType));
        List<Entity> entities = createAndAdd(entityType, refEntityType, 5);
        Set<Entity> refEntities = entities.stream().map(e -> e.getEntity(ATTR_XREF)).collect(toSet());
        assertPresent(entityType, entities);
        assertPresent(refEntityType, newArrayList(refEntities));
        dataService.deleteById(PACKAGE, "package_onetomany");
        assertNull(metadataService.getPackage("package_onetomany"));
        assertNull(dataService.getEntityType(entityType.getId()));
        assertNull(dataService.getEntityType(refEntityType.getId()));
        entities.forEach(this::assertNotPresent);
        refEntities.forEach(this::assertNotPresent);
    });
}
Also used : AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) MetaDataServiceImpl(org.molgenis.data.meta.MetaDataServiceImpl) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) RunAsSystemAspect.runAsSystem(org.molgenis.security.core.runas.RunAsSystemAspect.runAsSystem) IndexActionRegisterServiceImpl(org.molgenis.data.index.IndexActionRegisterServiceImpl) L10N_STRING(org.molgenis.data.i18n.model.L10nStringMetaData.L10N_STRING) EntityListener(org.molgenis.data.listeners.EntityListener) MetaDataService(org.molgenis.data.meta.MetaDataService) Collections.singletonList(java.util.Collections.singletonList) TestExecutionListeners(org.springframework.test.context.TestExecutionListeners) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) ElasticsearchService(org.molgenis.data.elasticsearch.ElasticsearchService) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Collectors.toSet(java.util.stream.Collectors.toSet) L10nString(org.molgenis.data.i18n.model.L10nString) AbstractTestNGSpringContextTests(org.springframework.test.context.testng.AbstractTestNGSpringContextTests) EntityTestHarness(org.molgenis.data.EntityTestHarness) LanguageService(org.molgenis.i18n.LanguageService) org.molgenis.data(org.molgenis.data) IndexJobScheduler(org.molgenis.data.index.job.IndexJobScheduler) IndexAction(org.molgenis.data.index.meta.IndexAction) READ(org.molgenis.data.security.EntityTypePermission.READ) PACKAGE(org.molgenis.data.meta.model.PackageMetadata.PACKAGE) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) WithMockUser(org.springframework.security.test.context.support.WithMockUser) LANGUAGE(org.molgenis.data.i18n.model.LanguageMetadata.LANGUAGE) java.util(java.util) org.testng.annotations(org.testng.annotations) QueryImpl(org.molgenis.data.support.QueryImpl) EntityListenersService(org.molgenis.data.listeners.EntityListenersService) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) AggregateResult(org.molgenis.data.aggregation.AggregateResult) Assert(org.testng.Assert) WithSecurityContextTestExecutionListener(org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener) Objects.requireNonNull(java.util.Objects.requireNonNull) L10nStringMetaData(org.molgenis.data.i18n.model.L10nStringMetaData) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) AttributeType(org.molgenis.data.meta.AttributeType) Logger(org.slf4j.Logger) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) IndexActionMetaData(org.molgenis.data.index.meta.IndexActionMetaData) L10nStringFactory(org.molgenis.data.i18n.model.L10nStringFactory) LanguageFactory(org.molgenis.data.i18n.model.LanguageFactory) Collectors.toList(java.util.stream.Collectors.toList) org.molgenis.data.meta.model(org.molgenis.data.meta.model) ContextConfiguration(org.springframework.test.context.ContextConfiguration) WRITE(org.molgenis.data.security.EntityTypePermission.WRITE) Package(org.molgenis.data.meta.model.Package) Transactional(org.springframework.transaction.annotation.Transactional) MetaDataService(org.molgenis.data.meta.MetaDataService) Package(org.molgenis.data.meta.model.Package) WithMockUser(org.springframework.security.test.context.support.WithMockUser)

Aggregations

Objects.requireNonNull (java.util.Objects.requireNonNull)4 Collectors.toList (java.util.stream.Collectors.toList)4 ENTITY_TYPE_META_DATA (org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA)4 Package (org.molgenis.data.meta.model.Package)4 PACKAGE (org.molgenis.data.meta.model.PackageMetadata.PACKAGE)4 Stream (java.util.stream.Stream)3 MetaDataService (org.molgenis.data.meta.MetaDataService)3 ATTRIBUTE_META_DATA (org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)2 java.util (java.util)2 Arrays.asList (java.util.Arrays.asList)2 Collections.singletonList (java.util.Collections.singletonList)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 Collectors.toSet (java.util.stream.Collectors.toSet)2 StreamSupport.stream (java.util.stream.StreamSupport.stream)2 org.molgenis.data (org.molgenis.data)2