Search in sources :

Example 1 with ItfWriter

use of ch.interlis.iom_j.itf.ItfWriter in project ili2db by claeis.

the class TransferToXtf method doit.

public void doit(String filename, IoxWriter iomFile, String sender, String[] exportParamModelnames, long[] basketSqlIds, Map<Long, BasketStat> stat) throws ch.interlis.iox.IoxException {
    this.basketStat = stat;
    boolean referrs = false;
    if (iomFile instanceof ItfWriter) {
        config.setValue(ch.interlis.iox_j.validator.Validator.CONFIG_DO_ITF_LINETABLES, ch.interlis.iox_j.validator.Validator.CONFIG_DO_ITF_LINETABLES_DO);
    }
    if (config.getVer4_translation() || config.getIli1Translation() != null) {
        languageFilter = new TranslateToTranslation(td, config);
    }
    if (config.getExportModels() != null) {
        List<Model> exportModels = Ili2db.getModels(config.getExportModels(), td);
        exportBaseModelFilter = new ReduceToBaseModel(exportModels, td, config);
    }
    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(filename);
        if (iomFile instanceof Iligml10Writer || iomFile instanceof Iligml20Writer) {
            String crsAuthority = config.getDefaultSrsAuthority();
            String crsCode = config.getDefaultSrsCode();
            if (crsAuthority != null && crsCode != null) {
                if (iomFile instanceof Iligml10Writer) {
                    ((Iligml10Writer) iomFile).setDefaultCrs(crsAuthority + ":" + crsCode);
                } else if (iomFile instanceof Iligml20Writer) {
                    ((Iligml20Writer) iomFile).setDefaultCrs(crsAuthority + ":" + crsCode);
                }
            }
        }
        PipelinePool pipelinePool = new PipelinePool();
        validator = new ch.interlis.iox_j.validator.Validator(td, modelConfig, errHandler, errFactory, pipelinePool, config);
    }
    StartTransferEvent startEvent = new StartTransferEvent();
    startEvent.setSender(sender);
    if (languageFilter != null) {
        startEvent = (StartTransferEvent) languageFilter.filter(startEvent);
    }
    if (exportBaseModelFilter != null) {
        startEvent = (StartTransferEvent) exportBaseModelFilter.filter(startEvent);
    }
    if (validator != null)
        validator.validate(startEvent);
    iomFile.write(startEvent);
    if (basketSqlIds != null) {
        for (long basketSqlId : basketSqlIds) {
            StringBuilder basketXtfId = new StringBuilder();
            Topic topic = getTopicByBasketId(basketSqlId, basketXtfId);
            if (topic == null) {
                throw new IoxException("no basketId " + basketSqlId + " in db");
            } else {
                referrs = referrs || doBasket(filename, iomFile, topic, basketSqlId, basketXtfId.toString());
            }
        }
    } else {
        // for all MODELs
        for (String modelName : exportParamModelnames) {
            Object mObj = td.getElement(Model.class, modelName);
            if (mObj != null && (mObj instanceof Model) && !(suppressModel((Model) mObj))) {
                Model model = (Model) mObj;
                // for all TOPICs
                Iterator topici = model.iterator();
                while (topici.hasNext()) {
                    Object tObj = topici.next();
                    if (tObj instanceof Topic && !(suppressTopic((Topic) tObj))) {
                        Topic topic = (Topic) tObj;
                        referrs = referrs || doBasket(filename, iomFile, topic, null, topic.getScopedName(null));
                    }
                }
            }
        }
    }
    if (referrs) {
        throw new IoxException("dangling references");
    }
    EndTransferEvent endEvent = new EndTransferEvent();
    if (languageFilter != null) {
        endEvent = (EndTransferEvent) languageFilter.filter(endEvent);
    }
    if (exportBaseModelFilter != null) {
        endEvent = (EndTransferEvent) exportBaseModelFilter.filter(endEvent);
    }
    if (validator != null)
        validator.validate(endEvent);
    iomFile.write(endEvent);
    if (validator != null)
        validator.close();
    if (languageFilter != null) {
        languageFilter.close();
    }
    if (exportBaseModelFilter != null) {
        exportBaseModelFilter.close();
    }
}
Also used : IoxLogging(ch.interlis.iox.IoxLogging) FileNotFoundException(java.io.FileNotFoundException) LogEventFactory(ch.interlis.iox_j.logging.LogEventFactory) TranslateToTranslation(ch.interlis.iox_j.filter.TranslateToTranslation) Iligml20Writer(ch.interlis.iom_j.iligml.Iligml20Writer) Iligml10Writer(ch.interlis.iom_j.iligml.Iligml10Writer) EndTransferEvent(ch.interlis.iox_j.EndTransferEvent) Iterator(java.util.Iterator) Topic(ch.interlis.ili2c.metamodel.Topic) StartTransferEvent(ch.interlis.iox_j.StartTransferEvent) ReduceToBaseModel(ch.interlis.iox_j.filter.ReduceToBaseModel) ValidationConfig(ch.interlis.iox_j.validator.ValidationConfig) PipelinePool(ch.interlis.iox_j.PipelinePool) 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) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IomObject(ch.interlis.iom.IomObject) ItfWriter(ch.interlis.iom_j.itf.ItfWriter) File(java.io.File) IoxException(ch.interlis.iox.IoxException)

