use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class AssignFromCollectorExplanation method customizeBinding.
@Override
protected void customizeBinding(Map<String, Object> binding, Cell cell, boolean html, ServiceProvider provider, Locale locale) {
// Default values for all bindings
binding.put("_constraintsEvaluated", false);
binding.put("_hasValue", false);
binding.put("_isReference", false);
Entity entity = CellUtil.getFirstEntity(cell.getTarget());
if (entity != null && entity.getDefinition().getDefinition() instanceof PropertyDefinition) {
PropertyDefinition resultProperty = (PropertyDefinition) entity.getDefinition().getDefinition();
TypeDefinition resultPropertyType = resultProperty.getPropertyType();
boolean isReference = resultProperty.getConstraint(Reference.class).isReference();
binding.put("_isReference", isReference);
// determine this.
if (isReference) {
binding.put("_hasValue", true);
} else {
boolean hasValue = resultPropertyType.getConstraint(HasValueFlag.class).isEnabled();
binding.put("_hasValue", hasValue);
}
binding.put("_constraintsEvaluated", true);
}
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class DefaultAlignmentIOTest method testBaseAlignmentSaveLoad.
/**
* Tests base alignment add, save and load.
*
* @throws Exception if an error occurs
*/
@Test
public void testBaseAlignmentSaveLoad() throws Exception {
DefaultAlignment baseAlignment = new DefaultAlignment();
MutableAlignment alignment = new DefaultAlignment();
Schema schema = TestUtil.loadSchema(getClass().getResource("/testdata/simple/t1.xsd").toURI());
Iterator<? extends TypeDefinition> iter = schema.getMappingRelevantTypes().iterator();
TypeDefinition t = iter.next();
if (!t.getName().getLocalPart().equals("T1")) {
t = iter.next();
}
DefaultCell cell1 = new DefaultCell();
cell1.setTransformationIdentifier("trans1");
ListMultimap<String, Type> source = ArrayListMultimap.create();
source.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.SOURCE, null)));
cell1.setSource(source);
ListMultimap<String, Type> target = ArrayListMultimap.create();
target.put(null, new DefaultType(new TypeEntityDefinition(t, SchemaSpaceID.TARGET, null)));
cell1.setTarget(target);
DefaultCell cell2 = new DefaultCell();
cell2.setTransformationIdentifier("trans2");
List<ChildContext> childContext = new ArrayList<ChildContext>();
PropertyDefinition child = DefinitionUtil.getChild(t, new QName("a1")).asProperty();
childContext.add(new ChildContext(child));
ListMultimap<String, Property> source2 = ArrayListMultimap.create();
source2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext, SchemaSpaceID.SOURCE, null)));
cell2.setSource(source2);
ListMultimap<String, Property> target2 = ArrayListMultimap.create();
target2.put(null, new DefaultProperty(new PropertyEntityDefinition(t, childContext, SchemaSpaceID.TARGET, null)));
cell2.setTarget(target2);
// add cell1 to base alignment
baseAlignment.addCell(cell1);
// save base alignment
File baseAlignmentFile = tmp.newFile("alignment_base.xml");
System.out.println(baseAlignmentFile.getAbsolutePath());
saveAlignment(baseAlignment, new BufferedOutputStream(new FileOutputStream(baseAlignmentFile)));
// add as base alignment to extended alignment
addBaseAlignment(alignment, baseAlignmentFile.toURI(), schema, schema);
assertEquals(1, alignment.getBaseAlignments().size());
String usedPrefix = alignment.getBaseAlignments().keySet().iterator().next();
assertEquals(1, alignment.getCells().size());
assertEquals(usedPrefix + ":" + cell1.getId(), alignment.getCells().iterator().next().getId());
// add cell2 to extended alignment
alignment.addCell(cell2);
assertEquals(2, alignment.getCells().size());
assertEquals(1, alignment.getPropertyCells(cell1).size());
// save extended alignment
File alignmentFile = tmp.newFile("alignment_extended.xml");
System.out.println(alignmentFile.getAbsolutePath());
saveAlignment(alignment, new BufferedOutputStream(new FileOutputStream(alignmentFile)));
// load extended
MutableAlignment alignment2 = loadAlignment(new FileInputStream(alignmentFile), schema, schema);
assertEquals(2, alignment2.getCells().size());
assertEquals(1, alignment2.getTypeCells().size());
Cell typeCell = alignment2.getTypeCells().iterator().next();
assertTrue(typeCell instanceof BaseAlignmentCell);
assertEquals(usedPrefix + ":" + cell1.getId(), typeCell.getId());
assertEquals(1, alignment2.getPropertyCells(typeCell).size());
assertFalse(alignment2.getPropertyCells(typeCell).iterator().next() instanceof BaseAlignmentCell);
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class EntityDefinitionComparator method compare.
@Override
public int compare(EntityDefinition o1, EntityDefinition o2) {
// first, compare type
int compare = compareType(o1, o2);
if (compare != 0) {
return compare;
}
Iterator<ChildContext> o1iterator = o1.getPropertyPath().iterator();
Iterator<ChildContext> o2iterator = o2.getPropertyPath().iterator();
while (o1iterator.hasNext() && o2iterator.hasNext()) {
ChildContext o1c = null;
PropertyDefinition o1def = null;
// get first property definition (skip groups)
while (o1def == null && o1iterator.hasNext()) {
o1c = o1iterator.next();
o1def = o1c.getChild().asProperty();
}
ChildContext o2c = null;
PropertyDefinition o2def = null;
// get first property definition (skip groups)
while (o2def == null && o2iterator.hasNext()) {
o2c = o2iterator.next();
o2def = o2c.getChild().asProperty();
}
if (o1def != null && o2def != null) {
// compare local name
int comp = o1def.getName().getLocalPart().compareTo(o2def.getName().getLocalPart());
if (comp == 0) {
// compare namespace
comp = o1def.getName().getNamespaceURI().compareTo(o2def.getName().getNamespaceURI());
}
if (comp == 0) {
// compare context
comp = compareContext(o1c, o2c);
}
if (comp != 0) {
// properties are different
return comp;
}
// properties are the same, continue
} else if (o1def != null && o2def == null) {
// o2 after o1
return -1;
} else if (o1def == null && o2def != null) {
// o1 after o2
return 1;
}
}
// shorter paths first
if (o1iterator.hasNext() && !o2iterator.hasNext()) {
// o1 after o2
return 1;
} else if (!o1iterator.hasNext() && o2iterator.hasNext()) {
// o2 after o1
return -1;
}
return 0;
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class JDBCInstanceWriter method populateInsertStatementOrExecuteAutoIncStatement.
/**
* Populate a prepared insert statement with values from the given instance.
* Checks if the property has auto incremental constraint, if it has then it
* maps the original old id to the id that is auto incremented while
* inserting the value. This mapping is used when inserting the foreign key
* values associated with those auto incremented column ids whose value has
* been changed. Thus, insertion of the foreign key wont fail. It will
* either execute the statement directly or add it into the batches
* depending upon the auto incremented flag.
*
* @param statement the insert statement
* @param properties the properties to fill the statement with
* @param instance the instance
* @param reporter the reporter
* @param conn Connection (used for Geometry conversion for oracle)
* @throws SQLException if configuring the statement fails
*/
private void populateInsertStatementOrExecuteAutoIncStatement(PreparedStatement statement, Set<QName> properties, Instance instance, IOReporter reporter, Connection conn) throws SQLException {
TypeDefinition type = instance.getDefinition();
int index = 1;
Object oldValue = null;
boolean isAutoIncremented = false;
for (QName propertyName : properties) {
PropertyDefinition property = (PropertyDefinition) type.getChild(propertyName);
Object[] values = instance.getProperty(propertyName);
SQLType sqlType = property.getPropertyType().getConstraint(SQLType.class);
if (!sqlType.isSet()) {
reporter.error(new IOMessageImpl("SQL type not set. Please only export to schemas read from a database.", null));
statement.setObject(index, null);
continue;
}
SQLArray arrayInfo = property.getPropertyType().getConstraint(SQLArray.class);
Object value;
if (arrayInfo.isArray() && arrayInfo.getDimension() <= 1) {
// array as multiple occurrence property
value = (values == null) ? (new Object[0]) : (values);
} else {
// single value
if (values != null && values.length > 1)
reporter.warn(new IOMessageImpl("Multiple values for a property. Only exporting first.", null));
value = (values == null || values.length == 0) ? null : values[0];
}
AutoGenerated auto = property.getConstraint(AutoGenerated.class);
if (!isWriteUnordered()) {
if (auto.isEnabled()) {
isAutoIncremented = true;
if (value != null) {
oldValue = value;
}
continue;
}
}
Reference ref = property.getConstraint(Reference.class);
if (ref.getReferencedTypes() != null) {
TypeDefinition td = (TypeDefinition) ref.getReferencedTypes().toArray()[0];
Map<Object, Long> marshMallow = typAuto.get(td);
if (marshMallow != null && value != null) {
// lookup identifier for reference
value = marshMallow.get(processLookupId(value));
}
}
if (values == null || values.length == 0) {
// XXX The default value could be a function call.
// Better would be to leave the column out of the insert
// statement, or set it to the SQL keyword "DEFAULT".
DefaultValue defaultValue = property.getConstraint(DefaultValue.class);
if (defaultValue.isSet())
statement.setObject(index, defaultValue.getValue(), sqlType.getType());
else if (property.getConstraint(NillableFlag.class).isEnabled())
statement.setNull(index, sqlType.getType());
else {
// no default, not nillable, will not work...
// set it to null here and let query fail (probably)
// XXX maybe skip this insert?
statement.setNull(index, sqlType.getType());
reporter.warn(new IOMessageImpl("Property no value, not nillable, no default value, insert will probably fail.", null));
}
} else if (value == null)
statement.setNull(index, sqlType.getType());
else
setStatementParameter(statement, index, value, property, sqlType.getType(), reporter, conn);
index++;
}
if (isAutoIncremented) {
statement.execute();
ResultSet rs = statement.getGeneratedKeys();
Long newValue = null;
while (rs.next()) {
newValue = rs.getLong(1);
}
addIDMapping(type, oldValue, newValue);
} else {
statement.addBatch();
}
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class IdentifierParameterPage method createContent.
@Override
protected void createContent(Composite parent) {
boolean relayout = this.parent != null;
this.parent = parent;
lastEntity = determineTargetEntity();
if (lastEntity == null) {
setPageComplete(false);
// can't create controls
return;
}
if (page != null) {
// page was created before
initialCountry = countryCode.getText();
initialProvider = providerName.getText();
initialProduct = productName.getText();
initialVersion = version.getText();
initialVersionNil = nilEditor.getAsText();
page.dispose();
}
PropertyFunctionDefinition function = FunctionUtil.getPropertyFunction(ID, HaleUI.getServiceProvider());
// create a composite to hold the widgets
page = new Composite(parent, SWT.NULL);
setControl(page);
// create layout for this wizard page
GridLayout gl = GridLayoutFactory.fillDefaults().create();
page.setLayout(gl);
AttributeEditorFactory aef = PlatformUI.getWorkbench().getService(AttributeEditorFactory.class);
DefinitionLabelFactory dlf = PlatformUI.getWorkbench().getService(DefinitionLabelFactory.class);
// identifier type
TypeDefinition identifierType = null;
Definition<?> def = lastEntity.getDefinition();
if (def instanceof PropertyDefinition) {
TypeDefinition typeDef = ((PropertyDefinition) def).getPropertyType();
for (ChildDefinition<?> child : typeDef.getChildren()) {
if (child.asProperty() != null && child.asProperty().getPropertyType().getName().getLocalPart().equals("IdentifierType")) {
identifierType = child.asProperty().getPropertyType();
}
}
}
// Namespace group
Group nsGroup = new Group(page, SWT.NONE);
nsGroup.setText(Messages.IdentifierFunctionWizardPage_2);
nsGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
nsGroup.setLayout(//
GridLayoutFactory.swtDefaults().numColumns(//
2).spacing(8, //
4).create());
// localId
if (identifierType != null) {
// $NON-NLS-1$
PropertyDefinition propDef = null;
for (ChildDefinition<?> child : identifierType.getChildren()) {
String localName = child.getName().getLocalPart();
if (localName.equals("namespace")) {
if (child.asProperty() != null) {
propDef = child.asProperty();
}
}
}
if (propDef != null) {
Control nsLabel = dlf.createLabel(nsGroup, propDef, false);
nsLabel.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
Label nsDesc = new Label(nsGroup, SWT.NONE);
nsDesc.setText(Messages.IdentifierFunctionWizardPage_4);
}
}
// Country code
Label ccLabel = new Label(nsGroup, SWT.NONE);
ccLabel.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
ccLabel.setText(Messages.IdentifierFunctionWizardPage_5);
FunctionParameterDefinition param = function.getParameter(COUNTRY_PARAMETER_NAME);
configureParameterLabel(ccLabel, param);
this.countryCode = new Text(nsGroup, SWT.BORDER | SWT.SINGLE);
// $NON-NLS-1$
this.countryCode.setText(initialCountry);
this.countryCode.setEnabled(true);
this.countryCode.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// Provider name
Label providerLabel = new Label(nsGroup, SWT.NONE);
providerLabel.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
providerLabel.setText(Messages.IdentifierFunctionWizardPage_7);
param = function.getParameter(DATA_PROVIDER_PARAMETER_NAME);
configureParameterLabel(providerLabel, param);
this.providerName = new Text(nsGroup, SWT.BORDER | SWT.SINGLE);
// $NON-NLS-1$
this.providerName.setText(initialProvider);
this.providerName.setEnabled(true);
this.providerName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// Product name
Label productLabel = new Label(nsGroup, SWT.NONE);
productLabel.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
productLabel.setText(Messages.IdentifierFunctionWizardPage_9);
param = function.getParameter(PRODUCT_PARAMETER_NAME);
configureParameterLabel(productLabel, param);
this.productName = new Text(nsGroup, SWT.BORDER | SWT.SINGLE);
// $NON-NLS-1$
this.productName.setText(initialProduct);
this.productName.setEnabled(true);
this.productName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// Local ID group
Group idGroup = new Group(page, SWT.NONE);
idGroup.setText(Messages.IdentifierFunctionWizardPage_11);
idGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
idGroup.setLayout(new GridLayout(2, false));
// localId
Control idLabel = null;
if (identifierType != null) {
// $NON-NLS-1$
PropertyDefinition propDef = null;
for (ChildDefinition<?> child : identifierType.getChildren()) {
String LocalName = child.getName().getLocalPart();
if (LocalName.equals("localId")) {
if (child.asProperty() != null) {
propDef = child.asProperty();
}
}
}
if (propDef != null) {
idLabel = dlf.createLabel(idGroup, propDef, false);
}
}
if (idLabel == null) {
idLabel = new Label(idGroup, SWT.NONE);
// $NON-NLS-1$
((Label) idLabel).setText("localId");
}
idLabel.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
Control localId = dlf.createLabel(idGroup, getWizard().getUnfinishedCell().getSource().get(null).get(0).getDefinition().getDefinition(), true);
localId.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// Version group
Group versGroup = new Group(page, SWT.NONE);
versGroup.setText(Messages.IdentifierFunctionWizardPage_14);
versGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
versGroup.setLayout(new GridLayout(2, false));
// Version
Control versionLabel = null;
if (identifierType != null) {
// $NON-NLS-1$
PropertyDefinition propDef = null;
for (ChildDefinition<?> child : identifierType.getChildren()) {
String localName = child.getName().getLocalPart();
if (localName.equals("versionId")) {
if (child.asProperty() != null) {
propDef = child.asProperty();
}
}
}
if (propDef != null) {
versionLabel = dlf.createLabel(versGroup, propDef, false);
}
}
if (versionLabel == null) {
versionLabel = new Label(versGroup, SWT.NONE);
((Label) versionLabel).setText(Messages.IdentifierFunctionWizardPage_16);
}
versionLabel.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
this.version = new Text(versGroup, SWT.BORDER | SWT.SINGLE);
// $NON-NLS-1$
this.version.setText(initialVersion);
this.version.setEnabled(true);
this.version.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// version nil reason
if (identifierType != null) {
// $NON-NLS-1$
PropertyDefinition propDef = null;
for (ChildDefinition<?> child : identifierType.getChildren()) {
String localName = child.getName().getLocalPart();
if (localName.equals("versionId")) {
if (child.asProperty() != null) {
propDef = child.asProperty();
}
}
}
if (propDef != null) {
for (ChildDefinition<?> child : propDef.getPropertyType().getChildren()) {
String localName = child.getName().getLocalPart();
if (localName.equals("nilReason")) {
if (child.asProperty() != null) {
propDef = child.asProperty();
}
}
}
}
if (propDef != null) {
// label
Control nilLabel = dlf.createLabel(versGroup, propDef, false);
nilLabel.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
// editor
nilEditor = aef.createEditor(versGroup, propDef, null, false);
nilEditor.getControl().setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// default to unknown
nilEditor.setAsText(initialVersionNil);
// //$NON-NLS-1$
}
}
if (relayout) {
parent.layout();
getContainer().getShell().pack();
}
setPageComplete(true);
}
Aggregations