Search in sources :

Example 1 with DatabaseType

use of com.serotonin.m2m2.db.DatabaseType in project ma-modules-public by infiniteautomation.

the class M2MReportImportDwr method generateJson.

@DwrPermission(admin = true)
public ProcessResult generateJson(String driverClassname, String connectionUrl, String username, String password) {
    ProcessResult result = new ProcessResult();
    // Validate the connection
    try {
        DriverManager.registerDriver((Driver) Class.forName(driverClassname).newInstance());
        Connection connection = DriverManager.getConnection(connectionUrl, username, password);
        connection.setAutoCommit(false);
        // Test the connection.
        DatabaseMetaData md = connection.getMetaData();
        String productName = md.getDatabaseProductName();
        DatabaseType type = DatabaseType.DERBY;
        if (productName.equalsIgnoreCase("mysql")) {
            type = DatabaseType.MYSQL;
        } else if (productName.equalsIgnoreCase("Apache Derby")) {
            type = DatabaseType.DERBY;
        } else if (productName.contains("Microsoft")) {
            type = DatabaseType.MSSQL;
        } else if (productName.equalsIgnoreCase("h2")) {
            type = DatabaseType.H2;
        } else if (productName.equalsIgnoreCase("postgressql")) {
            type = DatabaseType.MYSQL;
        }
        // Get the reports
        M2MReportDao dao = new M2MReportDao(connection, type);
        List<M2MReportVO> legacyReports = dao.getReports();
        StringWriter stringWriter = new StringWriter();
        JsonWriter jsonWriter = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
        jsonWriter.setPrettyIndent(3);
        jsonWriter.setPrettyOutput(true);
        int cnt = 0;
        jsonWriter.append("{");
        jsonWriter.indent();
        jsonWriter.quote("reports");
        jsonWriter.append(": [");
        // Convert the reports to our VOs
        for (M2MReportVO legacyReport : legacyReports) {
            legacyReport.jsonWrite(jsonWriter, dao);
            cnt++;
            if (cnt < legacyReports.size())
                jsonWriter.append(",");
        }
        jsonWriter.append(']');
        jsonWriter.append("}");
        jsonWriter.flush();
        result.addData("reports", stringWriter.toString());
    } catch (Exception e) {
        result.addContextualMessage("connectionUrl", "common.default", e.getMessage());
        return result;
    }
    return result;
}
Also used : DatabaseType(com.serotonin.m2m2.db.DatabaseProxy.DatabaseType) StringWriter(java.io.StringWriter) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) JsonWriter(com.serotonin.json.JsonWriter) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 2 with DatabaseType

use of com.serotonin.m2m2.db.DatabaseType in project ma-core-public by infiniteautomation.

the class AbstractDatabaseProxy method initialize.

