Search in sources :

Example 6 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance 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 7 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.

the class TransformationTreeContentProvider method getElements.

/**
 * @see ArrayContentProvider#getElements(Object)
 */
@SuppressWarnings("unchecked")
@Override
public Object[] getElements(Object inputElement) {
    if (inputElement instanceof TransformationTree)
        return collectNodes((TransformationTree) inputElement).toArray();
    Collection<Instance> instances = null;
    if (inputElement instanceof Pair<?, ?>) {
        Pair<?, ?> pair = (Pair<?, ?>) inputElement;
        inputElement = pair.getFirst();
        if (pair.getSecond() instanceof Collection<?>) {
            instances = (Collection<Instance>) pair.getSecond();
        }
    }
    if (inputElement instanceof Alignment) {
        Alignment alignment = (Alignment) inputElement;
        // input contained specific instances
        if (instances != null && !instances.isEmpty()) {
            Collection<Object> result = new ArrayList<Object>();
            // create transformation trees for each instance
            for (Instance instance : instances) {
                // find type cells matching the instance
                Collection<? extends Cell> typeCells = alignment.getActiveTypeCells();
                Collection<Cell> associatedTypeCells = new LinkedList<Cell>();
                for (Cell typeCell : typeCells) for (Entity entity : typeCell.getSource().values()) {
                    TypeEntityDefinition type = (TypeEntityDefinition) entity.getDefinition();
                    if (type.getDefinition().equals(instance.getDefinition()) && (type.getFilter() == null || type.getFilter().match(instance))) {
                        associatedTypeCells.add(typeCell);
                        break;
                    }
                }
                // for each type cell one tree
                for (Cell cell : associatedTypeCells) {
                    TransformationTree tree = createInstanceTree(instance, cell, alignment);
                    if (tree != null)
                        result.addAll(collectNodes(tree));
                }
            }
            return result.toArray();
        }
        // input was alignment only, show trees for all type cells
        Collection<? extends Cell> typeCells = alignment.getActiveTypeCells();
        Collection<Object> result = new ArrayList<Object>(typeCells.size());
        for (Cell typeCell : typeCells) {
            // create tree and add nodes for each cell
            result.addAll(collectNodes(new TransformationTreeImpl(alignment, typeCell)));
        }
        return result.toArray();
    }
    return super.getElements(inputElement);
}
Also used : TransformationTreeImpl(eu.esdihumboldt.hale.common.align.model.transformation.tree.impl.TransformationTreeImpl) Entity(eu.esdihumboldt.hale.common.align.model.Entity) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) ArrayList(java.util.ArrayList) TransformationTree(eu.esdihumboldt.hale.common.align.model.transformation.tree.TransformationTree) LinkedList(java.util.LinkedList) Alignment(eu.esdihumboldt.hale.common.align.model.Alignment) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) Collection(java.util.Collection) Cell(eu.esdihumboldt.hale.common.align.model.Cell) Pair(eu.esdihumboldt.util.Pair)

Example 8 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.

the class GroovyJoinPage method validate.

@Override
protected boolean validate(String document) {
    ParameterValue param = CellUtil.getFirstParameter(getWizard().getUnfinishedCell(), JoinFunction.PARAMETER_JOIN);
    JoinParameter joinParameter = param.as(JoinParameter.class);
    // check Join parameter
    if (joinParameter == null) {
        // setValidationError("Missing join configuration");
        return false;
    } else {
        String error = joinParameter.validate();
        if (!setValidationError(error)) {
            return false;
        }
    }
    // target type
    Type targetType = (Type) CellUtil.getFirstEntity(getWizard().getUnfinishedCell().getTarget());
    if (targetType == null) {
        // not yet selected (NewRelationWizard)
        return false;
    }
    /*
		 * FIXME use JoinParameter to fake joined instances!
		 * 
		 * XXX for now just base instance
		 */
    TypeEntityDefinition sourceType = joinParameter.getTypes().get(0);
    InstanceBuilder builder = new InstanceBuilder(false);
    Instance instance = getTestValues().get(sourceType);
    if (instance == null) {
        // use an empty instance as input for the script
        instance = new DefaultInstance(sourceType.getDefinition(), DataSet.SOURCE);
    }
    FamilyInstance source = new FamilyInstanceImpl(instance);
    // prepare binding
    Cell cell = getWizard().getUnfinishedCell();
    CellLog log = new CellLog(new DefaultTransformationReporter("dummy", false), cell);
    ExecutionContext context = new DummyExecutionContext(HaleUI.getServiceProvider());
    Binding binding = GroovyRetype.createBinding(source, cell, builder, log, context, targetType.getDefinition().getDefinition());
    GroovyService service = HaleUI.getServiceProvider().getService(GroovyService.class);
    Script script = null;
    try {
        script = service.parseScript(document, binding);
        GroovyUtil.evaluateAll(script, builder, targetType.getDefinition().getDefinition(), service, log);
    } catch (final Exception e) {
        return handleValidationResult(script, e);
    }
    return handleValidationResult(script, null);
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) ParameterValue(eu.esdihumboldt.hale.common.align.model.ParameterValue) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) DefaultTransformationReporter(eu.esdihumboldt.hale.common.align.transformation.report.impl.DefaultTransformationReporter) JoinParameter(eu.esdihumboldt.hale.common.align.model.functions.join.JoinParameter) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) FamilyInstanceImpl(eu.esdihumboldt.hale.common.align.transformation.function.impl.FamilyInstanceImpl) Type(eu.esdihumboldt.hale.common.align.model.Type) TypeEntityDefinition(eu.esdihumboldt.hale.common.align.model.impl.TypeEntityDefinition) ExecutionContext(eu.esdihumboldt.hale.common.align.transformation.function.ExecutionContext) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) FamilyInstance(eu.esdihumboldt.hale.common.instance.model.FamilyInstance) Cell(eu.esdihumboldt.hale.common.align.model.Cell) CellLog(eu.esdihumboldt.hale.common.align.transformation.report.impl.CellLog) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Example 9 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.

