Search in sources :

Example 6 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class XLSReaderTest method testPropertyType.

/**
 * Test - check declaration of properties' datatype.
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testPropertyType() throws Exception {
    Schema schema = readXLSSchema("/data/simpleOneSheet.xls", 0, typeName, "java.lang.Integer,java.lang.String,java.lang.String");
    // Test property datatype
    TypeDefinition schemaType = schema.getType(QName.valueOf(typeName));
    Binding binding = schemaType.getChildren().iterator().next().asProperty().getPropertyType().getConstraint(Binding.class);
    assertTrue("The type is not an Integer.", binding.getBinding().equals(Integer.class));
    // ### Instance
    InstanceCollection instances = readXLSInstances("/data/simpleOneSheet.xls", 0, typeName, true, schema);
    assertTrue(instances.hasSize());
    assertEquals(numberOfInstances, instances.size());
    // Check the values of the first (type) instance
    Instance instance = instances.iterator().next();
    Object[] value = instance.getProperty(QName.valueOf(properties[0]));
    assertEquals(Integer.valueOf(dataFirstColumn[0]), value[0]);
    assertTrue(value[0] instanceof Integer);
}
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)

Example 7 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class XLSReaderTest method testReadSameFileDiffSheet.

/**
 * Test - read a sample xls schema and data from same file and different
 * sheets. Check skip first
 *
 * @throws Exception , if an error occurs
 */
@Test
public void testReadSameFileDiffSheet() throws Exception {
    String sourceLocation = "/data/simpleTwoSheet.xls";
    Schema schema = readXLSSchema(sourceLocation, 0, typeName, "java.lang.String,java.lang.String,java.lang.String");
    TypeDefinition schemaType = schema.getType(QName.valueOf(typeName));
    // Check every property for their existence
    for (String propertyName : properties) {
        assertEquals(propertyName, schemaType.getChild(QName.valueOf(propertyName)).getDisplayName());
    }
    // Instance Read ###
    InstanceCollection instances = readXLSInstances(sourceLocation, 1, typeName, false, schema);
    assertTrue(instances.hasSize());
    assertEquals(numberOfInstances, instances.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 : 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)

Example 8 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition 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 ###
    InstanceCollection instances = readXLSInstances("/data/simpleOneSheet.xls", 0, typeName, true, schema);
    assertTrue(instances.hasSize());
    assertEquals(numberOfInstances, instances.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)

Example 9 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition in project hale by halestudio.

the class XLSInstanceWriter method execute.

/**
 * @see eu.esdihumboldt.hale.common.core.io.impl.AbstractIOProvider#execute(eu.esdihumboldt.hale.common.core.io.ProgressIndicator,
 *      eu.esdihumboldt.hale.common.core.io.report.IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    boolean solveNestedProperties = getParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES).as(Boolean.class, false);
    // get the parameter to get the type definition
    String exportType = getParameter(InstanceTableIOConstants.EXPORT_TYPE).as(String.class);
    QName selectedTypeName = null;
    if (exportType != null && !exportType.equals("") && !exportType.equals(" ")) {
        selectedTypeName = QName.valueOf(exportType);
    }
    // write xls file
    if (getContentType().getId().equals("eu.esdihumboldt.hale.io.xls.xls")) {
        wb = new HSSFWorkbook();
    } else // write xlsx file
    if (getContentType().getId().equals("eu.esdihumboldt.hale.io.xls.xlsx")) {
        wb = new XSSFWorkbook();
    } else {
        reporter.error(new IOMessageImpl("Content type is invalid!", null));
        return reporter;
    }
    cellStyle = XLSCellStyles.getNormalStyle(wb, false);
    headerStyle = XLSCellStyles.getHeaderStyle(wb);
    // get all instances of the selected Type
    InstanceCollection instances = getInstanceCollection(selectedTypeName);
    Iterator<Instance> instanceIterator = instances.iterator();
    Instance instance = null;
    try {
        instance = instanceIterator.next();
    } catch (NoSuchElementException e) {
        reporter.error(new IOMessageImpl("There are no instances for the selected type.", e));
        return reporter;
    }
    List<Instance> remainingInstances = new ArrayList<Instance>();
    headerRowStrings = new ArrayList<String>();
    // all instances with equal type definitions are stored in an extra
    // sheet
    TypeDefinition definition = instance.getDefinition();
    Sheet sheet = wb.createSheet(definition.getDisplayName());
    Row headerRow = sheet.createRow(0);
    int rowNum = 1;
    Row row = sheet.createRow(rowNum++);
    writeRow(row, super.getPropertyMap(instance, headerRowStrings, solveNestedProperties));
    while (instanceIterator.hasNext()) {
        Instance nextInst = instanceIterator.next();
        if (nextInst.getDefinition().equals(definition)) {
            row = sheet.createRow(rowNum++);
            writeRow(row, super.getPropertyMap(nextInst, headerRowStrings, solveNestedProperties));
        } else
            remainingInstances.add(nextInst);
    }
    writeHeaderRow(headerRow, headerRowStrings);
    setCellStyle(sheet, headerRowStrings.size());
    resizeSheet(sheet);
    // write file
    FileOutputStream out = new FileOutputStream(getTarget().getLocation().getPath());
    wb.write(out);
    out.close();
    reporter.setSuccess(true);
    return reporter;
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) ArrayList(java.util.ArrayList) RichTextString(org.apache.poi.ss.usermodel.RichTextString) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) NoSuchElementException(java.util.NoSuchElementException)

Example 10 with TypeDefinition

use of eu.esdihumboldt.hale.common.schema.model.TypeDefinition 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)

Aggregations

TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)192 QName (javax.xml.namespace.QName)58 PropertyDefinition (eu.esdihumboldt.hale.common.schema.model.PropertyDefinition)38 ArrayList (java.util.ArrayList)32 TypeEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition)26 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)26 Test (org.junit.Test)24 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)22 PropertyEntityDefinition (eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition)21 HashSet (java.util.HashSet)21 EntityDefinition (eu.esdihumboldt.hale.common.align.model.EntityDefinition)20 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)20 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)16 ChildContext (eu.esdihumboldt.hale.common.align.model.ChildContext)15 XmlElement (eu.esdihumboldt.hale.io.xsd.model.XmlElement)15 Cell (eu.esdihumboldt.hale.common.align.model.Cell)14 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)14 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)12 Binding (eu.esdihumboldt.hale.common.schema.model.constraint.type.Binding)12 DefaultPropertyDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultPropertyDefinition)12