DbRepositorySession.java 8.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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.util.HashMap;
import java.util.List;
import java.util.Map;

20 21 22 23
import org.activiti.engine.ProcessDefinition;
import org.activiti.engine.ProcessInstance;
import org.activiti.engine.impl.ProcessDefinitionQueryImpl;
import org.activiti.engine.impl.ProcessInstanceQueryImpl;
24
import org.activiti.engine.impl.cfg.RepositorySession;
25 26
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.interceptor.Session;
27
import org.activiti.engine.impl.persistence.repository.Deployer;
28
import org.activiti.engine.impl.persistence.repository.DeploymentEntity;
29
import org.activiti.engine.impl.persistence.repository.ProcessDefinitionEntity;
30
import org.activiti.engine.impl.persistence.repository.ResourceEntity;
31 32 33 34 35 36 37


/**
 * @author Tom Baeyens
 */
public class DbRepositorySession implements Session, RepositorySession {

38
  protected DbRepositorySessionFactory dbRepositorySessionFactory;
39 40
  protected DbSqlSession dbSqlSession;
  
41 42
  public DbRepositorySession(DbRepositorySessionFactory dbRepositorySessionFactory) {
    this.dbRepositorySessionFactory = dbRepositorySessionFactory;
43
    this.dbSqlSession = CommandContext
44
      .getCurrent()
45 46 47
      .getSession(DbSqlSession.class);
  }

48
  public void deployNew(DeploymentEntity deployment) {
49
    dbSqlSession.insert(deployment);
50 51 52 53
    for (ResourceEntity resource: deployment.getResources().values()) {
      resource.setDeploymentId(deployment.getId());
      dbSqlSession.insert(resource);
    }
54 55 56
    for (Deployer deployer: dbRepositorySessionFactory.getDeployers()) {
      List<ProcessDefinitionEntity> processDefinitions = deployer.deploy(deployment);
      for (ProcessDefinitionEntity processDefinition : processDefinitions) {
57
        processDefinition.setDeploymentId(deployment.getId());
58 59 60
        dbSqlSession.insert(processDefinition);
        addToProcessDefinitionCache(processDefinition);
      }
61
    }
62 63
  }

64
  public void deployExisting(DeploymentEntity deployment) {
65 66 67
    for (Deployer deployer: dbRepositorySessionFactory.getDeployers()) {
      List<ProcessDefinitionEntity> processDefinitions = deployer.deploy(deployment);
      for (ProcessDefinitionEntity processDefinition : processDefinitions) {
68 69
        String deploymentId = deployment.getId();
        processDefinition.setDeploymentId(deploymentId);
70 71 72 73 74
        ProcessDefinitionEntity persistedProcessDefinition = findProcessDefinitionByDeploymentAndKey(deploymentId, processDefinition.getKey());
        processDefinition.setId(persistedProcessDefinition.getId());
        processDefinition.setVersion(persistedProcessDefinition.getVersion());
        addToProcessDefinitionCache(processDefinition);
      }
75
    }
76 77
  }

78 79 80 81 82 83
  protected void addToProcessDefinitionCache(ProcessDefinitionEntity processDefinition) {
    Map<String, ProcessDefinitionEntity> processDefinitionCache = dbRepositorySessionFactory.getProcessDefinitionCache();
    String processDefinitionId = processDefinition.getId();
    processDefinitionCache.put(processDefinitionId, processDefinition);
  }

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  public void deleteDeployment(String deploymentId, boolean cascade) {
    if (cascade) {
      CommandContext commandContext = CommandContext.getCurrent();
      List<ProcessDefinition> processDefinitions = new ProcessDefinitionQueryImpl()
        .deploymentId(deploymentId)
        .executeList(commandContext, null);
      
      for (ProcessDefinition processDefinition: processDefinitions) {
        List<ProcessInstance> processInstances = new ProcessInstanceQueryImpl()
          .processDefinitionId(processDefinition.getId())
          .executeList(commandContext, null);
        
        for (ProcessInstance processInstance: processInstances) {
          commandContext
            .getRuntimeSession()
99
            .endProcessInstance(processInstance.getId());
100 101 102
        }
      }
    }
103 104
    dbSqlSession.delete("deleteProcessDefinitionsByDeploymentId", deploymentId);
    dbSqlSession.delete("deleteResourcesByDeploymentId", deploymentId);
105
    dbSqlSession.delete("deleteDeployment", deploymentId);
106
  }
107 108 109 110
  
