Search in sources :

Example 36 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class TransferToXtf method dumpItfTableObject.

private void dumpItfTableObject(IoxWriter out, AttributeDef attr, Long basketSqlId) {
    String stmt = createItfLineTableQueryStmt(attr, basketSqlId, geomConv);
    String sqlTabName = ili2sqlName.mapGeometryAsTable(attr);
    EhiLogger.traceBackendCmd(stmt);
    SurfaceOrAreaType type = (SurfaceOrAreaType) attr.getDomainResolvingAliases();
    String geomAttrName = ch.interlis.iom_j.itf.ModelUtilities.getHelperTableGeomAttrName(attr);
    String refAttrName = null;
    if (type instanceof SurfaceType) {
        refAttrName = ch.interlis.iom_j.itf.ModelUtilities.getHelperTableMainTableRef(attr);
    }
    java.sql.PreparedStatement dbstmt = null;
    try {
        dbstmt = conn.prepareStatement(stmt);
        dbstmt.clearParameters();
        int paramIdx = 1;
        if (basketSqlId != null) {
            dbstmt.setLong(paramIdx++, basketSqlId);
        }
        java.sql.ResultSet rs = dbstmt.executeQuery();
        while (rs.next()) {
            int valuei = 1;
            long sqlid = rs.getLong(valuei);
            valuei++;
            String sqlIliTid = null;
            if (writeIliTid) {
                sqlIliTid = rs.getString(valuei);
                valuei++;
            } else {
                sqlIliTid = Long.toString(sqlid);
            }
            Viewable aclass = (Viewable) attr.getContainer();
            Iom_jObject iomObj;
            iomObj = new Iom_jObject(aclass.getScopedName(null) + "_" + attr.getName(), sqlIliTid);
            // geomAttr
            Object geomobj = rs.getObject(valuei);
            valuei++;
            if (!rs.wasNull()) {
                try {
                    boolean is3D = false;
                    IomObject polyline = geomConv.toIomPolyline(geomobj, ili2sqlName.getSqlColNameItfLineTableGeomAttr(attr, sqlTabName), is3D);
                    iomObj.addattrobj(geomAttrName, polyline);
                } catch (ConverterException ex) {
                    EhiLogger.logError("Object " + sqlid + ": failed to convert polyline", ex);
                }
            }
            // is of type SURFACE?
            if (type instanceof SurfaceType) {
                // -> mainTable
                IomObject ref = iomObj.addattrobj(refAttrName, "REF");
                long refSqlId = rs.getLong(valuei);
                if (sqlidPool.containsSqlid(refSqlId)) {
                    String refTid = sqlidPool.getXtfid(refSqlId);
                    ref.setobjectrefoid(refTid);
                } else {
                    EhiLogger.logError("unknown referenced object " + attr.getContainer().getScopedName(null) + " sqlid " + refSqlId + " referenced from " + sqlTabName + " " + colT_ID + " " + sqlid);
                }
                valuei++;
            }
            Table lineAttrTable = type.getLineAttributeStructure();
            if (lineAttrTable != null) {
                Iterator attri = lineAttrTable.getAttributes();
                while (attri.hasNext()) {
                    AttributeDef lineattr = (AttributeDef) attri.next();
                    valuei = recConv.addAttrValue(rs, valuei, sqlid, iomObj, lineattr, null, class2wrapper.get(lineAttrTable), null);
                }
            }
            if (out != null) {
                // write object
                ObjectEvent objEvent = new ObjectEvent(iomObj);
                if (languageFilter != null) {
                    objEvent = (ObjectEvent) languageFilter.filter(objEvent);
                }
                if (exportBaseModelFilter != null) {
                    objEvent = (ObjectEvent) exportBaseModelFilter.filter(objEvent);
                }
                if (validator != null)
                    validator.validate(objEvent);
                out.write(objEvent);
            }
        }
    } catch (java.sql.SQLException ex) {
        EhiLogger.logError("failed to query " + attr.getScopedName(null), ex);
    } catch (ch.interlis.iox.IoxException ex) {
        EhiLogger.logError("failed to write " + attr.getScopedName(null), ex);
    } finally {
        if (dbstmt != null) {
            try {
                dbstmt.close();
            } catch (java.sql.SQLException ex) {
                EhiLogger.logError("failed to close query of " + attr.getScopedName(null), ex);
            }
        }
    }
}
Also used : ObjectEvent(ch.interlis.iox_j.ObjectEvent) ConverterException(ch.ehi.ili2db.converter.ConverterException) Table(ch.interlis.ili2c.metamodel.Table) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IoxException(ch.interlis.iox.IoxException) IomObject(ch.interlis.iom.IomObject) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iterator(java.util.Iterator) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Example 37 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class TransferToXtf method dumpStructs.

/**
 * dumps all struct values of a given struct attr.
 * @throws IoxException
 */
