Search in sources :

Example 1 with IliFiles

use of ch.interlis.ilirepository.IliFiles in project ili2db by claeis.

the class TransferFromIli method readIliFiles.

public static ch.interlis.ilirepository.IliFiles readIliFiles(java.sql.Connection conn, String schema) throws Ili2dbException {
    String sqlName = DbNames.MODELS_TAB;
    if (!DbUtility.tableExists(conn, new DbTableName(schema, sqlName))) {
        return null;
    }
    ch.interlis.ilirepository.IliFiles ret = new ch.interlis.ilirepository.IliFiles();
    try {
        String reposUri = conn.getMetaData().getURL();
        if (schema != null) {
            sqlName = schema + "." + sqlName;
            reposUri = reposUri + "/" + schema;
        }
        // select entries
        String insStmt = "SELECT " + DbNames.MODELS_TAB_FILE_COL + "," + DbNames.MODELS_TAB_ILIVERSION_COL + "," + DbNames.MODELS_TAB_MODELNAME_COL + " FROM " + sqlName;
        if (isMsSqlServer(conn)) {
            // 'file' is keyword in sql server
            insStmt = "SELECT \"" + DbNames.MODELS_TAB_FILE_COL + "\"," + DbNames.MODELS_TAB_ILIVERSION_COL + "," + DbNames.MODELS_TAB_MODELNAME_COL + " FROM " + sqlName;
        }
        EhiLogger.traceBackendCmd(insStmt);
        java.sql.PreparedStatement insPrepStmt = conn.prepareStatement(insStmt);
        try {
            java.sql.ResultSet rs = insPrepStmt.executeQuery();
            while (rs.next()) {
                String file = rs.getString(1);
                double iliversion = Double.parseDouble(rs.getString(2));
                String imports = rs.getString(3);
                ch.interlis.ili2c.modelscan.IliFile iliFile = IliImportsUtility.parseIliImports(iliversion, imports);
                iliFile.setPath(file);
                iliFile.setRepositoryUri(reposUri);
                ret.addFile(iliFile);
            }
        } catch (java.sql.SQLException ex) {
            throw new Ili2dbException("failed to read IliFiles from db", ex);
        } finally {
            insPrepStmt.close();
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to read models-table " + sqlName, ex);
    }
    return ret;
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) IliFiles(ch.interlis.ilirepository.IliFiles) SQLException(java.sql.SQLException) DbTableName(ch.ehi.sqlgen.repository.DbTableName) IliFiles(ch.interlis.ilirepository.IliFiles)

Example 2 with IliFiles

use of ch.interlis.ilirepository.IliFiles 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 3 with IliFiles

use of ch.interlis.ilirepository.IliFiles in project ili2db by claeis.

the class Ili2db method setupIli2cPathmap.

private static void setupIli2cPathmap(Config config, String appHome, String xtffile, java.sql.Connection conn) throws Ili2dbException {
    config.setValue(ch.interlis.ili2c.gui.UserSettings.ILIDIRS, config.getModeldir());
    java.util.HashMap pathMap = new java.util.HashMap();
    if (xtffile != null) {
        pathMap.put(Ili2db.XTF_DIR, new java.io.File(xtffile).getAbsoluteFile().getParent());
    } else {
        pathMap.put(Ili2db.XTF_DIR, null);
    }
    pathMap.put(Ili2db.JAR_DIR, appHome);
    config.setTransientObject(ch.interlis.ili2c.gui.UserSettings.ILIDIRS_PATHMAP, pathMap);
    // if ilimodels exists in db
    if (conn != null) {
        IliFiles iliFiles = null;
        String url = null;
        try {
            url = conn.getMetaData().getURL();
            iliFiles = TransferFromIli.readIliFiles(conn, config.getDbschema());
        } catch (SQLException e) {
            throw new Ili2dbException(e);
        }
        if (iliFiles != null) {
            String dbSchema = config.getDbschema();
            if (dbSchema != null) {
                url = url + "/" + dbSchema;
            }
            pathMap.put(Ili2db.ILI_FROM_DB, url);
            config.setTransientValue(ch.interlis.ili2c.gui.UserSettings.TEMP_REPOS_URI, url);
            config.setTransientObject(ch.interlis.ili2c.gui.UserSettings.TEMP_REPOS_ILIFILES, iliFiles);
            config.setTransientObject(ch.interlis.ili2c.gui.UserSettings.CUSTOM_ILI_RESOLVER, new IliFromDb(url, conn, dbSchema));
        }
    }
}
Also used : IliFromDb(ch.ehi.ili2db.fromili.IliFromDb) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) HashMap(java.util.HashMap) IliFiles(ch.interlis.ilirepository.IliFiles)

Aggregations

IliFiles (ch.interlis.ilirepository.IliFiles)3 SQLException (java.sql.SQLException)3 Ili2dbException (ch.ehi.ili2db.base.Ili2dbException)2 HashMap (java.util.HashMap)2 IliFromDb (ch.ehi.ili2db.fromili.IliFromDb)1 DbTableName (ch.ehi.sqlgen.repository.DbTableName)1 Model (ch.interlis.ili2c.metamodel.Model)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1