the class GmlInstanceCollectionTest method testLoadShiporder.

/**
 * Test loading a simple XML file with one instance
 *
 * @throws Exception if an error occurs
 */
@Test
public void testLoadShiporder() throws Exception {
    GmlInstanceCollection instances = loadInstances(getClass().getResource("/data/shiporder/shiporder.xsd").toURI(), getClass().getResource("/data/shiporder/shiporder.xml").toURI(), false);
    String ns = "http://www.example.com";
    ResourceIterator<Instance> it = instances.iterator();
    try {
        assertTrue(it.hasNext());
        Instance instance = it.next();
        assertNotNull(instance);
        // attribute
        Object[] orderid = instance.getProperty(new QName("orderid"));
        // form
        // not
        // qualified
        assertNotNull(orderid);
        assertEquals(1, orderid.length);
        assertEquals("889923", orderid[0]);
        Object[] orderperson = instance.getProperty(new QName(ns, "orderperson"));
        assertNotNull(orderperson);
        assertEquals(1, orderperson.length);
        assertEquals("John Smith", orderperson[0]);
        Object[] shipto = instance.getProperty(new QName(ns, "shipto"));
        assertNotNull(shipto);
        assertEquals(1, shipto.length);
        assertTrue(shipto[0] instanceof Instance);
        Instance shipto1 = (Instance) shipto[0];
        Object[] shiptoName = shipto1.getProperty(new QName(ns, "name"));
        assertNotNull(shiptoName);
        assertEquals(1, shiptoName.length);
        assertEquals("Ola Nordmann", shiptoName[0]);
        Object[] shiptoAddress = shipto1.getProperty(new QName(ns, "address"));
        assertNotNull(shiptoAddress);
        assertEquals(1, shiptoAddress.length);
        assertEquals("Langgt 23", shiptoAddress[0]);
        Object[] shiptoCity = shipto1.getProperty(new QName(ns, "city"));
        assertNotNull(shiptoCity);
        assertEquals(1, shiptoCity.length);
        assertEquals("4000 Stavanger", shiptoCity[0]);
        Object[] shiptoCountry = shipto1.getProperty(new QName(ns, "country"));
        assertNotNull(shiptoCountry);
        assertEquals(1, shiptoCountry.length);
        assertEquals("Norway", shiptoCountry[0]);
        // items
        Object[] items = instance.getProperty(new QName(ns, "item"));
        assertNotNull(items);
        assertEquals(2, items.length);
        // item 1
        Object item1 = items[0];
        assertTrue(item1 instanceof Instance);
        Object[] title1 = ((Instance) item1).getProperty(new QName(ns, "title"));
        assertNotNull(title1);
        assertEquals(1, title1.length);
        assertEquals("Empire Burlesque", title1[0]);
        // item 2
        Object item2 = items[1];
        assertTrue(item2 instanceof Instance);
        Object[] title2 = ((Instance) item2).getProperty(new QName(ns, "title"));
        assertNotNull(title2);
        assertEquals(1, title2.length);
        assertEquals("Hide your heart", title2[0]);
        // only one object
        assertFalse(it.hasNext());
    } finally {
        it.close();
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) Test(org.junit.Test)

Example 10 with Instance

use of eu.esdihumboldt.hale.common.instance.model.Instance in project hale by halestudio.

the class GmlInstanceCollectionTest method testLoadChoice2.

/**
 * Test loading a simple XML file with one instance including a choice and a
 * sub-choice.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testLoadChoice2() throws Exception {
    GmlInstanceCollection instances = loadInstances(getClass().getResource("/data/group/choice2.xsd").toURI(), getClass().getResource("/data/group/choice2.xml").toURI(), false);
    ResourceIterator<Instance> it = instances.iterator();
    try {
        assertTrue(it.hasNext());
        Instance instance = it.next();
        assertNotNull(instance);
        // choice
        Object[] choice_1 = instance.getProperty(new QName("/ItemsType", "choice_1"));
        assertNotNull(choice_1);
        assertEquals(7, choice_1.length);
        // TODO
        // only one object
        assertFalse(it.hasNext());
    } finally {
        it.close();
    }
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) Test(org.junit.Test)

Aggregations

Instance (eu.esdihumboldt.hale.common.instance.model.Instance)203 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)131 Test (org.junit.Test)122 AbstractHandlerTest (eu.esdihumboldt.hale.io.gml.geometry.handler.internal.AbstractHandlerTest)97 QName (javax.xml.namespace.QName)29 ArrayList (java.util.ArrayList)26 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)25 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)23 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)22 Group (eu.esdihumboldt.hale.common.instance.model.Group)15 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)13 Coordinate (com.vividsolutions.jts.geom.Coordinate)12 Geometry (com.vividsolutions.jts.geom.Geometry)12 FamilyInstance (eu.esdihumboldt.hale.common.instance.model.FamilyInstance)10 Polygon (com.vividsolutions.jts.geom.Polygon)9 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)8 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)8 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)8 HashSet (java.util.HashSet)8 Point (com.vividsolutions.jts.geom.Point)7