Search in sources :

Example 16 with Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException 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 17 with Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException in project ili2db by claeis.

the class DbExtMetaInfo method readTableTab.

private HashMap<String, HashMap<String, String>> readTableTab(Connection conn, String sqlName) throws Ili2dbException {
    HashMap<String, HashMap<String, String>> exstEntries = new HashMap<String, HashMap<String, String>>();
    try {
        // select entries
        String selStmt = "SELECT " + DbNames.META_INFO_TABLE_TAB_TABLENAME_COL + "," + DbNames.META_INFO_TABLE_TAB_TAG_COL + "," + DbNames.META_INFO_TABLE_TAB_SETTING_COL + " FROM " + sqlName;
        EhiLogger.traceBackendCmd(selStmt);
        java.sql.PreparedStatement selPrepStmt = conn.prepareStatement(selStmt);
        ResultSet rs = selPrepStmt.executeQuery();
        try {
            while (rs.next()) {
                String table = rs.getString(1);
                String tag = rs.getString(2);
                String value = rs.getString(3);
                HashMap<String, String> exstValues = exstEntries.get(table);
                if (exstValues == null) {
                    exstValues = new HashMap<String, String>();
                    exstEntries.put(table, exstValues);
                }
                exstValues.put(tag, value);
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to read meta info values from " + sqlName, ex);
        } finally {
            selPrepStmt.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to read meta-info table " + sqlName, ex);
    }
    return exstEntries;
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet)

Example 18 with Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException in project ili2db by claeis.

the class DbExtMetaInfo method saveColumnTab.

private void saveColumnTab(Connection conn, String schemaName) throws Ili2dbException {
    DbTableName tabName = new DbTableName(schemaName, DbNames.META_INFO_COLUMN_TAB);
    String sqlName = tabName.getQName();
    HashMap<ColKey, HashMap<String, String>> exstEntries = readColumnTab(conn, sqlName);
    try {
        // insert entries
        String insStmt = "INSERT INTO " + sqlName + " (" + DbNames.META_INFO_COLUMN_TAB_TABLENAME_COL + "," + DbNames.META_INFO_COLUMN_TAB_SUBTYPE_COL + "," + DbNames.META_INFO_COLUMN_TAB_COLUMNNAME_COL + "," + DbNames.META_INFO_COLUMN_TAB_TAG_COL + "," + DbNames.META_INFO_COLUMN_TAB_SETTING_COL + ") VALUES (?,?,?,?,?)";
        EhiLogger.traceBackendCmd(insStmt);
        java.sql.PreparedStatement insPrepStmt = conn.prepareStatement(insStmt);
        try {
            for (ColKey colKey : colInfo.keySet()) {
                HashMap<String, String> exstValues = exstEntries.get(colKey);
                if (exstValues == null) {
                    exstValues = new HashMap<String, String>();
                }
                HashMap<String, String> newValues = colInfo.get(colKey);
                for (String tag : newValues.keySet()) {
                    if (!exstValues.containsKey(tag)) {
                        String value = newValues.get(tag);
                        insPrepStmt.setString(1, colKey.getTable());
                        insPrepStmt.setString(2, colKey.getSubtype());
                        insPrepStmt.setString(3, colKey.getColumn());
                        insPrepStmt.setString(4, tag);
                        insPrepStmt.setString(5, value);
                        insPrepStmt.executeUpdate();
                    }
                }
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to insert meta info values to " + sqlName, ex);
        } finally {
            insPrepStmt.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to update meta-info table " + sqlName, ex);
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) HashMap(java.util.HashMap) DbTableName(ch.ehi.sqlgen.repository.DbTableName)

Example 19 with Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException in project ili2db by claeis.

the class FromIliRecordConverter method generateTable.

public void generateTable(ViewableWrapper def, int pass) throws Ili2dbException {
    if (!def.isSecondaryTable()) {
        surfaceAttrs = new ArrayList<AttributeDef>();
    }
    // EhiLogger.debug("viewable "+def);
    if (pass == 1) {
        DbTableName sqlName = new DbTableName(schema.getName(), def.getSqlTablename());
        DbTable dbTable = new DbTable();
        dbTable.setName(sqlName);
        schema.addTable(dbTable);
        return;
    }
    // second pass; add columns
    DbTableName sqlName = new DbTableName(schema.getName(), def.getSqlTablename());
    DbTable dbTable = schema.findTable(sqlName);
    ViewableWrapper base = def.getExtending();
    {
        StringBuffer cmt = new StringBuffer();
        String cmtSep = "";
        if (!def.isSecondaryTable()) {
            dbTable.setIliName(def.getViewable().getScopedName(null));
            if (def.getViewable().getDocumentation() != null) {
                cmt.append(cmtSep + def.getViewable().getDocumentation());
                cmtSep = nl;
            }
            cmt.append(cmtSep + "@iliname " + def.getViewable().getScopedName(null));
            cmtSep = nl;
        }
        if (cmt.length() > 0) {
            dbTable.setComment(cmt.toString());
        }
    }
    if (deleteExistingData) {
        dbTable.setDeleteDataIfTableExists(true);
    }
    if (base == null && !def.isSecondaryTable()) {
        dbTable.setRequiresSequence(true);
    }
    String baseRef = "";
    DbColId dbColId = addKeyCol(dbTable);
    if (base != null) {
        dbColId.setScriptComment("REFERENCES " + base.getViewable().getScopedName(null));
        if (createFk) {
            dbColId.setReferencedTable(getSqlType(base.getViewable()));
        }
    } else if (def.isSecondaryTable()) {
        if (createFk) {
            dbColId.setReferencedTable(new DbTableName(schema.getName(), def.getMainTable().getSqlTablename()));
        }
    }
    if (createBasketCol) {
        // add basketCol
        DbColId t_basket = new DbColId();
        t_basket.setName(DbNames.T_BASKET_COL);
        t_basket.setNotNull(true);
        t_basket.setScriptComment("REFERENCES " + DbNames.BASKETS_TAB);
        if (createFk) {
            t_basket.setReferencedTable(new DbTableName(schema.getName(), DbNames.BASKETS_TAB));
        }
        if (createFkIdx) {
            t_basket.setIndex(true);
        }
        dbTable.addColumn(t_basket);
    }
    if (createDatasetCol) {
        DbColVarchar t_dsName = new DbColVarchar();
        t_dsName.setName(DbNames.T_DATASET_COL);
        t_dsName.setSize(DbNames.DATASETNAME_COL_SIZE);
        t_dsName.setNotNull(true);
        t_dsName.setIndex(true);
        dbTable.addColumn(t_dsName);
    }
    DbColumn dbCol;
    if (base == null && !def.isSecondaryTable()) {
        if (createTypeDiscriminator || def.includesMultipleTypes()) {
            dbCol = createSqlTypeCol(DbNames.T_TYPE_COL);
            dbTable.addColumn(dbCol);
        }
        // if CLASS
        if (!def.isStructure()) {
            if (createIliTidCol || def.getOid() != null) {
                addIliTidCol(dbTable, def.getOid());
            }
        }
        // if STRUCTURE, add ref to parent
        if (def.isStructure()) {
            if (createGenericStructRef) {
                // add parentid
                DbColId dbParentId = new DbColId();
                dbParentId.setName(DbNames.T_PARENT_ID_COL);
                dbParentId.setNotNull(true);
                dbParentId.setPrimaryKey(false);
                dbTable.addColumn(dbParentId);
                // add parent_type
                dbCol = createSqlTypeCol(DbNames.T_PARENT_TYPE_COL);
                dbTable.addColumn(dbCol);
                // add parent_attr
                dbCol = createSqlTypeCol(DbNames.T_PARENT_ATTR_COL);
                dbTable.addColumn(dbCol);
            } else {
            // add reference to parent for each structAttr when generating structAttr
            }
            // add sequence attr
            DbColId dbSeq = new DbColId();
            dbSeq.setName(DbNames.T_SEQ_COL);
            // dbSeq.setNotNull(true); // must be optional for cases where struct is exdended by a class
            dbSeq.setPrimaryKey(false);
            dbTable.addColumn(dbSeq);
        }
    }
    // body
    Iterator<ViewableTransferElement> iter = def.getAttrIterator();
    while (iter.hasNext()) {
        ViewableTransferElement obj = iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            if (attr.getExtending() == null) {
                try {
                    if (createItfLineTables && attr.getDomainResolvingAll() instanceof SurfaceOrAreaType) {
                        surfaceAttrs.add(attr);
                    }
                    if (!attr.isTransient()) {
                        Type proxyType = attr.getDomain();
                        if (proxyType != null && (proxyType instanceof ObjectType)) {
                        // skip implicit particles (base-viewables) of views
                        } else {
                            generateAttr(dbTable, def.getViewable(), attr);
                        }
                    }
                } catch (Exception ex) {
                    throw new Ili2dbException(attr.getContainer().getScopedName(null) + "." + attr.getName(), ex);
                }
            } else {
                if (attr.isDomainBoolean()) {
                } else if (createEnumColAsItfCode && attr.getDomainResolvingAll() instanceof EnumerationType) {
                    throw new Ili2dbException("EXTENDED attributes with type enumeration not supported");
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            if (role.getExtending() == null) {
                // not an embedded role and roledef not defined in a lightweight association?
                if (!obj.embedded && !def.isAssocLightweight()) {
                    ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
                    for (ViewableWrapper targetTable : targetTables) {
                        dbColId = new DbColId();
                        DbTableName targetSqlTableName = targetTable.getSqlTable();
                        String roleSqlName = ili2sqlName.mapIliRoleDef(role, sqlName.getName(), targetSqlTableName.getName(), targetTables.size() > 1);
                        dbColId.setName(roleSqlName);
                        boolean notNull = false;
                        if (!sqlEnableNull) {
                            if (targetTables.size() > 1) {
                                // multiple alternative FK columns
                                notNull = false;
                            } else {
                                notNull = true;
                            }
                        }
                        dbColId.setNotNull(notNull);
                        dbColId.setPrimaryKey(false);
                        if (createFk) {
                            dbColId.setReferencedTable(targetSqlTableName);
                        }
                        if (createFkIdx) {
                            dbColId.setIndex(true);
                        }
                        String cmt = role.getDocumentation();
                        if (cmt != null && cmt.length() > 0) {
                            dbColId.setComment(cmt);
                        }
                        dbTable.addColumn(dbColId);
                        // handle ordered
                        if (role.isOrdered()) {
                            // add seqeunce attr
                            DbColId dbSeq = new DbColId();
                            dbSeq.setName(roleSqlName + "_" + DbNames.T_SEQ_COL);
                            dbSeq.setNotNull(notNull);
                            dbSeq.setPrimaryKey(false);
                            dbTable.addColumn(dbSeq);
                        }
                    }
                }
                // a role of an embedded association?
                if (obj.embedded) {
                    AssociationDef roleOwner = (AssociationDef) role.getContainer();
                    if (roleOwner.getDerivedFrom() == null) {
                        // role is oppend;
                        ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
                        for (ViewableWrapper targetTable : targetTables) {
                            dbColId = new DbColId();
                            DbTableName targetSqlTableName = targetTable.getSqlTable();
                            String roleSqlName = ili2sqlName.mapIliRoleDef(role, sqlName.getName(), targetSqlTableName.getName(), targetTables.size() > 1);
                            dbColId.setName(roleSqlName);
                            boolean notNull = false;
                            if (!sqlEnableNull) {
                                if (targetTables.size() > 1) {
                                    // multiple alternative FK columns
                                    notNull = false;
                                } else if (role.getOppEnd().getDestination() != def.getViewable()) {
                                    // other subtypes in of def don't have this FK
                                    notNull = false;
                                } else {
                                    if (role.getCardinality().getMinimum() == 0) {
                                        notNull = false;
                                    } else {
                                        notNull = true;
                                    }
                                }
                            }
                            dbColId.setNotNull(notNull);
                            dbColId.setPrimaryKey(false);
                            if (createFk) {
                                dbColId.setReferencedTable(targetSqlTableName);
                            }
                            if (createFkIdx) {
                                dbColId.setIndex(true);
                            }
                            String cmt = role.getDocumentation();
                            if (cmt != null && cmt.length() > 0) {
                                dbColId.setComment(cmt);
                            }
                            customMapping.fixupEmbeddedLink(dbTable, dbColId, roleOwner, role, targetSqlTableName, colT_ID);
                            dbTable.addColumn(dbColId);
                            // handle ordered
                            if (role.getOppEnd().isOrdered()) {
                                // add seqeunce attr
                                DbColId dbSeq = new DbColId();
                                dbSeq.setName(roleSqlName + "_" + DbNames.T_SEQ_COL);
                                dbSeq.setNotNull(notNull);
                                dbSeq.setPrimaryKey(false);
                                dbTable.addColumn(dbSeq);
                            }
                        }
                    }
                }
            }
        }
    }
    if (createStdCols) {
        addStdCol(dbTable);
    }
    if (createUnique && !def.isStructure()) {
        // check if UNIQUE mappable
        HashSet wrapperCols = getWrapperCols(def.getAttrv());
        Viewable aclass = def.getViewable();
        Iterator it = aclass.iterator();
        while (it.hasNext()) {
            Object cnstro = it.next();
            if (cnstro instanceof UniquenessConstraint) {
                UniquenessConstraint cnstr = (UniquenessConstraint) cnstro;
                HashSet attrs = getUniqueAttrs(cnstr, wrapperCols);
                // mappable?
                if (attrs != null) {
                    DbIndex dbIndex = new DbIndex();
                    dbIndex.setPrimary(false);
                    dbIndex.setUnique(true);
                    for (Object attro : attrs) {
                        String attrSqlName = null;
                        if (attro instanceof AttributeDef) {
                            attrSqlName = ili2sqlName.mapIliAttributeDef((AttributeDef) attro, def.getSqlTablename(), null);
                        } else if (attro instanceof RoleDef) {
                            RoleDef role = (RoleDef) attro;
                            DbTableName targetSqlTableName = getSqlType(role.getDestination());
                            attrSqlName = ili2sqlName.mapIliRoleDef(role, def.getSqlTablename(), targetSqlTableName.getName());
                        } else {
                            throw new IllegalStateException("unexpected attr " + attro);
                        }
                        DbColumn idxCol = dbTable.getColumn(attrSqlName);
                        dbIndex.addAttr(idxCol);
                    }
                    dbTable.addIndex(dbIndex);
                }
            }
        }
    }
    if (!def.isSecondaryTable()) {
        customMapping.fixupViewable(dbTable, def.getViewable());
    }
}
Also used : DbColumn(ch.ehi.sqlgen.repository.DbColumn) Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) DbIndex(ch.ehi.sqlgen.repository.DbIndex) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) Iterator(java.util.Iterator) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) HashSet(java.util.HashSet) ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) DbColVarchar(ch.ehi.sqlgen.repository.DbColVarchar) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) BasketType(ch.interlis.ili2c.metamodel.BasketType) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) TextType(ch.interlis.ili2c.metamodel.TextType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) NumericType(ch.interlis.ili2c.metamodel.NumericType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) AreaType(ch.interlis.ili2c.metamodel.AreaType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) Viewable(ch.interlis.ili2c.metamodel.Viewable) DbColId(ch.ehi.sqlgen.repository.DbColId) UniquenessConstraint(ch.interlis.ili2c.metamodel.UniquenessConstraint) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 20 with Ili2dbException

