Search in sources :

Example 1 with Binding

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

the class AbstractDBTest method checkBindingAndSqlType.

/**
 * It checks if the binding of a data type read from schema and the expected
 * binding are equal.
 *
 * @param map It maps a data type with a binding class to be expected. e.g.
 *            for postgresql db, for data type VARCHAR, the expected binding
 *            class is String.class
 * @param schema the schema read.
 * @throws Exception exception may thrown while getting the value of a
 *             static or instance field of type int.
 */
protected void checkBindingAndSqlType(Schema schema, Map<String, Class<?>> map) throws Exception {
    final Map<String, Integer> sqlTypeMap = new HashMap<>();
    // all types fields
    for (final Field f : Types.class.getFields()) {
        sqlTypeMap.put(f.getName(), f.getInt(null));
    }
    for (TypeDefinition td : schema.getTypes()) {
        for (ChildDefinition<?> cd : td.getChildren()) {
            PropertyDefinition property = cd.asProperty();
            String name = property.getPropertyType().getName().getLocalPart().toUpperCase();
            SQLType t = property.getPropertyType().getConstraint(SQLType.class);
            assertTrue(sqlTypeMap.containsValue(new Integer(t.getType())));
            Binding k = property.getPropertyType().getConstraint(Binding.class);
            // is mapped.
            if (map.containsKey(name))
                assertEquals("Binding mismatch for type \"" + name + "\"", map.get(name), k.getBinding());
            else
                fail(MessageFormat.format("No expected binding specified for type {0} (SQL type {1}) - binding is {2}", name, t.getType(), k.getBinding()));
        }
    }
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) SQLType(eu.esdihumboldt.hale.io.jdbc.constraints.SQLType) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 2 with Binding

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

the class InteriorPoint method evaluate.

