Java Code Examples for org.activiti.engine.impl.db.DbSqlSession

The following examples show how to use org.activiti.engine.impl.db.DbSqlSession. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: flowable-engine   Source File: MetaDataTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testMetaData() {
    ProcessEngineConfigurationImpl activiti5ProcessEngineConfig = (ProcessEngineConfigurationImpl) processEngineConfiguration.getFlowable5CompatibilityHandler().getRawProcessConfiguration();
    activiti5ProcessEngineConfig.getCommandExecutor().execute(new Command<Object>() {
        public Object execute(CommandContext commandContext) {
            // PRINT THE TABLE NAMES TO CHECK IF WE CAN USE METADATA INSTEAD
            // THIS IS INTENDED FOR TEST THAT SHOULD RUN ON OUR QA INFRASTRUCTURE TO SEE IF METADATA
            // CAN BE USED INSTEAD OF PERFORMING A QUERY THAT MIGHT FAIL
            try {
                SqlSession sqlSession = commandContext.getSession(DbSqlSession.class).getSqlSession();
                ResultSet tables = sqlSession.getConnection().getMetaData().getTables(null, null, null, null);
                while (tables.next()) {
                    ResultSetMetaData resultSetMetaData = tables.getMetaData();
                    int columnCount = resultSetMetaData.getColumnCount();
                    for (int i = 1; i <= columnCount; i++) {
                        LOGGER.info("result set column {}|{}|{}|{}", i, resultSetMetaData.getColumnName(i), resultSetMetaData.getColumnLabel(i), tables.getString(i));
                    }
                    LOGGER.info("-------------------------------------------------------");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    });
}
 
Example 2
Source Project: flowable-engine   Source File: TaskEntity.java    License: Apache License 2.0 6 votes vote down vote up
public void insert(ExecutionEntity execution, boolean fireEvents) {
    CommandContext commandContext = Context.getCommandContext();
    DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
    dbSqlSession.insert(this);

    // Inherit tenant id (if applicable)
    if (execution != null && execution.getTenantId() != null) {
        setTenantId(execution.getTenantId());
    }

    if (execution != null) {
        execution.addTask(this);
    }

    commandContext.getHistoryManager().recordTaskCreated(this, execution);

    if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled() && fireEvents) {
        commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_CREATED, this));
        commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_INITIALIZED, this));
    }
}
 
Example 3
Source Project: flowable-engine   Source File: TaskEntity.java    License: Apache License 2.0 6 votes vote down vote up
public void update() {
    // Needed to make history work: the setter will also update the historic task
    setOwner(this.getOwner());
    setAssignee(this.getAssignee(), true, false);
    setDelegationState(this.getDelegationState());
    setName(this.getName());
    setDescription(this.getDescription());
    setPriority(this.getPriority());
    setCategory(this.getCategory());
    setCreateTime(this.getCreateTime());
    setDueDate(this.getDueDate());
    setParentTaskId(this.getParentTaskId());
    setFormKey(formKey);

    CommandContext commandContext = Context.getCommandContext();
    DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
    dbSqlSession.update(this);

    if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_UPDATED, this));
    }
}
 
Example 4
Source Project: activiti6-boot2   Source File: AbstractMuleTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Each test is assumed to clean up all DB content it entered. After a test method executed, this method scans all tables to see if the DB is completely clean. It throws AssertionFailed in case the
 * DB is not clean. If the DB is not clean, it is cleaned by performing a create a drop.
 */