Example 2 with ItfWriter

use of ch.interlis.iom_j.itf.ItfWriter in project ili2db by claeis.

the class Ili2db method transferToXtf.

/**
 * transfer data from database to xml file
 */
private static void transferToXtf(Connection conn, String xtffile, NameMapping ili2sqlName, TransferDescription td, SqlColumnConverter geomConv, String sender, Config config, String[] exportParamModelnames, long[] basketSqlIds, Map<Long, BasketStat> stat, TrafoConfig trafoConfig, Viewable2TableMapping class2wrapper) {
    java.io.File outfile = new java.io.File(xtffile);
    IoxWriter ioxWriter = null;
    try {
        if (Config.ILIGML20.equals(config.getTransferFileFormat())) {
            ioxWriter = new Iligml20Writer(outfile, td);
        } else {
            String ext = ch.ehi.basics.view.GenericFileFilter.getFileExtension(xtffile).toLowerCase();
            if (config.isItfTransferfile()) {
                if (!config.getDoItfLineTables()) {
                    ioxWriter = new ItfWriter2(outfile, td);
                } else {
                    ioxWriter = new ItfWriter(outfile, td);
                }
                config.setValue(ch.interlis.iox_j.validator.Validator.CONFIG_DO_ITF_OIDPERTABLE, ch.interlis.iox_j.validator.Validator.CONFIG_DO_ITF_OIDPERTABLE_DO);
            } else if (ext != null && ext.equals("gml")) {
                ioxWriter = new Iligml10Writer(outfile, td);
            } else {
                ioxWriter = new XtfWriter(outfile, td);
            }
        }
        TransferToXtf trsfr = new TransferToXtf(ili2sqlName, td, conn, geomConv, config, trafoConfig, class2wrapper);
        trsfr.doit(outfile.getName(), ioxWriter, sender, exportParamModelnames, basketSqlIds, stat);
        // trsfr.doitJava();
        ioxWriter.flush();
    } catch (ch.interlis.iox.IoxException ex) {
        EhiLogger.logError("failed to write xml output", ex);
    } finally {
        if (ioxWriter != null) {
            try {
                ioxWriter.close();
            } catch (ch.interlis.iox.IoxException ex) {
                EhiLogger.logError("failed to close xml output", ex);
            }
        }
        ioxWriter = null;
    }
}
Also used : XtfWriter(ch.interlis.iom_j.xtf.XtfWriter) ItfWriter2(ch.interlis.iom_j.itf.ItfWriter2) Iligml20Writer(ch.interlis.iom_j.iligml.Iligml20Writer) Iligml10Writer(ch.interlis.iom_j.iligml.Iligml10Writer) IoxException(ch.interlis.iox.IoxException) TransferToXtf(ch.ehi.ili2db.toxtf.TransferToXtf) IoxWriter(ch.interlis.iox.IoxWriter) ItfWriter(ch.interlis.iom_j.itf.ItfWriter)

