Search in sources :

Example 1 with UnknownRepositoryException

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

the class AttributeRequestMapperImpl method processParentAndMappedby.

private void processParentAndMappedby(Map<String, Object> attributeRequest, Map<String, Attribute> attributes, EntityType entityType) {
    String attributeId = attributeRequest.get("id").toString();
    for (Entry<String, Object> entry : attributeRequest.entrySet()) {
        if (entry.getKey().equals("mappedByAttribute")) {
            String mappedByAttrId = getStringValue(entry.getValue());
            Attribute mappedByAttr = attributes.get(mappedByAttrId);
            if (mappedByAttr == null) {
                Repository<Attribute> repository = metaDataService.getRepository(AttributeMetadata.ATTRIBUTE_META_DATA, Attribute.class).orElseThrow(() -> new UnknownRepositoryException(AttributeMetadata.ATTRIBUTE_META_DATA));
                mappedByAttr = repository.findOneById(mappedByAttrId);
            }
            if (mappedByAttr == null) {
                throw new UnknownAttributeException(entityType, mappedByAttrId);
            }
            Attribute attribute = attributes.get(attributeId);
            attribute.setMappedBy(mappedByAttr);
        } else if (entry.getKey().equals("parent")) {
            String parentId = getStringValue(entry.getValue());
            Attribute parentAttr = attributes.get(parentId);
            if (parentAttr == null) {
                parentAttr = getAttributeFromParent(entityType.getExtends(), parentId);
            }
            Attribute attribute = attributes.get(attributeId);
            attribute.setParent(parentAttr);
        }
    }
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) UnknownAttributeException(org.molgenis.data.UnknownAttributeException) UnknownRepositoryException(org.molgenis.data.UnknownRepositoryException)

Example 2 with UnknownRepositoryException

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

the class EmxDataProviderTest method testGetEntitiesAlternativeEntityTypeIdUnknownRepository.

@Test
void testGetEntitiesAlternativeEntityTypeIdUnknownRepository() {
    String entityTypeId = "base_EntityTypeId";
    EntityType entityType = when(mock(EntityType.class).getId()).thenReturn(entityTypeId).getMock();
    RepositoryCollection repositoryCollection = mock(RepositoryCollection.class);
    doThrow(new UnknownRepositoryException("EntityTypeId")).when(repositoryCollection).getRepository("EntityTypeId");
    when(emxImportJob.getSource()).thenReturn(repositoryCollection);
    assertThrows(UnknownRepositoryException.class, () -> emxDataProvider.getEntities(entityType).collect(toList()));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) RepositoryCollection(org.molgenis.data.RepositoryCollection) UnknownRepositoryException(org.molgenis.data.UnknownRepositoryException) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 3 with UnknownRepositoryException

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

the class EmxDataProviderTest method testGetEntitiesUnknownRepository.

@Test
void testGetEntitiesUnknownRepository() {
    String entityTypeId = "EntityTypeId";
    EntityType entityType = when(mock(EntityType.class).getId()).thenReturn(entityTypeId).getMock();
    RepositoryCollection repositoryCollection = mock(RepositoryCollection.class);
    doThrow(new UnknownRepositoryException(entityTypeId)).when(repositoryCollection).getRepository(entityType);
    when(emxImportJob.getSource()).thenReturn(repositoryCollection);
    assertThrows(UnknownRepositoryException.class, () -> emxDataProvider.getEntities(entityType).collect(toList()));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) RepositoryCollection(org.molgenis.data.RepositoryCollection) UnknownRepositoryException(org.molgenis.data.UnknownRepositoryException) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 4 with UnknownRepositoryException

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

the class AttributeResponseMapperImpl method getCategories.

private List<Category> getCategories(EntityType entityType) {
    Repository<Entity> repository = metaDataService.getRepository(entityType).orElseThrow(() -> new UnknownRepositoryException(entityType.getId()));
    Attribute idAttribute = entityType.getIdAttribute();
    Attribute labelAttribute = entityType.getLabelAttribute();
    return repository.query().findAll().map(entity -> Category.builder().setId(entity.get(idAttribute)).setLabel(entity.get(labelAttribute).toString()).build()).collect(toList());
}
Also used : Range(org.molgenis.api.metadata.v3.model.Range) LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Iterables(com.google.common.collect.Iterables) Order(org.molgenis.data.Sort.Order) LABEL(org.molgenis.data.meta.model.AttributeMetadata.LABEL) AttributeResponse(org.molgenis.api.metadata.v3.model.AttributeResponse) AttributesResponse(org.molgenis.api.metadata.v3.model.AttributesResponse) UnknownRepositoryException(org.molgenis.data.UnknownRepositoryException) ONE_TO_MANY(org.molgenis.data.meta.AttributeType.ONE_TO_MANY) LinksUtils(org.molgenis.api.support.LinksUtils) LinksResponse(org.molgenis.api.model.response.LinksResponse) Attribute(org.molgenis.data.meta.model.Attribute) MetaDataService(org.molgenis.data.meta.MetaDataService) ArrayList(java.util.ArrayList) I18nValue(org.molgenis.api.metadata.v3.model.I18nValue) Sort(org.molgenis.data.Sort) Objects.requireNonNull(java.util.Objects.requireNonNull) URI(java.net.URI) AttributeType(org.molgenis.data.meta.AttributeType) Category(org.molgenis.api.metadata.v3.model.Category) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) EntityType(org.molgenis.data.meta.model.EntityType) ServletUriComponentsBuilder.fromCurrentRequestUri(org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromCurrentRequestUri) Collectors.toList(java.util.stream.Collectors.toList) AttributeType.getValueString(org.molgenis.data.meta.AttributeType.getValueString) List(java.util.List) Component(org.springframework.stereotype.Component) Builder(org.molgenis.api.metadata.v3.model.AttributeResponseData.Builder) Direction(org.molgenis.api.model.Order.Direction) Repository(org.molgenis.data.Repository) EntityTypeUtils(org.molgenis.data.util.EntityTypeUtils) Optional(java.util.Optional) AttributeResponseData(org.molgenis.api.metadata.v3.model.AttributeResponseData) DESCRIPTION(org.molgenis.data.meta.model.AttributeMetadata.DESCRIPTION) PageResponse(org.molgenis.api.model.response.PageResponse) Entity(org.molgenis.data.Entity) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) UnknownRepositoryException(org.molgenis.data.UnknownRepositoryException)