protected void assertAndEnsureCleanDb(ProcessEngine processEngine) throws Exception {
  log.debug("verifying that db is clean after test");
  Map<String, Long> tableCounts = processEngine.getManagementService().getTableCount();
  StringBuilder outputMessage = new StringBuilder();
  for (String tableName : tableCounts.keySet()) {
    String tableNameWithoutPrefix = tableName.replace(processEngine.getProcessEngineConfiguration().getDatabaseTablePrefix(), "");
    if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableNameWithoutPrefix)) {
      Long count = tableCounts.get(tableName);
      if (count != 0L) {
        outputMessage.append("  " + tableName + ": " + count + " record(s) ");
      }
    }
  }
  if (outputMessage.length() > 0) {
    outputMessage.insert(0, "DB NOT CLEAN: \n");
    log.error(EMPTY_LINE);
    log.error(outputMessage.toString());

    log.info("dropping and recreating db");

    CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor();
    CommandConfig config = new CommandConfig().transactionNotSupported();
    commandExecutor.execute(config, new Command<Object>() {
      public Object execute(CommandContext commandContext) {
        DbSqlSession session = commandContext.getSession(DbSqlSession.class);
        session.dbSchemaDrop();
        session.dbSchemaCreate();
        return null;
      }
    });

    Assert.fail(outputMessage.toString());

  } else {
    log.info("database was clean");
  }
}
 
Example 5
Source Project: activiti6-boot2   Source File: AbstractMuleTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Each test is assumed to clean up all DB content it entered. After a test method executed, this method scans all tables to see if the DB is completely clean. It throws AssertionFailed in case the
 * DB is not clean. If the DB is not clean, it is cleaned by performing a create a drop.
 */
protected void assertAndEnsureCleanDb(ProcessEngine processEngine) throws Exception {
  log.debug("verifying that db is clean after test");
  Map<String, Long> tableCounts = processEngine.getManagementService().getTableCount();
  StringBuilder outputMessage = new StringBuilder();
  for (String tableName : tableCounts.keySet()) {
    String tableNameWithoutPrefix = tableName.replace(processEngine.getProcessEngineConfiguration().getDatabaseTablePrefix(), "");
    if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableNameWithoutPrefix)) {
      Long count = tableCounts.get(tableName);
      if (count != 0L) {
        outputMessage.append("  " + tableName + ": " + count + " record(s) ");
      }
    }
  }
  if (outputMessage.length() > 0) {
    outputMessage.insert(0, "DB NOT CLEAN: \n");
    log.error(EMPTY_LINE);
    log.error(outputMessage.toString());

    log.info("dropping and recreating db");

    CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor();
    CommandConfig config = new CommandConfig().transactionNotSupported();
    commandExecutor.execute(config, new Command<Object>() {
      public Object execute(CommandContext commandContext) {
        DbSqlSession session = commandContext.getDbSqlSession();
        session.dbSchemaDrop();
        session.dbSchemaCreate();
        return null;
      }
    });

    Assert.fail(outputMessage.toString());

  } else {
    log.info("database was clean");
  }
}
 
Example 6
Source Project: activiti6-boot2   Source File: AbstractDataManager.java    License: Apache License 2.0 5 votes vote down vote up
protected List<EntityImpl> getListFromCache(CachedEntityMatcher<EntityImpl> entityMatcher, Object parameter) {
  Collection<CachedEntity> cachedObjects = getEntityCache().findInCacheAsCachedObjects(getManagedEntityClass());
  
  DbSqlSession dbSqlSession = getDbSqlSession();
  
  List<EntityImpl> result = new ArrayList<EntityImpl>(cachedObjects.size());
  if (cachedObjects != null && entityMatcher != null) {
    for (CachedEntity cachedObject : cachedObjects) {
      EntityImpl cachedEntity = (EntityImpl) cachedObject.getEntity();
      if (entityMatcher.isRetained(null, cachedObjects, cachedEntity, parameter) && !dbSqlSession.isEntityToBeDeleted(cachedEntity)) {
        result.add(cachedEntity);
      }
    }
  }
  
  if (getManagedEntitySubClasses() != null && entityMatcher != null) {
    for (Class<? extends EntityImpl> entitySubClass : getManagedEntitySubClasses()) {
      Collection<CachedEntity> subclassCachedObjects = getEntityCache().findInCacheAsCachedObjects(entitySubClass);
      if (subclassCachedObjects != null) {
        for (CachedEntity subclassCachedObject : subclassCachedObjects) {
          EntityImpl cachedSubclassEntity = (EntityImpl) subclassCachedObject.getEntity();
          if (entityMatcher.isRetained(null, cachedObjects, cachedSubclassEntity, parameter) && !dbSqlSession.isEntityToBeDeleted(cachedSubclassEntity)) {
            result.add(cachedSubclassEntity);
          }
        }
      }
    }
  }
  
  return result;
}
 
