Search in sources :

Example 1 with AbstractClassDef

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

the class ReduceToBaseModel method setupTopicTranslation.

private void setupTopicTranslation(Topic srcTopic, Topic baseTopic) {
    srctag2destElement.put(srcTopic.getScopedName(), baseTopic);
    if (srcTopic == baseTopic) {
        // no translation required
        return;
    }
    // for each class of srcTopic, find base in baseTopic
    while (srcTopic != null) {
        if (srcTopic.isExtending(baseTopic) && srcTopic != baseTopic) {
            Iterator<Element> eleIt = srcTopic.iterator();
            while (eleIt.hasNext()) {
                Element srcEle = eleIt.next();
                if (srcEle instanceof AbstractClassDef) {
                    AbstractClassDef baseEle = (AbstractClassDef) srcEle;
                    while (baseEle != null) {
                        if (baseEle.getContainer() == baseTopic) {
                            break;
                        }
                        baseEle = (AbstractClassDef) baseEle.getExtending();
                    }
                    if (baseEle != null) {
                        srctag2destElement.put(srcEle.getScopedName(), baseEle);
                        if (srcEle != baseEle) {
                            setupClassTranslation((AbstractClassDef) srcEle, baseEle);
                        } else {
                        // no object translation required
                        // setupClassTranslation((AbstractClassDef) srcEle,baseEle);
                        }
                    } else {
                        // delete object from export
                        srctag2destElement.put(srcEle.getScopedName(), null);
                    }
                }
            }
        } else {
            // keep elements, as they are (no translation required)
            Iterator<Element> eleIt = srcTopic.iterator();
            while (eleIt.hasNext()) {
                Element srcEle = eleIt.next();
                if (srcEle instanceof AbstractClassDef) {
                    AbstractClassDef baseEle = (AbstractClassDef) srcEle;
                    srctag2destElement.put(srcEle.getScopedName(), baseEle);
                // no object translation required
                // setupClassTranslation((AbstractClassDef) srcEle,baseEle);
                }
            }
        }
        srcTopic = (Topic) srcTopic.getExtending();
    }
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) Element(ch.interlis.ili2c.metamodel.Element) AbstractClassDef(ch.interlis.ili2c.metamodel.AbstractClassDef)

Example 2 with AbstractClassDef

use of ch.interlis.ili2c.metamodel.AbstractClassDef 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("}");
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Table(ch.interlis.ili2c.metamodel.Table) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) TypeAlias(ch.interlis.ili2c.metamodel.TypeAlias) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) CoordType(ch.interlis.ili2c.metamodel.CoordType) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) AbstractClassDef(ch.interlis.ili2c.metamodel.AbstractClassDef) Domain(ch.interlis.ili2c.metamodel.Domain) CoordType(ch.interlis.ili2c.metamodel.CoordType)

Example 3 with AbstractClassDef

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

the class TransferFromXtf method getStructs.

private HashSet<AbstractClassDef> getStructs(String topicQName) {
    Topic def = TransferToXtf.getTopicDef(td, topicQName);
    HashSet<AbstractClassDef> visitedStructs = new HashSet<AbstractClassDef>();
    while (def != null) {
        Iterator classi = def.iterator();
        while (classi.hasNext()) {
            Object classo = classi.next();
            if (classo instanceof Viewable) {
                if (classo instanceof Table && ((Table) classo).isIdentifiable()) {
                    getStructs_Helper((AbstractClassDef) classo, visitedStructs);
                }
            }
        }
        def = (Topic) def.getExtending();
    }
    return visitedStructs;
}
Also used : Table(ch.interlis.ili2c.metamodel.Table) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) IomObject(ch.interlis.iom.IomObject) AbstractClassDef(ch.interlis.ili2c.metamodel.AbstractClassDef) Topic(ch.interlis.ili2c.metamodel.Topic) HashSet(java.util.HashSet)

Example 4 with AbstractClassDef

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

the class TransferFromXtf method doit.

