Search in sources :

Example 6 with Model

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

the class TransferFromIli method setupTopicTagMap.

/**
 * setup a mapping from a qualified model or topic name
 * to the corresponding java object.
 */
private HashMap setupTopicTagMap(TransferDescription td) {
    HashMap ret = new HashMap();
    Iterator modeli = td.iterator();
    while (modeli.hasNext()) {
        Object mObj = modeli.next();
        if (mObj instanceof Model) {
            Model model = (Model) mObj;
            ret.put(model.getScopedName(null), model);
            Iterator topici = model.iterator();
            while (topici.hasNext()) {
                Object tObj = topici.next();
                if (tObj instanceof Topic) {
                    Topic topic = (Topic) tObj;
                    ret.put(topic.getScopedName(null), topic);
                }
            }
        }
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) Iterator(java.util.Iterator) Model(ch.interlis.ili2c.metamodel.Model) Topic(ch.interlis.ili2c.metamodel.Topic)

Example 7 with Model

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

use of ch.interlis.ili2c.metamodel.Model 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)

Example 9 with Model

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

the class ModelElementSelector method visitTopic.

private void visitTopic(HashSet<Element> visitedElements, HashSet<Model> accuScope, Topic def) {
    if (visitedElements.contains(def)) {
        return;
    }
    visitedElements.add(def);
    Iterator classi = def.iterator();
    while (classi.hasNext()) {
        Object classo = classi.next();
        if (classo instanceof Viewable) {
            visitViewable(visitedElements, accuScope, (Viewable) classo);
        } else if (classo instanceof Domain) {
            visitDomain(visitedElements, accuScope, (Domain) classo);
        }
    }
    // base topic
    Topic base = (Topic) def.getExtending();
    if (base != null) {
        visitTopic(visitedElements, accuScope, base);
    }
    // depends on
    Iterator depi = def.getDependentOn();
    while (depi.hasNext()) {
        Topic dep = (Topic) depi.next();
        visitTopic(visitedElements, accuScope, dep);
    }
    // parent model
    Model model = (Model) def.getContainer(Model.class);
    if (!accuScope.contains(model)) {
        accuScope.add(model);
    }
}
Also used : Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) Model(ch.interlis.ili2c.metamodel.Model) Domain(ch.interlis.ili2c.metamodel.Domain) Topic(ch.interlis.ili2c.metamodel.Topic)

Example 10 with Model

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

the class ModelElementSelector method getModelElements.

public List<Element> getModelElements(List<String> modelNames, TransferDescription td, boolean createItfLineTables, boolean includeEnums, Config config) {
    this.td = td;
    this.createItfLineTables = createItfLineTables;
    this.includeEnums = includeEnums;
    List<Model> models = new ArrayList<Model>();
    if (modelNames == null || modelNames.isEmpty()) {
        Model lastModel = td.getLastModel();
        models.add(lastModel);
    } else {
        for (String modelName : modelNames) {
            Model model = (Model) td.getElement(Model.class, modelName);
            if (model == null) {
                throw new IllegalArgumentException("unknown model <" + modelName + ">");
            }
            models.add(model);
        }
    }
    HashSet<Element> accu = new HashSet<Element>();
    HashSet<Model> accuScope = new HashSet<Model>();
    for (Model model : models) {
        if (config.getVer4_translation() || config.getIli1Translation() != null) {
            visitModel(accu, accuScope, (Model) model.getTranslationOfOrSame());
        } else {
            visitModel(accu, accuScope, model);
        }
    }
    visitStructsInScope(accu, accuScope, config);
    ArrayList<Element> ret = new ArrayList<Element>(accu);
    java.util.Collections.sort(ret, new java.util.Comparator<Element>() {

        @Override
        public int compare(Element arg0, Element arg1) {
            if (arg0.getDefidx() < arg1.getDefidx()) {
                return -1;
            }
            if (arg0.getDefidx() > arg1.getDefidx()) {
                return 1;
            }
            return 0;
        }
    });
    return ret;
}
Also used : Element(ch.interlis.ili2c.metamodel.Element) ArrayList(java.util.ArrayList) Model(ch.interlis.ili2c.metamodel.Model) HashSet(java.util.HashSet)

Aggregations

Model (ch.interlis.ili2c.metamodel.Model)13 Iterator (java.util.Iterator)11 Topic (ch.interlis.ili2c.metamodel.Topic)8 Viewable (ch.interlis.ili2c.metamodel.Viewable)6 IomObject (ch.interlis.iom.IomObject)6 ArrayList (java.util.ArrayList)5 Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)4 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)4 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)4 PredefinedModel (ch.interlis.ili2c.metamodel.PredefinedModel)4 TypeModel (ch.interlis.ili2c.metamodel.TypeModel)4 View (ch.interlis.ili2c.metamodel.View)4 Iom_jObject (ch.interlis.iom_j.Iom_jObject)4 ReduceToBaseModel (ch.interlis.iox_j.filter.ReduceToBaseModel)4 DbTableName (ch.ehi.sqlgen.repository.DbTableName)3 Domain (ch.interlis.ili2c.metamodel.Domain)3 HashSet (java.util.HashSet)3 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)2 Table (ch.interlis.ili2c.metamodel.Table)2 ItfWriter (ch.interlis.iom_j.itf.ItfWriter)2