Example 7
Source Project: activiti6-boot2   Source File: ManagementServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
public String databaseSchemaUpgrade(final Connection connection, final String catalog, final String schema) {
  CommandConfig config = commandExecutor.getDefaultConfig().transactionNotSupported();
  return commandExecutor.execute(config, new Command<String>() {
    public String execute(CommandContext commandContext) {
      DbSqlSessionFactory dbSqlSessionFactory = (DbSqlSessionFactory) commandContext.getSessionFactories().get(DbSqlSession.class);
      DbSqlSession dbSqlSession = new DbSqlSession(dbSqlSessionFactory, commandContext.getEntityCache(), connection, catalog, schema);
      commandContext.getSessions().put(DbSqlSession.class, dbSqlSession);
      return dbSqlSession.dbSchemaUpdate();
    }
  });
}
 
Example 8
public Object execute(CommandContext commandContext) {
  DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
  if (dbSqlSession != null) {
    dbSqlSession.performSchemaOperationsProcessEngineBuild();
  }
  return null;
}
 
Example 9
public void execute(DbSqlSession dbSqlSession) throws Exception {
  // As of 5.11, the history-setting is no longer stored in the database,
  // so inserting it in this upgrade and removing
  // in in a 5.10->5.11 upgrade is useless...

  // int historyLevel =
  // Context.getProcessEngineConfiguration().getHistoryLevel();
  // PropertyEntity property = new PropertyEntity("historyLevel",
  // Integer.toString(historyLevel));
  // dbSqlSession.insert(property);
}
 
Example 10
Source Project: activiti6-boot2   Source File: TestHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Each test is assumed to clean up all DB content it entered. After a test method executed, this method scans all tables to see if the DB is completely clean. It throws AssertionFailed in case the
 * DB is not clean. If the DB is not clean, it is cleaned by performing a create a drop.
 */
public static void assertAndEnsureCleanDb(ProcessEngine processEngine) {
  log.debug("verifying that db is clean after test");
  Map<String, Long> tableCounts = processEngine.getManagementService().getTableCount();
  StringBuilder outputMessage = new StringBuilder();
  for (String tableName : tableCounts.keySet()) {
    if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableName)) {
      Long count = tableCounts.get(tableName);
      if (count != 0L) {
        outputMessage.append("  ").append(tableName).append(": ").append(count).append(" record(s) ");
      }
    }
  }
  if (outputMessage.length() > 0) {
    outputMessage.insert(0, "DB NOT CLEAN: \n");
    log.error(EMPTY_LINE);
    log.error(outputMessage.toString());

    ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor().execute(new Command<Object>() {
      public Object execute(CommandContext commandContext) {
        DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
        dbSqlSession.dbSchemaDrop();
        dbSqlSession.dbSchemaCreate();
        return null;
      }
    });

    throw new AssertionError(outputMessage.toString());
  }
}
 
Example 11
Source Project: activiti6-boot2   Source File: BaseJPARestTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Each test is assumed to clean up all DB content it entered. After a test method executed, this method scans all tables to see if the DB is completely clean. It throws AssertionFailed in case the
 * DB is not clean. If the DB is not clean, it is cleaned by performing a create a drop.
 */
