Search in sources :

Example 31 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class MappingServiceImpl method compareTargetMetadatas.

/**
 * Compares the attributes between the target repository and the mapping target.
 * Applied Rules:
 * - The mapping target can not contain attributes which are not in the target repository
 * - The attributes of the mapping target with the same name as attributes in the target repository should have the same type
 * - If there are reference attributes, the name of the reference entity should be the same in both the target repository as in the mapping target
 *
 * @param targetRepositoryEntityType the target repository EntityType to check
 * @param mappingTargetEntityType    the mapping target EntityType to check
 * @throws MolgenisDataException if the types are not compatible
 */
private void compareTargetMetadatas(EntityType targetRepositoryEntityType, EntityType mappingTargetEntityType) {
    Map<String, Attribute> targetRepositoryAttributeMap = newHashMap();
    targetRepositoryEntityType.getAtomicAttributes().forEach(attribute -> targetRepositoryAttributeMap.put(attribute.getName(), attribute));
    for (Attribute mappingTargetAttribute : mappingTargetEntityType.getAtomicAttributes()) {
        String mappingTargetAttributeName = mappingTargetAttribute.getName();
        Attribute targetRepositoryAttribute = targetRepositoryAttributeMap.get(mappingTargetAttributeName);
        if (targetRepositoryAttribute == null) {
            throw new MolgenisDataException(format("Target repository does not contain the following attribute: %s", mappingTargetAttributeName));
        }
        AttributeType targetRepositoryAttributeType = targetRepositoryAttribute.getDataType();
        AttributeType mappingTargetAttributeType = mappingTargetAttribute.getDataType();
        if (!mappingTargetAttributeType.equals(targetRepositoryAttributeType)) {
            throw new MolgenisDataException(format("attribute %s in the mapping target is type %s while attribute %s in the target repository is type %s. Please make sure the types are the same", mappingTargetAttributeName, mappingTargetAttributeType, targetRepositoryAttribute.getName(), targetRepositoryAttributeType));
        }
        if (isReferenceType(mappingTargetAttribute)) {
            String mappingTargetRefEntityName = mappingTargetAttribute.getRefEntity().getId();
            String targetRepositoryRefEntityName = targetRepositoryAttribute.getRefEntity().getId();
            if (!mappingTargetRefEntityName.equals(targetRepositoryRefEntityName)) {
                throw new MolgenisDataException(format("In the mapping target, attribute %s of type %s has reference entity %s while in the target repository attribute %s of type %s has reference entity %s. " + "Please make sure the reference entities of your mapping target are pointing towards the same reference entities as your target repository", mappingTargetAttributeName, mappingTargetAttributeType, mappingTargetRefEntityName, targetRepositoryAttribute.getName(), targetRepositoryAttributeType, targetRepositoryRefEntityName));
            }
        }
    }
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 32 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class VcfToEntity method createEntityType.

private EntityType createEntityType(String entityTypeId, VcfMeta vcfMeta) {
    Attribute idAttribute = attrMetaFactory.create().setName(INTERNAL_ID).setDataType(STRING);
    idAttribute.setVisible(false);
    EntityType entityType = entityTypeFactory.create(entityTypeId);
    entityType.setLabel(entityTypeId);
    entityType.addAttribute(vcfAttributes.getChromAttribute());
    entityType.addAttribute(vcfAttributes.getAltAttribute());
    entityType.addAttribute(vcfAttributes.getPosAttribute());
    entityType.addAttribute(vcfAttributes.getRefAttribute());
    entityType.addAttribute(vcfAttributes.getFilterAttribute());
    entityType.addAttribute(vcfAttributes.getQualAttribute());
    entityType.addAttribute(vcfAttributes.getIdAttribute());
    entityType.addAttribute(idAttribute, ROLE_ID);
    Attribute infoMetaData = attrMetaFactory.create().setName(INFO).setDataType(COMPOUND).setNillable(true);
    for (VcfMetaInfo info : vcfMeta.getInfoMeta()) {
        String attrName = toAttributeName(info.getId());
        AttributeType attrType = vcfReaderFormatToMolgenisType(info);
        String attrDescription = StringUtils.isBlank(info.getDescription()) ? VcfRepository.DEFAULT_ATTRIBUTE_DESCRIPTION : info.getDescription();
        Attribute attribute = attrMetaFactory.create().setName(attrName).setDataType(attrType).setDescription(attrDescription).setAggregatable(true).setParent(infoMetaData);
        entityType.addAttribute(attribute);
    }
    entityType.addAttribute(infoMetaData);
    if (sampleEntityType != null) {
        Attribute samplesAttributeMeta = attrMetaFactory.create().setName(SAMPLES).setDataType(MREF).setRefEntity(sampleEntityType).setLabel("SAMPLES");
        entityType.addAttribute(samplesAttributeMeta);
    }
    return entityType;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Attribute(org.molgenis.data.meta.model.Attribute) VcfMetaInfo(org.molgenis.vcf.meta.VcfMetaInfo) AttributeType(org.molgenis.data.meta.AttributeType)

Example 33 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class AttributeValidator method validateUpdate.

private static void validateUpdate(Attribute newAttr, Attribute currentAttr) {
    // data type
    AttributeType currentDataType = currentAttr.getDataType();
    AttributeType newDataType = newAttr.getDataType();
    if (!Objects.equals(currentDataType, newDataType)) {
        validateUpdateDataType(currentDataType, newDataType);
        if (newAttr.isInversedBy()) {
            throw new MolgenisDataException(format("Attribute data type change not allowed for bidirectional attribute [%s]", newAttr.getName()));
        }
    }
    // orderBy
    Sort currentOrderBy = currentAttr.getOrderBy();
    Sort newOrderBy = newAttr.getOrderBy();
    if (!Objects.equals(currentOrderBy, newOrderBy)) {
        validateOrderBy(newAttr, newOrderBy);
    }
// note: mappedBy is a readOnly attribute, no need to verify for updates
}
Also used : AttributeType(org.molgenis.data.meta.AttributeType)

Example 34 with AttributeType

use of org.molgenis.data.meta.AttributeType 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 (fieldType.getMaxLength() != null && value.length() > fieldType.getMaxLength()) {
            throw new MolgenisDataException("Default value for attribute [" + attr.getName() + "] exceeds the maximum length for datatype " + attr.getDataType().name());
        }
        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.query(refEntityType.getId()).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)) {
                Iterable<Entity> refEntitiesValue = (Iterable<Entity>) typedValue;
                EntityType refEntityType = attr.getRefEntity();
                if (dataService.query(refEntityType.getId()).in(refEntityType.getIdAttribute().getName(), StreamSupport.stream(refEntitiesValue.spliterator(), false).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) AttributeType(org.molgenis.data.meta.AttributeType) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 35 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class EffectStructureConverter method parseEffectAttributeDescription.

// Create a map of attributes based on the pipe separated attribute names in the description
private ArrayList<Attribute> parseEffectAttributeDescription(String attributesString, List<Attribute> annotatorAttributes) {
    String[] attributeStrings = attributesString.replaceAll("^\\s'|'$", "").split("\\|");
    ArrayList<Attribute> attributeList = new ArrayList<>();
    Map<String, Attribute> annotatorAttributeMap = VcfUtils.getAttributesMapFromList(annotatorAttributes);
    for (String attribute : attributeStrings) {
        AttributeType type = annotatorAttributeMap.containsKey(attribute) ? annotatorAttributeMap.get(attribute).getDataType() : STRING;
        Attribute attr = attributeFactory.create().setName(StringUtils.deleteWhitespace(attribute)).setDataType(type).setLabel(attribute);
        attributeList.add(attr);
    }
    return attributeList;
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType) ArrayList(java.util.ArrayList)

Aggregations

AttributeType (org.molgenis.data.meta.AttributeType)46 Attribute (org.molgenis.data.meta.model.Attribute)24 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)24 LocalDate (java.time.LocalDate)11 Instant (java.time.Instant)10 Entity (org.molgenis.data.Entity)10 EntityType (org.molgenis.data.meta.model.EntityType)7 DataProvider (org.testng.annotations.DataProvider)7 ArrayList (java.util.ArrayList)5 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)2 String.format (java.lang.String.format)2 Collectors.toList (java.util.stream.Collectors.toList)2 Test (org.testng.annotations.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps.newLinkedHashMap (com.google.common.collect.Maps.newLinkedHashMap)1 UTC (java.time.ZoneOffset.UTC)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1