public void doit(IoxReader reader, Config config, Map<Long, BasketStat> stat) throws IoxException, Ili2dbException {
    if (functionCode == Config.FC_UPDATE || functionCode == Config.FC_REPLACE) {
        if (!createBasketCol) {
            throw new Ili2dbException("update/replace requires a basket column");
        }
    }
    // limit import to given BIDs
    HashSet<String> limitedToBids = null;
    {
        String baskets = config.getBaskets();
        if (baskets != null) {
            String[] basketidv = baskets.split(ch.interlis.ili2c.Main.MODELS_SEPARATOR);
            limitedToBids = new HashSet<String>();
            for (String basketid : basketidv) {
                limitedToBids.add(basketid);
            }
        }
    }
    // limit import to given TOPICs
    HashSet<String> limitedToTopics = null;
    {
        String topics = config.getTopics();
        if (topics != null) {
            String[] topicv = topics.split(ch.interlis.ili2c.Main.MODELS_SEPARATOR);
            limitedToTopics = new HashSet<String>();
            for (String topic : topicv) {
                limitedToTopics.add(topic);
            }
        }
    }
    String datasetName = config.getDatasetName();
    Long datasetSqlId = null;
    today = new java.sql.Timestamp(System.currentTimeMillis());
    if (doItfLineTables) {
        tag2class = ch.interlis.iom_j.itf.ModelUtilities.getTagMap(td);
    } else {
        tag2class = ch.interlis.ili2c.generator.XSDGenerator.getTagMap(td);
    }
    isItfReader = reader instanceof ItfReader;
    unknownTypev = new HashSet();
    structQueue = new ArrayList();
    boolean surfaceAsPolyline = true;
    boolean ignoreUnresolvedReferences = false;
    recman = new ObjectPoolManager();
    try {
        objPool = recman.newObjectPool();
        long importSqlId = 0;
        long basketSqlId = 0;
        long startTid = 0;
        long endTid = 0;
        long objCount = 0;
        boolean referrs = false;
        recConv = new FromXtfRecordConverter(td, ili2sqlName, tag2class, config, idGen, geomConv, conn, dbusr, isItfReader, oidPool, trafoConfig, class2wrapper, datasetName);
        if (functionCode == Config.FC_DELETE || functionCode == Config.FC_REPLACE) {
            if (datasetName == null) {
                throw new Ili2dbException("delete/replace requires a dataset name");
            }
            // delete existing data base on basketSqlId
            datasetSqlId = Ili2db.getDatasetId(datasetName, conn, config);
            if (datasetSqlId == null) {
                if (functionCode == Config.FC_DELETE) {
                // nothing to do
                } else if (functionCode == Config.FC_REPLACE) {
                    // new dataset, not a replace!
                    datasetSqlId = oidPool.newObjSqlId();
                    try {
                        writeDataset(datasetSqlId, datasetName);
                        importSqlId = writeImportStat(datasetSqlId, xtffilename, today, dbusr);
                    } catch (SQLException e) {
                        EhiLogger.logError(e);
                    } catch (ConverterException e) {
                        EhiLogger.logError(e);
                    }
                }
            } else {
                deleteObjectsOfExistingDataset(datasetSqlId, config);
                if (functionCode == Config.FC_DELETE) {
                    String sqlName = DbNames.DATASETS_TAB;
                    if (schema != null) {
                        sqlName = schema + "." + sqlName;
                    }
                    java.sql.PreparedStatement getstmt = null;
                    try {
                        String stmt = "DELETE FROM " + sqlName + " WHERE " + colT_ID + "= ?";
                        EhiLogger.traceBackendCmd(stmt);
                        getstmt = conn.prepareStatement(stmt);
                        getstmt.setLong(1, datasetSqlId);
                        getstmt.executeUpdate();
                    } catch (java.sql.SQLException ex) {
                        throw new Ili2dbException("failed to delete from " + sqlName, ex);
                    } finally {
                        if (getstmt != null) {
                            try {
                                getstmt.close();
                                getstmt = null;
                            } catch (java.sql.SQLException ex) {
                                EhiLogger.logError(ex);
                            }
                        }
                    }
                }
                try {
                    importSqlId = writeImportStat(datasetSqlId, xtffilename, today, dbusr);
                } catch (SQLException e) {
                    EhiLogger.logError(e);
                } catch (ConverterException e) {
                    EhiLogger.logError(e);
                }
            }
        } else if (functionCode == Config.FC_UPDATE) {
            if (datasetName == null) {
                throw new Ili2dbException("update requires a dataset name");
            }
            try {
                datasetSqlId = Ili2db.getDatasetId(datasetName, conn, config);
                if (datasetSqlId != null) {
                } else {
                    datasetSqlId = oidPool.newObjSqlId();
                    writeDataset(datasetSqlId, datasetName);
                }
                importSqlId = writeImportStat(datasetSqlId, xtffilename, today, dbusr);
            } catch (SQLException e) {
                EhiLogger.logError(e);
            } catch (ConverterException e) {
                EhiLogger.logError(e);
            }
        } else if (functionCode == Config.FC_IMPORT) {
            try {
                if (datasetName == null) {
                    datasetSqlId = oidPool.newObjSqlId();
                    if (xtffilename != null) {
                        datasetName = new java.io.File(xtffilename).getName() + "-" + Long.toString(datasetSqlId);
                    } else {
                        datasetName = Long.toString(datasetSqlId);
                    }
                } else {
                    datasetSqlId = Ili2db.getDatasetId(datasetName, conn, config);
                    if (datasetSqlId != null) {
                        throw new Ili2dbException("dataset " + datasetName + " already exists");
                    } else {
                        datasetSqlId = oidPool.newObjSqlId();
                    }
                }
                writeDataset(datasetSqlId, datasetName);
                importSqlId = writeImportStat(datasetSqlId, xtffilename, today, dbusr);
            } catch (SQLException e) {
                EhiLogger.logError(e);
            } catch (ConverterException e) {
                EhiLogger.logError(e);
            }
        } else {
            throw new IllegalArgumentException("unexpected function code " + functionCode);
        }
        if (functionCode == Config.FC_DELETE) {
            return;
        }
        if (reader instanceof ItfReader) {
            ((ItfReader) reader).setBidPrefix(datasetName);
        } else if (reader instanceof ItfReader2) {
            ((ItfReader2) reader).setBidPrefix(datasetName);
        }
        ch.interlis.iox_j.validator.Validator validator = null;
        if (config.isValidation()) {
            ValidationConfig modelConfig = new ValidationConfig();
            modelConfig.mergeIliMetaAttrs(td);
            String configFilename = config.getValidConfigFile();
            if (configFilename != null) {
                try {
                    modelConfig.mergeConfigFile(new File(configFilename));
                } catch (FileNotFoundException e) {
                    EhiLogger.logError("validator config file <" + configFilename + "> not found");
                }
            }
            modelConfig.setConfigValue(ValidationConfig.PARAMETER, ValidationConfig.AREA_OVERLAP_VALIDATION, config.isDisableAreaValidation() ? ValidationConfig.OFF : null);
            modelConfig.setConfigValue(ValidationConfig.PARAMETER, ValidationConfig.DEFAULT_GEOMETRY_TYPE_VALIDATION, config.isSkipGeometryErrors() ? ValidationConfig.OFF : null);
            modelConfig.setConfigValue(ValidationConfig.PARAMETER, ValidationConfig.ALLOW_ONLY_MULTIPLICITY_REDUCTION, config.isOnlyMultiplicityReduction() ? ValidationConfig.ON : null);
            IoxLogging errHandler = new ch.interlis.iox_j.logging.Log2EhiLogger();
            LogEventFactory errFactory = new LogEventFactory();
            errFactory.setDataSource(xtffilename);
            PipelinePool pipelinePool = new PipelinePool();
            validator = new ch.interlis.iox_j.validator.Validator(td, modelConfig, errHandler, errFactory, pipelinePool, config);
            if (reader instanceof ItfReader2) {
                ((ItfReader2) reader).setIoxDataPool(pipelinePool);
            }
        }
        // setup list of objects that have external/forward references
        delayedObjects = new ArrayList<FixIomObjectExtRefs>();
        HashMap<String, ClassStat> objStat = null;
        StartBasketEvent basket = null;
        // more baskets?
        IoxEvent event = reader.read();
        try {
            boolean skipBasket = false;
            while (event != null) {
                if (event instanceof StartBasketEvent) {
                    basket = (StartBasketEvent) event;
                    // do not import this this basket?
                    if ((limitedToBids != null && !limitedToBids.contains(basket.getBid())) || (limitedToTopics != null && !limitedToTopics.contains(basket.getType()))) {
                        // do not import this basket
                        skipBasket = true;
                        EhiLogger.logState("Skip Basket " + basket.getType() + "(oid " + basket.getBid() + ")");
                        if (validator != null)
                            validator.validate(event);
                        if (languageFilter != null) {
                            event = languageFilter.filter(event);
                        }
                    } else {
                        // import this basket
                        EhiLogger.logState("Basket " + basket.getType() + "(oid " + basket.getBid() + ")...");
                        skipBasket = false;
                        try {
                            if (validator != null)
                                validator.validate(event);
                            if (languageFilter != null) {
                                event = languageFilter.filter(event);
                            }
                            Long existingBasketSqlId = null;
                            if (functionCode == Config.FC_UPDATE) {
                                // read existing oid/sqlid mapping (but might also be a new basket)
                                existingObjectsOfCurrentBasket = new HashMap<String, HashSet<Long>>();
                                existingBasketSqlId = readExistingSqlObjIds(reader instanceof ItfReader, basket.getBid());
                                if (existingBasketSqlId == null) {
                                    // new basket
                                    basketSqlId = oidPool.getBasketSqlId(basket.getBid());
                                } else {
                                    // existing basket
                                    basketSqlId = existingBasketSqlId;
                                    // drop existing structeles
                                    dropExistingStructEles(basket.getType(), basketSqlId);
                                }
                            } else {
                                basketSqlId = oidPool.getBasketSqlId(basket.getBid());
                            }
                            if (attachmentKey == null) {
                                if (xtffilename != null) {
                                    attachmentKey = new java.io.File(xtffilename).getName() + "-" + Long.toString(basketSqlId);
                                } else {
                                    attachmentKey = Long.toString(basketSqlId);
                                }
                                config.setAttachmentKey(attachmentKey);
                            }
                            if (existingBasketSqlId == null) {
                                writeBasket(datasetSqlId, basket, basketSqlId, attachmentKey);
                            } else {
                            // TODO update attachmentKey of existing basket
                            }
                        } catch (SQLException ex) {
                            EhiLogger.logError("Basket " + basket.getType() + "(oid " + basket.getBid() + ")", ex);
                        } catch (ConverterException ex) {
                            EhiLogger.logError("Basket " + basket.getType() + "(oid " + basket.getBid() + ")", ex);
                        }
                        startTid = oidPool.getLastSqlId();
                        objStat = new HashMap<String, ClassStat>();
                        objCount = 0;
                    }
                } else if (event instanceof EndBasketEvent) {
                    if (validator != null)
                        validator.validate(event);
                    if (languageFilter != null) {
                        event = languageFilter.filter(event);
                    }
                    if (reader instanceof ItfReader2) {
                        ArrayList<IoxInvalidDataException> dataerrs = ((ItfReader2) reader).getDataErrs();
                        if (dataerrs.size() > 0) {
                            if (!skipBasket) {
                                if (!config.isSkipGeometryErrors()) {
                                    for (IoxInvalidDataException dataerr : dataerrs) {
                                        EhiLogger.logError(dataerr);
                                    }
                                }
                            }
                            ((ItfReader2) reader).clearDataErrs();
                        }
                    }
                    if (!skipBasket) {
                        // fix external/forward references
                        ArrayList<FixIomObjectExtRefs> fixedObjects = new ArrayList<FixIomObjectExtRefs>();
                        for (FixIomObjectExtRefs fixref : delayedObjects) {
                            boolean skipObj = false;
                            for (IomObject ref : fixref.getRefs()) {
                                String xtfid = ref.getobjectrefoid();
                                Viewable aclass = fixref.getTargetClass(ref);
                                String rootClassName = Ili2cUtility.getRootViewable(aclass).getScopedName(null);
                                if (oidPool.containsXtfid(rootClassName, xtfid)) {
                                // reference now resolvable
                                } else {
                                    // reference not yet known, try to resolve again at end of transfer
                                    skipObj = true;
                                }
                            }
                            if (!skipObj) {
                                doObject(datasetName, basketSqlId, objPool.get(fixref.getRootTid()), objStat);
                                fixedObjects.add(fixref);
                            }
                        }
                        delayedObjects.removeAll(fixedObjects);
                        if (functionCode == Config.FC_UPDATE) {
                            // delete no longer existing objects
                            deleteExisitingObjects(existingObjectsOfCurrentBasket);
                        }
                        // TODO update import counters
                        endTid = oidPool.getLastSqlId();
                        try {
                            String filename = null;
                            if (xtffilename != null) {
                                filename = new java.io.File(xtffilename).getName();
                            }
                            long importId = writeImportBasketStat(importSqlId, basketSqlId, startTid, endTid, objCount);
                            saveObjStat(stat, importId, basket.getBid(), basketSqlId, filename, basket.getType(), objStat);
                        } catch (SQLException ex) {
                            EhiLogger.logError("Basket " + basket.getType() + "(oid " + basket.getBid() + ")", ex);
                        } catch (ConverterException ex) {
                            EhiLogger.logError("Basket " + basket.getType() + "(oid " + basket.getBid() + ")", ex);
                        }
                    }
                    skipBasket = false;
                } else if (event instanceof ObjectEvent) {
                    if (validator != null)
                        validator.validate(event);
                    if (!skipBasket) {
                        if (languageFilter != null) {
                            event = languageFilter.filter(event);
                        }
                        objCount++;
                        IomObject iomObj = ((ObjectEvent) event).getIomObject();
                        if (allReferencesKnown(basketSqlId, iomObj)) {
                            // translate object
                            doObject(datasetName, basketSqlId, iomObj, objStat);
                        }
                    }
                } else if (event instanceof EndTransferEvent) {
                    if (validator != null)
                        validator.validate(event);
                    if (languageFilter != null) {
                        event = languageFilter.filter(event);
                    }
                    {
                        for (FixIomObjectExtRefs fixref : delayedObjects) {
                            boolean skipObj = false;
                            for (IomObject ref : fixref.getRefs()) {
                                String xtfid = ref.getobjectrefoid();
                                Viewable aclass = fixref.getTargetClass(ref);
                                String rootClassName = Ili2cUtility.getRootViewable(aclass).getScopedName(null);
                                if (oidPool.containsXtfid(rootClassName, xtfid)) {
                                // skip it; now resolvable
                                } else {
                                    // object in another basket
                                    if (readIliTid || (aclass instanceof AbstractClassDef && ((AbstractClassDef) aclass).getOid() != null)) {
                                        // read object
                                        Long sqlid = readObjectSqlid(aclass, xtfid);
                                        if (sqlid == null) {
                                            if (!ignoreUnresolvedReferences) {
                                                EhiLogger.logError("unknown referenced object " + aclass.getScopedName(null) + " TID " + xtfid + " referenced from " + fixref.getRootTag() + " TID " + fixref.getRootTid());
                                                referrs = true;
                                                skipObj = true;
                                            }
                                        }
                                    } else {
                                        if (!ignoreUnresolvedReferences) {
                                            EhiLogger.logError("unknown referenced object " + aclass.getScopedName(null) + " TID " + xtfid + " referenced from " + fixref.getRootTag() + " TID " + fixref.getRootTid());
                                            referrs = true;
                                            skipObj = true;
                                        }
                                    }
                                }
                            }
                            if (!skipObj) {
                                objStat = stat.get(fixref.getBasketSqlId()).getObjStat();
                                doObject(datasetName, fixref.getBasketSqlId(), objPool.get(fixref.getRootTid()), objStat);
                            }
                        }
                    }
                    break;
                } else if (event instanceof StartTransferEvent) {
                    if (validator != null)
                        validator.validate(event);
                    if (languageFilter != null) {
                        event = languageFilter.filter(event);
                    }
                }
                event = reader.read();
            }
            if (referrs) {
                throw new IoxException("dangling references");
            }
        } finally {
            if (validator != null) {
                validator.close();
                validator = null;
            }
            if (languageFilter != null) {
                languageFilter.close();
            }
        }
    } finally {
        recman.close();
    }
}
Also used : ConverterException(ch.ehi.ili2db.converter.ConverterException) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) LogEventFactory(ch.interlis.iox_j.logging.LogEventFactory) IomObject(ch.interlis.iom.IomObject) EndTransferEvent(ch.interlis.iox.EndTransferEvent) IoxEvent(ch.interlis.iox.IoxEvent) AbstractClassDef(ch.interlis.ili2c.metamodel.AbstractClassDef) ItfReader2(ch.interlis.iom_j.itf.ItfReader2) HashSet(java.util.HashSet) ObjectEvent(ch.interlis.iox_j.ObjectEvent) StartTransferEvent(ch.interlis.iox.StartTransferEvent) PreparedStatement(java.sql.PreparedStatement) ValidationConfig(ch.interlis.iox_j.validator.ValidationConfig) PipelinePool(ch.interlis.iox_j.PipelinePool) Viewable(ch.interlis.ili2c.metamodel.Viewable) File(java.io.File) Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) IoxLogging(ch.interlis.iox.IoxLogging) SQLException(java.sql.SQLException) EndBasketEvent(ch.interlis.iox.EndBasketEvent) SQLException(java.sql.SQLException) ObjectPoolManager(ch.ehi.iox.objpool.ObjectPoolManager) StartBasketEvent(ch.interlis.iox_j.StartBasketEvent) IoxInvalidDataException(ch.interlis.iox_j.IoxInvalidDataException) File(java.io.File) ItfReader(ch.interlis.iom_j.itf.ItfReader) IoxException(ch.interlis.iox.IoxException)

