DbManagementSession.java 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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 org.activiti.engine.impl.persistence.db;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
23
import java.util.logging.Logger;
24 25 26 27 28

import org.activiti.engine.ActivitiException;
import org.activiti.engine.ActivitiOptimisticLockingException;
import org.activiti.engine.TableMetaData;
import org.activiti.engine.TablePage;
29
import org.activiti.engine.impl.TablePageQueryImpl;
30 31 32 33
import org.activiti.engine.impl.cfg.ManagementSession;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.persistence.repository.PropertyEntity;
34
import org.apache.ibatis.session.RowBounds;
35 36 37 38 39 40


/**
 * @author Tom Baeyens
 */
public class DbManagementSession implements ManagementSession, Session {
41 42 43 44 45 46 47
  
  private static Logger log = Logger.getLogger(DbManagementSession.class.getName());

  protected static String[] tableNames = new String[]{
    "ACT_PROPERTY",
    "ACT_BYTEARRAY",
    "ACT_DEPLOYMENT",
48
    "ACT_EXECUTION",
49 50 51 52 53 54 55 56 57 58 59 60
    "ACT_ID_GROUP",
    "ACT_ID_MEMBERSHIP",
    "ACT_ID_USER",
    "ACT_JOB",
    "ACT_PROCESSDEFINITION",
    "ACT_TASK",
    "ACT_TASKINVOLVEMENT",
    "ACT_VARIABLE",
    "ACT_H_PROCINST",
    "ACT_H_ACTINST"
  };

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

  protected DbSqlSession dbSqlSession;

  public DbManagementSession() {
    this.dbSqlSession = CommandContext.getCurrentSession(DbSqlSession.class);
  }
  
  public Map<String, Long> getTableCount() {
    Map<String, Long> tableCount = new HashMap<String, Long>();
    try {
      for (String tableName: tableNames) {
        tableCount.put(tableName, getTableCount(tableName));
      }
    } catch (Exception e) {
      throw new ActivitiException("couldn't get table counts", e);
    }
    return tableCount;
  }

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

  @SuppressWarnings("unchecked")
88
 public TablePage getTablePage(TablePageQueryImpl tablePageQuery, int firstResult, int maxResults) {
89 90 91

    TablePage tablePage = new TablePage();

92 93 94
    List<Map<String, Object>> tableData = (List<Map<String, Object>>) dbSqlSession
      .getSqlSession()
      .selectList("selectTableData", tablePageQuery, new RowBounds(firstResult, maxResults));
95

96 97
    tablePage.setTableName(tablePageQuery.getTableName());
    tablePage.setTotal(getTableCount(tablePageQuery.getTableName()));
98
    tablePage.setRows(tableData);
99 100
    tablePage.setFirstResult(firstResult);
    
101 102 103 104 105 106 107
    return tablePage;
  }

  public TableMetaData getTableMetaData(String tableName) {
    TableMetaData result = new TableMetaData();
    try {
      result.setTableName(tableName);
108 109 110 111
      DatabaseMetaData metaData = dbSqlSession
        .getSqlSession()
        .getConnection()
        .getMetaData();
112 113 114 115 116 117 118 119 120 121 122 123 124
      ResultSet resultSet = metaData.getColumns(null, null, tableName, null);
      while(resultSet.next()) {
        String name = resultSet.getString("COLUMN_NAME");
        String type = resultSet.getString("TYPE_NAME");
        result.addColumnMetaData(name, type);
      }
    } catch (SQLException e) {
      throw new ActivitiException("Could not retrieve database metadata: " + e.getMessage());
    }

    return result;
  }

125
  public IdBlock getNextIdBlock() {
126 127
    String statement = dbSqlSession.getDbSqlSessionFactory().mapStatement("selectProperty");
    PropertyEntity property = (PropertyEntity) dbSqlSession.selectOne(statement, "next.dbid");
128
    long oldValue = Long.parseLong(property.getValue());
129 130 131 132 133 134 135
    
    long idBlockSize = CommandContext
      .getCurrent()
      .getProcessEngineConfiguration()
      .getIdBlockSize();
    
    long newValue = oldValue+idBlockSize;
136 137 138 139 140
    Map<String, Object> updateValues = new HashMap<String, Object>();
    updateValues.put("name", property.getName());
    updateValues.put("revision", property.getDbversion());
    updateValues.put("newRevision", property.getDbversion()+1);
    updateValues.put("value", Long.toString(newValue));
141
    int rowsUpdated = dbSqlSession.getSqlSession().update("updateProperty", updateValues);
142 143 144 145 146 147 148 149 150 151 152 153
    if (rowsUpdated!=1) {
      throw new ActivitiOptimisticLockingException("couldn't get next block of dbids");
    }
    return new IdBlock(oldValue, newValue-1);
  }

  public void close() {
  }

  public void flush() {
  }
}