protected void assertAndEnsureCleanDb() throws Throwable {
  log.debug("verifying that db is clean after test");
  Map<String, Long> tableCounts = managementService.getTableCount();
  StringBuilder outputMessage = new StringBuilder();
  for (String tableName : tableCounts.keySet()) {
    String tableNameWithoutPrefix = tableName.replace(processEngineConfiguration.getDatabaseTablePrefix(), "");
    if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableNameWithoutPrefix)) {
      Long count = tableCounts.get(tableName);
      if (count != 0L) {
        outputMessage.append("  " + tableName + ": " + count + " record(s) ");
      }
    }
  }
  if (outputMessage.length() > 0) {
    outputMessage.insert(0, "DB NOT CLEAN: \n");
    log.error(EMPTY_LINE);
    log.error(outputMessage.toString());

    log.info("dropping and recreating db");

    CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor();
    commandExecutor.execute(new Command<Object>() {
      public Object execute(CommandContext commandContext) {
        DbSqlSession session = commandContext.getDbSqlSession();
        session.dbSchemaDrop();
        session.dbSchemaCreate();
        return null;
      }
    });

    if (exception != null) {
      throw exception;
    } else {
      Assert.fail(outputMessage.toString());
    }
  } else {
    log.info("database was clean");
  }
}
 
Example 12
Source Project: activiti6-boot2   Source File: BaseSpringRestTestCase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Each test is assumed to clean up all DB content it entered. After a test method executed, this method scans all tables to see if the DB is completely clean. It throws AssertionFailed in case the
 * DB is not clean. If the DB is not clean, it is cleaned by performing a create a drop.
 */
protected void assertAndEnsureCleanDb() throws Throwable {
  log.debug("verifying that db is clean after test");
  Map<String, Long> tableCounts = managementService.getTableCount();
  StringBuilder outputMessage = new StringBuilder();
  for (String tableName : tableCounts.keySet()) {
    String tableNameWithoutPrefix = tableName.replace(processEngineConfiguration.getDatabaseTablePrefix(), "");
    if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableNameWithoutPrefix)) {
      Long count = tableCounts.get(tableName);
      if (count != 0L) {
        outputMessage.append("  " + tableName + ": " + count + " record(s) ");
      }
    }
  }
  if (outputMessage.length() > 0) {
    outputMessage.insert(0, "DB NOT CLEAN: \n");
    log.error(EMPTY_LINE);
    log.error(outputMessage.toString());

    log.info("dropping and recreating db");

    CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor();
    commandExecutor.execute(new Command<Object>() {
      public Object execute(CommandContext commandContext) {
        DbSqlSession session = commandContext.getDbSqlSession();
        session.dbSchemaDrop();
        session.dbSchemaCreate();
        return null;
      }
    });

    if (exception != null) {
      throw exception;
    } else {
      Assert.fail(outputMessage.toString());
    }
  } else {
    log.info("database was clean");
  }
}
 
Example 13
Source Project: activiti6-boot2   Source File: TestHelper.java    License: Apache License 2.0 5 votes vote down vote up
/** 
 * Each test is assumed to clean up all DB content it entered.
 * After a test method executed, this method scans all tables to see if the DB is completely clean. 
 * It throws AssertionFailed in case the DB is not clean.
 * If the DB is not clean, it is cleaned by performing a create a drop. 
 */
public static void assertAndEnsureCleanDb(ProcessEngine processEngine) {
  log.debug("verifying that db is clean after test");
  Map<String, Long> tableCounts = processEngine.getManagementService().getTableCount();
  StringBuilder outputMessage = new StringBuilder();
  for (String tableName : tableCounts.keySet()) {
    if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableName)) {
      Long count = tableCounts.get(tableName);
      if (count!=0L) {
        outputMessage.append("  ").append(tableName).append(": ").append(count).append(" record(s) ");
      }
    }
  }
  if (outputMessage.length() > 0) {
    outputMessage.insert(0, "DB NOT CLEAN: \n");
    log.error(EMPTY_LINE);
    log.error(outputMessage.toString());

    ((ProcessEngineImpl)processEngine)
    .getProcessEngineConfiguration().getCommandExecutor()
      .execute(new Command<Object>() {
        public Object execute(CommandContext commandContext) {
          DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
          dbSqlSession.dbSchemaDrop();
          dbSqlSession.dbSchemaCreate();
          return null;
        }
      });
    
    throw new AssertionError(outputMessage.toString());
  }
}
 
