Search in sources :

Example 16 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class TransferToXtf method createQueryStmt4xtfid.

private String createQueryStmt4xtfid(Viewable aclass) {
    ArrayList<ViewableWrapper> wrappers = recConv.getTargetTables(aclass);
    StringBuffer ret = new StringBuffer();
    int i = 1;
    ret.append("SELECT " + colT_ID + "," + DbNames.T_ILI_TID_COL + "," + DbNames.T_TYPE_COL + " FROM (");
    String sep = "";
    for (ViewableWrapper wrapper : wrappers) {
        ret.append(sep);
        ret.append("SELECT r" + i + "." + colT_ID);
        if (writeIliTid || wrapper.getOid() != null) {
            ret.append(", r" + i + "." + DbNames.T_ILI_TID_COL);
        } else {
            ret.append(", NULL " + DbNames.T_ILI_TID_COL);
        }
        if (recConv.createTypeDiscriminator() || wrapper.includesMultipleTypes()) {
            ret.append(", r" + i + "." + DbNames.T_TYPE_COL);
        } else {
            ret.append(", '" + wrapper.getSqlTable().getName() + "' " + DbNames.T_TYPE_COL);
        }
        ret.append(" FROM ");
        ret.append(wrapper.getSqlTable().getQName());
        ret.append(" r" + i + "");
        i++;
        sep = " UNION ";
    }
    ret.append(") r0");
    ret.append(" WHERE r0." + colT_ID + "=?");
    return ret.toString();
}
Also used : ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper)

Example 17 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class TransferToXtf method doBasket.

