Search in sources :

Example 11 with IomObject

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

the class TransferFromXtf method allReferencesKnown.

private boolean allReferencesKnown(long basketSqlId, IomObject iomObj) {
    String tag = iomObj.getobjecttag();
    // EhiLogger.debug("tag "+tag);
    Object modelele = tag2class.get(tag);
    if (modelele == null) {
        return true;
    }
    // is it a SURFACE or AREA line table?
    if (createItfLineTables && modelele instanceof AttributeDef) {
        return true;
    }
    String tid = iomObj.getobjectoid();
    if ((tid == null || tid.length() == 0) && modelele instanceof AssociationDef) {
        tid = getAssociationId(iomObj, (AssociationDef) modelele);
    }
    if (tid != null && tid.length() > 0) {
        oidPool.createObjSqlId(Ili2cUtility.getRootViewable((Viewable) modelele).getScopedName(null), tag, tid);
    }
    FixIomObjectExtRefs extref = new FixIomObjectExtRefs(basketSqlId, tag, tid);
    allReferencesKnownHelper(iomObj, extref);
    if (!extref.needsFixing()) {
        return true;
    }
    // EhiLogger.debug("needs fixing "+iomObj.getobjectoid());
    delayedObjects.add(extref);
    objPool.put(tid, iomObj);
    return false;
}
Also used : AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Example 12 with IomObject

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

the class TransferFromXtf method writeItfLineTableObject.

private void writeItfLineTableObject(String datasetName, long basketSqlId, IomObject iomObj, AttributeDef attrDef) throws java.sql.SQLException, ConverterException {
    SurfaceOrAreaType type = (SurfaceOrAreaType) attrDef.getDomainResolvingAliases();
    String geomAttrName = ch.interlis.iom_j.itf.ModelUtilities.getHelperTableGeomAttrName(attrDef);
    String refAttrName = null;
    if (type instanceof SurfaceType) {
        refAttrName = ch.interlis.iom_j.itf.ModelUtilities.getHelperTableMainTableRef(attrDef);
    }
    Table lineAttrTable = type.getLineAttributeStructure();
    // map oid of transfer file to a sql id
    String idTag = attrDef.getContainer().getScopedName(null) + "." + attrDef.getName();
    long sqlId = oidPool.createObjSqlId(idTag, idTag, iomObj.getobjectoid());
    String sqlTableName = getSqlTableNameItfLineTable(attrDef).getQName();
    String insert = createItfLineTableInsertStmt(attrDef);
    EhiLogger.traceBackendCmd(insert);
    PreparedStatement ps = conn.prepareStatement(insert);
    try {
        int valuei = 1;
        ps.setLong(valuei, sqlId);
        valuei++;
        if (createBasketCol) {
            ps.setLong(valuei, basketSqlId);
            valuei++;
        }
        if (createDatasetCol) {
            ps.setString(valuei, datasetName);
            valuei++;
        }
        if (readIliTid) {
            // import TID from transfer file
            ps.setString(valuei, iomObj.getobjectoid());
            valuei++;
        }
        IomObject value = iomObj.getattrobj(geomAttrName, 0);
        if (value != null) {
            boolean is3D = ((CoordType) (type).getControlPointDomain().getType()).getDimensions().length == 3;
            ps.setObject(valuei, geomConv.fromIomPolyline(value, recConv.getSrsid(attrDef), is3D, recConv.getP(type)));
        } else {
            geomConv.setPolylineNull(ps, valuei);
        }
        valuei++;
        if (type instanceof SurfaceType) {
            IomObject structvalue = iomObj.getattrobj(refAttrName, 0);
            String refoid = structvalue.getobjectrefoid();
            long refsqlId = oidPool.getObjSqlId(attrDef.getContainer().getScopedName(null), refoid);
            ps.setLong(valuei, refsqlId);
            valuei++;
        }
        if (lineAttrTable != null) {
            Iterator attri = lineAttrTable.getAttributes();
            while (attri.hasNext()) {
                AttributeDef lineattr = (AttributeDef) attri.next();
                valuei = recConv.addAttrValue(iomObj, ili2sqlName.mapGeometryAsTable(attrDef), sqlId, sqlTableName, ps, valuei, lineattr, null);
            }
        }
        if (createStdCols) {
            // T_LastChange
            ps.setTimestamp(valuei, today);
            valuei++;
            // T_CreateDate
            ps.setTimestamp(valuei, today);
            valuei++;
            // T_User
            ps.setString(valuei, dbusr);
            valuei++;
        }
        ps.executeUpdate();
    } finally {
        ps.close();
    }
}
Also used : Table(ch.interlis.ili2c.metamodel.Table) IomObject(ch.interlis.iom.IomObject) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) Iterator(java.util.Iterator) PreparedStatement(java.sql.PreparedStatement) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CoordType(ch.interlis.ili2c.metamodel.CoordType)

Example 13 with IomObject

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

the class TransferFromXtf method allReferencesKnownHelper.

