Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* 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.db;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.ActivitiOptimisticLockingException;
import org.activiti.engine.ActivitiWrongDbException;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.impl.DeploymentQueryImpl;
import org.activiti.engine.impl.ExecutionQueryImpl;
import org.activiti.engine.impl.HistoricActivityInstanceQueryImpl;
import org.activiti.engine.impl.HistoricDetailQueryImpl;
import org.activiti.engine.impl.HistoricProcessInstanceQueryImpl;
import org.activiti.engine.impl.HistoricTaskInstanceQueryImpl;
import org.activiti.engine.impl.HistoricVariableInstanceQueryImpl;
import org.activiti.engine.impl.JobQueryImpl;
import org.activiti.engine.impl.ModelQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.ProcessDefinitionQueryImpl;
import org.activiti.engine.impl.ProcessInstanceQueryImpl;
import org.activiti.engine.impl.TaskQueryImpl;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.db.upgrade.DbUpgradeStep;
import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.persistence.cache.CachedEntity;
import org.activiti.engine.impl.persistence.cache.EntityCache;
import org.activiti.engine.impl.persistence.entity.Entity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.PropertyEntity;
import org.activiti.engine.impl.util.IoUtil;
import org.activiti.engine.impl.util.ReflectUtil;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*/
public class DbSqlSession implements Session {
private static final Logger log = LoggerFactory.getLogger(DbSqlSession.class);
protected static final Pattern CLEAN_VERSION_REGEX = Pattern.compile("\\d\\.\\d*");
protected static final String LAST_V5_VERSION = "5.99.0.0";
protected static final List ACTIVITI_VERSIONS = new ArrayList();
static {
/* Previous */
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.7"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.8"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.9"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.10"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.11"));
// 5.12.1 was a bugfix release on 5.12 and did NOT change the version in ACT_GE_PROPERTY
// On top of that, DB2 create script for 5.12.1 was shipped with a 'T' suffix ...
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.12",
Arrays.asList("5.12.1",
"5.12T")));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.13"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.14"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.15"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.15.1"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.1"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.2-SNAPSHOT"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.2"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.3.0"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.4.0"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.17.0.0"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.17.0.1"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.17.0.2"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.18.0.0"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.18.0.1"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.20.0.0"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.20.0.1"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.20.0.2"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("5.21.0.0"));
/*
* Version 5.18.0.1 is the latest v5 version in the list here, although if you would look at the v5 code,
* you'll see there are a few other releases afterwards.
*
* The reasoning is as follows: after 5.18.0.1, no database changes were done anymore.
* And if there would be database changes, they would have been part of both 5.x _and_ 6.x upgrade scripts.
* The logic below will assume it's one of these releases in case it isn't found in the list here
* and do the upgrade from the 'virtual' release 5.99.0.0 to make sure th v6 changes are applied.
*/
// This is the latest version of the 5 branch. It's a 'virtual' version cause it doesn't exist, but it is
// there to make sure all previous version can upgrade to the 6 version correctly.
ACTIVITI_VERSIONS.add(new ActivitiVersion(LAST_V5_VERSION));
// Version 6
ACTIVITI_VERSIONS.add(new ActivitiVersion("6.0.0.0"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("6.0.0.1"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("6.0.0.2"));
ACTIVITI_VERSIONS.add(new ActivitiVersion("6.0.0.3"));
/* Current */
ACTIVITI_VERSIONS.add(new ActivitiVersion(ProcessEngine.VERSION));
}
protected SqlSession sqlSession;
protected DbSqlSessionFactory dbSqlSessionFactory;
protected EntityCache entityCache;
protected Map, Map> insertedObjects
= new HashMap, Map>();
protected Map, Map> deletedObjects
= new HashMap, Map>();
protected Map, List> bulkDeleteOperations
= new HashMap, List>();
protected List updatedObjects = new ArrayList();
protected String connectionMetadataDefaultCatalog;
protected String connectionMetadataDefaultSchema;
public DbSqlSession(DbSqlSessionFactory dbSqlSessionFactory,
EntityCache entityCache) {
this.dbSqlSessionFactory = dbSqlSessionFactory;
this.sqlSession = dbSqlSessionFactory.getSqlSessionFactory().openSession();
this.entityCache = entityCache;
this.connectionMetadataDefaultCatalog = dbSqlSessionFactory.getDatabaseCatalog();
this.connectionMetadataDefaultSchema = dbSqlSessionFactory.getDatabaseSchema();
}
public DbSqlSession(DbSqlSessionFactory dbSqlSessionFactory,
EntityCache entityCache,
Connection connection,
String catalog,
String schema) {
this.dbSqlSessionFactory = dbSqlSessionFactory;
this.sqlSession = dbSqlSessionFactory.getSqlSessionFactory().openSession(connection); // Note the use of connection param here, different from other constructor
this.entityCache = entityCache;
this.connectionMetadataDefaultCatalog = catalog;
this.connectionMetadataDefaultSchema = schema;
}
// insert ///////////////////////////////////////////////////////////////////
public void insert(Entity entity) {
if (entity.getId() == null) {
String id = dbSqlSessionFactory.getIdGenerator().getNextId();
entity.setId(id);
}
Class extends Entity> clazz = entity.getClass();
if (!insertedObjects.containsKey(clazz)) {
insertedObjects.put(clazz,
new LinkedHashMap()); // order of insert is important, hence LinkedHashMap
}
insertedObjects.get(clazz).put(entity.getId(),
entity);
entityCache.put(entity,
false); // False -> entity is inserted, so always changed
entity.setInserted(true);
}
// update
// ///////////////////////////////////////////////////////////////////
public void update(Entity entity) {
entityCache.put(entity,
false); // false -> we don't store state, meaning it will always be seen as changed
entity.setUpdated(true);
}
public int update(String statement,
Object parameters) {
String updateStatement = dbSqlSessionFactory.mapStatement(statement);
return getSqlSession().update(updateStatement,
parameters);
}
// delete
// ///////////////////////////////////////////////////////////////////
/**
* Executes a {@link BulkDeleteOperation}, with the sql in the statement parameter.
* The passed class determines when this operation will be executed: it will be executed
* when the particular class has passed in the {@link EntityDependencyOrder}.
*/
public void delete(String statement,
Object parameter,
Class extends Entity> entityClass) {
if (!bulkDeleteOperations.containsKey(entityClass)) {
bulkDeleteOperations.put(entityClass,
new ArrayList(1));
}
bulkDeleteOperations.get(entityClass).add(new BulkDeleteOperation(dbSqlSessionFactory.mapStatement(statement),
parameter));
}
public void delete(Entity entity) {
Class extends Entity> clazz = entity.getClass();
if (!deletedObjects.containsKey(clazz)) {
deletedObjects.put(clazz,
new LinkedHashMap()); // order of insert is important, hence LinkedHashMap
}
deletedObjects.get(clazz).put(entity.getId(),
entity);
entity.setDeleted(true);
}
// select
// ///////////////////////////////////////////////////////////////////
@SuppressWarnings({"rawtypes"})
public List selectList(String statement) {
return selectList(statement,
null,
0,
Integer.MAX_VALUE);
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
Object parameter) {
return selectList(statement,
parameter,
0,
Integer.MAX_VALUE);
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
Object parameter,
boolean useCache) {
return selectList(statement,
parameter,
0,
Integer.MAX_VALUE,
useCache);
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
Object parameter,
Page page) {
return selectList(statement,
parameter,
page,
true);
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
Object parameter,
Page page,
boolean useCache) {
if (page != null) {
return selectList(statement,
parameter,
page.getFirstResult(),
page.getMaxResults(),
useCache);
} else {
return selectList(statement,
parameter,
0,
Integer.MAX_VALUE,
useCache);
}
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
ListQueryParameterObject parameter,
Page page) {
return selectList(statement,
parameter,
page,
true);
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
ListQueryParameterObject parameter,
Page page,
boolean useCache) {
ListQueryParameterObject parameterToUse = parameter;
if (parameterToUse == null) {
parameterToUse = new ListQueryParameterObject();
}
if (page != null) {
parameterToUse.setFirstResult(page.getFirstResult());
parameterToUse.setMaxResults(page.getMaxResults());
}
return selectList(statement,
parameterToUse,
useCache);
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
Object parameter,
int firstResult,
int maxResults) {
return selectList(statement,
parameter,
firstResult,
maxResults,
true);
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
Object parameter,
int firstResult,
int maxResults,
boolean useCache) {
return selectList(statement,
new ListQueryParameterObject(parameter,
firstResult,
maxResults),
useCache);
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
ListQueryParameterObject parameter) {
return selectList(statement,
parameter,
true);
}
@SuppressWarnings("rawtypes")
public List selectList(String statement,
ListQueryParameterObject parameter,
boolean useCache) {
return selectListWithRawParameter(statement,
parameter,
parameter.getFirstResult(),
parameter.getMaxResults(),
useCache);
}
@SuppressWarnings("rawtypes")
public List selectListWithRawParameter(String statement,
Object parameter,
int firstResult,
int maxResults) {
return selectListWithRawParameter(statement,
parameter,
firstResult,
maxResults,
true);
}
@SuppressWarnings({"rawtypes", "unchecked"})
public List selectListWithRawParameter(String statement,
Object parameter,
int firstResult,
int maxResults,
boolean useCache) {
statement = dbSqlSessionFactory.mapStatement(statement);
if (firstResult == -1 || maxResults == -1) {
return Collections.EMPTY_LIST;
}
List loadedObjects = sqlSession.selectList(statement,
parameter);
if (useCache) {
return cacheLoadOrStore(loadedObjects);
} else {
return loadedObjects;
}
}
@SuppressWarnings({"rawtypes"})
public List selectListWithRawParameterWithoutFilter(String statement,
Object parameter,
int firstResult,
int maxResults) {
statement = dbSqlSessionFactory.mapStatement(statement);
if (firstResult == -1 || maxResults == -1) {
return Collections.EMPTY_LIST;
}
return sqlSession.selectList(statement,
parameter);
}
public Object selectOne(String statement,
Object parameter) {
statement = dbSqlSessionFactory.mapStatement(statement);
Object result = sqlSession.selectOne(statement,
parameter);
if (result instanceof Entity) {
Entity loadedObject = (Entity) result;
result = cacheLoadOrStore(loadedObject);
}
return result;
}
public T selectById(Class entityClass,
String id) {
return selectById(entityClass,
id,
true);
}
@SuppressWarnings("unchecked")
public T selectById(Class entityClass,
String id,
boolean useCache) {
T entity = null;
if (useCache) {
entity = entityCache.findInCache(entityClass,
id);
if (entity != null) {
return entity;
}
}
String selectStatement = dbSqlSessionFactory.getSelectStatement(entityClass);
selectStatement = dbSqlSessionFactory.mapStatement(selectStatement);
entity = (T) sqlSession.selectOne(selectStatement,
id);
if (entity == null) {
return null;
}
entityCache.put(entity,
true); // true -> store state so we can see later if it is updated later on
return entity;
}
// internal session cache
// ///////////////////////////////////////////////////
@SuppressWarnings("rawtypes")
protected List cacheLoadOrStore(List