@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
    // get input geometry
    PropertyValue input = variables.get(null).get(0);
    Object inputValue = input.getValue();
    GeometryProperty<?> result = calculateInteriorPoint(inputValue);
    // try to yield a result compatible to the target
    TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
    // TODO check element type?
    Class<?> binding = targetType.getConstraint(Binding.class).getBinding();
    if (Geometry.class.isAssignableFrom(binding) && binding.isAssignableFrom(result.getClass())) {
        return result.getGeometry();
    }
    return result;
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) Geometry(org.locationtech.jts.geom.Geometry) PropertyValue(eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 3 with Binding

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

the class XmlSchemaReaderTest method testRead_definitive_attributegroup.

/**
 * Test reading a simple XML schema that uses an attribute group and an
 * attribute with xs:date type.
 *
 * @throws Exception if reading the schema fails
 */
@Test
public void testRead_definitive_attributegroup() throws Exception {
    URI location = getClass().getResource("/testdata/definitive/attributegroup.xsd").toURI();
    LocatableInputSupplier<? extends InputStream> input = new DefaultInputSupplier(location);
    XmlIndex schema = (XmlIndex) readSchema(input);
    // ShirtType
    TypeDefinition type = schema.getType(new QName("ShirtType"));
    assertNotNull(type);
    // not there any more because it is flattened away
    // // IdentifierGroup
    // GroupPropertyDefinition group = type.getChild(new QName("IdentifierGroup")).asGroup();
    // assertNotNull(group);
    // // not a choice
    // assertFalse(group.getConstraint(ChoiceFlag.class).isEnabled());
    // id
    PropertyDefinition id = type.getChild(new QName("id")).asProperty();
    assertNotNull(id);
    // property type must be a simple type
    assertTrue(id.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
    // binding must be string
    assertEquals(String.class, id.getPropertyType().getConstraint(Binding.class).getBinding());
    // required
    Cardinality cc = id.getConstraint(Cardinality.class);
    assertEquals(1, cc.getMinOccurs());
    assertEquals(1, cc.getMaxOccurs());
    // version
    PropertyDefinition version = type.getChild(new QName("version")).asProperty();
    assertNotNull(version);
    // property type must be a simple type
    assertTrue(version.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
    // effDate
    PropertyDefinition effDate = type.getChild(new QName("effDate")).asProperty();
    assertNotNull(effDate);
    // binding must be compatible to Date
    assertTrue(Date.class.isAssignableFrom(effDate.getPropertyType().getConstraint(Binding.class).getBinding()));
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) QName(javax.xml.namespace.QName) XmlIndex(eu.esdihumboldt.hale.io.xsd.model.XmlIndex) URI(java.net.URI) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) Date(java.util.Date) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Example 4 with Binding

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

the class XmlSchemaReaderTest method testShiporderStructure.

/**
 * Test the shiporder structure
 *
 * @param shiporder the shiporder element
 * @param ns the namespace
 */
private void testShiporderStructure(XmlElement shiporder, String ns) {
    assertNotNull(shiporder);
    assertEquals("shiporder", shiporder.getName().getLocalPart());
    // shiporder type
    TypeDefinition shiporderType = shiporder.getType();
    assertNotNull(shiporderType);
    Collection<? extends ChildDefinition<?>> properties = shiporderType.getChildren();
    assertEquals(4, properties.size());
    // orderperson
    PropertyDefinition orderperson = shiporderType.getChild(new QName(ns, "orderperson")).asProperty();
    assertNotNull(orderperson);
    // property type must be a simple type
    assertTrue(orderperson.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
    // binding must be string
    assertEquals(String.class, orderperson.getPropertyType().getConstraint(Binding.class).getBinding());
    // cardinality
    Cardinality cc = orderperson.getConstraint(Cardinality.class);
    assertEquals(1, cc.getMinOccurs());
    assertEquals(1, cc.getMaxOccurs());
    // not nillable
    assertFalse(orderperson.getConstraint(NillableFlag.class).isEnabled());
    // shipto
    PropertyDefinition shipto = shiporderType.getChild(new QName(ns, "shipto")).asProperty();
    assertNotNull(shipto);
    // property type must be a complex type
    assertFalse(shipto.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
    // item
    PropertyDefinition item = shiporderType.getChild(new QName(ns, "item")).asProperty();
    assertNotNull(item);
    // property type must be a complex type
    assertFalse(item.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
    // item cardinality
    cc = item.getConstraint(Cardinality.class);
    assertEquals(1, cc.getMinOccurs());
    assertEquals(Cardinality.UNBOUNDED, cc.getMaxOccurs());
    // item properties
    TypeDefinition itemType = item.getPropertyType();
    Collection<? extends ChildDefinition<?>> itemProps = itemType.getChildren();
    assertEquals(4, itemProps.size());
    // title
    assertNotNull(itemType.getChild(new QName(ns, "title")).asProperty());
    // note
    PropertyDefinition note = itemType.getChild(new QName(ns, "note")).asProperty();
    assertNotNull(note);
    cc = note.getConstraint(Cardinality.class);
    assertEquals(0, cc.getMinOccurs());
    assertEquals(1, cc.getMaxOccurs());
    // quantity
    PropertyDefinition quantity = itemType.getChild(new QName(ns, "quantity")).asProperty();
    assertNotNull(quantity);
    assertTrue(quantity.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
    assertTrue(Number.class.isAssignableFrom(quantity.getPropertyType().getConstraint(Binding.class).getBinding()));
    // price
    PropertyDefinition price = itemType.getChild(new QName(ns, "price")).asProperty();
    assertNotNull(price);
    assertTrue(price.getPropertyType().getConstraint(HasValueFlag.class).isEnabled());
    assertTrue(Number.class.isAssignableFrom(price.getPropertyType().getConstraint(Binding.class).getBinding()));
    // orderid
    PropertyDefinition orderid = shiporderType.getChild(new QName(ns, "orderid")).asProperty();
    assertNotNull(orderid);
    // binding must be string
    assertEquals(String.class, orderid.getPropertyType().getConstraint(Binding.class).getBinding());
    // required
    cc = orderid.getConstraint(Cardinality.class);
    assertEquals(1, cc.getMinOccurs());
    assertEquals(1, cc.getMaxOccurs());
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) Cardinality(eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality) QName(javax.xml.namespace.QName) GroupPropertyDefinition(eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition) PropertyDefinition(eu.esdihumboldt.hale.common.schema.model.PropertyDefinition) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Example 5 with Binding

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

the class XLSReaderTest method testReadSimple.

/**
 * Test - read a sample xls schema and data from same file and sheet (simple
 * io test). Check the type, check the properties, check the values of the
 * properties, check the datatype of the properties
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testReadSimple() throws Exception {
    // read Schema ###
    Schema schema = readXLSSchema("/data/simpleOneSheet.xls", 0, typeName, "java.lang.String,java.lang.String,java.lang.String");
    // Test properties and their datatype
    TypeDefinition schemaType = schema.getType(QName.valueOf(typeName));
    Binding binding;
    for (ChildDefinition<?> child : schemaType.getChildren()) {
        binding = child.asProperty().getPropertyType().getConstraint(Binding.class);
        assertTrue(binding.getBinding().equals(String.class));
    }
    // Check every property for their existence
    for (String propertyName : properties) {
        assertEquals(propertyName, schemaType.getChild(QName.valueOf(propertyName)).getDisplayName());
    }
    // read Instances - not header ###
    InstanceCollection instances = readXLSInstances("/data/simpleOneSheet.xls", 0, typeName, 1, schema);
    assertTrue(instances.hasSize());
    assertEquals(numberOfInstances, instances.size());
    // read and skip N instances ###
    // no. of lines = header + 3 instances
    int numberOfLines = 4;
    for (int i = 0; i <= numberOfLines; i++) {
        InstanceCollection remainingInstances = readXLSInstances("/data/simpleOneSheet.xls", 0, typeName, i, schema);
        assertEquals(numberOfLines - i, remainingInstances.size());
    }
    // get Type to check property definition (schema and instance
    // combination)
    TypeDefinition type = instances.iterator().next().getDefinition();
    ChildDefinition<?> child = null;
    assertEquals(typeName, type.getDisplayName());
    for (int i = 0; i < properties.length; i++) {
        child = type.getChild(QName.valueOf(properties[i]));
        assertEquals(properties[i], child.getDisplayName());
    }
    // Check the values of the first (type) instance
    Instance instance = instances.iterator().next();
    Object[] value;
    for (int i = 0; i < dataFirstColumn.length; i++) {
        value = instance.getProperty(QName.valueOf(properties[i]));
        assertEquals(dataFirstColumn[i], value[0]);
        assertTrue(value[0] instanceof String);
    }
}
Also used : Binding(eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) Test(org.junit.Test)

Aggregations

Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)17 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)12 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)6 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)4 ElementType (eu.esdihumboldt.hale.common.schema.model.constraint.type.ElementType)4 Geometry (org.locationtech.jts.geom.Geometry)4 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)3 HasValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.HasValueFlag)3 Test (org.junit.Test)3 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)2 GroupPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.GroupPropertyDefinition)2 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)2 Cardinality (eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality)2 AugmentedValueFlag (eu.esdihumboldt.hale.common.schema.model.constraint.type.AugmentedValueFlag)2 GeometryType (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryType)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 MultiValue (eu.esdihumboldt.cst.MultiValue)1 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)1 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1