Search in sources :

Example 11 with Ili2dbException

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

the class TransferFromIli method updateSettings.

public static void updateSettings(java.sql.Connection conn, Config settings, String schema) throws Ili2dbException {
    String sqlName = DbNames.SETTINGS_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    try {
        // insert entries
        String insStmt = "INSERT INTO " + sqlName + " (" + DbNames.SETTINGS_TAB_TAG_COL + "," + DbNames.SETTINGS_TAB_SETTING_COL + ") VALUES (?,?)";
        EhiLogger.traceBackendCmd(insStmt);
        java.sql.PreparedStatement insPrepStmt = conn.prepareStatement(insStmt);
        try {
            java.util.Iterator entri = settings.getValues().iterator();
            while (entri.hasNext()) {
                String tag = (String) entri.next();
                insPrepStmt.clearParameters();
                insPrepStmt.setString(1, tag);
                insPrepStmt.setString(2, settings.getValue(tag));
                insPrepStmt.executeUpdate();
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to insert setting", ex);
        } finally {
            insPrepStmt.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to update settings-table " + sqlName, ex);
    }
}
Also used : SQLException(java.sql.SQLException) Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) Iterator(java.util.Iterator)

Example 12 with Ili2dbException

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

the class TransferFromIli method generatModelEles.

private void generatModelEles(java.util.List<Element> modelEles, int pass) throws Ili2dbException {
    Iterator modeli = modelEles.iterator();
    while (modeli.hasNext()) {
        Object modelo = modeli.next();
        if (modelo instanceof Model) {
            Model model = (Model) modelo;
        // generateModel(model);
        } else if (modelo instanceof Topic) {
        // generateTopic((Topic)modelo);
        } else if (modelo instanceof Domain) {
            if (pass == 2) {
                generateDomain((Domain) modelo);
                visitedElements.add((Domain) modelo);
            }
        } else if (modelo instanceof Viewable) {
            if (modelo instanceof Table && ((Table) modelo).isIli1LineAttrStruct()) {
            // skip it
            } else if ((modelo instanceof View) && !isTransferableView(modelo)) {
            // skip it
            } else {
                try {
                    ViewableWrapper wrapper = class2wrapper.get((Viewable) modelo);
                    if (wrapper != null) {
                        generateViewable(wrapper, pass);
                    }
                    if (pass == 2) {
                        visitedElements.add((Viewable) modelo);
                    }
                } catch (Ili2dbException ex) {
                    throw new Ili2dbException("mapping of " + ((Viewable) modelo).getScopedName(null) + " failed", ex);
                }
            }
        } else if (modelo instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) modelo;
            if (attr.getDomainResolvingAll() instanceof SurfaceOrAreaType) {
                generateItfLineTable(attr, pass);
            } else if (attr.getDomainResolvingAll() instanceof EnumerationType) {
                if (pass == 2) {
                    visitedEnums.add(attr);
                }
            } else {
            // skip it
            }
        } else {
        // skip it
        }
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) DbTable(ch.ehi.sqlgen.repository.DbTable) Table(ch.interlis.ili2c.metamodel.Table) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) View(ch.interlis.ili2c.metamodel.View) Iterator(java.util.Iterator) Model(ch.interlis.ili2c.metamodel.Model) Viewable(ch.interlis.ili2c.metamodel.Viewable) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) Topic(ch.interlis.ili2c.metamodel.Topic) Domain(ch.interlis.ili2c.metamodel.Domain)

Example 13 with Ili2dbException

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

the class TransferFromIli method updateInheritanceTable.