private void allReferencesKnownHelper(IomObject iomObj, FixIomObjectExtRefs extref) {
    String tag = iomObj.getobjecttag();
    // EhiLogger.debug("tag "+tag);
    Object modelele = tag2class.get(tag);
    if (modelele == null) {
        return;
    }
    // ASSERT: an ordinary class/table
    Viewable aclass = (Viewable) modelele;
    Iterator iter = aclass.getAttributesAndRoles2();
    while (iter.hasNext()) {
        ViewableTransferElement obj = (ViewableTransferElement) iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            if (!attr.isTransient()) {
                Type proxyType = attr.getDomain();
                if (proxyType != null && (proxyType instanceof ObjectType)) {
                // skip implicit particles (base-viewables) of views
                } else {
                    allReferencesKnownHelper(iomObj, attr, extref);
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            if (role.getExtending() == null) {
                String roleName = role.getName();
                // a role of an embedded association?
                if (obj.embedded) {
                    AssociationDef roleOwner = (AssociationDef) role.getContainer();
                    if (roleOwner.getDerivedFrom() == null) {
                        // not just a link?
                        IomObject structvalue = iomObj.getattrobj(roleName, 0);
                        if (roleOwner.getAttributes().hasNext() || roleOwner.getLightweightAssociations().iterator().hasNext()) {
                        // TODO handle attributes of link
                        }
                        if (structvalue != null) {
                            String refoid = structvalue.getobjectrefoid();
                            Viewable targetClass = role.getDestination();
                            if (!oidPool.containsXtfid(Ili2cUtility.getRootViewable(targetClass).getScopedName(null), refoid)) {
                                extref.addFix(structvalue, targetClass);
                            }
                        }
                    }
                } else {
                    IomObject structvalue = iomObj.getattrobj(roleName, 0);
                    String refoid = structvalue.getobjectrefoid();
                    Viewable targetClass = role.getDestination();
                    if (!oidPool.containsXtfid(Ili2cUtility.getRootViewable(targetClass).getScopedName(null), refoid)) {
                        extref.addFix(structvalue, targetClass);
                    }
                }
            }
        }
    }
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) NumericType(ch.interlis.ili2c.metamodel.NumericType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) IomObject(ch.interlis.iom.IomObject) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Example 14 with IomObject

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

the class TransferFromXtf method writeObject.

/**
 * if structEle==null, iomObj is an object. If structEle!=null iomObj is a struct value.
 */
private void writeObject(String datasetName, long basketSqlId, IomObject iomObj, StructWrapper structEle, Map<String, ClassStat> objStat) throws java.sql.SQLException, ConverterException {
    String tag = iomObj.getobjecttag();
    // EhiLogger.debug("tag "+tag);
    Object modelele = tag2class.get(tag);
    if (modelele == null) {
        if (!unknownTypev.contains(tag)) {
            EhiLogger.logError("unknown type <" + tag + ">, line " + Integer.toString(iomObj.getobjectline()) + ", col " + Integer.toString(iomObj.getobjectcol()));
        }
        return;
    }
    // is it a SURFACE or AREA line table?
    if (createItfLineTables && modelele instanceof AttributeDef) {
        writeItfLineTableObject(datasetName, basketSqlId, iomObj, (AttributeDef) modelele);
        return;
    }
    // ASSERT: an ordinary class/table
    Viewable aclass1 = (Viewable) modelele;
    String sqlType = (String) ili2sqlName.mapIliClassDef(aclass1);
    long sqlId;
    boolean updateObj = false;
    // is it an object?
    if (structEle == null) {
        // map oid of transfer file to a sql id
        String tid = iomObj.getobjectoid();
        if (tid != null && tid.length() > 0) {
            sqlId = oidPool.getObjSqlId(Ili2cUtility.getRootViewable(aclass1).getScopedName(null), tid);
            if (functionCode == Config.FC_UPDATE && existingObjectsContains(sqlType, sqlId)) {
                updateObj = true;
                existingObjectsRemove(sqlType, sqlId);
            }
        } else {
            // it is an assoc without tid
            // get a new sql id
            sqlId = oidPool.newObjSqlId();
        }
    } else {
        // it is a struct value
        // get a new sql id
        sqlId = oidPool.newObjSqlId();
    }
    updateObjStat(objStat, tag, sqlId);
    // loop over all classes; start with leaf, end with the base of the inheritance hierarchy
    ViewableWrapper aclass = class2wrapper.get(aclass1);
    while (aclass != null) {
        {
            String insert = getInsertStmt(updateObj, aclass1, aclass, structEle);
            EhiLogger.traceBackendCmd(insert);
            PreparedStatement ps = conn.prepareStatement(insert);
            try {
                recConv.writeRecord(basketSqlId, iomObj, aclass1, structEle, aclass, sqlType, sqlId, updateObj, ps, structQueue);
                ps.executeUpdate();
            } finally {
                ps.close();
            }
        }
        for (ViewableWrapper secondary : aclass.getSecondaryTables()) {
            // secondarytable contains attributes of this class?
            if (secondary.containsAttributes(recConv.getIomObjectAttrs(aclass1))) {
                String insert = getInsertStmt(updateObj, aclass1, secondary, structEle);
                EhiLogger.traceBackendCmd(insert);
                PreparedStatement ps = conn.prepareStatement(insert);
                try {
                    recConv.writeRecord(basketSqlId, iomObj, aclass1, structEle, secondary, sqlType, sqlId, updateObj, ps, structQueue);
                    ps.executeUpdate();
                } finally {
                    ps.close();
                }
            }
        }
        aclass = aclass.getExtending();
    }
}
Also used : Viewable(ch.interlis.ili2c.metamodel.Viewable) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) PreparedStatement(java.sql.PreparedStatement) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper)

Example 15 with IomObject

use of ch.interlis.iom.IomObject 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)

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