Search in sources :

Example 1 with DefaultDataPointPropertiesTemplateFactory

use of com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory in project ma-core-public by infiniteautomation.

the class Upgrade10 method upgrade.

@Override
public void upgrade() throws Exception {
    // Run the script.
    Map<String, String[]> scripts = new HashMap<>();
    scripts.put(DatabaseProxy.DatabaseType.DERBY.name(), derbyScript);
    scripts.put(DatabaseProxy.DatabaseType.MYSQL.name(), mysqlScript);
    scripts.put(DatabaseProxy.DatabaseType.MSSQL.name(), mssqlScript);
    scripts.put(DatabaseProxy.DatabaseType.H2.name(), h2Script);
    runScript(scripts);
    /**
     * Add a startup task to run after the Audit system is ready
     */
    Providers.get(IMangoLifecycle.class).addStartupTask(new Runnable() {

        @Override
        public void run() {
            DefaultDataPointPropertiesTemplateFactory factory = new DefaultDataPointPropertiesTemplateFactory();
            factory.saveDefaultTemplates();
        }
    });
}
Also used : HashMap(java.util.HashMap) DefaultDataPointPropertiesTemplateFactory(com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle)

Example 2 with DefaultDataPointPropertiesTemplateFactory

use of com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory in project ma-core-public by infiniteautomation.

the class H2InMemoryDatabaseProxy method initialize.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.db.DatabaseProxy#initialize(java.lang.ClassLoader)
     */
@Override
public void initialize(ClassLoader classLoader) {
    JdbcDataSource jds = new JdbcDataSource();
    String url = "jdbc:h2:mem:" + databaseName + ";DB_CLOSE_DELAY=-1";
    jds.setUrl(url);
    dataSource = JdbcConnectionPool.create(jds);
    transactionManager = new DataSourceTransactionManager(dataSource);
    if (initWebConsole) {
        String[] webArgs = new String[4];
        webArgs[0] = "-webPort";
        webArgs[1] = webPort.toString();
        webArgs[2] = "-ifExists";
        webArgs[3] = "-webAllowOthers";
        try {
            this.web = Server.createWebServer(webArgs);
            this.web.start();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
    ejt.setDataSource(getDataSource());
    // Create the empty database
    if (!tableExists(ejt, SchemaDefinition.USERS_TABLE)) {
        // The users table wasn't found, so assume that this is a new instance.
        // Create the tables
        runScript(this.getClass().getResourceAsStream("/createTables-" + getType().name() + ".sql"), null);
        for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) def.newInstallationCheck(ejt);
        SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_SCHEMA_VERSION, Integer.toString(Common.getDatabaseSchemaVersion()));
        SystemSettingsDao.instance.setValue(SystemSettingsDao.BACKUP_ENABLED, "false");
        SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_BACKUP_ENABLED, "false");
        // 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);
        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);
                UserDao.instance.saveUser(user);
                DefaultDataPointPropertiesTemplateFactory factory = new DefaultDataPointPropertiesTemplateFactory();
                factory.saveDefaultTemplates();
            }
        });
    }
    // Check if we are using NoSQL
    if (NoSQLProxyFactory.instance.getProxy() != null) {
        noSQLProxy = NoSQLProxyFactory.instance.getProxy();
        noSQLProxy.initialize();
    }
    initialized = true;
}
Also used : User(com.serotonin.m2m2.vo.User) SQLException(java.sql.SQLException) DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) DefaultDataPointPropertiesTemplateFactory(com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Example 3 with DefaultDataPointPropertiesTemplateFactory

use of com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory 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)

Aggregations

DefaultDataPointPropertiesTemplateFactory (com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory)3 ExtendedJdbcTemplate (com.serotonin.db.spring.ExtendedJdbcTemplate)2 IMangoLifecycle (com.serotonin.m2m2.IMangoLifecycle)2 DatabaseSchemaDefinition (com.serotonin.m2m2.module.DatabaseSchemaDefinition)2 User (com.serotonin.m2m2.vo.User)2 SQLException (java.sql.SQLException)2 DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 FileNotFoundException (java.io.FileNotFoundException)1 HashMap (java.util.HashMap)1 MissingResourceException (java.util.MissingResourceException)1 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)1 CannotGetJdbcConnectionException (org.springframework.jdbc.CannotGetJdbcConnectionException)1