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;
}
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);
}
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);
}
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();
}
}
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();
}
}
Aggregations