Example 5 with AbstractClassDef

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

the class TransferFromXtf method dropExistingStructEles.

private void dropExistingStructEles(String topic, long basketSqlId) {
    // get all structs that are reachable from this topic
    HashSet<AbstractClassDef> classv = getStructs(topic);
    // delete all structeles
    HashSet<ViewableWrapper> visitedTables = new HashSet<ViewableWrapper>();
    for (AbstractClassDef aclass1 : classv) {
        ViewableWrapper wrapper = class2wrapper.get(aclass1);
        while (wrapper != null) {
            {
                if (!visitedTables.contains(wrapper)) {
                    visitedTables.add(wrapper);
                    // if table exists?
                    // get sql name
                    DbTableName sqlName = wrapper.getSqlTable();
                    if (DbUtility.tableExists(conn, sqlName)) {
                        // delete it
                        dropRecords(sqlName, basketSqlId);
                    } else {
                    // skip it; no table
                    }
                }
            }
            wrapper = wrapper.getExtending();
        }
    }
}
Also used : AbstractClassDef(ch.interlis.ili2c.metamodel.AbstractClassDef) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) DbTableName(ch.ehi.sqlgen.repository.DbTableName) HashSet(java.util.HashSet)

Aggregations

AbstractClassDef (ch.interlis.ili2c.metamodel.AbstractClassDef)9 Table (ch.interlis.ili2c.metamodel.Table)4 Viewable (ch.interlis.ili2c.metamodel.Viewable)4 HashSet (java.util.HashSet)4 IomObject (ch.interlis.iom.IomObject)3 Iterator (java.util.Iterator)3 ConverterException (ch.ehi.ili2db.converter.ConverterException)2 AssociationDef (ch.interlis.ili2c.metamodel.AssociationDef)2 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)2 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)2 CoordType (ch.interlis.ili2c.metamodel.CoordType)2 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)2 PolylineType (ch.interlis.ili2c.metamodel.PolylineType)2 RoleDef (ch.interlis.ili2c.metamodel.RoleDef)2 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)2 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)2 Type (ch.interlis.ili2c.metamodel.Type)2 ViewableTransferElement (ch.interlis.ili2c.metamodel.ViewableTransferElement)2 Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)1 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)1