private static void validateUpdateDataType( AttributeType currentDataType, AttributeType newDataType) { EnumSet<AttributeType> allowedDatatypes = DATA_TYPE_ALLOWED_TRANSITIONS.get(currentDataType); if (!allowedDatatypes.contains(newDataType)) { throw new MolgenisDataException( format( "Attribute data type update from [%s] to [%s] not allowed, allowed types are %s", currentDataType.toString(), newDataType.toString(), allowedDatatypes.toString())); } }
private Instant convertToDateTime(Object value) { try { return value != null ? Instant.ofEpochMilli(Double.valueOf(value.toString()).longValue()) : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), DATE_TIME.toString())); } }
private Double convertToDouble(Object value) { try { return value != null ? parseDouble(value.toString()) : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), DECIMAL.toString())); } }
private Long convertToLong(Object value) { Long convertedValue; try { convertedValue = value != null ? round(parseDouble(value.toString())) : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), LONG.toString())); } return convertedValue; } }
LocalDate convertToDate(Object value) { try { return value != null ? Instant.ofEpochMilli(Double.valueOf(value.toString()).longValue()) .atZone(ZoneId.systemDefault()) .toLocalDate() : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), DATE.toString())); } }
private Instant convertToDateTime(Object value) { try { return value != null ? Instant.ofEpochMilli(Double.valueOf(value.toString()).longValue()) : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), DATE_TIME.toString())); } }
private Double convertToDouble(Object value) { try { return value != null ? parseDouble(value.toString()) : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), DECIMAL.toString())); } }
LocalDate convertToDate(Object value) { try { return value != null ? Instant.ofEpochMilli(Double.valueOf(value.toString()).longValue()) .atZone(ZoneId.systemDefault()).toLocalDate() : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), DATE.toString())); } }
private Integer convertToInteger(Object value) { Integer convertedValue; try { convertedValue = value != null ? toIntExact(round(parseDouble(value.toString()))) : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), INT.toString())); } catch (ArithmeticException e) { LOG.debug("", e); throw new AlgorithmException( format( "'%s' is larger than the maximum allowed value for type '%s'", value.toString(), INT.toString())); } return convertedValue; }
private Long convertToLong(Object value) { Long convertedValue; try { convertedValue = value != null ? round(parseDouble(value.toString())) : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), LONG.toString())); } return convertedValue; } }
private Integer convertToInteger(Object value) { Integer convertedValue; try { convertedValue = value != null ? toIntExact(round(parseDouble(value.toString()))) : null; } catch (NumberFormatException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' can't be converted to type '%s'", value.toString(), INT.toString())); } catch (ArithmeticException e) { LOG.debug("", e); throw new AlgorithmException( format("'%s' is larger than the maximum allowed value for type '%s'", value.toString(), INT.toString())); } return convertedValue; }
private void recursivelyCollectAttributeIdentifiers( Iterable<Entity> attributeEntities, List<String> attributeIdentifiers) { for (Entity attributeEntity : attributeEntities) { if (!attributeEntity.getString(AttributeMetadata.TYPE).equals(COMPOUND.toString())) { attributeIdentifiers.add(attributeEntity.getString(AttributeMetadata.ID)); } Iterable<Entity> entities = attributeEntity.getEntities(AttributeMetadata.CHILDREN); if (entities != null) { recursivelyCollectAttributeIdentifiers(entities, attributeIdentifiers); } } }
private static String getSqlCheckConstraint(EntityType entityType, Attribute attr) { if (attr.getDataType() != ENUM) { throw new MolgenisDataException( format("Check constraint only allowed for attribute type [%s]", ENUM.toString())); } return "CONSTRAINT " + getCheckConstraintName(entityType, attr) + " CHECK (" + getColumnName(attr) + " IN (" + attr.getEnumOptions() .stream() .map(enumOption -> '\'' + enumOption + '\'') .collect(joining(",")) + "))"; }
private static Map<String, Object> attributeToMap(Attribute attribute) { Map<String, Object> map = new HashMap<>(); map.put(AttributeMetadata.NAME, attribute.getName()); map.put(AttributeMetadata.LABEL, attribute.getLabel()); map.put(AttributeMetadata.DESCRIPTION, attribute.getDescription()); map.put(AttributeMetadata.TYPE, attribute.getDataType().toString()); map.put(AttributeMetadata.IS_NULLABLE, attribute.isNillable()); map.put(AttributeMetadata.IS_UNIQUE, attribute.isUnique()); if (attribute.getRefEntity() != null) { map.put(AttributeMetadata.REF_ENTITY_TYPE, attribute.getRefEntity().getId()); } return map; } }
static String getSqlDropCheckConstraint(EntityType entityType, Attribute attr) { if (attr.getDataType() != ENUM) { throw new MolgenisDataException( format("Check constraint only allowed for attribute type [%s]", ENUM.toString())); } return "ALTER TABLE " + getTableName(entityType) + " DROP CONSTRAINT " + getCheckConstraintName(entityType, attr); }
private void recursivelyCollectAttributeIdentifiers(Iterable<Entity> attributeEntities, List<String> attributeIdentifiers) { for (Entity attributeEntity : attributeEntities) { if (!attributeEntity.getString(AttributeMetadata.TYPE).equals(COMPOUND.toString())) { attributeIdentifiers.add(attributeEntity.getString(AttributeMetadata.ID)); } Iterable<Entity> entities = attributeEntity.getEntities(AttributeMetadata.CHILDREN); if (entities != null) { recursivelyCollectAttributeIdentifiers(entities, attributeIdentifiers); } } }
private static Map<String, Object> attributeToMap(Attribute attribute) { Map<String, Object> map = new HashMap<String, Object>(); map.put(AttributeMetadata.NAME, attribute.getName()); map.put(AttributeMetadata.LABEL, attribute.getLabel()); map.put(AttributeMetadata.DESCRIPTION, attribute.getDescription()); map.put(AttributeMetadata.TYPE, attribute.getDataType().toString()); map.put(AttributeMetadata.IS_NULLABLE, attribute.isNillable()); map.put(AttributeMetadata.IS_UNIQUE, attribute.isUnique()); if (attribute.getRefEntity() != null) { map.put(AttributeMetadata.REF_ENTITY_TYPE, attribute.getRefEntity().getId()); } return map; } }
private ConstraintViolation createConstraintViolation( Entity entity, Attribute attribute, EntityType entityType, String message) { Object value = getDataValuesForType(entity, attribute); String dataValue = value != null ? value.toString() : null; String fullMessage = format( "Invalid [%s] value [%s] for attribute [%s] of entity [%s] with type [%s].", attribute.getDataType().toString().toLowerCase(), dataValue, attribute.getLabel(), entity.getLabelValue(), entityType.getId()); fullMessage += " " + message; return new ConstraintViolation(fullMessage); }
public static AttributeResult create(Attribute attr, String languageCode) { return create( attr.getLabel(languageCode), attr.getDescription(languageCode), attr.getDataType().toString()); } }
private static ConstraintViolation createConstraintViolation( Entity entity, Attribute attribute, EntityType entityType) { String message = format( "Invalid %s value '%s' for attribute '%s' of entity '%s'.", attribute.getDataType().toString().toLowerCase(), entity.get(attribute.getName()), attribute.getLabel(), entityType.getId()); Range range = attribute.getRange(); if (range != null) { message += format("Value must be between %d and %d", range.getMin(), range.getMax()); } Long maxLength = attribute.getDataType().getMaxLength(); if (maxLength != null) { message += format("Value must be less than or equal to %d characters", maxLength); } return new ConstraintViolation(message); }