private void dumpStructs(StructWrapper structWrapper, FixIomObjectRefs fixref) throws IoxException {
    Viewable baseClass = ((CompositionType) structWrapper.getParentAttr().getDomain()).getComponentType();
    HashMap<String, IomObject> structelev = new HashMap<String, IomObject>();
    HashSet<Viewable> structClassv = new HashSet<Viewable>();
    String stmt = createQueryStmt4Type(baseClass, structWrapper);
    EhiLogger.traceBackendCmd(stmt);
    java.sql.Statement dbstmt = null;
    try {
        dbstmt = conn.createStatement();
        java.sql.ResultSet rs = dbstmt.executeQuery(stmt);
        while (rs.next()) {
            String sqlid = rs.getString(1);
            String structEleClass = null;
            Viewable structClass = null;
            String structEleSqlType = rs.getString(2);
            structEleClass = ili2sqlName.mapSqlTableName(structEleSqlType);
            if (structEleClass == null) {
                throw new IoxException("unknown " + DbNames.T_TYPE_COL + " '" + structEleSqlType + "' in table " + getStructRootTableName(baseClass));
            }
            structClass = (Viewable) tag2class.get(structEleClass);
            IomObject iomObj = structWrapper.getParent().addattrobj(structWrapper.getParentAttr().getName(), structEleClass);
            structelev.put(sqlid, iomObj);
            structClassv.add(structClass);
        }
    } catch (java.sql.SQLException ex) {
        EhiLogger.logError("failed to query structure elements " + baseClass.getScopedName(null), ex);
    } finally {
        if (dbstmt != null) {
            try {
                dbstmt.close();
            } catch (java.sql.SQLException ex) {
                EhiLogger.logError("failed to close query of structure elements " + baseClass.getScopedName(null), ex);
            }
        }
    }
    Iterator<Viewable> classi = structClassv.iterator();
    while (classi.hasNext()) {
        Viewable aclass = classi.next();
        dumpObjHelper(null, aclass, null, fixref, structWrapper, structelev);
    }
}
Also used : HashMap(java.util.HashMap) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) IomObject(ch.interlis.iom.IomObject) Viewable(ch.interlis.ili2c.metamodel.Viewable) IoxException(ch.interlis.iox.IoxException) HashSet(java.util.HashSet)

Example 38 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class ToXtfRecordConverter method convertRecord.

public Iom_jObject convertRecord(java.sql.ResultSet rs, Viewable aclass1, FixIomObjectRefs fixref, StructWrapper structWrapper, HashMap structelev, ArrayList<StructWrapper> structQueue, long sqlid) throws SQLException {
    ViewableWrapper aclass = class2wrapper.get(aclass1);
    Iom_jObject iomObj;
    int valuei = 1;
    valuei++;
    if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
        // String t_type=rs.getString(valuei);
        valuei++;
    }
    String sqlIliTid = null;
    if (structWrapper == null) {
        if (!aclass.isStructure()) {
            if (createIliTidCol || aclass.getOid() != null) {
                sqlIliTid = rs.getString(valuei);
                sqlid2xtfid.putSqlid2Xtfid(sqlid, sqlIliTid);
                valuei++;
            } else {
                sqlIliTid = Long.toString(sqlid);
                sqlid2xtfid.putSqlid2Xtfid(sqlid, sqlIliTid);
            }
        }
    }
    if (structWrapper == null) {
        if (!aclass.isStructure()) {
            iomObj = new Iom_jObject(aclass1.getScopedName(null), sqlIliTid);
        } else {
            iomObj = new Iom_jObject(aclass1.getScopedName(null), null);
        }
        iomObj.setattrvalue(ItfWriter2.INTERNAL_T_ID, Long.toString(sqlid));
        fixref.setRoot(iomObj);
    } else {
        iomObj = (Iom_jObject) structelev.get(Long.toString(sqlid));
        if (createGenericStructRef) {
            valuei += 4;
        } else {
            valuei += 2;
        }
    }
    HashSet<AttributeDef> visitedAttrs = new HashSet<AttributeDef>();
    for (ViewableWrapper table : aclass.getWrappers()) {
        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 {
                            valuei = addAttrValue(rs, valuei, sqlid, iomObj, baseAttr, structQueue, table, fixref);
                        }
                    }
                }
            }
            if (obj.obj instanceof RoleDef) {
                RoleDef role = (RoleDef) obj.obj;
                if (role.getExtending() == null) {
                    String roleName = role.getName();
                    ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
                    boolean refAlreadyDefined = false;
                    for (ViewableWrapper targetTable : targetTables) {
                        String sqlRoleName = ili2sqlName.mapIliRoleDef(role, getSqlType(table.getViewable()).getName(), 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){
                                long value = rs.getLong(valuei);
                                valuei++;
                                if (!rs.wasNull()) {
                                    if (refAlreadyDefined) {
                                        EhiLogger.logAdaption("Table " + table.getSqlTablename() + "(id " + sqlid + ") more than one value for role " + roleName + "; value of " + sqlRoleName + " ignored");
                                    } else {
                                        IomObject ref = iomObj.addattrobj(roleName, roleOwner.getScopedName(null));
                                        mapSqlid2Xtfid(fixref, value, ref, role.getDestination());
                                        refAlreadyDefined = true;
                                    }
                                }
                            }
                        } else {
                            // TODO if(orderPos!=0){
                            long value = rs.getLong(valuei);
                            valuei++;
                            if (!rs.wasNull()) {
                                if (refAlreadyDefined) {
                                    EhiLogger.logAdaption("Table " + table.getSqlTablename() + "(id " + sqlid + ") more than one value for role " + roleName + "; value of " + sqlRoleName + " ignored");
                                } else {
                                    IomObject ref = iomObj.addattrobj(roleName, "REF");
                                    mapSqlid2Xtfid(fixref, value, ref, role.getDestination());
                                    refAlreadyDefined = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return iomObj;
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) Iom_jObject(ch.interlis.iom_j.Iom_jObject) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) AreaType(ch.interlis.ili2c.metamodel.AreaType) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) IomObject(ch.interlis.iom.IomObject) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) HashSet(java.util.HashSet)

Example 39 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class ReduceToBaseModel method translateAttrValue.

private void translateAttrValue(IomObject iomObj, AttributeDef srcAttr) {
    String srcAttrName = srcAttr.getName();
    int attrc = iomObj.getattrvaluecount(srcAttrName);
    if (attrc == 0) {
        return;
    }
    boolean isCompType = srcAttr.getDomain() instanceof CompositionType ? true : false;
    boolean isEnumType = srcAttr.getDomainResolvingAliases() instanceof EnumerationType ? true : false;
    EnumerationType srcEnumType = null;
    if (isEnumType) {
        srcEnumType = (EnumerationType) srcAttr.getDomainResolvingAliases();
    }
    AttributeDef destAttr = (AttributeDef) ((ViewableTransferElement) srctag2destElement.get(srcAttr.getScopedName())).obj;
    for (int attri = 0; attri < attrc; attri++) {
        String attrValue = iomObj.getattrprim(srcAttrName, attri);
        if (attrValue != null) {
            if (isEnumType) {
                attrValue = translateEnumValue((String) attrValue, srcEnumType, (EnumerationType) destAttr.getDomainResolvingAliases());
                iomObj.setattrvalue(srcAttrName, (String) attrValue);
            }
        } else {
            IomObject structValue = iomObj.getattrobj(srcAttrName, attri);
            if (isCompType) {
                translateObject(structValue);
            }
        }
    }
}
Also used : IomObject(ch.interlis.iom.IomObject) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) CompositionType(ch.interlis.ili2c.metamodel.CompositionType)

