/* 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.entity;

import java.util.List;

import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.HistoricProcessInstanceQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.AbstractHistoricManager;
import org.activiti.engine.repository.ProcessDefinition;


/**
 * @author Tom Baeyens
 */
public class HistoricProcessInstanceManager extends AbstractHistoricManager {

  public HistoricProcessInstanceEntity findHistoricProcessInstance(String processInstanceId) {
    return (HistoricProcessInstanceEntity) getPersistenceSession().selectById(HistoricProcessInstanceEntity.class, processInstanceId);
  }

  public void deleteHistoricProcessInstancesByProcessDefinition(ProcessDefinition processDefinition) {
    List<HistoricProcessInstance> historicProcessInstances = getPersistenceSession()
      .createHistoricProcessInstanceQuery()
      .processDefinitionId(processDefinition.getId())
      .list();
    
    for (HistoricProcessInstance historicProcessInstance: historicProcessInstances) {
      deleteHistoricProcessInstance(historicProcessInstance.getId());
    }
  }
  
  public void deleteHistoricProcessInstance(String historicProcessInstanceId) {
    CommandContext commandContext = Context.getCommandContext();
    
    commandContext
      .getHistoricDetailManager()
      .deleteHistoricDetailsByProcessInstanceId(historicProcessInstanceId);
    
    commandContext
      .getHistoricActivityInstanceManager()
      .deleteHistoricActivityInstancesByProcessInstanceId(historicProcessInstanceId);

    commandContext
      .getHistoricTaskInstanceManager()
      .deleteHistoricTaskInstancesByProcessInstanceId(historicProcessInstanceId);

    if (historyLevel>ProcessEngineConfigurationImpl.HISTORYLEVEL_NONE) {
      getPersistenceSession().delete(HistoricProcessInstanceEntity.class, historicProcessInstanceId);
    }
  }
  
  public long findHistoricProcessInstanceCountByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery) {
    return (Long) getPersistenceSession().selectOne("selectHistoricProcessInstanceCountByQueryCriteria", historicProcessInstanceQuery);
  }

  @SuppressWarnings("unchecked")
  public List<HistoricProcessInstance> findHistoricProcessInstancesByQueryCriteria(HistoricProcessInstanceQueryImpl historicProcessInstanceQuery, Page page) {
    return getPersistenceSession().selectList("selectHistoricProcessInstancesByQueryCriteria", historicProcessInstanceQuery, page);
  }


}

@@ -1547,24 +1547,99 @@
 finition) %7B%0A
+    if (historyLevel%3EProcessEngineConfigurationImpl.HISTORYLEVEL_NONE) %7B%0A  
     List%3CHis
@@ -1710,23 +1710,16 @@
 ession()
-%0A      
 .createH
@@ -1747,23 +1747,16 @@
 eQuery()
-%0A      
 .process
@@ -1768,16 +1768,31 @@
 itionId(
+%0A              
 processD
@@ -1809,23 +1809,16 @@
 getId())
-%0A      
 .list();
@@ -1822,19 +1822,16 @@
 ();%0A    
-%0A  
   for (H
@@ -1876,16 +1876,17 @@
 Instance
+ 
 : histor
@@ -1913,16 +1913,18 @@
 %7B%0A      
+ 
+ 
 deleteHi
@@ -1979,30 +1979,38 @@
 tId());%0A    
-%7D%0A
+  %7D%0A  
   %7D%0A
+  %7D%0A
   %0A  public 
@@ -2076,24 +2076,99 @@
 stanceId) %7B%0A
+    if (historyLevel%3EProcessEngineConfigurationImpl.HISTORYLEVEL_NONE) %7B%0A  
     CommandC
@@ -2220,25 +2220,29 @@
 text();%0A    
-%0A
+  %0A  
     commandC
@@ -2246,32 +2246,34 @@
 ndContext%0A      
+  
 .getHistoricDeta
@@ -2276,32 +2276,34 @@
 DetailManager()%0A
+  
       .deleteHis
@@ -2362,25 +2362,29 @@
 nceId);%0A    
-%0A
+  %0A  
     commandC
@@ -2388,32 +2388,34 @@
 ndContext%0A      
+ 
+ 
 .getHistoricActi
@@ -2428,32 +2428,34 @@
 stanceManager()%0A
+  
       .deleteHis
@@ -2521,24 +2521,26 @@
 stanceId);%0A%0A
+  
     commandC
@@ -2548,24 +2548,26 @@
 ntext%0A      
+  
 .getHistoric
@@ -2584,24 +2584,26 @@
 ceManager()%0A
+  
       .delet
@@ -2677,81 +2677,8 @@
 );%0A%0A
-    if (historyLevel%3EProcessEngineConfigurationImpl.HISTORYLEVEL_NONE) %7B%0A
     

fixing history problem with history level is none