Search in sources :

Example 11 with Viewable

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

the class TransferFromXtf method deleteExisitingObjects.

private void deleteExisitingObjects(HashMap<String, HashSet<Long>> existingObjects) {
    for (String sqlType : existingObjects.keySet()) {
        HashSet<Long> objs = existingObjects.get(sqlType);
        StringBuilder ids = new StringBuilder();
        String sep = "";
        if (objs.size() > 0) {
            for (Long sqlId : objs) {
                ids.append(sep);
                ids.append(sqlId);
                sep = ",";
            }
            Object classo = tag2class.get(ili2sqlName.mapSqlTableName(sqlType));
            if (classo instanceof Viewable) {
                Viewable aclass = (Viewable) classo;
                while (aclass != null) {
                    deleteExistingObjectsHelper(recConv.getSqlType(aclass), ids.toString());
                    aclass = (Viewable) aclass.getExtending();
                }
            } else if (classo instanceof AttributeDef) {
                deleteExistingObjectsHelper(getSqlTableNameItfLineTable((AttributeDef) classo), ids.toString());
            } else {
                throw new IllegalStateException("unexpetced sqlType <" + sqlType + ">");
            }
        }
    }
}
Also used : Viewable(ch.interlis.ili2c.metamodel.Viewable) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Example 12 with Viewable

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

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

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

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

Viewable (ch.interlis.ili2c.metamodel.Viewable)36 Iterator (java.util.Iterator)16 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)14 IomObject (ch.interlis.iom.IomObject)13 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)11 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)10 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)9 ArrayList (java.util.ArrayList)9 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)8 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)8 ViewableTransferElement (ch.interlis.ili2c.metamodel.ViewableTransferElement)8 Topic (ch.interlis.ili2c.metamodel.Topic)7 Type (ch.interlis.ili2c.metamodel.Type)7 HashSet (java.util.HashSet)7 Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)6 Model (ch.interlis.ili2c.metamodel.Model)6 ObjectType (ch.interlis.ili2c.metamodel.ObjectType)6 DbTableName (ch.ehi.sqlgen.repository.DbTableName)5 CoordType (ch.interlis.ili2c.metamodel.CoordType)5 NumericType (ch.interlis.ili2c.metamodel.NumericType)5