Example 40 with IomObject

use of ch.interlis.iom.IomObject in project ili2db by claeis.

the class Iox2wkbTest method testSurface2wkb.

@Test
public void testSurface2wkb() throws Iox2wkbException, ParseException {
    Iox2wkb conv = new Iox2wkb(2);
    String value = "MULTISURFACE {surface SURFACE {boundary BOUNDARY {polyline POLYLINE {sequence SEGMENTS {segment [COORD {C1 614899.152, C2 226049.103}, ARC {A1 614901.943, A2 226054.966, C1 614908.417, C2 226055.465}, ARC {A1 614909.049, A2 226044.618, C1 614899.152, C2 226049.103}]}}}}}";
    String v = "MULTISURFACE {surface SURFACE {boundary BOUNDARY {polyline POLYLINE {sequence SEGMENTS {segment [COORD {C1 614899.152, C2 226049.103}, ARC {A1 614901.943, A2 226054.966, C1 614908.417, C2 226055.465}, ARC {A1 614909.049, A2 226044.618, C1 614899.152, C2 226049.103}]}}}}}";
    IomObject polyline = newPolyline();
    addCoord(polyline, 614899.152, 226049.103);
    addArc(polyline, 614901.943, 226054.966, 614908.417, 226055.465);
    addArc(polyline, 614909.049, 226044.618, 614899.152, 226049.103);
    IomObject polygon = createPolgon(polyline);
    System.out.println(polygon);
    assertEquals(value, polygon.toString());
    byte[] wkb = conv.surface2wkb(polygon, false, 0.001);
    Wkb2iox backConv = new Wkb2iox();
    IomObject result = backConv.read(wkb);
    System.out.println(result);
}
Also used : IomObject(ch.interlis.iom.IomObject) Test(org.junit.Test)

Aggregations

IomObject (ch.interlis.iom.IomObject)66 EndBasketEvent (ch.interlis.iox.EndBasketEvent)35 EndTransferEvent (ch.interlis.iox.EndTransferEvent)35 IoxEvent (ch.interlis.iox.IoxEvent)35 StartTransferEvent (ch.interlis.iox.StartTransferEvent)35 File (java.io.File)35 HashMap (java.util.HashMap)35 Config (ch.ehi.ili2db.gui.Config)34 ObjectEvent (ch.interlis.iox.ObjectEvent)34 StartBasketEvent (ch.interlis.iox.StartBasketEvent)34 Connection (java.sql.Connection)34 XtfReader (ch.interlis.iom_j.xtf.XtfReader)30 Test (org.junit.Test)29 IoxException (ch.interlis.iox.IoxException)12 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)11 Iom_jObject (ch.interlis.iom_j.Iom_jObject)9 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)8 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)8 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)8 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)8