use of eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition in project hale by halestudio.
the class GeopackageSchemaBuilder method getOrCreatePropertyType.
private TypeDefinition getOrCreatePropertyType(UserColumn column, GeometryColumns geomColumns, DefaultSchema schema) {
String localName;
if (column instanceof FeatureColumn && ((FeatureColumn) column).isGeometry()) {
localName = ((FeatureColumn) column).getGeometryType().getName();
} else {
localName = column.getDataType().name();
}
QName typeName = new QName(defaultNamespace, localName);
TypeDefinition type = schema.getType(typeName);
if (type != null) {
// use existing type
return type;
} else {
DefaultTypeDefinition typeDef = new DefaultTypeDefinition(typeName);
typeDef.setConstraint(MappingRelevantFlag.DISABLED);
typeDef.setConstraint(MappableFlag.DISABLED);
// simple type
typeDef.setConstraint(HasValueFlag.ENABLED);
if (column instanceof FeatureColumn && ((FeatureColumn) column).isGeometry()) {
mil.nga.sf.GeometryType geomType = ((FeatureColumn) column).getGeometryType();
typeDef.setConstraint(Binding.get(GeometryProperty.class));
Class<? extends Geometry> geomClass = Geometry.class;
switch(geomType) {
case LINESTRING:
geomClass = LineString.class;
break;
case MULTIPOINT:
geomClass = MultiPoint.class;
break;
case MULTILINESTRING:
geomClass = MultiLineString.class;
break;
case MULTIPOLYGON:
geomClass = MultiPolygon.class;
break;
case POINT:
geomClass = Point.class;
break;
case POLYGON:
geomClass = Polygon.class;
break;
default:
break;
}
typeDef.setConstraint(GeometryType.get(geomClass));
SpatialReferenceSystem srs = geomColumns.getSrs();
if (srs != null) {
String code = String.valueOf(srs.getOrganizationCoordsysId());
int dimension = GeometryMetadata.UNKNOWN_DIMENSION;
String srsText = srs.getDefinition();
String auth_name = srs.getOrganization();
typeDef.setConstraint(new GeometryMetadata(code, dimension, srsText, auth_name));
}
} else {
GeoPackageDataType dataType = column.getDataType();
Class<?> binding;
switch(dataType) {
case DATETIME:
binding = Instant.class;
break;
case DATE:
binding = LocalDate.class;
break;
default:
binding = dataType.getClassType();
}
typeDef.setConstraint(Binding.get(binding));
}
return typeDef;
}
}
use of eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition in project hale by halestudio.
the class CSVSchemaReader method loadFromSource.
@Override
protected Schema loadFromSource(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
// $NON-NLS-1$
progress.begin("Load CSV schema", ProgressIndicator.UNKNOWN);
String namespace = CSVFileIO.CSVFILE_NS;
DefaultSchema schema = new DefaultSchema(namespace, getSource().getLocation());
CSVReader reader = CSVUtil.readFirst(this);
try {
// initializes the first line of the table (names of the columns)
firstLine = reader.readNext();
// create type definition
String typename = getParameter(CommonSchemaConstants.PARAM_TYPENAME).as(String.class);
if (typename == null || typename.isEmpty()) {
reporter.setSuccess(false);
reporter.error(new IOMessageImpl("No Typename was set", null));
return null;
}
DefaultTypeDefinition type = new DefaultTypeDefinition(new QName(typename));
// constraints on main type
type.setConstraint(MappingRelevantFlag.ENABLED);
type.setConstraint(MappableFlag.ENABLED);
type.setConstraint(HasValueFlag.DISABLED);
type.setConstraint(AbstractFlag.DISABLED);
// set metadata for main type
type.setLocation(getSource().getLocation());
StringBuffer defaultPropertyTypeBuffer = new StringBuffer();
String[] comboSelections;
if (getParameter(PARAM_PROPERTYTYPE).isEmpty()) {
for (int i = 0; i < firstLine.length; i++) {
defaultPropertyTypeBuffer.append("java.lang.String");
defaultPropertyTypeBuffer.append(",");
}
defaultPropertyTypeBuffer.deleteCharAt(defaultPropertyTypeBuffer.lastIndexOf(","));
String combs = defaultPropertyTypeBuffer.toString();
comboSelections = combs.split(",");
} else {
comboSelections = getParameter(PARAM_PROPERTYTYPE).as(String.class).split(",");
}
String[] properties;
if (getParameter(PARAM_PROPERTY).isEmpty()) {
properties = firstLine;
} else {
properties = getParameter(PARAM_PROPERTY).as(String.class).split(",");
}
// than the entries in the first line
if ((firstLine.length != properties.length && properties.length != 0) || (firstLine.length != comboSelections.length && comboSelections.length != 0)) {
fail("Not the same number of entries for property names, property types and words in the first line of the file");
}
for (int i = 0; i < comboSelections.length; i++) {
PropertyType propertyType;
propertyType = PropertyTypeExtension.getInstance().getFactory(comboSelections[i]).createExtensionObject();
DefaultPropertyDefinition property = new DefaultPropertyDefinition(new QName(properties[i]), type, propertyType.getTypeDefinition());
// set constraints on property
// property.setConstraint(NillableFlag.DISABLED); // nillable
// nillable FIXME
property.setConstraint(NillableFlag.ENABLED);
// should be
// configurable
// per field
// (see also
// CSVInstanceReader)
// cardinality
property.setConstraint(Cardinality.CC_EXACTLY_ONCE);
// set metadata for property
property.setLocation(getSource().getLocation());
}
boolean skip = Arrays.equals(properties, firstLine);
type.setConstraint(new CSVConfiguration(CSVUtil.getSep(this), CSVUtil.getQuote(this), CSVUtil.getEscape(this), skip ? 1 : 0));
schema.addType(type);
} catch (Exception ex) {
reporter.error(new IOMessageImpl("Cannot load csv schema", ex));
reporter.setSuccess(false);
return null;
}
reporter.setSuccess(true);
return schema;
}
use of eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition in project hale by halestudio.
the class GeopackageSchemaBuilder method buildType.
private TypeDefinition buildType(UserDao<?, ?, ?, ?> dao, GeometryColumns geomColumns, DefaultSchema schema) {
QName name = new QName(defaultNamespace, dao.getTableName());
DefaultTypeDefinition type = new DefaultTypeDefinition(name);
type.setConstraint(MappableFlag.ENABLED);
type.setConstraint(MappingRelevantFlag.ENABLED);
type.setConstraint(HasValueFlag.DISABLED);
type.setConstraint(AbstractFlag.DISABLED);
type.setConstraint(Binding.get(Instance.class));
UserTable<? extends UserColumn> table = dao.getTable();
// set primary key constraint
UserColumn pkColumn = table.getPkColumn();
if (pkColumn != null) {
type.setConstraint(new PrimaryKey(Collections.singletonList(new QName(pkColumn.getName()))));
}
for (String columnName : table.getColumnNames()) {
UserColumn column = table.getColumn(columnName);
QName propertyName = new QName(columnName);
// determine property type
TypeDefinition propertyType = getOrCreatePropertyType(column, geomColumns, schema);
DefaultPropertyDefinition property = new DefaultPropertyDefinition(propertyName, type, propertyType);
property.setConstraint(Cardinality.CC_EXACTLY_ONCE);
if (column.isNotNull()) {
property.setConstraint(NillableFlag.DISABLED);
} else {
property.setConstraint(NillableFlag.ENABLED);
}
}
return type;
}
Aggregations