@PostConstruct
@Override
public void initialize() {
    initializeImpl();
    DataSource dataSource = getDataSource();
    this.jdbcTemplate = new ExtendedJdbcTemplate(dataSource);
    this.context = DSL.using(getConfig());
    this.transactionManager = new DataSourceTransactionManager(dataSource);
    SystemSettingsAccessor systemSettingsAccessor = () -> context;
    DatabaseSchemaUpgrader upgrader = new DatabaseSchemaUpgrader(this, systemSettingsAccessor, classLoader);
    // VO objects stored in blobs
    try {
        systemSettingsAccessor.getSystemSetting(SystemSettingsDao.LANGUAGE).ifPresent(Common::setSystemLanguage);
    } catch (DataAccessException e) {
    // that's ok, table probably doesn't exist yet
    }
    // First confirm that if we are MySQL we have JSON Support
    if (getType().name().equals(DatabaseType.MYSQL.name())) {
        try {
            runScript(new String[] { "CREATE TABLE mangoUpgrade28 (test JSON)engine=InnoDB;", "DROP TABLE mangoUpgrade28;" }, null);
        } catch (BadSqlGrammarException e) {
            String version = "?";
            try {
                DatabaseMetaData dmd = getDataSource().getConnection().getMetaData();
                version = dmd.getDatabaseProductVersion();
            } catch (Exception ex) {
                log.error("Failed to create test table for JSON compatibility" + ex);
            }
            throw new ShouldNeverHappenException("Unable to start Mango, MySQL version must be at least 5.7.8 to support JSON columns. Your version is " + version);
        }
    }
    try {
        boolean newDatabase = false;
        String convertTypeStr = env.getProperty(propertyPrefix + "convert.db.type");
        boolean willConvert = false;
        if (!databaseExists()) {
            if (!restoreTables()) {
                createTables();
                // Check if we should convert from another database.
                if (!StringUtils.isBlank(convertTypeStr)) {
                    willConvert = true;
                } else {
                    newDatabase = true;
                }
            }
        }
        if (newDatabase) {
            doInTransaction(txStatus -> {
                initializeCoreDatabase(context);
            });
        } else if (!willConvert) {
            // Make sure the core schema version matches the application version.  If we are running a conversion
            // then the responsibility is on the User to be converting from a compatible version
            upgrader.checkCoreUpgrade();
        }
        // Ensure the modules are installed after the core schema is updated
        for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) {
            try {
                def.setDatabaseProxy(this);
                def.newInstallationCheck();
            } catch (Exception e) {
                log.error("Module " + def.getModule().getName() + " new installation check failed", e);
            }
        }
        if (!willConvert) {
            // Allow modules to upgrade their schemas
            for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) {
                upgrader.checkModuleUpgrade(def);
            }
        }
        if (willConvert) {
            // Found a database type from which to convert.
            DatabaseType convertType = DatabaseType.valueOf(convertTypeStr.toUpperCase());
            // TODO check that the convert source has the current DB version, or upgrade it if not.
            AbstractDatabaseProxy sourceProxy = (AbstractDatabaseProxy) factory.createDatabaseProxy(convertType, propertyPrefix + "convert.");
            try {
                sourceProxy.initialize();
                DBConvert convert = new DBConvert();
                convert.setSource(sourceProxy);
                convert.setTarget(this);
                try {
                    convert.execute();
                } catch (SQLException e) {
                    throw new ShouldNeverHappenException(e);
                }
            } finally {
                sourceProxy.terminate();
            }
        }
        listeners.forEach(l -> l.onInitialize(this));
    } catch (CannotGetJdbcConnectionException e) {
        log.error("Unable to connect to database of type " + getType().name(), e);
        throw e;
    } catch (IOException e) {
        log.error("Exception initializing database proxy: " + e.getMessage(), e);
        throw new UncheckedIOException(e);
    } catch (Exception e) {
        log.error("Exception initializing database proxy: " + e.getMessage(), e);
        throw e;
    }
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) SQLException(java.sql.SQLException) DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DataAccessException(org.jooq.exception.DataAccessException) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) IOException(java.io.IOException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) UncheckedIOException(java.io.UncheckedIOException) DataSource(javax.sql.DataSource) Common(com.serotonin.m2m2.Common) SystemSettingsAccessor(com.serotonin.m2m2.db.upgrade.SystemSettingsAccessor) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) DataAccessException(org.jooq.exception.DataAccessException) DatabaseSchemaUpgrader(com.serotonin.m2m2.db.upgrade.DatabaseSchemaUpgrader) PostConstruct(javax.annotation.PostConstruct)

Example 3 with DatabaseType

use of com.serotonin.m2m2.db.DatabaseType in project ma-core-public by infiniteautomation.

the class AbstractDatabaseProxy method initialize.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.db.DatabaseProxy#initialize(java.lang.ClassLoader)
     */