public void updateInheritanceTable(java.sql.Connection conn, String schema) throws Ili2dbException {
    String sqlName = DbNames.INHERIT_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    // String stmt="CREATE TABLE "+tabname+" ("+thisClassCol+" VARCHAR2(30) NOT NULL,"+baseClassCol+" VARCHAR2(30) NULL)";
    HashSet<String> exstEntries = readInheritanceTable(conn, schema);
    try {
        // insert entries
        String stmt = "INSERT INTO " + sqlName + " (" + DbNames.INHERIT_TAB_THIS_COL + "," + DbNames.INHERIT_TAB_BASE_COL + ") VALUES (?,?)";
        EhiLogger.traceBackendCmd(stmt);
        java.sql.PreparedStatement ps = conn.prepareStatement(stmt);
        String thisClass = null;
        try {
            for (Object aclass : visitedElements) {
                if (aclass instanceof Viewable) {
                    thisClass = ((Viewable) aclass).getScopedName(null);
                    if (!exstEntries.contains(thisClass)) {
                        Viewable base = (Viewable) ((Viewable) aclass).getExtending();
                        ps.setString(1, thisClass);
                        if (base != null) {
                            ps.setString(2, base.getScopedName(null));
                        } else {
                            ps.setNull(2, java.sql.Types.VARCHAR);
                        }
                        ps.executeUpdate();
                    }
                }
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to insert inheritance-relation for class " + thisClass, ex);
        } finally {
            ps.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to update inheritance-table " + sqlName, ex);
    }
}
Also used : SQLException(java.sql.SQLException) Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) Viewable(ch.interlis.ili2c.metamodel.Viewable)

Example 14 with Ili2dbException

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

the class TransferFromXtf method readExistingSqlObjIds.

private Long readExistingSqlObjIds(boolean isItf, String bid) throws Ili2dbException {
    StringBuilder topicQName = new StringBuilder();
    Long basketSqlId = Ili2db.getBasketSqlIdFromBID(bid, conn, schema, colT_ID, topicQName);
    if (basketSqlId == null) {
        // new basket
        return null;
    }
    Topic topic = TransferToXtf.getTopicDef(td, topicQName.toString());
    if (topic == null) {
        throw new Ili2dbException("unkown topic " + topicQName.toString());
    }
    Model model = (Model) topic.getContainer();
    // for all Viewables
    Iterator iter = null;
    if (isItf) {
        ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
        iter = itftablev.iterator();
    } else {
        iter = getXtfTables(td, topic).iterator();
    }
    HashSet<String> visitedTables = new HashSet<String>();
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Viewable) {
            if ((obj instanceof View) && !TransferFromIli.isTransferableView(obj)) {
            // skip it
            } else if (!TransferToXtf.suppressViewable((Viewable) obj)) {
                Viewable aclass = (Viewable) obj;
                if (aclass.isAbstract()) {
                    throw new IllegalArgumentException("unexpected abstract viewable " + aclass.getScopedName(null));
                }
                // get sql name
                DbTableName sqlName = recConv.getSqlType(aclass);
                ViewableWrapper wrapper = recConv.getViewableWrapper(sqlName.getName());
                ViewableWrapper base = wrapper.getExtending();
                while (base != null) {
                    wrapper = base;
                    base = wrapper.getExtending();
                }
                sqlName = wrapper.getSqlTable();
                if (!visitedTables.contains(sqlName.getQName())) {
                    visitedTables.add(sqlName.getQName());
                    // if table exists?
                    if (DbUtility.tableExists(conn, sqlName)) {
                        // dump it
                        EhiLogger.logState(aclass.getScopedName(null) + " read ids...");
                        readObjectSqlIds(!wrapper.includesMultipleTypes(), sqlName, basketSqlId);
                    } else {
                        // skip it
                        EhiLogger.traceUnusualState(aclass.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                    }
                }
            }
        } else if (obj instanceof AttributeDef) {
            if (isItf) {
                AttributeDef attr = (AttributeDef) obj;
                // get sql name
                DbTableName sqlName = getSqlTableNameItfLineTable(attr);
                // if table exists?
                if (DbUtility.tableExists(conn, sqlName)) {
                    // dump it
                    EhiLogger.logState(attr.getContainer().getScopedName(null) + "_" + attr.getName() + " read ids...");
                    readObjectSqlIds(isItf, sqlName, basketSqlId);
                } else {
                    // skip it
                    EhiLogger.traceUnusualState(attr.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                }
            }
        }
    }
    return basketSqlId;
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) View(ch.interlis.ili2c.metamodel.View) Model(ch.interlis.ili2c.metamodel.Model) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) Topic(ch.interlis.ili2c.metamodel.Topic) DbTableName(ch.ehi.sqlgen.repository.DbTableName) HashSet(java.util.HashSet)

Example 15 with Ili2dbException

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

the class TransferFromXtf method deleteObjectsOfBasket.

private void deleteObjectsOfBasket(long basketSqlId, String topicQName) throws Ili2dbException {
    boolean isItf = false;
    Topic topic = TransferToXtf.getTopicDef(td, topicQName);
    if (topic == null) {
        throw new Ili2dbException("unkown topic " + topicQName.toString());
    }
    Model model = (Model) topic.getContainer();
    // for all Viewables
    // see also export
    Iterator iter = null;
    if (isItf) {
        ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
        iter = itftablev.iterator();
    } else {
        // get transferable viewables of topic
        iter = topic.getViewables().iterator();
    }
    HashSet<ViewableWrapper> visitedTables = new HashSet<ViewableWrapper>();
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Viewable) {
            if ((obj instanceof View) && !TransferFromIli.isTransferableView(obj)) {
            // skip it
            } else if (!TransferToXtf.suppressViewable((Viewable) obj)) {
                Viewable aclass1 = (Viewable) obj;
                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
                            }
                        }
                    }
                    for (ViewableWrapper secondary : wrapper.getSecondaryTables()) {
                        if (!visitedTables.contains(secondary)) {
                            visitedTables.add(secondary);
                            // if table exists?
                            // get sql name
                            DbTableName sqlName = secondary.getSqlTable();
                            if (DbUtility.tableExists(conn, sqlName)) {
                                // delete it
                                dropRecords(sqlName, basketSqlId);
                            } else {
                            // skip it; no table
                            }
                        }
                    }
                    wrapper = wrapper.getExtending();
                }
            }
        } else if (obj instanceof AttributeDef) {
            if (isItf) {
                AttributeDef attr = (AttributeDef) obj;
                // get sql name
                DbTableName sqlName = getSqlTableNameItfLineTable(attr);
                // if table exists?
                if (DbUtility.tableExists(conn, sqlName)) {
                    dropRecords(sqlName, basketSqlId);
                } else {
                // skip it; no table
                }
            }
        }
    }
    dropExistingStructEles(topicQName, basketSqlId);
    String sqlName = DbNames.IMPORTS_BASKETS_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    java.sql.PreparedStatement getstmt = null;
    try {
        String stmt = "DELETE FROM " + sqlName + " WHERE " + DbNames.IMPORTS_BASKETS_TAB_BASKET_COL + "= ?";
        EhiLogger.traceBackendCmd(stmt);
        getstmt = conn.prepareStatement(stmt);
        getstmt.setLong(1, basketSqlId);
        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);
            }
        }
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) PreparedStatement(java.sql.PreparedStatement) View(ch.interlis.ili2c.metamodel.View) SQLException(java.sql.SQLException) Model(ch.interlis.ili2c.metamodel.Model) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) Topic(ch.interlis.ili2c.metamodel.Topic) DbTableName(ch.ehi.sqlgen.repository.DbTableName) HashSet(java.util.HashSet)

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