Search in sources :

Example 1 with Model

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

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

the class TransferToXtf method getTopicDef.

public static Topic getTopicDef(TransferDescription td, String topicQName) {
    String modelName = null;
    String topicName = null;
    int endModelName = topicQName.indexOf('.');
    if (endModelName <= 0) {
        // just a topicname
        topicName = topicQName;
    } else {
        // qualified topicname; get model name
        modelName = topicQName.substring(0, endModelName);
        topicName = topicQName.substring(endModelName + 1);
    }
    Iterator modeli = td.iterator();
    while (modeli.hasNext()) {
        Object mObj = modeli.next();
        if (mObj instanceof Model) {
            Model model = (Model) mObj;
            if (modelName == null || modelName.equals(model.getName())) {
                Iterator topici = model.iterator();
                while (topici.hasNext()) {
                    Object tObj = topici.next();
                    if (tObj instanceof Topic) {
                        Topic topic = (Topic) tObj;
                        if (topicName.equals(topic.getName())) {
                            return topic;
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : Iterator(java.util.Iterator) 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) Topic(ch.interlis.ili2c.metamodel.Topic)

Example 3 with Model

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

the class TransferToXtf method doitJava.

public void doitJava() {
    try {
        expgen = new ch.interlis.ili2c.generator.IndentPrintWriter(new java.io.BufferedWriter(new java.io.FileWriter("c:/tmp/ili2db/export.java")));
        expgen.println("import ch.ehi.basics.logging.EhiLogger;");
        expgen.println("import ch.interlis.iom.IomObject;");
        expgen.println("public class TransferToXtf implements IdMapper {");
        expgen.indent();
        expgen.println("private java.sql.Connection conn=null;");
        expgen.println("private IomObject newObject(String className,String tid)");
        expgen.println("{");
        expgen.indent();
        expgen.println("// TODO create new object");
        expgen.println("return null;");
        expgen.unindent();
        expgen.println("}");
        expgen.println("private void writeObject(IomObject iomObj)");
        expgen.println("{");
        expgen.indent();
        expgen.println("// TODO write object");
        expgen.unindent();
        expgen.println("}");
        expgen.println("public String mapId(String idSpace,String id)");
        expgen.println("{");
        expgen.indent();
        expgen.println("// TODO mapId");
        expgen.println("return id;");
        expgen.unindent();
        expgen.println("}");
        expgen.println("public String newId()");
        expgen.println("{");
        expgen.indent();
        expgen.println("// TODO newId");
        expgen.println("throw new UnsupportedOperationException(\"this mapper doesn't generate new ids\");");
        expgen.unindent();
        expgen.println("}");
    } catch (java.io.IOException ex) {
        EhiLogger.logError("failed to open file for java output", ex);
    }
    try {
        // for all MODELs
        Iterator modeli = td.iterator();
        while (modeli.hasNext()) {
            Object mObj = modeli.next();
            if ((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;
                        // IomBasket iomBasket=null;
                        // for all Viewables
                        Iterator iter = topic.getViewables().iterator();
                        while (iter.hasNext()) {
                            Object obj = iter.next();
                            if ((obj instanceof Viewable) && !suppressViewableIfJava((Viewable) obj)) {
                                Viewable aclass = (Viewable) obj;
                                // dump it
                                EhiLogger.logState(aclass.getScopedName(null) + "...");
                                genClassHelper(aclass);
                            } else if (obj instanceof Domain) {
                                Domain domainDef = (Domain) obj;
                                if (domainDef.getType() instanceof EnumerationType) {
                                    String enumName = domainDef.getScopedName(null);
                                    enumTypes.add(enumName);
                                }
                            }
                        }
                    } else if ((tObj instanceof Viewable) && !suppressViewableIfJava((Viewable) tObj)) {
                        Viewable aclass = (Viewable) tObj;
                        // dump it
                        EhiLogger.logState(aclass.getScopedName(null) + "...");
                        genClassHelper(aclass);
                    } else if (tObj instanceof Domain) {
                        Domain domainDef = (Domain) tObj;
                        if (domainDef.getType() instanceof EnumerationType) {
                            String enumName = domainDef.getScopedName(null);
                            enumTypes.add(enumName);
                        }
                    }
                }
            }
        }
    } finally {
        if (expgen != null) {
            {
                expgen.println("private void addAny(String iliClassName,String select){");
                expgen.indent();
                Iterator linei = addanyLines.iterator();
                String sep = "";
                while (linei.hasNext()) {
                    String line = (String) linei.next();
                    expgen.println(sep + line);
                    sep = "else ";
                }
                if (sep.length() > 0) {
                    expgen.println("else{EhiLogger.logError(\"unknown class \"+iliClassName);}");
                } else {
                    expgen.println("EhiLogger.logError(\"unknown class \"+iliClassName);");
                }
                expgen.unindent();
                expgen.println("}");
            }
            {
                expgen.println("private EnumMapper getEnumMapper(String enumName){");
                expgen.indent();
                Iterator enumi = enumTypes.iterator();
                String sep = "";
                while (enumi.hasNext()) {
                    String enumName = (String) enumi.next();
                    expgen.println(sep + "if(enumName.equals(\"" + enumName + "\")){return new IdentityEnumMapper();}");
                    sep = "else ";
                }
                if (sep.length() > 0) {
                    expgen.println("else{throw new IllegalArgumentException(\"unknown enum \"+enumName);}");
                } else {
                    expgen.println("throw new IllegalArgumentException(\"unknown enum \"+enumName);");
                }
                expgen.unindent();
                expgen.println("}");
            }
            expgen.unindent();
            expgen.println("}");
            expgen.close();
        }
    }
}
Also used : EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) Iterator(java.util.Iterator) 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) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iom_jObject(ch.interlis.iom_j.Iom_jObject) IomObject(ch.interlis.iom.IomObject) Topic(ch.interlis.ili2c.metamodel.Topic) Domain(ch.interlis.ili2c.metamodel.Domain)

Example 4 with Model

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

the class TransferFromIli method addModels.

public static void addModels(java.sql.Connection conn, TransferDescription td, String schema) throws Ili2dbException {
    // read existing models from db
    IliFiles iliModelsInDb = TransferFromIli.readIliFiles(conn, schema);
    String sqlName = DbNames.MODELS_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    java.sql.Timestamp today = new java.sql.Timestamp(System.currentTimeMillis());
    try {
        // insert entries
        String insStmt = "INSERT INTO " + sqlName + " (" + DbNames.MODELS_TAB_FILE_COL + "," + DbNames.MODELS_TAB_ILIVERSION_COL + "," + DbNames.MODELS_TAB_MODELNAME_COL + "," + DbNames.MODELS_TAB_CONTENT_COL + "," + DbNames.MODELS_TAB_IMPORTDATE_COL + ") VALUES (?,?,?,?,?)";
        if (isMsSqlServer(conn)) {
            // 'file' is keyword in sql server
            insStmt = "INSERT INTO " + sqlName + " (\"" + DbNames.MODELS_TAB_FILE_COL + "\"," + DbNames.MODELS_TAB_ILIVERSION_COL + "," + DbNames.MODELS_TAB_MODELNAME_COL + "," + DbNames.MODELS_TAB_CONTENT_COL + "," + DbNames.MODELS_TAB_IMPORTDATE_COL + ") VALUES (?,?,?,?,?)";
        }
        EhiLogger.traceBackendCmd(insStmt);
        java.sql.PreparedStatement insPrepStmt = conn.prepareStatement(insStmt);
        java.util.Iterator entri = td.iterator();
        HashMap<java.io.File, ch.interlis.ili2c.modelscan.IliFile> ilifiles = new HashMap<java.io.File, ch.interlis.ili2c.modelscan.IliFile>();
        while (entri.hasNext()) {
            Object entro = entri.next();
            if (entro instanceof ch.interlis.ili2c.metamodel.Model) {
                if (entro instanceof ch.interlis.ili2c.metamodel.PredefinedModel) {
                    continue;
                }
                ch.interlis.ili2c.metamodel.Model model = (ch.interlis.ili2c.metamodel.Model) entro;
                java.io.File file = new java.io.File(model.getFileName());
                ch.interlis.ili2c.modelscan.IliFile ilifile = null;
                if (ilifiles.containsKey(file)) {
                    ilifile = ilifiles.get(file);
                } else {
                    ilifile = new ch.interlis.ili2c.modelscan.IliFile();
                    ilifile.setFilename(file);
                    ilifiles.put(file, ilifile);
                }
                ch.interlis.ili2c.modelscan.IliModel ilimodel = new ch.interlis.ili2c.modelscan.IliModel();
                ilimodel.setIliVersion(Double.parseDouble(model.getIliVersion()));
                ilimodel.setName(model.getName());
                Model[] imports = model.getImporting();
                for (Model importm : imports) {
                    ilimodel.addDepenedency(importm.getName());
                }
                Model translatedModel = (Model) model.getTranslationOf();
                if (translatedModel != null) {
                    ilimodel.addDepenedency(translatedModel.getName());
                }
                ilifile.addModel(ilimodel);
            }
        }
        try {
            for (ch.interlis.ili2c.modelscan.IliFile ilifile : ilifiles.values()) {
                ch.interlis.ili2c.modelscan.IliModel ilimodel = (ch.interlis.ili2c.modelscan.IliModel) ilifile.iteratorModel().next();
                if (iliModelsInDb == null || iliModelsInDb.getFileWithModel(ilimodel.getName(), ilimodel.getIliVersion()) == null) {
                    insPrepStmt.clearParameters();
                    insPrepStmt.setString(1, ilifile.getFilename().getName());
                    insPrepStmt.setString(2, Double.toString(ilifile.getIliVersion()));
                    insPrepStmt.setString(3, IliImportsUtility.getIliImports(ilifile));
                    insPrepStmt.setString(4, readFileAsString(ilifile.getFilename()));
                    insPrepStmt.setTimestamp(5, today);
                    insPrepStmt.executeUpdate();
                }
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to insert model", ex);
        } catch (IOException e) {
            throw new Ili2dbException("failed to update models-table " + sqlName, e);
        } finally {
            insPrepStmt.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to update models-table " + sqlName, ex);
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) IliFiles(ch.interlis.ilirepository.IliFiles) Iterator(java.util.Iterator) IOException(java.io.IOException) Model(ch.interlis.ili2c.metamodel.Model) Model(ch.interlis.ili2c.metamodel.Model)

Example 5 with Model

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

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