Example 14
Source Project: flowable-engine   Source File: ModelEntityManager.java    License: Apache License 2.0 5 votes vote down vote up
public void updateModel(ModelEntity updatedModel) {
    CommandContext commandContext = Context.getCommandContext();
    updatedModel.setLastUpdateTime(Context.getProcessEngineConfiguration().getClock().getCurrentTime());
    DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
    dbSqlSession.update(updatedModel);

    if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_UPDATED, updatedModel));
    }
}
 
Example 15
Source Project: flowable-engine   Source File: HistoricDetailEntity.java    License: Apache License 2.0 5 votes vote down vote up
public void delete() {
    DbSqlSession dbSqlSession = Context
            .getCommandContext()
            .getDbSqlSession();

    dbSqlSession.delete(this);
}
 
Example 16
public void updateProcessDefinitionInfo(ProcessDefinitionInfoEntity updatedProcessDefinitionInfo) {
    CommandContext commandContext = Context.getCommandContext();
    DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
    dbSqlSession.update(updatedProcessDefinitionInfo);

    if (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_UPDATED, updatedProcessDefinitionInfo));
    }
}
 
Example 17
@Override
public void execute(DbSqlSession dbSqlSession) throws Exception {
    // As of 5.11, the history-setting is no longer stored in the database, so inserting it in this upgrade and removing
    // in in a 5.10->5.11 upgrade is useless...

    // int historyLevel = Context.getProcessEngineConfiguration().getHistoryLevel();
    // PropertyEntity property = new PropertyEntity("historyLevel", Integer.toString(historyLevel));
    // dbSqlSession.insert(property);
}
 
Example 18
Source Project: flowable-engine   Source File: GetAttachmentContentCmd.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public InputStream execute(CommandContext commandContext) {
    DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
    AttachmentEntity attachment = dbSqlSession.selectById(AttachmentEntity.class, attachmentId);

    String contentId = attachment.getContentId();
    if (contentId == null) {
        return null;
    }

    ByteArrayEntity byteArray = dbSqlSession.selectById(ByteArrayEntity.class, contentId);
    byte[] bytes = byteArray.getBytes();

    return new ByteArrayInputStream(bytes);
}
 
Example 19
protected DbSqlSession getDbSqlSession() {
  return commandContext.getDbSqlSession();
}
 
Example 20
Source Project: activiti6-boot2   Source File: CommandContext.java    License: Apache License 2.0 4 votes vote down vote up
public DbSqlSession getDbSqlSession() {
  return getSession(DbSqlSession.class);
}
 
Example 21
Source Project: activiti6-boot2   Source File: TableDataManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
protected DbSqlSession getDbSqlSession() {
  return getSession(DbSqlSession.class);
}
 