Example 5 with UnknownRepositoryException

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

the class AttributeValidator method validateDefaultValue.

void validateDefaultValue(Attribute attr, boolean validateEntityReferences) {
    String value = attr.getDefaultValue();
    if (value != null) {
        if (attr.isUnique()) {
            throw new MolgenisDataException("Unique attribute " + attr.getName() + " cannot have default value");
        }
        if (attr.getExpression() != null) {
            throw new MolgenisDataException("Computed attribute " + attr.getName() + " cannot have default value");
        }
        AttributeType fieldType = attr.getDataType();
        if (attr.getMaxLength() != null && value.length() > attr.getMaxLength()) {
            throw new MolgenisDataException("Default value for attribute [" + attr.getName() + "] exceeds the maximum length [" + attr.getMaxLength() + "]");
        }
        if (fieldType == AttributeType.EMAIL) {
            checkEmail(value);
        }
        if (fieldType == AttributeType.HYPERLINK) {
            checkHyperlink(value);
        }
        if (fieldType == AttributeType.ENUM) {
            checkEnum(attr, value);
        }
        // Get typed value to check if the value is of the right type.
        Object typedValue;
        try {
            typedValue = EntityUtils.getTypedValue(value, attr, entityManager);
        } catch (NumberFormatException e) {
            throw new MolgenisValidationException(new ConstraintViolation(format("Invalid default value [%s] for data type [%s]", value, attr.getDataType())));
        }
        if (validateEntityReferences) {
            if (isSingleReferenceType(attr)) {
                Entity refEntity = (Entity) typedValue;
                EntityType refEntityType = attr.getRefEntity();
                if (dataService.getMeta().getRepository(refEntityType).orElseThrow(() -> new UnknownRepositoryException(refEntityType.getId())).query().eq(refEntityType.getIdAttribute().getName(), refEntity.getIdValue()).count() == 0) {
                    throw new MolgenisValidationException(new ConstraintViolation(format("Default value [%s] refers to an unknown entity", value)));
                }
            } else if (isMultipleReferenceType(attr)) {
                @SuppressWarnings("unchecked") Iterable<Entity> refEntitiesValue = (Iterable<Entity>) typedValue;
                EntityType refEntityType = attr.getRefEntity();
                if (dataService.query(refEntityType.getId()).in(refEntityType.getIdAttribute().getName(), stream(refEntitiesValue).map(Entity::getIdValue).collect(toList())).count() < Iterables.size(refEntitiesValue)) {
                    throw new MolgenisValidationException(new ConstraintViolation(format("Default value [%s] refers to one or more unknown entities", value)));
                }
            }
        }
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Entity(org.molgenis.data.Entity) MolgenisDataException(org.molgenis.data.MolgenisDataException) AttributeType(org.molgenis.data.meta.AttributeType) ConstraintViolation(org.molgenis.validation.ConstraintViolation) UnknownRepositoryException(org.molgenis.data.UnknownRepositoryException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Aggregations

UnknownRepositoryException (org.molgenis.data.UnknownRepositoryException)5 EntityType (org.molgenis.data.meta.model.EntityType)4 Test (org.junit.jupiter.api.Test)2 Entity (org.molgenis.data.Entity)2 RepositoryCollection (org.molgenis.data.RepositoryCollection)2 AttributeType (org.molgenis.data.meta.AttributeType)2 Attribute (org.molgenis.data.meta.model.Attribute)2 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterables (com.google.common.collect.Iterables)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Collectors.toList (java.util.stream.Collectors.toList)1 AttributeResponse (org.molgenis.api.metadata.v3.model.AttributeResponse)1 AttributeResponseData (org.molgenis.api.metadata.v3.model.AttributeResponseData)1 Builder (org.molgenis.api.metadata.v3.model.AttributeResponseData.Builder)1