@Override
public void initialize(ClassLoader classLoader) {
    initializeImpl("");
    useMetrics = Common.envProps.getBoolean("db.useMetrics", false);
    ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
    ejt.setDataSource(getDataSource());
    transactionManager = new DataSourceTransactionManager(getDataSource());
    try {
        if (newDatabaseCheck(ejt)) {
            // Check if we should convert from another database.
            String convertTypeStr = null;
            try {
                convertTypeStr = Common.envProps.getString("convert.db.type");
            } catch (MissingResourceException e) {
                convertTypeStr = "";
            }
            if (!StringUtils.isBlank(convertTypeStr)) {
                // Found a database type from which to convert.
                DatabaseType convertType = DatabaseType.valueOf(convertTypeStr.toUpperCase());
                if (convertType == null)
                    throw new IllegalArgumentException("Unknown convert database type: " + convertType);
                // TODO check that the convert source has the current DB version, or upgrade it if not.
                AbstractDatabaseProxy sourceProxy = convertType.getImpl();
                sourceProxy.initializeImpl("convert.");
                DBConvert convert = new DBConvert();
                convert.setSource(sourceProxy);
                convert.setTarget(this);
                try {
                    convert.execute();
                } catch (SQLException e) {
                    throw new ShouldNeverHappenException(e);
                }
                sourceProxy.terminate(false);
            } else {
                // Record the current version.
                SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_SCHEMA_VERSION, Integer.toString(Common.getDatabaseSchemaVersion()));
                // Add the settings flag that this is a new instance. This flag is removed when an administrator
                // logs in.
                SystemSettingsDao.instance.setBooleanValue(SystemSettingsDao.NEW_INSTANCE, true);
                /**
                 * Add a startup task to run after the Audit system is ready
                 */
                Providers.get(IMangoLifecycle.class).addStartupTask(new Runnable() {

                    @Override
                    public void run() {
                        // New database. Create a default user.
                        User user = new User();
                        user.setId(Common.NEW_ID);
                        user.setName("Administrator");
                        user.setUsername("admin");
                        user.setPassword(Common.encrypt("admin"));
                        user.setEmail("admin@yourMangoDomain.com");
                        user.setPhone("");
                        user.setPermissions(SuperadminPermissionDefinition.GROUP_NAME);
                        user.setDisabled(false);
                        user.setHomeUrl("/ui/administration/home");
                        UserDao.instance.saveUser(user);
                        DefaultDataPointPropertiesTemplateFactory factory = new DefaultDataPointPropertiesTemplateFactory();
                        factory.saveDefaultTemplates();
                    }
                });
            }
        } else
            // The database exists, so let's make its schema version matches the application version.
            DBUpgrade.checkUpgrade();
        // Check if we are using NoSQL
        if (NoSQLProxyFactory.instance.getProxy() != null) {
            noSQLProxy = NoSQLProxyFactory.instance.getProxy();
            noSQLProxy.initialize();
        }
    } catch (CannotGetJdbcConnectionException e) {
        log.fatal("Unable to connect to database of type " + getType().name(), e);
        throw e;
    } catch (Exception e) {
        log.fatal("Exception initializing database proxy: " + e.getMessage(), e);
        throw e;
    }
    // Allow modules to upgrade themselves
    for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) DBUpgrade.checkUpgrade(def, classLoader);
    postInitialize(ejt);
}
Also used : User(com.serotonin.m2m2.vo.User) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) SQLException(java.sql.SQLException) DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) MissingResourceException(java.util.MissingResourceException) DefaultDataPointPropertiesTemplateFactory(com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) SQLException(java.sql.SQLException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) MissingResourceException(java.util.MissingResourceException) FileNotFoundException(java.io.FileNotFoundException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Example 4 with DatabaseType

use of com.serotonin.m2m2.db.DatabaseType in project ma-modules-public by infiniteautomation.

the class M2MReportImportDwr method migrate.

@DwrPermission(admin = true)
public ProcessResult migrate(String driverClassname, String connectionUrl, String username, String password) {
    ProcessResult result = new ProcessResult();
    // Validate the connection
    try {
        DriverManager.registerDriver((Driver) Class.forName(driverClassname).newInstance());
        Connection connection = DriverManager.getConnection(connectionUrl, username, password);
        connection.setAutoCommit(false);
        // Test the connection.
        DatabaseMetaData md = connection.getMetaData();
        String productName = md.getDatabaseProductName();
        DatabaseType type = DatabaseType.DERBY;
        if (productName.equalsIgnoreCase("mysql")) {
            type = DatabaseType.MYSQL;
        } else if (productName.equalsIgnoreCase("Apache Derby")) {
            type = DatabaseType.DERBY;
        } else if (productName.contains("Microsoft")) {
            type = DatabaseType.MSSQL;
        } else if (productName.equalsIgnoreCase("h2")) {
            type = DatabaseType.H2;
        } else if (productName.equalsIgnoreCase("postgressql")) {
            type = DatabaseType.MYSQL;
        }
        // Get the reports
        M2MReportDao dao = new M2MReportDao(connection, type);
        List<M2MReportVO> legacyReports = dao.getReports();
        // Convert the reports to our VOs
        List<ReportVO> reports = new ArrayList<ReportVO>();
        for (M2MReportVO legacyReport : legacyReports) {
            try {
                ReportVO report = legacyReport.convert(dao);
                report.validate(result);
                reports.add(report);
            } catch (Exception e) {
                result.addGenericMessage("common.default", e.getMessage());
            }
        }
        if (!result.getHasMessages()) {
            ReportDao reportDao = ReportDao.instance;
            for (ReportVO vo : reports) {
                vo.validate(result);
                reportDao.saveReport(vo);
            }
        }
        result.addData("reports", reports);
    } catch (Exception e) {
        result.addContextualMessage("connectionUrl", "common.default", e.getMessage());
        return result;
    }
    return result;
}
Also used : DatabaseType(com.serotonin.m2m2.db.DatabaseProxy.DatabaseType) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ReportVO(com.serotonin.m2m2.reports.vo.ReportVO) DatabaseMetaData(java.sql.DatabaseMetaData) ReportDao(com.serotonin.m2m2.reports.ReportDao) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

DatabaseMetaData (java.sql.DatabaseMetaData)3 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 ExtendedJdbcTemplate (com.serotonin.db.spring.ExtendedJdbcTemplate)2 DatabaseType (com.serotonin.m2m2.db.DatabaseProxy.DatabaseType)2 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)2 DatabaseSchemaDefinition (com.serotonin.m2m2.module.DatabaseSchemaDefinition)2 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 CannotGetJdbcConnectionException (org.springframework.jdbc.CannotGetJdbcConnectionException)2 DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)2 JsonWriter (com.serotonin.json.JsonWriter)1 Common (com.serotonin.m2m2.Common)1 IMangoLifecycle (com.serotonin.m2m2.IMangoLifecycle)1 DatabaseSchemaUpgrader (com.serotonin.m2m2.db.upgrade.DatabaseSchemaUpgrader)1 SystemSettingsAccessor (com.serotonin.m2m2.db.upgrade.SystemSettingsAccessor)1 ReportDao (com.serotonin.m2m2.reports.ReportDao)1 ReportVO (com.serotonin.m2m2.reports.vo.ReportVO)1 User (com.serotonin.m2m2.vo.User)1 DefaultDataPointPropertiesTemplateFactory (com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory)1