Example 22
Source Project: activiti6-boot2   Source File: TableDataManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public List<String> getTablesPresentInDatabase() {
  List<String> tableNames = new ArrayList<String>();
  Connection connection = null;
  try {
    connection = getDbSqlSession().getSqlSession().getConnection();
    DatabaseMetaData databaseMetaData = connection.getMetaData();
    ResultSet tables = null;
    try {
      log.debug("retrieving activiti tables from jdbc metadata");
      String databaseTablePrefix = getDbSqlSession().getDbSqlSessionFactory().getDatabaseTablePrefix();
      String tableNameFilter = databaseTablePrefix + "ACT_%";
      if ("postgres".equals(getDbSqlSession().getDbSqlSessionFactory().getDatabaseType())) {
        tableNameFilter = databaseTablePrefix + "act_%";
      }
      if ("oracle".equals(getDbSqlSession().getDbSqlSessionFactory().getDatabaseType())) {
        tableNameFilter = databaseTablePrefix + "ACT" + databaseMetaData.getSearchStringEscape() + "_%";
      }
      
      String catalog = null;
      if (getProcessEngineConfiguration().getDatabaseCatalog() != null && getProcessEngineConfiguration().getDatabaseCatalog().length() > 0) {
        catalog = getProcessEngineConfiguration().getDatabaseCatalog();
      }
      
      String schema = null;
      if (getProcessEngineConfiguration().getDatabaseSchema() != null && getProcessEngineConfiguration().getDatabaseSchema().length() > 0) {
        if ("oracle".equals(getDbSqlSession().getDbSqlSessionFactory().getDatabaseType())) {
          schema = getProcessEngineConfiguration().getDatabaseSchema().toUpperCase();
        } else {
          schema = getProcessEngineConfiguration().getDatabaseSchema();
        }
      }
      
      tables = databaseMetaData.getTables(catalog, schema, tableNameFilter, DbSqlSession.JDBC_METADATA_TABLE_TYPES);
      while (tables.next()) {
        String tableName = tables.getString("TABLE_NAME");
        tableName = tableName.toUpperCase();
        tableNames.add(tableName);
        log.debug("  retrieved activiti table name {}", tableName);
      }
    } finally {
      tables.close();
    }
  } catch (Exception e) {
    throw new ActivitiException("couldn't get activiti table names using metadata: " + e.getMessage(), e);
  }
  return tableNames;
}
 
Example 23
Source Project: activiti6-boot2   Source File: AbstractDataManager.java    License: Apache License 2.0 4 votes vote down vote up
protected DbSqlSession getDbSqlSession() {
  return getSession(DbSqlSession.class);
}
 
Example 24
Source Project: activiti6-boot2   Source File: AbstractActivitiTestCase.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Each test is assumed to clean up all DB content it entered. After a test method executed, this method scans all tables to see if the DB is completely clean. It throws AssertionFailed in case the
 * DB is not clean. If the DB is not clean, it is cleaned by performing a create a drop.
 */
protected void assertAndEnsureCleanDb() throws Throwable {
  log.debug("verifying that db is clean after test");
  Map<String, Long> tableCounts = managementService.getTableCount();
  StringBuilder outputMessage = new StringBuilder();
  for (String tableName : tableCounts.keySet()) {
    String tableNameWithoutPrefix = tableName.replace(processEngineConfiguration.getDatabaseTablePrefix(), "");
    if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableNameWithoutPrefix)) {
      Long count = tableCounts.get(tableName);
      if (count != 0L) {
        outputMessage.append("  ").append(tableName).append(": ").append(count).append(" record(s) ");
      }
    }
  }
  if (outputMessage.length() > 0) {
    outputMessage.insert(0, "DB NOT CLEAN: \n");
    log.error(EMPTY_LINE);
    log.error(outputMessage.toString());

    log.info("dropping and recreating db");

    CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor();
    CommandConfig config = new CommandConfig().transactionNotSupported();
    commandExecutor.execute(config, new Command<Object>() {
      public Object execute(CommandContext commandContext) {
        DbSqlSession session = commandContext.getDbSqlSession();
        session.dbSchemaDrop();
        session.dbSchemaCreate();
        return null;
      }
    });

    if (exception != null) {
      throw exception;
    } else {
      Assert.fail(outputMessage.toString());
    }
  } else {
    log.info("database was clean");
  }
}
 
Example 25
Source Project: activiti6-boot2   Source File: AbstractActivitiTestCase.java    License: Apache License 2.0 4 votes vote down vote up
/** Each test is assumed to clean up all DB content it entered.
 * After a test method executed, this method scans all tables to see if the DB is completely clean. 
 * It throws AssertionFailed in case the DB is not clean.
 * If the DB is not clean, it is cleaned by performing a create a drop. */
