Search in sources :

Example 1 with HasValueFlag

use of eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag in project hale by halestudio.

the class AppSchemaMappingUtils method asCqlLiteral.

/**
 * Convert the provided value to a CQL literal, based on the property
 * definition.
 *
 * <p>
 * In practice, this means that for properties whose binding type is a
 * {@link Number}, the value is returned as is; otherwise, value is
 * translated to a string literal and wrapped in single quotes.
 * </p>
 *
 * @param propertyDef the property definition
 * @param value the value to convert
 * @return the value as CQL literal
 */
public static String asCqlLiteral(PropertyDefinition propertyDef, String value) {
    if (propertyDef != null && value != null) {
        TypeDefinition typeDef = propertyDef.getPropertyType();
        HasValueFlag hasValue = typeDef.getConstraint(HasValueFlag.class);
        if (hasValue != null && hasValue.equals(HasValueFlag.get(true))) {
            Binding binding = typeDef.getConstraint(Binding.class);
            if (binding != null && Number.class.isAssignableFrom(binding.getBinding())) {
                return value;
            } else {
                // treat value as a string literal and hope for the best
                return "'" + value + "'";
            }
        }
    }
    return value;
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) HasValueFlag(eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)1 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)1