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));
}
}
}
}
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;
}
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
}
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)));
}
}
}
}
}
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;
}
Aggregations