Search in sources :

Example 21 with Viewable

use of ch.interlis.ili2c.metamodel.Viewable in project ili2db by claeis.

the class ModelElementSelector method visitViewable.

private void visitViewable(HashSet<Element> visitedElements, HashSet<Model> accuScope, Viewable def) {
    if (visitedElements.contains(def)) {
        return;
    }
    visitedElements.add(def);
    // generateViewable(def);
    Iterator attri = def.iterator();
    while (attri.hasNext()) {
        Object attro = attri.next();
        if (attro instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) attro;
            Type type = attr.getDomain();
            if (type instanceof CompositionType) {
                CompositionType compType = (CompositionType) type;
                visitViewable(visitedElements, accuScope, compType.getComponentType());
                Iterator resti = compType.iteratorRestrictedTo();
                while (resti.hasNext()) {
                    Viewable rest = (Viewable) resti.next();
                    visitViewable(visitedElements, accuScope, rest);
                }
            } else if (createItfLineTables && attr.getDomainResolvingAll() instanceof SurfaceOrAreaType) {
                visitItfLineTable(visitedElements, accuScope, attr);
            } else if (includeEnums && attr.getDomainResolvingAll() instanceof EnumerationType) {
                visitAttributeDef(visitedElements, accuScope, attr);
            }
            // collect referenced domains
            if (type instanceof TypeAlias) {
                visitDomain(visitedElements, accuScope, ((TypeAlias) type).getAliasing());
            }
        }
    }
    // base viewable
    Viewable base = (Viewable) def.getExtending();
    if (base != null) {
        visitViewable(visitedElements, accuScope, base);
    }
}
Also used : EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) Type(ch.interlis.ili2c.metamodel.Type) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) TypeAlias(ch.interlis.ili2c.metamodel.TypeAlias) CompositionType(ch.interlis.ili2c.metamodel.CompositionType)

Example 22 with Viewable

use of ch.interlis.ili2c.metamodel.Viewable in project ili2db by claeis.

the class TransferToXtf method doBasket.

private boolean doBasket(String filename, IoxWriter iomFile, Topic topic, Long basketSqlId, String basketXtfId) throws IoxException {
    Model model = (Model) topic.getContainer();
    boolean referrs = false;
    StartBasketEvent iomBasket = null;
    delayedObjects = new ArrayList<FixIomObjectRefs>();
    // for all Viewables
    Iterator iter = null;
    if (iomFile instanceof ItfWriter) {
        ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
        iter = itftablev.iterator();
    } else {
        iter = topic.getViewables().iterator();
    }
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Viewable) {
            if ((obj instanceof View) && !TransferFromIli.isTransferableView(obj)) {
            // skip it
            } else if (!suppressViewable((Viewable) obj)) {
                Viewable aclass = (Viewable) obj;
                if (languageFilter != null) {
                    aclass = (Viewable) aclass.getTranslationOfOrSame();
                }
                ViewableWrapper wrapper = class2wrapper.get(aclass);
                // get sql name
                DbTableName sqlName = recConv.getSqlType(wrapper.getViewable());
                // if table exists?
                if (DbUtility.tableExists(conn, sqlName)) {
                    // dump it
                    EhiLogger.logState(aclass.getScopedName(null) + "...");
                    if (iomBasket == null) {
                        iomBasket = new StartBasketEvent(topic.getScopedName(null), basketXtfId);
                        if (languageFilter != null) {
                            iomBasket = (StartBasketEvent) languageFilter.filter(iomBasket);
                        }
                        if (exportBaseModelFilter != null) {
                            iomBasket = (StartBasketEvent) exportBaseModelFilter.filter(iomBasket);
                        }
                        if (validator != null)
                            validator.validate(iomBasket);
                        iomFile.write(iomBasket);
                    }
                    dumpObject(iomFile, aclass, basketSqlId);
                } else {
                    // skip it
                    EhiLogger.traceUnusualState(aclass.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                }
            }
        } else if (obj instanceof AttributeDef) {
            if (iomFile instanceof ItfWriter) {
                AttributeDef attr = (AttributeDef) obj;
                // get sql name
                DbTableName sqlName = getSqlTableNameItfLineTable(attr);
                // if table exists?
                if (DbUtility.tableExists(conn, sqlName)) {
                    // dump it
                    EhiLogger.logState(attr.getContainer().getScopedName(null) + "_" + attr.getName() + "...");
                    if (iomBasket == null) {
                        iomBasket = new StartBasketEvent(topic.getScopedName(null), topic.getScopedName(null));
                        if (languageFilter != null) {
                            iomBasket = (StartBasketEvent) languageFilter.filter(iomBasket);
                        }
                        if (exportBaseModelFilter != null) {
                            iomBasket = (StartBasketEvent) exportBaseModelFilter.filter(iomBasket);
                        }
                        if (validator != null)
                            validator.validate(iomBasket);
                        iomFile.write(iomBasket);
                    }
                    dumpItfTableObject(iomFile, attr, basketSqlId);
                } else {
                    // skip it
                    EhiLogger.traceUnusualState(attr.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                }
            }
        }
    }
    if (iomBasket != null) {
        // fix forward references
        for (FixIomObjectRefs fixref : delayedObjects) {
            boolean skipObj = false;
            for (IomObject ref : fixref.getRefs()) {
                long sqlid = fixref.getTargetSqlid(ref);
                if (sqlidPool.containsSqlid(sqlid)) {
                    // fix it
                    ref.setobjectrefoid(sqlidPool.getXtfid(sqlid));
                } else {
                    // object in another basket
                    Viewable aclass = fixref.getTargetClass(ref);
                    // read object
                    String tid = readObjectTid(aclass, sqlid);
                    if (tid == null) {
                        EhiLogger.logError("unknown referenced object " + aclass.getScopedName(null) + " sqlid " + fixref.getTargetSqlid(ref) + " referenced from " + fixref.getRoot().getobjecttag() + " TID " + fixref.getRoot().getobjectoid());
                        referrs = true;
                        skipObj = true;
                    } else {
                        // fix reference
                        ref.setobjectrefoid(tid);
                    }
                }
            }
            if (!skipObj) {
                ObjectEvent objEvent = new ObjectEvent(fixref.getRoot());
                if (languageFilter != null) {
                    objEvent = (ObjectEvent) languageFilter.filter(objEvent);
                }
                if (exportBaseModelFilter != null) {
                    objEvent = (ObjectEvent) exportBaseModelFilter.filter(objEvent);
                }
                if (objEvent != null) {
                    if (validator != null)
                        validator.validate(objEvent);
                    iomFile.write(objEvent);
                }
            }
        }
        EndBasketEvent endBasket = new EndBasketEvent();
        if (languageFilter != null) {
            endBasket = (EndBasketEvent) languageFilter.filter(endBasket);
        }
        if (exportBaseModelFilter != null) {
            endBasket = (EndBasketEvent) exportBaseModelFilter.filter(endBasket);
        }
        if (validator != null)
            validator.validate(endBasket);
        iomFile.write(endBasket);
        saveObjStat(iomBasket.getBid(), basketSqlId, filename, iomBasket.getType());
    }
    return referrs;
}
Also used : ObjectEvent(ch.interlis.iox_j.ObjectEvent) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) View(ch.interlis.ili2c.metamodel.View) EndBasketEvent(ch.interlis.iox_j.EndBasketEvent) IomObject(ch.interlis.iom.IomObject) ReduceToBaseModel(ch.interlis.iox_j.filter.ReduceToBaseModel) PredefinedModel(ch.interlis.ili2c.metamodel.PredefinedModel) TypeModel(ch.interlis.ili2c.metamodel.TypeModel) Model(ch.interlis.ili2c.metamodel.Model) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) StartBasketEvent(ch.interlis.iox_j.StartBasketEvent) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) ItfWriter(ch.interlis.iom_j.itf.ItfWriter) DbTableName(ch.ehi.sqlgen.repository.DbTableName)

