use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class ToXtfRecordConverter method addAttrValue.
public int addAttrValue(java.sql.ResultSet rs, int valuei, long sqlid, Iom_jObject iomObj, AttributeDef attr, ArrayList<StructWrapper> structQueue, ViewableWrapper table, FixIomObjectRefs fixref) throws SQLException {
if (attr.getExtending() == null) {
String attrName = attr.getName();
String sqlAttrName = ili2sqlName.mapIliAttributeDef(attr, table.getSqlTablename(), null);
if (attr.isDomainBoolean()) {
boolean value = rs.getBoolean(valuei);
valuei++;
if (!rs.wasNull()) {
if (value) {
iomObj.setattrvalue(attrName, "true");
} else {
iomObj.setattrvalue(attrName, "false");
}
}
} else if (attr.isDomainIli1Date()) {
java.sql.Date value = rs.getDate(valuei);
valuei++;
if (!rs.wasNull()) {
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyyMMdd");
GregorianCalendar date = new GregorianCalendar();
date.setGregorianChange(PURE_GREGORIAN_CALENDAR);
fmt.setCalendar(date);
iomObj.setattrvalue(attrName, fmt.format(value));
}
} else if (attr.isDomainIli2Date()) {
java.sql.Date value = rs.getDate(valuei);
valuei++;
if (!rs.wasNull()) {
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar date = new GregorianCalendar();
date.setGregorianChange(PURE_GREGORIAN_CALENDAR);
fmt.setCalendar(date);
iomObj.setattrvalue(attrName, fmt.format(value));
}
} else if (attr.isDomainIli2Time()) {
java.sql.Time value = rs.getTime(valuei);
valuei++;
if (!rs.wasNull()) {
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("HH:mm:ss.SSS");
iomObj.setattrvalue(attrName, fmt.format(value));
}
} else if (attr.isDomainIli2DateTime()) {
java.sql.Timestamp value = rs.getTimestamp(valuei);
valuei++;
if (!rs.wasNull()) {
// with timezone: yyyy-MM-dd'T'HH:mm:ss.SSSZ
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
GregorianCalendar date = new GregorianCalendar();
date.setGregorianChange(PURE_GREGORIAN_CALENDAR);
fmt.setCalendar(date);
iomObj.setattrvalue(attrName, fmt.format(value));
}
} else {
Type type = attr.getDomainResolvingAliases();
if (type instanceof CompositionType) {
if (TrafoConfigNames.CATALOGUE_REF_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.CATALOGUE_REF_TRAFO))) {
Table catalogueReferenceTyp = ((CompositionType) type).getComponentType();
long value = rs.getLong(valuei);
valuei++;
if (!rs.wasNull()) {
IomObject catref = iomObj.addattrobj(attrName, catalogueReferenceTyp.getScopedName(null));
IomObject ref = catref.addattrobj(IliNames.CHBASE1_CATALOGUEREFERENCE_REFERENCE, "REF");
mapSqlid2Xtfid(fixref, value, ref, ((ReferenceType) ((AttributeDef) catalogueReferenceTyp.getAttributes().next()).getDomain()).getReferred());
}
} else if (TrafoConfigNames.MULTISURFACE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTISURFACE_TRAFO))) {
MultiSurfaceMapping attrMapping = multiSurfaceAttrs.getMapping(attr);
Table multiSurfaceType = ((CompositionType) type).getComponentType();
Table surfaceStructureType = ((CompositionType) ((AttributeDef) multiSurfaceType.getElement(AttributeDef.class, attrMapping.getBagOfSurfacesAttrName())).getDomain()).getComponentType();
String multiSurfaceQname = multiSurfaceType.getScopedName(null);
String surfaceStructureQname = surfaceStructureType.getScopedName(null);
SurfaceType surface = ((SurfaceType) ((AttributeDef) surfaceStructureType.getElement(AttributeDef.class, attrMapping.getSurfaceAttrName())).getDomainResolvingAliases());
CoordType coord = (CoordType) surface.getControlPointDomain().getType();
boolean is3D = coord.getDimensions().length == 3;
Object geomobj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
IomObject iomMultiSurface = geomConv.toIomMultiSurface(geomobj, sqlAttrName, is3D);
IomObject iomChbaseMultiSurface = new Iom_jObject(multiSurfaceQname, null);
int surfacec = iomMultiSurface.getattrvaluecount("surface");
for (int surfacei = 0; surfacei < surfacec; surfacei++) {
IomObject iomSurface = iomMultiSurface.getattrobj("surface", surfacei);
IomObject iomChbaseSurfaceStructure = iomChbaseMultiSurface.addattrobj(attrMapping.getBagOfSurfacesAttrName(), surfaceStructureQname);
IomObject iomSurfaceClone = new ch.interlis.iom_j.Iom_jObject("MULTISURFACE", null);
iomSurfaceClone.addattrobj("surface", iomSurface);
iomChbaseSurfaceStructure.addattrobj(attrMapping.getSurfaceAttrName(), iomSurfaceClone);
}
iomObj.addattrobj(attrName, iomChbaseMultiSurface);
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert surface/area", ex);
}
}
} else if (TrafoConfigNames.MULTILINE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTILINE_TRAFO))) {
MultiLineMapping attrMapping = multiLineAttrs.getMapping(attr);
Table multiLineType = ((CompositionType) type).getComponentType();
Table lineStructureType = ((CompositionType) ((AttributeDef) multiLineType.getElement(AttributeDef.class, attrMapping.getBagOfLinesAttrName())).getDomain()).getComponentType();
String multiLineQname = multiLineType.getScopedName(null);
String lineStructureQname = lineStructureType.getScopedName(null);
PolylineType surface = ((PolylineType) ((AttributeDef) lineStructureType.getElement(AttributeDef.class, attrMapping.getLineAttrName())).getDomainResolvingAliases());
CoordType coord = (CoordType) surface.getControlPointDomain().getType();
boolean is3D = coord.getDimensions().length == 3;
Object geomobj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
IomObject iomMultiPolygon = geomConv.toIomMultiPolyline(geomobj, sqlAttrName, is3D);
IomObject iomChbaseMultiLine = new Iom_jObject(multiLineQname, null);
int linec = iomMultiPolygon.getattrvaluecount(Wkb2iox.ATTR_POLYLINE);
for (int linei = 0; linei < linec; linei++) {
IomObject iomPolygon = iomMultiPolygon.getattrobj(Wkb2iox.ATTR_POLYLINE, linei);
IomObject iomChbaseSurfaceStructure = iomChbaseMultiLine.addattrobj(attrMapping.getBagOfLinesAttrName(), lineStructureQname);
iomChbaseSurfaceStructure.addattrobj(attrMapping.getLineAttrName(), iomPolygon);
}
iomObj.addattrobj(attrName, iomChbaseMultiLine);
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert polyline", ex);
}
}
} else if (TrafoConfigNames.MULTIPOINT_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTIPOINT_TRAFO))) {
MultiPointMapping attrMapping = multiPointAttrs.getMapping(attr);
Table multiPointType = ((CompositionType) type).getComponentType();
Table pointStructureType = ((CompositionType) ((AttributeDef) multiPointType.getElement(AttributeDef.class, attrMapping.getBagOfPointsAttrName())).getDomain()).getComponentType();
String multiPointQname = multiPointType.getScopedName(null);
String pointStructureQname = pointStructureType.getScopedName(null);
CoordType coord = ((CoordType) ((AttributeDef) pointStructureType.getElement(AttributeDef.class, attrMapping.getPointAttrName())).getDomainResolvingAliases());
boolean is3D = coord.getDimensions().length == 3;
Object geomobj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
IomObject iomMultiPoint = geomConv.toIomMultiCoord(geomobj, sqlAttrName, is3D);
IomObject iomChbaseMultiPoint = new Iom_jObject(multiPointQname, null);
int pointc = iomMultiPoint.getattrvaluecount(Wkb2iox.ATTR_COORD);
for (int pointi = 0; pointi < pointc; pointi++) {
IomObject iomPoint = iomMultiPoint.getattrobj(Wkb2iox.ATTR_COORD, pointi);
IomObject iomChbasePointStructure = iomChbaseMultiPoint.addattrobj(attrMapping.getBagOfPointsAttrName(), pointStructureQname);
iomChbasePointStructure.addattrobj(attrMapping.getPointAttrName(), iomPoint);
}
iomObj.addattrobj(attrName, iomChbaseMultiPoint);
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert coord", ex);
}
}
} else if (TrafoConfigNames.ARRAY_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.ARRAY_TRAFO))) {
ArrayMapping attrMapping = arrayAttrs.getMapping(attr);
Object dbValue = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
Table valueStructType = ((CompositionType) type).getComponentType();
String valueStructQname = valueStructType.getScopedName(null);
String[] iomArray = geomConv.toIomArray(attrMapping.getValueAttr(), dbValue, enumTypes);
for (int elei = 0; elei < iomArray.length; elei++) {
IomObject iomValueStruct = new Iom_jObject(valueStructQname, null);
iomValueStruct.setattrvalue(attrMapping.getValueAttr().getName(), iomArray[elei]);
iomObj.addattrobj(attrName, iomValueStruct);
}
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert array", ex);
}
}
} else if (TrafoConfigNames.MULTILINGUAL_TRAFO_EXPAND.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTILINGUAL_TRAFO))) {
IomObject iomMulti = null;
Table multilingualTextType = ((CompositionType) type).getComponentType();
String multilingualTextQname = multilingualTextType.getScopedName(null);
String localizedTextQname = ((CompositionType) ((AttributeDef) multilingualTextType.getAttributes().next()).getDomain()).getComponentType().getScopedName(null);
for (String sfx : DbNames.MULTILINGUAL_TXT_COL_SUFFIXS) {
String value = rs.getString(valuei);
valuei++;
if (!rs.wasNull()) {
if (iomMulti == null) {
iomMulti = new Iom_jObject(multilingualTextQname, null);
}
IomObject iomTxt = iomMulti.addattrobj(IliNames.CHBASE1_LOCALISEDTEXT, localizedTextQname);
iomTxt.setattrvalue(IliNames.CHBASE1_LOCALISEDTEXT_LANGUAGE, sfx.length() == 0 ? null : sfx.substring(LEN_LANG_PREFIX));
iomTxt.setattrvalue(IliNames.CHBASE1_LOCALISEDTEXT_TEXT, value);
}
}
if (iomMulti != null) {
iomObj.addattrobj(attrName, iomMulti);
}
} else {
// enque iomObj as parent
structQueue.add(new StructWrapper(sqlid, attr, iomObj, table));
}
} else if (type instanceof PolylineType) {
Object geomobj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
boolean is3D = ((CoordType) ((PolylineType) type).getControlPointDomain().getType()).getDimensions().length == 3;
IomObject polyline = geomConv.toIomPolyline(geomobj, sqlAttrName, is3D);
iomObj.addattrobj(attrName, polyline);
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert polyline", ex);
}
}
} else if (type instanceof SurfaceOrAreaType) {
if (createItfLineTables) {
} else {
Object geomobj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
boolean is3D = ((CoordType) ((SurfaceOrAreaType) type).getControlPointDomain().getType()).getDimensions().length == 3;
IomObject surface = geomConv.toIomSurface(geomobj, sqlAttrName, is3D);
iomObj.addattrobj(attrName, surface);
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert surface/area", ex);
}
}
}
if (createItfAreaRef) {
if (type instanceof AreaType) {
Object geomobj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
boolean is3D = false;
IomObject coord = geomConv.toIomCoord(geomobj, sqlAttrName, is3D);
iomObj.addattrobj(attrName, coord);
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert coord", ex);
}
}
}
}
} else if (type instanceof CoordType) {
Object geomobj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
boolean is3D = ((CoordType) type).getDimensions().length == 3;
IomObject coord = geomConv.toIomCoord(geomobj, sqlAttrName, is3D);
iomObj.addattrobj(attrName, coord);
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert coord", ex);
}
}
} else if (type instanceof EnumerationType) {
if (createEnumColAsItfCode) {
int value = rs.getInt(valuei);
valuei++;
if (!rs.wasNull()) {
iomObj.setattrvalue(attrName, mapItfCode2XtfCode((EnumerationType) type, value));
}
} else {
String value = rs.getString(valuei);
valuei++;
if (!rs.wasNull()) {
iomObj.setattrvalue(attrName, value);
}
}
} else if (type instanceof ReferenceType) {
ArrayList<ViewableWrapper> targetTables = getTargetTables(((ReferenceType) type).getReferred());
boolean refAlreadyDefined = false;
for (ViewableWrapper targetTable : targetTables) {
long value = rs.getLong(valuei);
valuei++;
if (!rs.wasNull()) {
if (refAlreadyDefined) {
sqlAttrName = ili2sqlName.mapIliAttributeDef(attr, table.getSqlTablename(), targetTable.getSqlTablename(), targetTables.size() > 1);
EhiLogger.logAdaption("Table " + table.getSqlTablename() + "(id " + sqlid + ") more than one value for refattr " + attrName + "; value of " + sqlAttrName + " ignored");
} else {
IomObject ref = iomObj.addattrobj(attrName, "REF");
mapSqlid2Xtfid(fixref, value, ref, ((ReferenceType) type).getReferred());
refAlreadyDefined = true;
}
}
}
} else if (type instanceof BlackboxType) {
if (((BlackboxType) type).getKind() == BlackboxType.eXML) {
Object obj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
iomObj.setattrvalue(attrName, geomConv.toIomXml(obj));
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert blackbox xml", ex);
}
}
} else {
Object obj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
iomObj.setattrvalue(attrName, geomConv.toIomBlob(obj));
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert blackbox binary", ex);
}
}
}
} else {
String value = rs.getString(valuei);
valuei++;
if (!rs.wasNull()) {
iomObj.setattrvalue(attrName, value);
}
}
}
}
return valuei;
}
use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class ToXtfRecordConverter method createQueryStmt.
/**
* creates sql query statement for a class.
* @param aclass type of objects to build query for
* @param wrapper not null, if building query for struct values
* @return SQL-Query statement
*/
public String createQueryStmt(Viewable aclass1, Long basketSqlId, StructWrapper structWrapper) {
ViewableWrapper aclass = class2wrapper.get(aclass1);
ViewableWrapper rootWrapper = aclass.getWrappers().get(0);
StringBuffer ret = new StringBuffer();
ret.append("SELECT r0." + colT_ID);
if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
ret.append(", r0." + DbNames.T_TYPE_COL);
}
if (!aclass.isStructure()) {
if (createIliTidCol || aclass.getOid() != null) {
ret.append(", r0." + DbNames.T_ILI_TID_COL);
}
}
if (structWrapper != null) {
if (createGenericStructRef) {
ret.append(", r0." + DbNames.T_PARENT_ID_COL);
ret.append(", r0." + DbNames.T_PARENT_TYPE_COL);
ret.append(", r0." + DbNames.T_PARENT_ATTR_COL);
} else {
ret.append(", r0." + ili2sqlName.mapIliAttributeDefReverse(structWrapper.getParentAttr(), getSqlType(aclass.getViewable()).getName(), getSqlType(structWrapper.getParentTable().getViewable()).getName()));
}
ret.append(", r0." + DbNames.T_SEQ_COL);
}
String sep = ",";
int tableAliasIdx = 0;
HashSet<AttributeDef> visitedAttrs = new HashSet<AttributeDef>();
for (ViewableWrapper table : aclass.getWrappers()) {
String tableAlias = "r" + tableAliasIdx;
String sqlTableName = table.getSqlTablename();
Iterator iter = table.getAttrIterator();
while (iter.hasNext()) {
ViewableTransferElement obj = (ViewableTransferElement) iter.next();
if (obj.obj instanceof AttributeDef) {
AttributeDef attr = (AttributeDef) obj.obj;
AttributeDef baseAttr = attr;
while (true) {
AttributeDef baseAttr1 = (AttributeDef) baseAttr.getExtending();
if (baseAttr1 == null) {
break;
}
baseAttr = baseAttr1;
}
if (!visitedAttrs.contains(baseAttr)) {
visitedAttrs.add(baseAttr);
if (!baseAttr.isTransient()) {
Type proxyType = baseAttr.getDomain();
if (proxyType != null && (proxyType instanceof ObjectType)) {
// skip implicit particles (base-viewables) of views
} else {
sep = addAttrToQueryStmt(ret, sep, tableAlias, baseAttr, sqlTableName);
}
}
}
}
if (obj.obj instanceof RoleDef) {
RoleDef role = (RoleDef) obj.obj;
if (role.getExtending() == null) {
ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
for (ViewableWrapper targetTable : targetTables) {
String roleSqlName = ili2sqlName.mapIliRoleDef(role, sqlTableName, targetTable.getSqlTablename(), targetTables.size() > 1);
// a role of an embedded association?
if (obj.embedded) {
AssociationDef roleOwner = (AssociationDef) role.getContainer();
if (roleOwner.getDerivedFrom() == null) {
// TODO if(orderPos!=0){
ret.append(sep);
sep = ",";
ret.append(makeColumnRef(tableAlias, roleSqlName));
}
} else {
// TODO if(orderPos!=0){
ret.append(sep);
sep = ",";
ret.append(makeColumnRef(tableAlias, roleSqlName));
}
}
}
}
}
// next table alias
tableAliasIdx++;
}
// stdcols
if (createStdCols) {
ret.append(sep);
sep = ",";
ret.append("r0." + DbNames.T_LAST_CHANGE_COL);
ret.append(sep);
sep = ",";
ret.append("r0." + DbNames.T_CREATE_DATE_COL);
ret.append(sep);
sep = ",";
ret.append("r0." + DbNames.T_USER_COL);
}
ret.append(" FROM ");
ArrayList<ViewableWrapper> tablev = new ArrayList<ViewableWrapper>(10);
tablev.addAll(aclass.getWrappers());
sep = "";
int tablec = tablev.size();
if (isMsAccess) {
for (int i = 0; i < tablec; i++) {
ret.append("(");
}
}
for (int i = 0; i < tablec; i++) {
ret.append(sep);
ret.append(tablev.get(i).getSqlTableQName());
ret.append(" r" + Integer.toString(i));
if (i > 0) {
ret.append(" ON r0." + colT_ID + "=r" + Integer.toString(i) + "." + colT_ID);
}
if (isMsAccess) {
ret.append(")");
}
sep = " LEFT JOIN ";
}
sep = " WHERE";
if (createTypeDiscriminator || rootWrapper.includesMultipleTypes()) {
ret.append(sep + " r0." + DbNames.T_TYPE_COL + "='" + getSqlType(aclass1).getName() + "'");
sep = " AND";
}
if (structWrapper != null) {
if (createGenericStructRef) {
ret.append(sep + " r0." + DbNames.T_PARENT_ID_COL + "=? AND r0." + DbNames.T_PARENT_ATTR_COL + "=?");
} else {
ret.append(sep + " r0." + ili2sqlName.mapIliAttributeDefReverse(structWrapper.getParentAttr(), getSqlType(aclass.getViewable()).getName(), getSqlType(structWrapper.getParentTable().getViewable()).getName()) + "=?");
}
sep = " AND";
}
if (basketSqlId != null) {
ret.append(sep + " r0." + DbNames.T_BASKET_COL + "=?");
}
if (structWrapper != null) {
ret.append(" ORDER BY r0." + DbNames.T_SEQ_COL + " ASC");
}
return ret.toString();
}
use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class TransferToXtf method createItfLineTableQueryStmt.
private String createItfLineTableQueryStmt(AttributeDef attr, Long basketSqlId, SqlColumnConverter conv) {
StringBuffer ret = new StringBuffer();
ret.append("SELECT r0." + colT_ID);
if (writeIliTid) {
ret.append(", r0." + DbNames.T_ILI_TID_COL);
}
String sep = ",";
SurfaceOrAreaType type = (SurfaceOrAreaType) attr.getDomainResolvingAliases();
String sqlTabName = ili2sqlName.mapGeometryAsTable(attr);
// geomAttr
ret.append(sep);
sep = ",";
ret.append(conv.getSelectValueWrapperPolyline(ili2sqlName.getSqlColNameItfLineTableGeomAttr(attr, sqlTabName)));
// is it of type SURFACE?
if (type instanceof SurfaceType) {
// -> mainTable
ret.append(sep);
sep = ",";
ret.append(ili2sqlName.getSqlColNameItfLineTableRefAttr(attr, sqlTabName));
}
Table lineAttrTable = type.getLineAttributeStructure();
if (lineAttrTable != null) {
Iterator attri = lineAttrTable.getAttributes();
while (attri.hasNext()) {
AttributeDef lineattr = (AttributeDef) attri.next();
sep = recConv.addAttrToQueryStmt(ret, sep, null, lineattr, sqlTabName);
}
}
ret.append(" FROM ");
if (schema != null) {
sqlTabName = schema + "." + sqlTabName;
}
ret.append(sqlTabName);
ret.append(" r0");
if (basketSqlId != null) {
ret.append(" WHERE r0." + DbNames.T_BASKET_COL + "=?");
}
return ret.toString();
}
use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class TransferToXtf method genClassHelper.
private void genClassHelper(Viewable aclass) {
boolean doStruct = false;
if (aclass instanceof Table) {
doStruct = !((Table) aclass).isIdentifiable();
}
if (doStruct) {
expgen.println("private void add" + aclass.getName() + "(String parentTid,String parentAttrSql,IomObject parent,String parentAttrIli)");
} else {
expgen.println("private void add" + aclass.getName() + "(String subset)");
String addany = "if(iliClassName.equals(\"" + aclass.getScopedName(null) + "\")){add" + aclass.getName() + "(select);}";
addanyLines.add(addany);
}
expgen.println("{");
expgen.indent();
expgen.println("String tabName=\"" + createQueryStmtFromClause(aclass) + "\";");
expgen.println("String stmt=\"" + recConv.createQueryStmt(aclass, null, null) + "\";");
if (!doStruct) {
expgen.println("if(subset!=null){");
expgen.println("stmt=stmt+\" AND \"+subset;");
expgen.println("}");
}
expgen.println("EhiLogger.traceBackendCmd(stmt);");
expgen.println("java.sql.PreparedStatement dbstmt = null;");
expgen.println("try{");
expgen.indent();
// expgen.println("dbstmt = conn.createStatement();");
// expgen.println("java.sql.ResultSet rs=dbstmt.executeQuery(stmt);");
expgen.println("dbstmt = conn.prepareStatement(stmt);");
expgen.println("dbstmt.clearParameters();");
if (doStruct) {
expgen.println("dbstmt.setString(1,parentTid);");
expgen.println("dbstmt.setString(2,parentAttrSql);");
}
expgen.println("java.sql.ResultSet rs=dbstmt.executeQuery();");
expgen.println("while(rs.next()){");
expgen.indent();
expgen.println("String tid=DbUtility.getString(rs,\"T_Id\",false,tabName);");
expgen.println("String recInfo=tabName+\" \"+tid;");
expgen.println("IomObject iomObj;");
if (!doStruct) {
expgen.println("iomObj=newObject(\"" + aclass.getScopedName(null) + "\",mapId(\"" + recConv.getSqlType(aclass) + "\",tid));");
} else {
expgen.println("iomObj=(IomObject)parent.addattrobj(parentAttrIli,\"" + aclass.getScopedName(null) + "\");");
}
Iterator iter = aclass.getAttributesAndRoles2();
while (iter.hasNext()) {
ViewableTransferElement obj = (ViewableTransferElement) iter.next();
if (obj.obj instanceof AttributeDef) {
AttributeDef attr = (AttributeDef) obj.obj;
if (attr.getExtending() == null) {
String attrName = attr.getName();
String sqlAttrName = ili2sqlName.mapIliAttributeDef(attr, recConv.getSqlType(aclass).getName(), null);
Type type = attr.getDomain();
if ((type instanceof TypeAlias) && Ili2cUtility.isBoolean(td, type)) {
expgen.println("Boolean prop_" + attrName + "=Db2Xtf.getBoolean(rs,\"" + sqlAttrName + "\"," + (type.isMandatoryConsideringAliases() ? "false" : "true") + ",recInfo,iomObj,\"" + attrName + "\");");
} else {
type = attr.getDomainResolvingAliases();
if (type instanceof CompositionType) {
// enque iomObj as parent
// enqueueStructAttr(new StructWrapper(tid,attr,iomObj));
CompositionType ct = (CompositionType) type;
expgen.println("add" + ct.getComponentType().getName() + "(tid,\"" + ili2sqlName.mapIliAttributeDef(attr, recConv.getSqlType(aclass).getName(), null) + "\",iomObj,\"" + attr.getName() + "\");");
} else if (type instanceof PolylineType) {
} else if (type instanceof SurfaceOrAreaType) {
} else if (type instanceof CoordType) {
} else if (type instanceof EnumerationType) {
String enumName = null;
if (attr.getDomain() instanceof TypeAlias) {
Domain domainDef = ((TypeAlias) attr.getDomain()).getAliasing();
enumName = domainDef.getScopedName(null);
} else {
enumName = aclass.getScopedName(null) + "->" + attrName;
enumTypes.add(enumName);
}
expgen.println("String prop_" + attrName + "=Db2Xtf.getEnum(rs,\"" + sqlAttrName + "\"," + (type.isMandatoryConsideringAliases() ? "false" : "true") + ",recInfo,getEnumMapper(\"" + enumName + "\"),iomObj,\"" + attrName + "\");");
} else {
expgen.println("String prop_" + attrName + "=Db2Xtf.getString(rs,\"" + sqlAttrName + "\"," + (type.isMandatoryConsideringAliases() ? "false" : "true") + ",recInfo,iomObj,\"" + attrName + "\");");
}
}
}
}
if (obj.obj instanceof RoleDef) {
RoleDef role = (RoleDef) obj.obj;
if (role.getExtending() == null) {
String roleName = role.getName();
String sqlRoleName = ili2sqlName.mapIliRoleDef(role, recConv.getSqlType(aclass).getName(), recConv.getSqlType(role.getDestination()).getName());
// a role of an embedded association?
if (obj.embedded) {
AssociationDef roleOwner = (AssociationDef) role.getContainer();
if (roleOwner.getDerivedFrom() == null) {
// TODO if(orderPos!=0){
expgen.println("String prop_" + roleName + "=Db2Xtf.getRef(rs,\"" + sqlRoleName + "\"," + "true" + ",recInfo,this,\"" + recConv.getSqlType(role.getDestination()) + "\",iomObj,\"" + roleName + "\",\"" + roleOwner.getScopedName(null) + "\");");
}
} else {
// TODO if(orderPos!=0){
expgen.println("String prop_" + roleName + "=Db2Xtf.getRef(rs,\"" + sqlRoleName + "\"," + "false" + ",recInfo,this,\"" + recConv.getSqlType(role.getDestination()) + "\",iomObj,\"" + roleName + "\",\"REF\");");
}
}
}
}
// add referenced and referencing objects if it is not a struct
if (!doStruct && aclass instanceof AbstractClassDef) {
AbstractClassDef aclass2 = (AbstractClassDef) aclass;
Iterator associ = aclass2.getTargetForRoles();
while (associ.hasNext()) {
RoleDef roleThis = (RoleDef) associ.next();
if (roleThis.getKind() == RoleDef.Kind.eAGGREGATE || roleThis.getKind() == RoleDef.Kind.eCOMPOSITE) {
RoleDef oppEnd = roleThis.getOppEnd();
if (roleThis.isAssociationEmbedded()) {
expgen.println("addPendingObject(\"" + oppEnd.getDestination().getScopedName(null) + "\",\"T_Id\",prop_" + oppEnd.getName() + ");");
} else if (oppEnd.isAssociationEmbedded()) {
expgen.println("addPendingObject(\"" + oppEnd.getDestination().getScopedName(null) + "\",\"" + ili2sqlName.mapIliRoleDef(roleThis, null, null) + "\",tid);");
} else {
expgen.println("addPendingObject(\"" + ((AssociationDef) (oppEnd.getContainer())).getScopedName(null) + "\",\"" + ili2sqlName.mapIliRoleDef(roleThis, null, null) + "\",prop_" + roleThis.getName() + ");");
}
}
}
}
// writeObject if it is not a struct
if (!doStruct) {
expgen.println("writeObject(iomObj);");
}
expgen.unindent();
expgen.println("}");
expgen.unindent();
expgen.println("}catch(java.sql.SQLException ex){");
expgen.indent();
expgen.println("EhiLogger.logError(\"failed to query \"+tabName,ex);");
expgen.unindent();
expgen.println("}finally{");
expgen.indent();
expgen.println("if(dbstmt!=null){");
expgen.indent();
expgen.println("try{");
expgen.indent();
expgen.println("dbstmt.close();");
expgen.unindent();
expgen.println("}catch(java.sql.SQLException ex){");
expgen.indent();
expgen.println("EhiLogger.logError(\"failed to close query of \"+tabName,ex);");
expgen.unindent();
expgen.println("}");
expgen.unindent();
expgen.println("}");
expgen.unindent();
expgen.println("}");
expgen.unindent();
expgen.println("}");
}
use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class Viewable2TableMapper method addProps.
private void addProps(ViewableWrapper viewable, List<ViewableTransferElement> attrv, Iterator<ViewableTransferElement> iter) {
boolean hasGeometry = false;
// only one geometry column per table?
if (singleGeom) {
for (ViewableTransferElement attrE : attrv) {
if (attrE.obj instanceof AttributeDef) {
AttributeDef attr = (AttributeDef) attrE.obj;
ch.interlis.ili2c.metamodel.Type type = attr.getDomainResolvingAliases();
if (type instanceof ch.interlis.ili2c.metamodel.CoordType || type instanceof ch.interlis.ili2c.metamodel.LineType || (Ili2cUtility.isMultiSurfaceAttr(getTransferDescription(attr), attr) && (coalesceMultiSurface || TrafoConfigNames.MULTISURFACE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTISURFACE_TRAFO)))) || (Ili2cUtility.isMultiLineAttr(getTransferDescription(attr), attr) && (coalesceMultiLine || TrafoConfigNames.MULTILINE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTILINE_TRAFO)))) || (Ili2cUtility.isMultiPointAttr(getTransferDescription(attr), attr) && (coalesceMultiPoint || TrafoConfigNames.MULTIPOINT_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTIPOINT_TRAFO))))) {
hasGeometry = true;
break;
}
}
}
}
while (iter.hasNext()) {
ViewableTransferElement obj = iter.next();
if (obj.obj instanceof AttributeDef) {
AttributeDef attr = (AttributeDef) obj.obj;
String sqlname = trafoConfig.getAttrConfig(attr, TrafoConfigNames.SECONDARY_TABLE);
// attribute configured to be in a secondary table?
if (sqlname != null) {
// add attribute to given secondary table
ViewableWrapper attrWrapper = viewable.getSecondaryTable(sqlname);
if (attrWrapper == null) {
attrWrapper = viewable.createSecondaryTable(sqlname);
}
List<ViewableTransferElement> attrProps = new java.util.ArrayList<ViewableTransferElement>();
attrProps.add(obj);
attrWrapper.setAttrv(attrProps);
} else {
// only one geometry column per table?
if (singleGeom) {
ch.interlis.ili2c.metamodel.Type type = attr.getDomainResolvingAliases();
if (type instanceof ch.interlis.ili2c.metamodel.CoordType || type instanceof ch.interlis.ili2c.metamodel.LineType || (Ili2cUtility.isMultiSurfaceAttr(getTransferDescription(attr), attr) && (coalesceMultiSurface || TrafoConfigNames.MULTISURFACE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTISURFACE_TRAFO)))) || (Ili2cUtility.isMultiLineAttr(getTransferDescription(attr), attr) && (coalesceMultiLine || TrafoConfigNames.MULTILINE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTILINE_TRAFO))))) {
if (createItfLineTables && type instanceof ch.interlis.ili2c.metamodel.SurfaceOrAreaType) {
// ignore it; will be created by legacy code
} else {
// table already has a geometry column?
if (hasGeometry) {
// create a new secondary table
sqlname = nameMapping.mapGeometryAsTable(attr);
ViewableWrapper attrWrapper = viewable.getSecondaryTable(sqlname);
if (attrWrapper == null) {
attrWrapper = viewable.createSecondaryTable(sqlname);
}
// add attribute to new secondary table
List<ViewableTransferElement> attrProps = new java.util.ArrayList<ViewableTransferElement>();
attrProps.add(obj);
attrWrapper.setAttrv(attrProps);
trafoConfig.setAttrConfig(attr, TrafoConfigNames.SECONDARY_TABLE, sqlname);
} else {
// table has not yet a geometry column
// add it
hasGeometry = true;
attrv.add(obj);
}
}
} else {
// not a Geom type
attrv.add(obj);
}
} else {
attrv.add(obj);
}
}
}
if (obj.obj instanceof RoleDef) {
RoleDef role = (RoleDef) obj.obj;
AssociationDef roleOwner = (AssociationDef) role.getContainer();
// not an embedded role and roledef not defined in a lightweight association?
if (!obj.embedded && !roleOwner.isLightweight()) {
attrv.add(obj);
}
// a role of an embedded association?
if (obj.embedded) {
if (roleOwner.getDerivedFrom() == null) {
attrv.add(obj);
}
}
}
}
}
Aggregations