Skip to content

Commit

Permalink
support cascade remove process/order record
Browse files Browse the repository at this point in the history
  • Loading branch information
snakerflow committed Dec 24, 2014
1 parent 2573cfa commit e732f2f
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 18 deletions.
18 changes: 18 additions & 0 deletions snaker-core/src/main/java/org/snaker/engine/DBAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public interface DBAccess {
* @param process 流程定义对象
*/
public void updateProcess(Process process);

/**
* 删除流程定义对象
* @param process 流程定义对象
*/
public void deleteProcess(Process process);

/**
* 更新流程定义类别
Expand Down Expand Up @@ -146,6 +152,18 @@ public interface DBAccess {
*/
public void saveHistory(HistoryTask task);

/**
* 删除历史实例记录
* @param historyOrder 历史实例
*/
public void deleteHistoryOrder(HistoryOrder historyOrder);

/**
* 删除历史任务记录
* @param historyTask 历史任务
*/
public void deleteHistoryTask(HistoryTask historyTask);

/**
* 更新实例变量(包括历史实例表)
* @param order 实例对象
Expand Down
11 changes: 11 additions & 0 deletions snaker-core/src/main/java/org/snaker/engine/IOrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,15 @@ public interface IOrderService {
* @param actorId 参与者id
*/
void deleteCCOrder(String orderId, String actorId);

/**
* 谨慎使用.数据恢复非常痛苦,你懂得~~
* 级联删除指定流程实例的所有数据:
* 1.wf_order,wf_hist_order
* 2.wf_task,wf_hist_task
* 3.wf_task_actor,wf_hist_task_actor
* 4.wf_cc_order
* @param id
*/
void cascadeRemove(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public interface IProcessService {
* 2.wf_order,wf_hist_order
* 3.wf_task,wf_hist_task
* 4.wf_task_actor,wf_hist_task_actor
* 5.wf_cc_order
* @param id
*/
void cascadeRemove(String id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,32 @@ public abstract class AbstractDBAccess implements DBAccess {

protected static final String PROCESS_INSERT = "insert into wf_process (id,name,display_Name,type,instance_Url,state,version,create_Time,creator) values (?,?,?,?,?,?,?,?,?)";
protected static final String PROCESS_UPDATE = "update wf_process set name=?, display_Name=?,state=?,instance_Url=?,create_Time=?,creator=? where id=? ";
protected static final String PROCESS_DELETE = "delete from wf_process where id = ?";
protected static final String PROCESS_UPDATE_BLOB = "update wf_process set content=? where id=?";
protected static final String PROCESS_UPDATE_TYPE = "update wf_process set type=? where id=?";

protected static final String ORDER_INSERT = "insert into wf_order (id,process_Id,creator,create_Time,parent_Id,parent_Node_Name,expire_Time,last_Update_Time,last_Updator,order_No,variable,version) values (?,?,?,?,?,?,?,?,?,?,?,?)";
protected static final String ORDER_UPDATE = "update wf_order set last_Updator=?, last_Update_Time=?, variable = ?, version = version + 1 where id=? and version = ?";
protected static final String ORDER_UPDATE = "update wf_order set last_Updator=?, last_Update_Time=?, variable = ?, expire_Time=?, version = version + 1 where id=? and version = ?";
protected static final String ORDER_DELETE = "delete from wf_order where id = ?";
protected static final String ORDER_HISTORY_INSERT = "insert into wf_hist_order (id,process_Id,order_State,creator,create_Time,end_Time,parent_Id,expire_Time,order_No,variable) values (?,?,?,?,?,?,?,?,?,?)";
protected static final String ORDER_HISTORY_UPDATE = "update wf_hist_order set order_State = ?, end_Time = ?, variable = ? where id = ? ";
protected static final String ORDER_DELETE = "delete from wf_order where id = ?";
protected static final String ORDER_HISTORY_DELETE = "delete from wf_hist_order where id = ?";

protected static final String CCORDER_INSERT = "insert into wf_cc_order (order_Id, actor_Id, creator, create_Time, status) values (?, ?, ?, ?, ?)";
protected static final String CCORDER_UPDATE = "update wf_cc_order set status = ?, finish_Time = ? where order_Id = ? and actor_Id = ?";
protected static final String CCORDER_DELETE = "delete from wf_cc_order where order_Id = ? and actor_Id = ?";

protected static final String TASK_INSERT = "insert into wf_task (id,order_Id,task_Name,display_Name,task_Type,perform_Type,operator,create_Time,finish_Time,expire_Time,action_Url,parent_Task_Id,variable,version) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
protected static final String TASK_UPDATE = "update wf_task set finish_Time=?, operator=?, variable=?, version = version + 1 where id=? and version = ?";
protected static final String TASK_HISTORY_INSERT = "insert into wf_hist_task (id,order_Id,task_Name,display_Name,task_Type,perform_Type,task_State,operator,create_Time,finish_Time,expire_Time,action_Url,parent_Task_Id,variable) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
protected static final String TASK_UPDATE = "update wf_task set finish_Time=?, operator=?, variable=?, expire_Time=?, action_Url=?, version = version + 1 where id=? and version = ?";
protected static final String TASK_DELETE = "delete from wf_task where id = ?";
protected static final String TASK_HISTORY_INSERT = "insert into wf_hist_task (id,order_Id,task_Name,display_Name,task_Type,perform_Type,task_State,operator,create_Time,finish_Time,expire_Time,action_Url,parent_Task_Id,variable) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
protected static final String TASK_HISTORY_DELETE = "delete from wf_hist_task where id = ?";

protected static final String TASK_ACTOR_INSERT = "insert into wf_task_actor (task_Id, actor_Id) values (?, ?)";
protected static final String TASK_ACTOR_HISTORY_INSERT = "insert into wf_hist_task_actor (task_Id, actor_Id) values (?, ?)";
protected static final String TASK_ACTOR_DELETE = "delete from wf_task_actor where task_Id = ?";
protected static final String TASK_ACTOR_REDUCE = "delete from wf_task_actor where task_Id = ? and actor_Id = ?";
protected static final String TASK_ACTOR_HISTORY_INSERT = "insert into wf_hist_task_actor (task_Id, actor_Id) values (?, ?)";
protected static final String TASK_ACTOR_HISTORY_DELETE = "delete from wf_hist_task_actor where task_Id = ?";

protected static final String QUERY_VERSION = "select max(version) from wf_process ";
protected static final String QUERY_PROCESS = "select id,name,display_Name,type,instance_Url,state, content, version,create_Time,creator from wf_process ";
Expand Down Expand Up @@ -194,6 +198,14 @@ public void updateProcess(Process process) {
saveOrUpdate(buildMap(PROCESS_UPDATE, args, type));
}
}

public void deleteProcess(Process process) {
if(!isORM()) {
Object[] args = new Object[]{process.getId()};
int[] type = new int[]{Types.VARCHAR};
saveOrUpdate(buildMap(PROCESS_DELETE, args, type));
}
}

public void updateProcessType(String id, String type) {
if(isORM()) {
Expand Down Expand Up @@ -257,8 +269,8 @@ public void updateTask(Task task) {
if(isORM()) {
saveOrUpdate(buildMap(task, UPDATE));
} else {
Object[] args = new Object[]{task.getFinishTime(), task.getOperator(), task.getVariable(), task.getId(), task.getVersion() };
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER};
Object[] args = new Object[]{task.getFinishTime(), task.getOperator(), task.getVariable(), task.getExpireTime(), task.getActionUrl(), task.getId(), task.getVersion() };
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER};
saveOrUpdate(buildMap(TASK_UPDATE, args, type));
}
}
Expand All @@ -267,8 +279,8 @@ public void updateOrder(Order order) {
if(isORM()) {
saveOrUpdate(buildMap(order, UPDATE));
} else {
Object[] args = new Object[]{order.getLastUpdator(), order.getLastUpdateTime(), order.getVariable(), order.getId(), order.getVersion() };
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER};
Object[] args = new Object[]{order.getLastUpdator(), order.getLastUpdateTime(), order.getVariable(), order.getExpireTime(), order.getId(), order.getVersion() };
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER};
saveOrUpdate(buildMap(ORDER_UPDATE, args, type));
}
}
Expand Down Expand Up @@ -337,6 +349,14 @@ public void updateHistory(HistoryOrder order) {
}
}

public void deleteHistoryOrder(HistoryOrder historyOrder) {
if(!isORM()) {
Object[] args = new Object[]{historyOrder.getId()};
int[] type = new int[]{Types.VARCHAR};
saveOrUpdate(buildMap(ORDER_HISTORY_DELETE, args, type));
}
}

public void saveHistory(HistoryTask task) {
if(isORM()) {
saveOrUpdate(buildMap(task, SAVE));
Expand Down Expand Up @@ -365,6 +385,15 @@ public void saveHistory(HistoryTask task) {
}
}

public void deleteHistoryTask(HistoryTask historyTask) {
if(!isORM()) {
Object[] args = new Object[]{historyTask.getId()};
int[] type = new int[]{Types.VARCHAR};
saveOrUpdate(buildMap(TASK_ACTOR_HISTORY_DELETE, args, type));
saveOrUpdate(buildMap(TASK_HISTORY_DELETE, args, type));
}
}

public void updateOrderVariable(Order order) {
updateOrder(order);
HistoryOrder hist = getHistOrder(order.getId());
Expand Down Expand Up @@ -489,11 +518,17 @@ public Order getOrder(String orderId) {

public List<CCOrder> getCCOrder(String orderId, String... actorIds) {
StringBuilder where = new StringBuilder(QUERY_CCORDER);
where.append(" where status = 1 and order_Id = ? ");
where.append(" and actor_Id in (");
where.append(StringUtils.repeat("?,", actorIds.length));
where.deleteCharAt(where.length() - 1);
where.append(") ");
where.append(" where 1 = 1 ");

if(StringHelper.isNotEmpty(orderId)) {
where.append(" and order_Id = ?");
}
if(actorIds != null && actorIds.length > 0) {
where.append(" and actor_Id in (");
where.append(StringUtils.repeat("?,", actorIds.length));
where.deleteCharAt(where.length() - 1);
where.append(") ");
}
return queryList(CCOrder.class, where.toString(), ArrayUtils.add(actorIds, 0, orderId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public abstract class TransactionInterceptor implements MethodInterceptor {
txMethods.add("withdraw*");
txMethods.add("reject*");
txMethods.add("add*");
txMethods.add("cascade*");
txMethods.add("get*");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void saveOrder(Order order) {
}

/**
* 更新活动实例的last_Updator、last_Update_Time、version、variable
* 更新活动实例的last_Updator、last_Update_Time、expire_Time、version、variable
*/
public void updateOrder(Order order) {
access().updateOrder(order);
Expand Down Expand Up @@ -215,4 +215,35 @@ public Order resume(String orderId) {
}
return order;
}

/**
* 级联删除指定流程实例的所有数据:
* 1.wf_order,wf_hist_order
* 2.wf_task,wf_hist_task
* 3.wf_task_actor,wf_hist_task_actor
* 4.wf_cc_order
* @param id 实例id
*/
public void cascadeRemove(String id) {
HistoryOrder historyOrder = access().getHistOrder(id);
AssertHelper.notNull(historyOrder);
List<Task> activeTasks = access().getActiveTasks(null, new QueryFilter().setOrderId(id));
List<HistoryTask> historyTasks = access().getHistoryTasks(null, new QueryFilter().setOrderId(id));
for(Task task : activeTasks) {
access().deleteTask(task);
}
for(HistoryTask historyTask : historyTasks) {
access().deleteHistoryTask(historyTask);
}
List<CCOrder> ccOrders = access().getCCOrder(id);
for(CCOrder ccOrder : ccOrders) {
access().deleteCCOrder(ccOrder);
}

Order order = access().getOrder(id);
access().deleteHistoryOrder(historyOrder);
if(order != null) {
access().deleteOrder(order);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snaker.engine.Context;
import org.snaker.engine.IProcessService;
import org.snaker.engine.SnakerException;
import org.snaker.engine.access.Page;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.cache.Cache;
import org.snaker.engine.cache.CacheManager;
import org.snaker.engine.cache.CacheManagerAware;
import org.snaker.engine.entity.HistoryOrder;
import org.snaker.engine.entity.Process;
import org.snaker.engine.helper.AssertHelper;
import org.snaker.engine.helper.DateHelper;
Expand Down Expand Up @@ -248,7 +250,14 @@ public void undeploy(String id) {
* 级联删除指定流程定义的所有数据
*/
public void cascadeRemove(String id) {

Process entity = access().getProcess(id);
List<HistoryOrder> historyOrders = access().getHistoryOrders(null, new QueryFilter().setProcessId(id));

for(HistoryOrder historyOrder : historyOrders) {
ServiceContext.getEngine().order().cascadeRemove(historyOrder.getId());
}
access().deleteProcess(entity);
clear(entity);
}

/**
Expand All @@ -269,7 +278,7 @@ public List<Process> getProcesss(Page<Process> page, QueryFilter filter) {

/**
* 缓存实体
* @param entity
* @param entity 流程定义对象
*/
private void cache(Process entity) {
Cache<String, String> nameCache = ensureAvailableNameCache();
Expand All @@ -291,6 +300,20 @@ private void cache(Process entity) {
}
}

/**
* 清除实体
* @param entity 流程定义对象
*/
private void clear(Process entity) {
Cache<String, String> nameCache = ensureAvailableNameCache();
Cache<String, Process> entityCache = ensureAvailableEntityCache();
String processName = entity.getName() + DEFAULT_SEPARATOR + entity.getVersion();
if(nameCache != null && entityCache != null) {
nameCache.remove(entity.getId());
entityCache.remove(processName);
}
}

public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public void saveProcess(Process process) {

getSession().saveOrUpdate(process);
}

public void deleteProcess(Process process) {
getSession().delete(process);
}

public void deleteTask(Task task) {
List<TaskActor> actors = getTaskActorsByTaskId(task.getId());
Expand All @@ -116,7 +120,19 @@ public void deleteTask(Task task) {
public void deleteOrder(Order order) {
getSession().delete(order);
}


public void deleteHistoryOrder(HistoryOrder historyOrder) {
getSession().delete(historyOrder);
}

public void deleteHistoryTask(HistoryTask historyTask) {
List<HistoryTaskActor> actors = getHistTaskActorsByTaskId(historyTask.getId());
for(HistoryTaskActor actor : actors) {
getSession().delete(actor);
}
getSession().delete(historyTask);
}

public void deleteSurrogate(Surrogate surrogate) {
getSession().delete(surrogate);
}
Expand Down

0 comments on commit e732f2f

Please sign in to comment.