uap.workflow.engine.mgr.TableDataManager.java Source code

Java tutorial

Introduction

Here is the source code for uap.workflow.engine.mgr.TableDataManager.java

Source

/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uap.workflow.engine.mgr;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.apache.ibatis.session.RowBounds;

import uap.workflow.engine.exception.WorkflowException;
import uap.workflow.engine.management.TableMetaData;
import uap.workflow.engine.management.TablePage;
import uap.workflow.engine.persistence.AbstractManager;
import uap.workflow.engine.query.TablePageQueryImpl;

/**
 * @author Tom Baeyens
 */
public class TableDataManager extends AbstractManager {
    private static Logger log = Logger.getLogger(TableDataManager.class.getName());

    public Map<String, Long> getTableCount() {
        Map<String, Long> tableCount = new HashMap<String, Long>();
        try {
            for (String tableName : getTablesPresentInDatabase()) {
                tableCount.put(tableName, getTableCount(tableName));
            }
            log.fine("Number of rows per activiti table: " + tableCount);
        } catch (Exception e) {
            throw new WorkflowException("couldn't get table counts", e);
        }
        return tableCount;
    }

    @SuppressWarnings("static-access")
    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.fine("retrieving activiti tables from jdbc metadata");
                String tableNameFilter = "ACT_%";
                if ("postgres".equals(getDbSqlSession().getDbSqlSessionFactory().getDatabaseType())) {
                    tableNameFilter = "act_%";
                }
                tables = databaseMetaData.getTables(null, null, tableNameFilter,
                        getDbSqlSession().JDBC_METADATA_TABLE_TYPES);
                while (tables.next()) {
                    String tableName = tables.getString("TABLE_NAME");
                    tableName = tableName.toUpperCase();
                    tableNames.add(tableName);
                    log.fine("  retrieved activiti table name " + tableName);
                }
            } finally {
                tables.close();
            }
        } catch (Exception e) {
            throw new WorkflowException("couldn't get activiti table names using metadata: " + e.getMessage(), e);
        }
        return tableNames;
    }

    protected long getTableCount(String tableName) {
        log.fine("selecting table count for " + tableName);
        Long count = (Long) getDbSqlSession().selectOne("selectTableCount",
                Collections.singletonMap("tableName", tableName));
        return count;
    }

    @SuppressWarnings("unchecked")
    public TablePage getTablePage(TablePageQueryImpl tablePageQuery, int firstResult, int maxResults) {
        TablePage tablePage = new TablePage();
        List<Map<String, Object>> tableData = (List<Map<String, Object>>) getDbSqlSession().getSqlSession()
                .selectList("selectTableData", tablePageQuery, new RowBounds(firstResult, maxResults));
        tablePage.setTableName(tablePageQuery.getTableName());
        tablePage.setTotal(getTableCount(tablePageQuery.getTableName()));
        tablePage.setRows(tableData);
        tablePage.setFirstResult(firstResult);
        return tablePage;
    }

    public TableMetaData getTableMetaData(String tableName) {
        TableMetaData result = new TableMetaData();
        try {
            result.setTableName(tableName);
            DatabaseMetaData metaData = getDbSqlSession().getSqlSession().getConnection().getMetaData();
            if ("postgres".equals(getDbSqlSession().getDbSqlSessionFactory().getDatabaseType())) {
                tableName = tableName.toLowerCase();
            }
            ResultSet resultSet = metaData.getColumns(null, null, tableName, null);
            while (resultSet.next()) {
                String name = resultSet.getString("COLUMN_NAME").toUpperCase();
                String type = resultSet.getString("TYPE_NAME").toUpperCase();
                result.addColumnMetaData(name, type);
            }
        } catch (SQLException e) {
            throw new WorkflowException("Could not retrieve database metadata: " + e.getMessage());
        }
        if (result.getColumnNames().size() == 0) {
            // According to API, when a table doesn't exist, null should be
            // returned
            result = null;
        }
        return result;
    }
}