Search in sources :

Example 11 with ExtendedJdbcTemplate

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

the class SqlController method handleRequest.

@Override
public View handleRequest(HttpServletRequest request, HttpServletResponse response, final Map<String, Object> model) throws Exception {
    Permissions.ensureAdmin(Common.getHttpUser());
    model.put("updateResult", -1);
    try {
        final String serializedDataMsg = ControllerUtils.translate(request, "sql.serializedData");
        final String sqlString = request.getParameter("sqlString");
        model.put("sqlString", sqlString);
        model.put("updateResult", -1);
        if (WebUtils.hasSubmitParameter(request, "query"))
            query(sqlString, serializedDataMsg, model);
        else if (WebUtils.hasSubmitParameter(request, "update")) {
            ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
            ejt.setDataSource(Common.databaseProxy.getDataSource());
            int result = ejt.update(sqlString);
            model.put("updateResult", result);
        } else if (WebUtils.hasSubmitParameter(request, "tables"))
            query(Common.databaseProxy.getTableListQuery(), serializedDataMsg, model);
    } catch (RuntimeException e) {
        model.put("error", e.getMessage());
        LOG.warn("", e);
    }
    return null;
}
Also used : ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate)

Example 12 with ExtendedJdbcTemplate

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

the class ScheduledEventDao method deleteScheduledEvent.

public void deleteScheduledEvent(final int scheduledEventId) {
    ScheduledEventVO se = getScheduledEvent(scheduledEventId);
    final ExtendedJdbcTemplate ejt2 = ejt;
    if (se != null) {
        getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                ejt2.update("delete from eventHandlersMapping where eventTypeName=? and eventTypeRef1=?", new Object[] { ScheduledEventType.TYPE_NAME, scheduledEventId });
                ejt2.update("delete from scheduledEvents where id=?", new Object[] { scheduledEventId });
            }
        });
        AuditEventType.raiseDeletedEvent(AuditEvent.TYPE_NAME, se);
        this.countMonitor.decrement();
    }
}
Also used : ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 13 with ExtendedJdbcTemplate

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

the class MaintenanceEventDao method deleteMaintenanceEvent.

public void deleteMaintenanceEvent(final int maintenanceEventId) {
    MaintenanceEventVO me = getMaintenanceEvent(maintenanceEventId);
    final ExtendedJdbcTemplate ejt2 = ejt;
    if (me != null) {
        getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                ejt2.update("delete from eventHandlersMapping where eventTypeName=? and eventTypeRef1=?", new Object[] { MaintenanceEventType.TYPE_NAME, maintenanceEventId });
                ejt2.update("delete from maintenanceEvents where id=?", new Object[] { maintenanceEventId });
            }
        });
        AuditEventType.raiseDeletedEvent(AuditEvent.TYPE_NAME, me);
    }
}
Also used : ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 14 with ExtendedJdbcTemplate

use of com.serotonin.db.spring.ExtendedJdbcTemplate 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 15 with ExtendedJdbcTemplate

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

the class CompoundEventDetectorDao method deleteCompoundEventDetector.

public void deleteCompoundEventDetector(final int compoundEventDetectorId) {
    final ExtendedJdbcTemplate ejt2 = ejt;
    CompoundEventDetectorVO ced = getCompoundEventDetector(compoundEventDetectorId);
    if (ced != null) {
        getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                ejt2.update("delete from eventHandlers where eventTypeId=" + EventType.EventTypeNames.COMPOUND + " and eventTypeRef1=?", new Object[] { compoundEventDetectorId });
                ejt2.update("delete from compoundEventDetectors where id=?", new Object[] { compoundEventDetectorId });
            }
        });
        AuditEventType.raiseDeletedEvent(AuditEventType.TYPE_COMPOUND_EVENT_DETECTOR, ced);
    }
}
Also used : ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) CompoundEventDetectorVO(com.serotonin.m2m2.vo.event.CompoundEventDetectorVO) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

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