  public DeploymentEntity findDeploymentById(String deploymentId) {
    return (DeploymentEntity) dbSqlSession.selectOne("selectDeploymentById", deploymentId);
  }
111

112 113 114 115 116
  public ResourceEntity findResourceByDeploymentIdAndResourceName(String deploymentId, String resourceName) {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("deploymentId", deploymentId);
    params.put("resourceName", resourceName);
    return (ResourceEntity) dbSqlSession.selectOne("selectResourceByDeploymentIdAndResourceName", params);
117 118
  }

119 120 121 122
  @SuppressWarnings("unchecked")
  public List<ResourceEntity> findResourcesByDeploymentId(String deploymentId) {
    return dbSqlSession.selectList("selectResourcesByDeploymentId", deploymentId);
  }
123 124 125 126 127
  
  @SuppressWarnings("unchecked")
  public List<String> findDeploymentResourceNames(String deploymentId) {
    return dbSqlSession.selectList("selectResourceNamesByDeploymentId", deploymentId);
  }
128

129 130 131 132 133 134 135 136 137
  @SuppressWarnings("unchecked")
  public List<DeploymentEntity> findDeployments() {
    return (List<DeploymentEntity>) dbSqlSession.selectList("selectDeployments");
  };

  public DeploymentEntity findLatestDeploymentByName(String deploymentName) {
    return (DeploymentEntity) dbSqlSession.selectOne("selectLatestDeploymentByName", deploymentName);
  }

138
  @SuppressWarnings("unchecked")
139
  public List<ProcessDefinitionEntity> findProcessDefinitions() {
140 141 142
    return dbSqlSession.selectList("selectProcessDefinitions");
  }

143 144 145 146 147
  public ProcessDefinitionEntity findProcessDefinitionByDeploymentAndKey(String deploymentId, String processDefinitionKey) {
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("deploymentId", deploymentId);
    parameters.put("processDefinitionKey", processDefinitionKey);
    return (ProcessDefinitionEntity) dbSqlSession.selectOne("selectProcessDefinitionByDeploymentAndKey", parameters);
148
  }
149

150 151 152 153 154
  @SuppressWarnings("unchecked")
  public List<ProcessDefinitionEntity> findProcessDefinitionsByDeploymentId(String deploymentId) {
    return dbSqlSession.selectList("selectProcessDefinitionsByDeploymentId", deploymentId);
  }

155 156 157 158
  public ProcessDefinitionEntity findProcessDefinitionById(String processDefinitionId) {
    return (ProcessDefinitionEntity) dbSqlSession.selectOne("selectProcessDefinitionById", processDefinitionId);
  }

159 160 161 162
  public ProcessDefinitionEntity findDeployedLatestProcessDefinitionByKey(String processDefinitionKey) {
    ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) dbSqlSession.selectOne("selectLatestProcessDefinitionByKey", processDefinitionKey);
    processDefinition = resolveProcessDefinition(processDefinition);
    return processDefinition;
163 164
  }

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  public ProcessDefinitionEntity findDeployedProcessDefinitionById(String processDefinitionId) {
    ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) dbSqlSession.selectOne("selectProcessDefinitionById", processDefinitionId);
    processDefinition = resolveProcessDefinition(processDefinition);
    return processDefinition;
  }

  protected ProcessDefinitionEntity resolveProcessDefinition(ProcessDefinitionEntity processDefinition) {
    String processDefinitionId = processDefinition.getId();
    String deploymentId = processDefinition.getDeploymentId();
    processDefinition = dbRepositorySessionFactory.getProcessDefinitionCache().get(processDefinitionId);
    if (processDefinition==null) {
      DeploymentEntity deployment = findDeploymentById(deploymentId);
      deployExisting(deployment);
      processDefinition = dbRepositorySessionFactory.getProcessDefinitionCache().get(processDefinitionId);
    }
    return processDefinition;
181
  }
182
  
183 184 185 186
  public void close() {
  }

  public void flush() {
187
  }
188
}