protected void assertAndEnsureCleanDb() throws Throwable {
  log.debug("verifying that db is clean after test");
  Map<String, Long> tableCounts = managementService.getTableCount();
  StringBuilder outputMessage = new StringBuilder();
  for (String tableName : tableCounts.keySet()) {
    String tableNameWithoutPrefix = tableName.replace(processEngineConfiguration.getDatabaseTablePrefix(), "");
    if (!TABLENAMES_EXCLUDED_FROM_DB_CLEAN_CHECK.contains(tableNameWithoutPrefix)) {
      Long count = tableCounts.get(tableName);
      if (count!=0L) {
        outputMessage.append("  ").append(tableName).append(": ").append(count).append(" record(s) ");
      }
    }
  }
  if (outputMessage.length() > 0) {
    outputMessage.insert(0, "DB NOT CLEAN: \n");
    log.error(EMPTY_LINE);
    log.error(outputMessage.toString());
    
    log.info("dropping and recreating db");
    
    CommandExecutor commandExecutor = ((ProcessEngineImpl)processEngine).getProcessEngineConfiguration().getCommandExecutor();
    CommandConfig config = new CommandConfig().transactionNotSupported();
    commandExecutor.execute(config, new Command<Object>() {
      public Object execute(CommandContext commandContext) {
        DbSqlSession session = commandContext.getDbSqlSession();
        session.dbSchemaDrop();
        session.dbSchemaCreate();
        return null;
      }
    });

    if (exception!=null) {
      throw exception;
    } else {
      Assert.fail(outputMessage.toString());
    }
  } else {
    log.info("database was clean");
  }
}
 
Example 26
protected DbSqlSession getDbSqlSession() {
    return commandContext.getSession(DbSqlSession.class);
}
 
Example 27
Source Project: flowable-engine   Source File: CommandContext.java    License: Apache License 2.0 4 votes vote down vote up
public DbSqlSession getDbSqlSession() {
    return getSession(DbSqlSession.class);
}
 
Example 28
Source Project: flowable-engine   Source File: AbstractManager.java    License: Apache License 2.0 4 votes vote down vote up
protected DbSqlSession getDbSqlSession() {
    return getSession(DbSqlSession.class);
}
 
Example 29
Source Project: flowable-engine   Source File: CreateAttachmentCmd.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Attachment execute(CommandContext commandContext) {

    verifyParameters(commandContext);

    AttachmentEntity attachment = new AttachmentEntity();
    attachment.setName(attachmentName);
    attachment.setDescription(attachmentDescription);
    attachment.setType(attachmentType);
    attachment.setTaskId(taskId);
    attachment.setProcessInstanceId(processInstanceId);
    attachment.setUrl(url);
    attachment.setUserId(Authentication.getAuthenticatedUserId());
    attachment.setTime(commandContext.getProcessEngineConfiguration().getClock().getCurrentTime());

    DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
    dbSqlSession.insert(attachment);

    if (content != null) {
        byte[] bytes = IoUtil.readInputStream(content, attachmentName);
        ByteArrayEntity byteArray = ByteArrayEntity.createAndInsert(bytes);
        attachment.setContentId(byteArray.getId());
        attachment.setContent(byteArray);
    }

    commandContext.getHistoryManager()
            .createAttachmentComment(taskId, processInstanceId, attachmentName, true);

    if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        // Forced to fetch the process-instance to associate the right process definition
        String processDefinitionId = null;
        if (attachment.getProcessInstanceId() != null) {
            ExecutionEntity process = commandContext.getExecutionEntityManager().findExecutionById(processInstanceId);
            if (process != null) {
                processDefinitionId = process.getProcessDefinitionId();
            }
        }

        commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_CREATED, attachment, processInstanceId, processInstanceId, processDefinitionId));
        commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_INITIALIZED, attachment, processInstanceId, processInstanceId, processDefinitionId));
    }

    return attachment;
}
 
Example 30
Source Project: activiti6-boot2   Source File: DbUpgradeStep.java    License: Apache License 2.0 votes vote down vote up
void execute(DbSqlSession dbSqlSession) throws Exception;