private boolean doBasket(String filename, IoxWriter iomFile, Topic topic, Long basketSqlId, String basketXtfId) throws IoxException {
    Model model = (Model) topic.getContainer();
    boolean referrs = false;
    StartBasketEvent iomBasket = null;
    delayedObjects = new ArrayList<FixIomObjectRefs>();
    // for all Viewables
    Iterator iter = null;
    if (iomFile instanceof ItfWriter) {
        ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
        iter = itftablev.iterator();
    } else {
        iter = topic.getViewables().iterator();
    }
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Viewable) {
            if ((obj instanceof View) && !TransferFromIli.isTransferableView(obj)) {
            // skip it
            } else if (!suppressViewable((Viewable) obj)) {
                Viewable aclass = (Viewable) obj;
                if (languageFilter != null) {
                    aclass = (Viewable) aclass.getTranslationOfOrSame();
                }
                ViewableWrapper wrapper = class2wrapper.get(aclass);
                // get sql name
                DbTableName sqlName = recConv.getSqlType(wrapper.getViewable());
                // if table exists?
                if (DbUtility.tableExists(conn, sqlName)) {
                    // dump it
                    EhiLogger.logState(aclass.getScopedName(null) + "...");
                    if (iomBasket == null) {
                        iomBasket = new StartBasketEvent(topic.getScopedName(null), basketXtfId);
                        if (languageFilter != null) {
                            iomBasket = (StartBasketEvent) languageFilter.filter(iomBasket);
                        }
                        if (exportBaseModelFilter != null) {
                            iomBasket = (StartBasketEvent) exportBaseModelFilter.filter(iomBasket);
                        }
                        if (validator != null)
                            validator.validate(iomBasket);
                        iomFile.write(iomBasket);
                    }
                    dumpObject(iomFile, aclass, basketSqlId);
                } else {
                    // skip it
                    EhiLogger.traceUnusualState(aclass.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                }
            }
        } else if (obj instanceof AttributeDef) {
            if (iomFile instanceof ItfWriter) {
                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() + "...");
                    if (iomBasket == null) {
                        iomBasket = new StartBasketEvent(topic.getScopedName(null), topic.getScopedName(null));
                        if (languageFilter != null) {
                            iomBasket = (StartBasketEvent) languageFilter.filter(iomBasket);
                        }
                        if (exportBaseModelFilter != null) {
                            iomBasket = (StartBasketEvent) exportBaseModelFilter.filter(iomBasket);
                        }
                        if (validator != null)
                            validator.validate(iomBasket);
                        iomFile.write(iomBasket);
                    }
                    dumpItfTableObject(iomFile, attr, basketSqlId);
                } else {
                    // skip it
                    EhiLogger.traceUnusualState(attr.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                }
            }
        }
    }
    if (iomBasket != null) {
        // fix forward references
        for (FixIomObjectRefs fixref : delayedObjects) {
            boolean skipObj = false;
            for (IomObject ref : fixref.getRefs()) {
                long sqlid = fixref.getTargetSqlid(ref);
                if (sqlidPool.containsSqlid(sqlid)) {
                    // fix it
                    ref.setobjectrefoid(sqlidPool.getXtfid(sqlid));
                } else {
                    // object in another basket
                    Viewable aclass = fixref.getTargetClass(ref);
                    // read object
                    String tid = readObjectTid(aclass, sqlid);
                    if (tid == null) {
                        EhiLogger.logError("unknown referenced object " + aclass.getScopedName(null) + " sqlid " + fixref.getTargetSqlid(ref) + " referenced from " + fixref.getRoot().getobjecttag() + " TID " + fixref.getRoot().getobjectoid());
                        referrs = true;
                        skipObj = true;
                    } else {
                        // fix reference
                        ref.setobjectrefoid(tid);
                    }
                }
            }
            if (!skipObj) {
                ObjectEvent objEvent = new ObjectEvent(fixref.getRoot());
                if (languageFilter != null) {
                    objEvent = (ObjectEvent) languageFilter.filter(objEvent);
                }
                if (exportBaseModelFilter != null) {
                    objEvent = (ObjectEvent) exportBaseModelFilter.filter(objEvent);
                }
                if (objEvent != null) {
                    if (validator != null)
                        validator.validate(objEvent);
                    iomFile.write(objEvent);
                }
            }
        }
        EndBasketEvent endBasket = new EndBasketEvent();
        if (languageFilter != null) {
            endBasket = (EndBasketEvent) languageFilter.filter(endBasket);
        }
        if (exportBaseModelFilter != null) {
            endBasket = (EndBasketEvent) exportBaseModelFilter.filter(endBasket);
        }
        if (validator != null)
            validator.validate(endBasket);
        iomFile.write(endBasket);
        saveObjStat(iomBasket.getBid(), basketSqlId, filename, iomBasket.getType());
    }
    return referrs;
}
Also used : ObjectEvent(ch.interlis.iox_j.ObjectEvent) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) View(ch.interlis.ili2c.metamodel.View) EndBasketEvent(ch.interlis.iox_j.EndBasketEvent) IomObject(ch.interlis.iom.IomObject) ReduceToBaseModel(ch.interlis.iox_j.filter.ReduceToBaseModel) PredefinedModel(ch.interlis.ili2c.metamodel.PredefinedModel) TypeModel(ch.interlis.ili2c.metamodel.TypeModel) Model(ch.interlis.ili2c.metamodel.Model) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) StartBasketEvent(ch.interlis.iox_j.StartBasketEvent) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) ItfWriter(ch.interlis.iom_j.itf.ItfWriter) DbTableName(ch.ehi.sqlgen.repository.DbTableName)

Example 18 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class ToXtfRecordConverter method convertRecord.