use of ch.ehi.ili2db.base.Ili2dbException in project ili2db by claeis.

the class Assoc23Test method importXtfRefUnkownFail.

@Test
public void importXtfRefUnkownFail() throws Exception {
    // EhiLogger.getInstance().setTraceFilter(false);
    Connection jdbcConnection = null;
    try {
        Class driverClass = Class.forName("org.postgresql.Driver");
        jdbcConnection = DriverManager.getConnection(dburl, dbuser, dbpwd);
        stmt = jdbcConnection.createStatement();
        stmt.execute("DROP SCHEMA IF EXISTS " + DBSCHEMA + " CASCADE");
        {
            {
                File data = new File("test/data/Assoc23/Assoc1z.xtf");
                Config config = initConfig(data.getPath(), DBSCHEMA, data.getPath() + ".log");
                config.setFunction(Config.FC_IMPORT);
                config.setCreateFk(config.CREATE_FK_YES);
                config.setTidHandling(Config.TID_HANDLING_PROPERTY);
                config.setBasketHandling(config.BASKET_HANDLING_READWRITE);
                config.setCatalogueRefTrafo(null);
                config.setMultiSurfaceTrafo(null);
                config.setMultilingualTrafo(null);
                config.setInheritanceTrafo(null);
                Ili2db.readSettingsFromDb(config);
                try {
                    Ili2db.run(config, null);
                    Assert.fail();
                } catch (Ili2dbException ex) {
                }
            }
        }
    } finally {
        if (jdbcConnection != null) {
            jdbcConnection.close();
        }
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) Config(ch.ehi.ili2db.gui.Config) Connection(java.sql.Connection) File(java.io.File) Test(org.junit.Test)

Aggregations

Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)31 SQLException (java.sql.SQLException)13 DbTableName (ch.ehi.sqlgen.repository.DbTableName)10 HashSet (java.util.HashSet)10 Iterator (java.util.Iterator)8 HashMap (java.util.HashMap)7 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)6 Viewable (ch.interlis.ili2c.metamodel.Viewable)6 PreparedStatement (java.sql.PreparedStatement)5 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)4 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)4 Model (ch.interlis.ili2c.metamodel.Model)4 Domain (ch.interlis.ili2c.metamodel.Domain)3 Topic (ch.interlis.ili2c.metamodel.Topic)3 View (ch.interlis.ili2c.metamodel.View)3 IomObject (ch.interlis.iom.IomObject)3 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 DbTable (ch.ehi.sqlgen.repository.DbTable)2 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)2