use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition 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(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()));
}
}
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class JDBCInstanceWriter method getInsertStatement.
/**
* Create a prepared insert statement, based on the given type definition.
* Currently, auto incremented fields are not inserted into the statement.
*
* @param type the type definition
* @param properties the set properties of the instance for which this
* statement is
* @param typeStatements the already created statements
* @param connection the database connection
* @return the insert statement
* @throws SQLException if creating the prepared statement fails
*/
private PreparedStatement getInsertStatement(TypeDefinition type, Set<QName> properties, Map<TypeDefinition, Map<Set<QName>, PreparedStatement>> typeStatements, Connection connection) throws SQLException {
Map<Set<QName>, PreparedStatement> typeSpecificMap = typeStatements.get(type);
if (typeSpecificMap == null) {
typeSpecificMap = new HashMap<Set<QName>, PreparedStatement>();
typeStatements.put(type, typeSpecificMap);
}
PreparedStatement result = typeSpecificMap.get(properties);
if (result == null) {
String tableName = type.getConstraint(DatabaseTable.class).getFullTableName();
// create prepared statement SQL
StringBuffer pSql = new StringBuffer();
pSql.append("INSERT INTO ");
pSql.append(tableName);
pSql.append(" (");
StringBuffer valuesSql = new StringBuffer();
boolean first = true;
for (QName property : properties) {
AutoGenerated auto = ((PropertyDefinition) type.getChild(property)).getConstraint(AutoGenerated.class);
// statement (if writing is ordered)
if (!auto.isEnabled() || isWriteUnordered()) {
if (first)
first = false;
else {
pSql.append(", ");
valuesSql.append(",");
}
pSql.append('"').append(property.getLocalPart()).append('"');
valuesSql.append('?');
}
}
pSql.append(") VALUES (");
pSql.append(valuesSql);
pSql.append(")");
// XXX Actually we don't necessarily need the auto generated keys,
// we need the primary key!
// XXX , Statement.RETURN_GENERATED_KEYS does not work with batches
// in PostgreSQL
// Auto generated keys are returned, for creating a map with old
// value and auto generated.
// As it does not work with batches in PostGreSQL, each instance
// which has auto incremented ids are written one at a time and
// returned the auto generated key.
result = connection.prepareStatement(pSql.toString(), Statement.RETURN_GENERATED_KEYS);
typeSpecificMap.put(properties, result);
}
return result;
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class GeographicalName method evaluate.
/**
* @see AbstractSingleTargetPropertyTransformation#evaluate(String,
* TransformationEngine, ListMultimap, String,
* PropertyEntityDefinition, Map, TransformationLog)
*/
@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 {
// list of all source properties
List<PropertyValue> inputs = variables.get(null);
if (inputs.isEmpty()) {
// no input, so don't create any structure
throw new NoResultException();
}
// get all parameters defined by the wizard page
String ipa = getParameterChecked(PROPERTY_PRONUNCIATIONIPA).as(String.class);
// we need a default value and a try/catch-block because in older
// version we couldn't edit the pronunciationSoundLink text field
String sound = "";
try {
sound = getParameterChecked(PROPERTY_PRONUNCIATIONSOUNDLINK).as(String.class);
} catch (Exception e) {
// do nothing
}
String language = getParameterChecked(PROPERTY_LANGUAGE).as(String.class);
String sourceOfName = getParameterChecked(PROPERTY_SOURCEOFNAME).as(String.class);
String nameStatus = getParameterChecked(PROPERTY_NAMESTATUS).as(String.class);
String nativeness = getParameterChecked(PROPERTY_NATIVENESS).as(String.class);
String gender = getParameterChecked(PROPERTY_GRAMMA_GENDER).as(String.class);
String number = getParameterChecked(PROPERTY_GRAMMA_NUMBER).as(String.class);
// get the script and transliteration parameters
// should have the same order like source properties
ListMultimap<String, ParameterValue> params = getParameters();
List<ParameterValue> scripts = params.get(PROPERTY_SCRIPT);
List<ParameterValue> trans = params.get(PROPERTY_TRANSLITERATION);
if (inputs.size() != scripts.size() || inputs.size() != trans.size()) {
throw new TransformationException("Number of inputs does not match number of configured spellings, can't determine script and transliteration of spellings.");
// XXX could this be dealt with if the property "text" would be used
// again for this purpose?
}
// definition of the target property
TypeDefinition targetType = resultProperty.getDefinition().getPropertyType();
// instance that can be changed (add property/instance as child)
DefaultInstance targetInstance = new DefaultInstance(targetType, null);
// search for the child named "GeographicalName"
PropertyDefinition targetChildGeoName = Util.getChild("GeographicalName", targetType);
// get type definition to create the "GeographicalName" instance
TypeDefinition geoType = targetChildGeoName.getPropertyType();
// name/GeographicalName/
DefaultInstance geoInstance = new DefaultInstance(geoType, null);
targetInstance.addProperty(targetChildGeoName.getName(), geoInstance);
// name/GeographicalName/grammaticalGender/
if (gender != null && !gender.isEmpty()) {
PropertyDefinition geoChildGramGender = Util.getChild("grammaticalGender", geoType);
TypeDefinition grammarGenderType = geoChildGramGender.getPropertyType();
DefaultInstance grammarGenderInst = new DefaultInstance(grammarGenderType, null);
grammarGenderInst.setValue(gender);
geoInstance.addProperty(geoChildGramGender.getName(), grammarGenderInst);
}
// name/GeographicalName/grammaticalNumber
if (number != null && !number.isEmpty()) {
PropertyDefinition geoChildGramNumber = Util.getChild("grammaticalNumber", geoType);
TypeDefinition grammarNumberType = geoChildGramNumber.getPropertyType();
DefaultInstance grammarNumberInst = new DefaultInstance(grammarNumberType, null);
// set value of the grammaticalNumber instance
grammarNumberInst.setValue(number);
geoInstance.addProperty(geoChildGramNumber.getName(), grammarNumberInst);
}
// name/GeographicalName/language
if (language != null && !language.isEmpty()) {
PropertyDefinition geoChildLanguage = Util.getChild("language", geoType);
TypeDefinition languageType = geoChildLanguage.getPropertyType();
DefaultInstance languageInstance = new DefaultInstance(languageType, null);
// set value of the language instance
languageInstance.setValue(language);
geoInstance.addProperty(geoChildLanguage.getName(), languageInstance);
}
// name/GeographicalName/nameStatus
if (nameStatus != null && !nameStatus.isEmpty()) {
PropertyDefinition geoChildNameStatus = Util.getChild("nameStatus", geoType);
TypeDefinition nameStatusType = geoChildNameStatus.getPropertyType();
DefaultInstance nameStatusInstance = new DefaultInstance(nameStatusType, null);
// set value of the nameStatus instance
nameStatusInstance.setValue(nameStatus);
geoInstance.addProperty(geoChildNameStatus.getName(), nameStatusInstance);
}
// name/GeographicalName/nativeness
if (nativeness != null && !nativeness.isEmpty()) {
PropertyDefinition geoChildNativeness = Util.getChild("nativeness", geoType);
TypeDefinition nativenessType = geoChildNativeness.getPropertyType();
DefaultInstance nativenessInstance = new DefaultInstance(nativenessType, null);
// set value of the nativeness instance
nativenessInstance.setValue(nativeness);
geoInstance.addProperty(geoChildNativeness.getName(), nativenessInstance);
}
if ((ipa != null && !ipa.isEmpty()) || (sound != null && !sound.isEmpty())) {
// name/GeographicalName/pronunciation
PropertyDefinition geoChildPronun = Util.getChild("pronunciation", geoType);
TypeDefinition pronunType = geoChildPronun.getPropertyType();
DefaultInstance pronunInstance = new DefaultInstance(pronunType, null);
geoInstance.addProperty(geoChildPronun.getName(), pronunInstance);
// name/GeographicalName/pronunciation/PronunciationOfName
PropertyDefinition pronunChildPronOfName = Util.getChild("PronunciationOfName", pronunType);
TypeDefinition pronOfNameType = pronunChildPronOfName.getPropertyType();
DefaultInstance pronOfNameInst = new DefaultInstance(pronOfNameType, null);
pronunInstance.addProperty(pronunChildPronOfName.getName(), pronOfNameInst);
if (ipa != null && !ipa.isEmpty()) {
// name/GeographicalName/pronunciation/PronunciationOfName/pronunciationIPA
PropertyDefinition pronOfNameChildIPA = Util.getChild("pronunciationIPA", pronOfNameType);
TypeDefinition pronunIpaType = pronOfNameChildIPA.getPropertyType();
DefaultInstance pronunIpaInstance = new DefaultInstance(pronunIpaType, null);
pronunIpaInstance.setValue(ipa);
pronOfNameInst.addProperty(pronOfNameChildIPA.getName(), pronunIpaInstance);
}
if (sound != null && !sound.isEmpty()) {
// name/GeographicalName/pronunciation/PronunciationOfName/pronunciationSoundLink
PropertyDefinition pronOfNameChildSound = Util.getChild("pronunciationSoundLink", pronOfNameType);
TypeDefinition pronunSoundType = pronOfNameChildSound.getPropertyType();
DefaultInstance pronunSoundInstance = new DefaultInstance(pronunSoundType, null);
pronunSoundInstance.setValue(sound);
pronOfNameInst.addProperty(pronOfNameChildSound.getName(), pronunSoundInstance);
}
}
// name/GeographicalName/sourceOfName
if (sourceOfName != null && !sourceOfName.isEmpty()) {
PropertyDefinition geoChildSource = Util.getChild("sourceOfName", geoType);
TypeDefinition sourceType = geoChildSource.getPropertyType();
DefaultInstance sourceInstance = new DefaultInstance(sourceType, null);
// set value of the sourceOfName instance
sourceInstance.setValue(sourceOfName);
geoInstance.addProperty(geoChildSource.getName(), sourceInstance);
}
// name/GeographicalName/spelling
PropertyDefinition geoChildSpelling = Util.getChild("spelling", geoType);
TypeDefinition spellingType = geoChildSpelling.getPropertyType();
// name/GeographicalName/spelling/SpellingOfName
PropertyDefinition spellingChildSpellOfName = Util.getChild("SpellingOfName", spellingType);
TypeDefinition spellOfNameType = spellingChildSpellOfName.getPropertyType();
// create a "spelling" instance for each spelling
for (int i = 0; i < scripts.size(); i++) {
DefaultInstance spellingInstance = new DefaultInstance(spellingType, null);
DefaultInstance spellOfNameInst = new DefaultInstance(spellOfNameType, null);
// name/GeographicalName/spelling/SpellingOfName/script
PropertyDefinition spellOfNameChildScript = Util.getChild("script", spellOfNameType);
TypeDefinition scriptType = spellOfNameChildScript.getPropertyType();
DefaultInstance scriptInstance = new DefaultInstance(scriptType, null);
// name/GeographicalName/spelling/SpellingOfName/text
PropertyDefinition spellOfNameChildText = Util.getChild("text", spellOfNameType);
// name/GeographicalName/spelling/SpellingOfName/transliterationScheme
PropertyDefinition spellOfNameChildTransliteration = Util.getChild("transliterationScheme", spellOfNameType);
TypeDefinition transliterationType = spellOfNameChildTransliteration.getPropertyType();
DefaultInstance transliterationInstance = new DefaultInstance(transliterationType, null);
// build the spelling instance
scriptInstance.setValue(scripts.get(i).as(String.class));
transliterationInstance.setValue(trans.get(i).as(String.class));
spellOfNameInst.addProperty(spellOfNameChildScript.getName(), scriptInstance);
// set text value from inputs
spellOfNameInst.addProperty(spellOfNameChildText.getName(), inputs.get(i).getValue());
spellOfNameInst.addProperty(spellOfNameChildTransliteration.getName(), transliterationInstance);
spellingInstance.addProperty(spellingChildSpellOfName.getName(), spellOfNameInst);
geoInstance.addProperty(geoChildSpelling.getName(), spellingInstance);
}
return targetInstance;
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class XPathPropertyDefinitionDialog method getInputDefinition.
private static TypeDefinition getInputDefinition(EntityDefinition entityDef) {
if (entityDef.getPropertyPath() == null || entityDef.getPropertyPath().isEmpty()) {
// types?
return entityDef.getType();
} else {
PropertyDefinition def = (PropertyDefinition) entityDef.getDefinition();
// create a dummy type for the input
TypeDefinition dummyType = new DefaultTypeDefinition(new QName("ValueFilterDummy"));
TypeDefinition emptyType = new DefaultTypeDefinition(new QName("EmptyDummy"));
// with .. as parent link
new DefaultPropertyDefinition(PARENT_NAME, dummyType, emptyType);
// and the value property added as itself
dummyType.addChild(def);
return dummyType;
}
}
use of eu.esdihumboldt.hale.common.schema.model.PropertyDefinition in project hale by halestudio.
the class CityGMLXsltExport method writeContainerIntro.
@Override
protected void writeContainerIntro(XMLStreamWriter writer, XsltGenerationContext context) throws XMLStreamException, IOException {
if (targetCityModel != null && sourceCityModel != null) {
// copy GML boundedBy
// do it in a special template
String template = context.reserveTemplateName("copyBoundedBy");
writer.writeStartElement(NS_URI_XSL, "call-template");
writer.writeAttribute("name", template);
writer.writeEndElement();
// find source property
PropertyDefinition sourceBB = null;
for (ChildDefinition<?> child : sourceCityModel.getType().getChildren()) {
if (child.asProperty() != null && child.getName().getLocalPart().equals("boundedBy") && child.getName().getNamespaceURI().startsWith(GML_NAMESPACE_CORE)) {
sourceBB = child.asProperty();
break;
}
}
// find target property
PropertyDefinition targetBB = null;
for (ChildDefinition<?> child : targetCityModel.getType().getChildren()) {
if (child.asProperty() != null && child.getName().getLocalPart().equals("boundedBy") && child.getName().getNamespaceURI().startsWith(GML_NAMESPACE_CORE)) {
targetBB = child.asProperty();
break;
}
}
if (sourceBB != null && targetBB != null) {
// create templated
OutputStreamWriter out = new OutputStreamWriter(context.addInclude().openBufferedStream(), getCharset());
try {
out.write("<xsl:template name=\"" + template + "\">");
StringBuilder selectSource = new StringBuilder();
selectSource.append('/');
selectSource.append(GroovyXslHelpers.asPrefixedName(sourceCityModel.getName(), context));
selectSource.append('/');
selectSource.append(GroovyXslHelpers.asPrefixedName(sourceBB.getName(), context));
selectSource.append("[1]");
out.write("<xsl:for-each select=\"" + selectSource.toString() + "\">");
String elementName = GroovyXslHelpers.asPrefixedName(targetBB.getName(), context);
out.write("<" + elementName + ">");
// create bogus rename cell
DefaultCell cell = new DefaultCell();
// source
ListMultimap<String, Entity> source = ArrayListMultimap.create();
List<ChildContext> sourcePath = new ArrayList<ChildContext>();
sourcePath.add(new ChildContext(sourceBB));
PropertyEntityDefinition sourceDef = new PropertyEntityDefinition(sourceCityModel.getType(), sourcePath, SchemaSpaceID.SOURCE, null);
source.put(null, new DefaultProperty(sourceDef));
cell.setSource(source);
// target
ListMultimap<String, Entity> target = ArrayListMultimap.create();
List<ChildContext> targetPath = new ArrayList<ChildContext>();
targetPath.add(new ChildContext(targetBB));
PropertyEntityDefinition targetDef = new PropertyEntityDefinition(targetCityModel.getType(), targetPath, SchemaSpaceID.TARGET, null);
target.put(null, new DefaultProperty(targetDef));
cell.setTarget(target);
// parameters
ListMultimap<String, ParameterValue> parameters = ArrayListMultimap.create();
parameters.put(RenameFunction.PARAMETER_STRUCTURAL_RENAME, new ParameterValue("true"));
parameters.put(RenameFunction.PARAMETER_IGNORE_NAMESPACES, new ParameterValue("true"));
cell.setTransformationParameters(parameters);
// variables
ListMultimap<String, XslVariable> variables = ArrayListMultimap.create();
variables.put(null, new XslVariableImpl(sourceDef, "."));
try {
out.write(context.getPropertyTransformation(RenameFunction.ID).selectFunction(cell).getSequence(cell, variables, context, null));
} catch (TransformationException e) {
throw new IllegalStateException("Failed to create template for boundedBy copy.", e);
}
out.write("</" + elementName + ">");
out.write("</xsl:for-each>");
out.write("</xsl:template>");
} finally {
out.close();
}
}
}
}
Aggregations