public Iom_jObject convertRecord(java.sql.ResultSet rs, Viewable aclass1, FixIomObjectRefs fixref, StructWrapper structWrapper, HashMap structelev, ArrayList<StructWrapper> structQueue, long sqlid) throws SQLException {
    ViewableWrapper aclass = class2wrapper.get(aclass1);
    Iom_jObject iomObj;
    int valuei = 1;
    valuei++;
    if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
        // String t_type=rs.getString(valuei);
        valuei++;
    }
    String sqlIliTid = null;
    if (structWrapper == null) {
        if (!aclass.isStructure()) {
            if (createIliTidCol || aclass.getOid() != null) {
                sqlIliTid = rs.getString(valuei);
                sqlid2xtfid.putSqlid2Xtfid(sqlid, sqlIliTid);
                valuei++;
            } else {
                sqlIliTid = Long.toString(sqlid);
                sqlid2xtfid.putSqlid2Xtfid(sqlid, sqlIliTid);
            }
        }
    }
    if (structWrapper == null) {
        if (!aclass.isStructure()) {
            iomObj = new Iom_jObject(aclass1.getScopedName(null), sqlIliTid);
        } else {
            iomObj = new Iom_jObject(aclass1.getScopedName(null), null);
        }
        iomObj.setattrvalue(ItfWriter2.INTERNAL_T_ID, Long.toString(sqlid));
        fixref.setRoot(iomObj);
    } else {
        iomObj = (Iom_jObject) structelev.get(Long.toString(sqlid));
        if (createGenericStructRef) {
            valuei += 4;
        } else {
            valuei += 2;
        }
    }
    HashSet<AttributeDef> visitedAttrs = new HashSet<AttributeDef>();
    for (ViewableWrapper table : aclass.getWrappers()) {
        Iterator iter = table.getAttrIterator();
        while (iter.hasNext()) {
            ViewableTransferElement obj = (ViewableTransferElement) iter.next();
            if (obj.obj instanceof AttributeDef) {
                AttributeDef attr = (AttributeDef) obj.obj;
                AttributeDef baseAttr = attr;
                while (true) {
                    AttributeDef baseAttr1 = (AttributeDef) baseAttr.getExtending();
                    if (baseAttr1 == null) {
                        break;
                    }
                    baseAttr = baseAttr1;
                }
                if (!visitedAttrs.contains(baseAttr)) {
                    visitedAttrs.add(baseAttr);
                    if (!baseAttr.isTransient()) {
                        Type proxyType = baseAttr.getDomain();
                        if (proxyType != null && (proxyType instanceof ObjectType)) {
                        // skip implicit particles (base-viewables) of views
                        } else {
                            valuei = addAttrValue(rs, valuei, sqlid, iomObj, baseAttr, structQueue, table, fixref);
                        }
                    }
                }
            }
            if (obj.obj instanceof RoleDef) {
                RoleDef role = (RoleDef) obj.obj;
                if (role.getExtending() == null) {
                    String roleName = role.getName();
                    ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
                    boolean refAlreadyDefined = false;
                    for (ViewableWrapper targetTable : targetTables) {
                        String sqlRoleName = ili2sqlName.mapIliRoleDef(role, getSqlType(table.getViewable()).getName(), targetTable.getSqlTablename(), targetTables.size() > 1);
                        // a role of an embedded association?
                        if (obj.embedded) {
                            AssociationDef roleOwner = (AssociationDef) role.getContainer();
                            if (roleOwner.getDerivedFrom() == null) {
                                // TODO if(orderPos!=0){
                                long value = rs.getLong(valuei);
                                valuei++;
                                if (!rs.wasNull()) {
                                    if (refAlreadyDefined) {
                                        EhiLogger.logAdaption("Table " + table.getSqlTablename() + "(id " + sqlid + ") more than one value for role " + roleName + "; value of " + sqlRoleName + " ignored");
                                    } else {
                                        IomObject ref = iomObj.addattrobj(roleName, roleOwner.getScopedName(null));
                                        mapSqlid2Xtfid(fixref, value, ref, role.getDestination());
                                        refAlreadyDefined = true;
                                    }
                                }
                            }
                        } else {
                            // TODO if(orderPos!=0){
                            long value = rs.getLong(valuei);
                            valuei++;
                            if (!rs.wasNull()) {
                                if (refAlreadyDefined) {
                                    EhiLogger.logAdaption("Table " + table.getSqlTablename() + "(id " + sqlid + ") more than one value for role " + roleName + "; value of " + sqlRoleName + " ignored");
                                } else {
                                    IomObject ref = iomObj.addattrobj(roleName, "REF");
                                    mapSqlid2Xtfid(fixref, value, ref, role.getDestination());
                                    refAlreadyDefined = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return iomObj;
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) Iom_jObject(ch.interlis.iom_j.Iom_jObject) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) AreaType(ch.interlis.ili2c.metamodel.AreaType) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) IomObject(ch.interlis.iom.IomObject) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) HashSet(java.util.HashSet)

Example 19 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class TransferFromIli method doit.

public DbSchema doit(TransferDescription td1, java.util.List<Element> modelEles, ch.ehi.ili2db.mapping.NameMapping ili2sqlName, ch.ehi.ili2db.gui.Config config, DbIdGen idGen, TrafoConfig trafoConfig, Viewable2TableMapping class2wrapper1, CustomMapping customMapping1) throws Ili2dbException {
    this.ili2sqlName = ili2sqlName;
    createEnumTable = config.getCreateEnumDefs();
    createStdCols = config.CREATE_STD_COLS_ALL.equals(config.getCreateStdCols());
    createFk = config.CREATE_FK_YES.equals(config.getCreateFk());
    createFkIdx = config.CREATE_FKIDX_YES.equals(config.getCreateFkIdx());
    colT_ID = config.getColT_ID();
    if (colT_ID == null) {
        colT_ID = DbNames.T_ID_COL;
    }
    deleteExistingData = config.DELETE_DATA.equals(config.getDeleteMode());
    if (deleteExistingData) {
        EhiLogger.logState("delete existing data...");
    }
    createIliTidCol = config.TID_HANDLING_PROPERTY.equals(config.getTidHandling());
    createBasketCol = config.BASKET_HANDLING_READWRITE.equals(config.getBasketHandling());
    createDatasetCol = config.CREATE_DATASET_COL.equals(config.getCreateDatasetCols());
    isIli1Model = td1.getIli1Format() != null;
    createItfLineTables = isIli1Model && config.getDoItfLineTables();
    customMapping = customMapping1;
    customMapping.fromIliInit(config);
    schema = new DbSchema();
    schema.setName(config.getDbschema());
    visitedElements = new HashSet<Element>();
    class2wrapper = class2wrapper1;
    visitedEnums = new HashSet();
    td = td1;
    recConv = new FromIliRecordConverter(td, ili2sqlName, config, schema, customMapping, idGen, visitedEnums, trafoConfig, class2wrapper, metaInfo);
    visitedWrapper = new HashSet<ViewableWrapper>();
    generatModelEles(modelEles, 1);
    visitedWrapper = new HashSet<ViewableWrapper>();
    generatModelEles(modelEles, 2);
    // sys_interlisnames
    // interlis LONGVARCHAR(767)
    // db VARCHAR(30)
    customMapping.fromIliEnd(config);
    return schema;
}
Also used : DbSchema(ch.ehi.sqlgen.repository.DbSchema) Element(ch.interlis.ili2c.metamodel.Element) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) HashSet(java.util.HashSet)

Example 20 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class TransferFromIli method generateViewable.

private void generateViewable(ViewableWrapper def, int pass) throws Ili2dbException {
    if (def.getViewable() instanceof AssociationDef) {
        AssociationDef assoc = (AssociationDef) def.getViewable();
        if (assoc.getDerivedFrom() != null) {
            return;
        }
        if (assoc.isLightweight() && !assoc.getAttributes().hasNext() && !assoc.getLightweightAssociations().iterator().hasNext()) {
            if (pass == 1) {
                customMapping.fixupViewable(null, def.getViewable());
            }
            return;
        }
    }
    EhiLogger.traceState("wrapper of viewable " + def.getViewable());
    if (!visitedWrapper.contains(def)) {
        visitedWrapper.add(def);
        recConv.generateTable(def, pass);
        for (ViewableWrapper secondary : def.getSecondaryTables()) {
            recConv.generateTable(secondary, pass);
        }
        if (false && createItfLineTables) {
            for (AttributeDef attr : recConv.getSurfaceAttrs()) {
                generateItfLineTable(attr, pass);
            }
        }
    }
}
Also used : AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper)

Aggregations

ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)25 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)12 Viewable (ch.interlis.ili2c.metamodel.Viewable)11 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)9 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)9 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)9 ArrayList (java.util.ArrayList)9 AreaType (ch.interlis.ili2c.metamodel.AreaType)8 BlackboxType (ch.interlis.ili2c.metamodel.BlackboxType)8 CoordType (ch.interlis.ili2c.metamodel.CoordType)8 ObjectType (ch.interlis.ili2c.metamodel.ObjectType)8 PolylineType (ch.interlis.ili2c.metamodel.PolylineType)8 ReferenceType (ch.interlis.ili2c.metamodel.ReferenceType)8 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)8 Type (ch.interlis.ili2c.metamodel.Type)8 HashSet (java.util.HashSet)8 Iterator (java.util.Iterator)8 DbTableName (ch.ehi.sqlgen.repository.DbTableName)7 IomObject (ch.interlis.iom.IomObject)7 AssociationDef (ch.interlis.ili2c.metamodel.AssociationDef)5