Search in sources :

Example 1 with ExtendedJdbcTemplate

use of com.serotonin.db.spring.ExtendedJdbcTemplate in project ma-core-public by infiniteautomation.

the class H2InMemoryDatabaseProxy method clean.

/**
 * @throws Exception
 */
public void clean() throws Exception {
    ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
    ejt.setDataSource(getDataSource());
    runScript(new String[] { "DROP ALL OBJECTS;" }, null);
    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()));
    // 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);
    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);
}
Also used : User(com.serotonin.m2m2.vo.User) DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate)

Example 2 with ExtendedJdbcTemplate

use of com.serotonin.db.spring.ExtendedJdbcTemplate in project ma-core-public by infiniteautomation.

the class H2InMemoryDatabaseProxy method runScript.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.db.DatabaseProxy#runScript(java.lang.String[], java.io.OutputStream)
     */
@Override
public void runScript(String[] script, OutputStream out) throws Exception {
    ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
    ejt.setDataSource(getDataSource());
    StringBuilder statement = new StringBuilder();
    for (String line : script) {
        // Trim whitespace
        line = line.trim();
        // Skip comments
        if (line.startsWith("--"))
            continue;
        // Replace macros in the line
        // line = com.serotonin.util.StringUtils.replaceMacro(line, "blob", replacement("blob"));
        // line = com.serotonin.util.StringUtils.replaceMacro(line, "char", replacement("char"));
        // line = com.serotonin.util.StringUtils.replaceMacro(line, "clob", replacement("clob"));
        // line = com.serotonin.util.StringUtils.replaceMacro(line, "double", replacement("double"));
        // line = com.serotonin.util.StringUtils.replaceMacro(line, "identity", replacement("identity"));
        // line = com.serotonin.util.StringUtils.replaceMacro(line, "int", replacement("int"));
        // line = com.serotonin.util.StringUtils.replaceMacro(line, "varchar", replacement("varchar"));
        // 
        // line = com.serotonin.util.StringUtils.replaceMacro(line, "ALTER COLUMN", replacement("ALTER COLUMN"));
        // line = com.serotonin.util.StringUtils.replaceMacro(line, "DROP FOREIGN KEY",
        // replacement("DROP FOREIGN KEY"));
        statement.append(line);
        statement.append(" ");
        if (line.endsWith(";")) {
            // Execute the statement
            ejt.execute(statement.toString());
            statement.delete(0, statement.length() - 1);
        }
    }
}
Also used : ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate)

Example 3 with ExtendedJdbcTemplate

use of com.serotonin.db.spring.ExtendedJdbcTemplate in project ma-core-public by infiniteautomation.

the class EventDao method purgeEventsBefore.

public int purgeEventsBefore(final long time) {
    // Find a list of event ids with no remaining acknowledgments pending.
    final ExtendedJdbcTemplate ejt2 = ejt;
    int count = getTransactionTemplate().execute(new TransactionCallback<Integer>() {

        @Override
        public Integer doInTransaction(TransactionStatus status) {
            int count = ejt2.update("delete from events where activeTs<?", new Object[] { time });
            // Delete orphaned user comments.
            ejt2.update("delete from userComments where commentType=" + UserCommentVO.TYPE_EVENT + "  and typeKey not in (select id from events)");
            return count;
        }
    });
    return count;
}
Also used : ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 4 with ExtendedJdbcTemplate

use of com.serotonin.db.spring.ExtendedJdbcTemplate in project ma-core-public by infiniteautomation.

the class EventDao method purgeEventsBefore.

/**
 * Purge Events Before a given time with a given alarmLevel
 * @param time
 * @param typeName
 * @return
 */
public int purgeEventsBefore(final long time, final int alarmLevel) {
    // Find a list of event ids with no remaining acknowledgments pending.
    final ExtendedJdbcTemplate ejt2 = ejt;
    int count = getTransactionTemplate().execute(new TransactionCallback<Integer>() {

        @Override
        public Integer doInTransaction(TransactionStatus status) {
            int count = ejt2.update("delete from events where activeTs<? and alarmLevel=?", new Object[] { time, alarmLevel });
            // Delete orphaned user comments.
            ejt2.update("delete from userComments where commentType=" + UserCommentVO.TYPE_EVENT + "  and typeKey not in (select id from events)");
            return count;
        }
    });
    return count;
}
Also used : ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 5 with ExtendedJdbcTemplate

use of com.serotonin.db.spring.ExtendedJdbcTemplate in project ma-core-public by infiniteautomation.

the class EventDao method purgeEventsBefore.

/**
 * Purge Events Before a given time with a given typeName
 * @param time
 * @param typeName
 * @return
 */
public int purgeEventsBefore(final long time, final String typeName) {
    // Find a list of event ids with no remaining acknowledgments pending.
    final ExtendedJdbcTemplate ejt2 = ejt;
    int count = getTransactionTemplate().execute(new TransactionCallback<Integer>() {

        @Override
        public Integer doInTransaction(TransactionStatus status) {
            int count = ejt2.update("delete from events where activeTs<? and typeName=?", new Object[] { time, typeName });
            // Delete orphaned user comments.
            ejt2.update("delete from userComments where commentType=" + UserCommentVO.TYPE_EVENT + "  and typeKey not in (select id from events)");
            return count;
        }
    });
    return count;
}
Also used : ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus)

Aggregations

ExtendedJdbcTemplate (com.serotonin.db.spring.ExtendedJdbcTemplate)22 TransactionStatus (org.springframework.transaction.TransactionStatus)12 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)8 DatabaseSchemaDefinition (com.serotonin.m2m2.module.DatabaseSchemaDefinition)3 User (com.serotonin.m2m2.vo.User)3 File (java.io.File)3 DefaultDataPointPropertiesTemplateFactory (com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 IMangoLifecycle (com.serotonin.m2m2.IMangoLifecycle)1 CompoundEventDetectorVO (com.serotonin.m2m2.vo.event.CompoundEventDetectorVO)1 PointFolder (com.serotonin.m2m2.vo.hierarchy.PointFolder)1 PointHierarchy (com.serotonin.m2m2.vo.hierarchy.PointHierarchy)1 DirectoryInfo (com.serotonin.util.DirectoryInfo)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 MissingResourceException (java.util.MissingResourceException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1