Example 3 with ItfWriter

use of ch.interlis.iom_j.itf.ItfWriter in project ili2db by claeis.

the class TransferToXtf method dumpObjHelper.

/**
 * helper to dump all objects/structvalues of a given class/structure.
 */
private void dumpObjHelper(IoxWriter out, Viewable aclass, Long basketSqlId, FixIomObjectRefs fixref, StructWrapper structWrapper, HashMap<String, IomObject> structelev) {
    String stmt = recConv.createQueryStmt(aclass, basketSqlId, structWrapper);
    EhiLogger.traceBackendCmd(stmt);
    java.sql.PreparedStatement dbstmt = null;
    try {
        dbstmt = conn.prepareStatement(stmt);
        recConv.setStmtParams(dbstmt, basketSqlId, fixref, structWrapper);
        java.sql.ResultSet rs = dbstmt.executeQuery();
        while (rs.next()) {
            // list of not yet processed struct attrs
            ArrayList<StructWrapper> structQueue = new ArrayList<StructWrapper>();
            long sqlid = recConv.getT_ID(rs);
            Iom_jObject iomObj = null;
            if (structWrapper == null) {
                fixref = new FixIomObjectRefs();
            }
            iomObj = recConv.convertRecord(rs, aclass, fixref, structWrapper, structelev, structQueue, sqlid);
            updateObjStat(iomObj.getobjecttag(), sqlid);
            // collect structvalues
            while (!structQueue.isEmpty()) {
                StructWrapper wrapper = (StructWrapper) structQueue.remove(0);
                dumpStructs(wrapper, fixref);
            }
            if (structWrapper == null) {
                if (!fixref.needsFixing() || out instanceof ItfWriter) {
                    // no forward references
                    // write object
                    ObjectEvent objEvent = new ObjectEvent(iomObj);
                    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);
                        if (out != null) {
                            out.write(objEvent);
                        }
                    }
                } else {
                    delayedObjects.add(fixref);
                }
            }
        }
    // while rs
    } catch (java.sql.SQLException ex) {
        EhiLogger.logError("failed to query " + aclass.getScopedName(null), ex);
    } catch (ch.interlis.iox.IoxException ex) {
        EhiLogger.logError("failed to write " + aclass.getScopedName(null), ex);
    } finally {
        if (dbstmt != null) {
            try {
                dbstmt.close();
            } catch (java.sql.SQLException ex) {
                EhiLogger.logError("failed to close query of " + aclass.getScopedName(null), ex);
            }
        }
    }
}
Also used : ObjectEvent(ch.interlis.iox_j.ObjectEvent) ArrayList(java.util.ArrayList) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IoxException(ch.interlis.iox.IoxException) ItfWriter(ch.interlis.iom_j.itf.ItfWriter)

Example 4 with ItfWriter

use of ch.interlis.iom_j.itf.ItfWriter 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)

Aggregations

ItfWriter (ch.interlis.iom_j.itf.ItfWriter)4 Iom_jObject (ch.interlis.iom_j.Iom_jObject)3 IoxException (ch.interlis.iox.IoxException)3 Model (ch.interlis.ili2c.metamodel.Model)2 PredefinedModel (ch.interlis.ili2c.metamodel.PredefinedModel)2 TypeModel (ch.interlis.ili2c.metamodel.TypeModel)2 IomObject (ch.interlis.iom.IomObject)2 Iligml10Writer (ch.interlis.iom_j.iligml.Iligml10Writer)2 Iligml20Writer (ch.interlis.iom_j.iligml.Iligml20Writer)2 ObjectEvent (ch.interlis.iox_j.ObjectEvent)2 ReduceToBaseModel (ch.interlis.iox_j.filter.ReduceToBaseModel)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)1 TransferToXtf (ch.ehi.ili2db.toxtf.TransferToXtf)1 DbTableName (ch.ehi.sqlgen.repository.DbTableName)1 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)1 Topic (ch.interlis.ili2c.metamodel.Topic)1 View (ch.interlis.ili2c.metamodel.View)1 Viewable (ch.interlis.ili2c.metamodel.Viewable)1