Example 23 with Viewable

use of ch.interlis.ili2c.metamodel.Viewable 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 24 with Viewable

use of ch.interlis.ili2c.metamodel.Viewable 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 25 with Viewable

use of ch.interlis.ili2c.metamodel.Viewable in project ili2db by claeis.

the class ReduceToBaseModel method translateObject.

private void translateObject(IomObject iomObj) {
    Element modelElement = (Element) tag2class.get(iomObj.getobjecttag());
    Element destModelEle = getTranslatedElement(modelElement);
    if (destModelEle == modelElement) {
        // no translation required
        return;
    }
    Viewable aclass = (Viewable) modelElement;
    Viewable destClass = (Viewable) destModelEle;
    String destName = destClass.getScopedName();
    iomObj.setobjecttag(destName);
    // handle attrs
    Iterator iter = aclass.getAttributesAndRoles2();
    while (iter.hasNext()) {
        ViewableTransferElement srcProp = (ViewableTransferElement) iter.next();
        ViewableTransferElement destProp = (ViewableTransferElement) srctag2destElement.get(((Element) srcProp.obj).getScopedName());
        if (destProp == null) {
            iomObj.setattrundefined(((Element) srcProp.obj).getName());
        } else {
            if (srcProp.obj instanceof AttributeDef) {
                AttributeDef attr = (AttributeDef) srcProp.obj;
                if (!attr.isTransient()) {
                    Type proxyType = attr.getDomain();
                    if (proxyType != null && (proxyType instanceof ObjectType)) {
                    // skip implicit particles (base-viewables) of views
                    } else {
                        translateAttrValue(iomObj, attr);
                    }
                }
            }
        }
    }
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) Type(ch.interlis.ili2c.metamodel.Type) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) Element(ch.interlis.ili2c.metamodel.Element) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iterator(java.util.Iterator) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Aggregations

Viewable (ch.interlis.ili2c.metamodel.Viewable)36 Iterator (java.util.Iterator)16 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)14 IomObject (ch.interlis.iom.IomObject)13 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)11 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)10 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)9 ArrayList (java.util.ArrayList)9 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)8 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)8 ViewableTransferElement (ch.interlis.ili2c.metamodel.ViewableTransferElement)8 Topic (ch.interlis.ili2c.metamodel.Topic)7 Type (ch.interlis.ili2c.metamodel.Type)7 HashSet (java.util.HashSet)7 Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)6 Model (ch.interlis.ili2c.metamodel.Model)6 ObjectType (ch.interlis.ili2c.metamodel.ObjectType)6 DbTableName (ch.ehi.sqlgen.repository.DbTableName)5 CoordType (ch.interlis.ili2c.metamodel.CoordType)5 NumericType (ch.interlis.ili2c.metamodel.NumericType)5