
org.snaker.engine.access.AbstractDBAccess Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snaker-core Show documentation
Show all versions of snaker-core Show documentation
snaker-core as open source.
/* Copyright 2012-2013 the original author or authors.
*
* 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.snaker.engine.access;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.snaker.engine.DBAccess;
import org.snaker.engine.access.dialect.Dialect;
import org.snaker.engine.core.ServiceContext;
import org.snaker.engine.entity.CCOrder;
import org.snaker.engine.entity.HistoryOrder;
import org.snaker.engine.entity.HistoryTask;
import org.snaker.engine.entity.HistoryTaskActor;
import org.snaker.engine.entity.Order;
import org.snaker.engine.entity.Process;
import org.snaker.engine.entity.Surrogate;
import org.snaker.engine.entity.Task;
import org.snaker.engine.entity.TaskActor;
import org.snaker.engine.entity.WorkItem;
import org.snaker.engine.helper.StringHelper;
/**
* 抽象数据库访问类
* 封装SQL语句的构造
* @author yuqs
* @version 1.0
*/
public abstract class AbstractDBAccess implements DBAccess {
protected static final String KEY_SQL = "SQL";
protected static final String KEY_ARGS = "ARGS";
protected static final String KEY_TYPE = "TYPE";
protected static final String KEY_ENTITY = "ENTITY";
protected static final String KEY_SU = "SU";
protected static final String SAVE = "SAVE";
protected static final String UPDATE = "UPDATE";
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_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 (?,?,?,?,?,?,?,?,?,?,?,0)";
protected static final String ORDER_UPDATE = "update wf_order set last_Updator=?, last_Update_Time=?, version = version + 1 where id=? and version = ?";
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 = ? where id = ? ";
protected static final String ORDER_DELETE = "delete from wf_order where id = ?";
protected static final String CCORDER_INSERT = "insert into wf_cc_order (order_Id, actor_Id, status) values (?, ?, ?)";
protected static final String CCORDER_UPDATE = "update wf_cc_order set status = ? 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 (?,?,?,?,?,?,?,?,?,?,?,?,?,0)";
protected static final String TASK_UPDATE = "update wf_task set finish_Time=?, operator=?, 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_DELETE = "delete from wf_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 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 ";
protected static final String QUERY_ORDER = "select id,process_Id,creator,create_Time,parent_Id,parent_Node_Name,expire_Time,last_Update_Time,last_Updator,priority,order_No,variable, version from wf_order ";
protected static final String QUERY_TASK = "select 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 from wf_task ";
protected static final String QUERY_TASK_ACTOR = "select task_Id, actor_Id from wf_task_actor ";
protected static final String QUERY_CCORDER = "select order_Id, actor_Id, status from wf_cc_order ";
protected static final String QUERY_HIST_ORDER = "select id,process_Id,order_State,priority,creator,create_Time,end_Time,parent_Id,expire_Time,order_No,variable from wf_hist_order ";
protected static final String QUERY_HIST_TASK = "select 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 from wf_hist_task ";
protected static final String QUERY_HIST_TASK_ACTOR = "select task_Id, actor_Id from wf_hist_task_actor ";
/**委托代理CRUD*/
protected static final String SURROGATE_INSERT = "insert into wf_surrogate (id, process_Name, operator, surrogate, odate, sdate, edate, state) values (?,?,?,?,?,?,?,?)";
protected static final String SURROGATE_UPDATE = "update wf_surrogate set process_Name=?, surrogate=?, odate=?, sdate=?, edate=?, state=? where id = ?";
protected static final String SURROGATE_DELETE = "delete from wf_surrogate where id = ?";
protected static final String SURROGATE_QUERY = "select id, process_Name, operator, surrogate, odate, sdate, edate, state from wf_surrogate";
protected Dialect dialect;
/**
* 是否为ORM框架,用以标识对象直接持久化
* @return
*/
public abstract boolean isORM();
/**
* 保存或更新对象
* isORM为true,则参数map只存放对象
* isORM为false,则参数map需要放入SQL、ARGS、TYPE
* @param map
*/
public abstract void saveOrUpdate(Map map);
public void initialize(Object accessObject) {
}
/**
* isORM为false,需要构造map传递给实现类
* @param sql
* @param args
* @param type
* @return
*/
private Map buildMap(String sql, Object[] args, int[] type) {
Map map = new HashMap();
map.put(KEY_SQL, sql);
map.put(KEY_ARGS, args);
map.put(KEY_TYPE, type);
return map;
}
/**
* isORM为true,只存放对象传递给orm框架
* @param entity
* @return
*/
private Map buildMap(Object entity, String su) {
Map map = new HashMap();
map.put(KEY_ENTITY, entity);
map.put(KEY_SU, su);
return map;
}
/**
* 获取数据库方言
* @return
*/
protected Dialect getDialect() {
if(dialect == null) {
dialect = ServiceContext.getContext().find(Dialect.class);
}
return dialect;
}
/**
* 由于process中涉及blob字段,未对各种框架统一,所以process操作交给具体的实现类处理
*/
public void saveProcess(Process process) {
if(isORM()) {
saveOrUpdate(buildMap(process, SAVE));
} else {
Object[] args = new Object[]{process.getId(), process.getName(), process.getDisplayName(), process.getType(),
process.getInstanceUrl(), process.getState(), process.getVersion(), process.getCreateTime(), process.getCreator()};
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(PROCESS_INSERT, args, type));
}
}
/**
* 由于process中涉及blob字段,未对各种框架统一,所以process操作交给具体的实现类处理
*/
public void updateProcess(Process process) {
if(isORM()) {
saveOrUpdate(buildMap(process, UPDATE));
} else {
Object[] args = new Object[]{process.getName(), process.getDisplayName(), process.getState(),
process.getInstanceUrl(), process.getCreateTime(), process.getCreator(), process.getId()};
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(PROCESS_UPDATE, args, type));
}
}
public void updateProcessType(String id, String type) {
if(isORM()) {
Process process = getProcess(id);
process.setType(type);
saveOrUpdate(buildMap(process, UPDATE));
} else {
Object[] args = new Object[]{type, id};
int[] types = new int[]{Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(PROCESS_UPDATE_TYPE, args, types));
}
}
public void saveTask(Task task) {
if(isORM()) {
saveOrUpdate(buildMap(task, SAVE));
} else {
Object[] args = new Object[]{task.getId(), task.getOrderId(), task.getTaskName(), task.getDisplayName(), task.getTaskType(),
task.getPerformType(), task.getOperator(), task.getCreateTime(), task.getFinishTime(),
task.getExpireTime(), task.getActionUrl(), task.getParentTaskId(), task.getVariable()};
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(TASK_INSERT, args, type));
}
}
public void saveOrder(Order order) {
if(isORM()) {
saveOrUpdate(buildMap(order, SAVE));
} else {
Object[] args = new Object[]{order.getId(), order.getProcessId(), order.getCreator(), order.getCreateTime(), order.getParentId(),
order.getParentNodeName(), order.getExpireTime(), order.getLastUpdateTime(), order.getLastUpdator(), order.getOrderNo(), order.getVariable()};
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(ORDER_INSERT, args, type));
}
}
public void saveCCOrder(CCOrder ccorder) {
if(isORM()) {
saveOrUpdate(buildMap(ccorder, SAVE));
} else {
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.INTEGER};
saveOrUpdate(buildMap(CCORDER_INSERT, new Object[]{ccorder.getOrderId(), ccorder.getActorId(), ccorder.getStatus()}, type));
}
}
public void saveTaskActor(TaskActor taskActor) {
if(isORM()) {
saveOrUpdate(buildMap(taskActor, SAVE));
} else {
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(TASK_ACTOR_INSERT, new Object[]{taskActor.getTaskId(), taskActor.getActorId() }, type));
}
}
public void updateTask(Task task) {
if(isORM()) {
saveOrUpdate(buildMap(task, UPDATE));
} else {
Object[] args = new Object[]{task.getFinishTime(), task.getOperator(), task.getId(), task.getVersion() };
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER};
saveOrUpdate(buildMap(TASK_UPDATE, args, type));
}
}
public void updateOrder(Order order) {
if(isORM()) {
saveOrUpdate(buildMap(order, UPDATE));
} else {
Object[] args = new Object[]{order.getLastUpdator(), order.getLastUpdateTime(), order.getId(), order.getVersion() };
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER};
saveOrUpdate(buildMap(ORDER_UPDATE, args, type));
}
}
public void updateCCOrder(CCOrder ccorder) {
if(isORM()) {
saveOrUpdate(buildMap(ccorder, UPDATE));
} else {
Object[] args = new Object[]{ccorder.getStatus(), ccorder.getOrderId(), ccorder.getActorId() };
int[] type = new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(CCORDER_UPDATE, args, type));
}
}
public void deleteTask(Task task) {
if(!isORM()) {
Object[] args = new Object[]{task.getId()};
int[] type = new int[]{Types.VARCHAR};
saveOrUpdate(buildMap(TASK_ACTOR_DELETE, args, type));
saveOrUpdate(buildMap(TASK_DELETE, args, type));
}
}
public void deleteOrder(Order order) {
if(!isORM()) {
int[] type = new int[]{Types.VARCHAR};
saveOrUpdate(buildMap(ORDER_DELETE, new Object[]{order.getId()}, type));
}
}
public void deleteCCOrder(CCOrder ccorder) {
if(!isORM()) {
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(CCORDER_DELETE, new Object[]{ccorder.getOrderId(), ccorder.getActorId()}, type));
}
}
public void removeTaskActor(String taskId, String... actors) {
if(!isORM()) {
for(String actorId : actors) {
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(TASK_ACTOR_REDUCE, new Object[]{taskId, actorId}, type));
}
}
}
public void saveHistory(HistoryOrder order) {
if(isORM()) {
saveOrUpdate(buildMap(order, SAVE));
} else {
Object[] args = new Object[]{order.getId(), order.getProcessId(), order.getOrderState(), order.getCreator(),
order.getCreateTime(), order.getEndTime(), order.getParentId(), order.getExpireTime(), order.getOrderNo(), order.getVariable()};
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR,
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(ORDER_HISTORY_INSERT, args, type));
}
}
public void updateHistory(HistoryOrder order) {
if(isORM()) {
saveOrUpdate(buildMap(order, UPDATE));
} else {
Object[] args = new Object[]{order.getOrderState(), order.getEndTime(), order.getId()};
int[] type = new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(ORDER_HISTORY_UPDATE, args, type));
}
}
public void saveHistory(HistoryTask task) {
if(isORM()) {
saveOrUpdate(buildMap(task, SAVE));
if(task.getActorIds() == null) {
return;
}
for(String actorId : task.getActorIds()) {
if(StringHelper.isEmpty(actorId)) continue;
HistoryTaskActor hist = new HistoryTaskActor();
hist.setActorId(actorId);
hist.setTaskId(task.getId());
saveOrUpdate(buildMap(hist, SAVE));
}
} else {
Object[] args = new Object[]{task.getId(), task.getOrderId(), task.getTaskName(), task.getDisplayName(), task.getTaskType(),
task.getPerformType(), task.getTaskState(), task.getOperator(), task.getCreateTime(), task.getFinishTime(),
task.getExpireTime(), task.getActionUrl(), task.getParentTaskId(), task.getVariable()};
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR};
saveOrUpdate(buildMap(TASK_HISTORY_INSERT, args, type));
if(task.getActorIds() == null) {
return;
}
for(String actorId : task.getActorIds()) {
if(StringHelper.isEmpty(actorId)) continue;
saveOrUpdate(buildMap(TASK_ACTOR_HISTORY_INSERT, new Object[]{task.getId(), actorId}, new int[]{Types.VARCHAR, Types.VARCHAR}));
}
}
}
public void saveSurrogate(Surrogate surrogate) {
if(isORM()) {
saveOrUpdate(buildMap(surrogate, SAVE));
} else {
Object[] args = new Object[]{surrogate.getId(), surrogate.getProcessName(), surrogate.getOperator(),
surrogate.getSurrogate(), surrogate.getOdate(), surrogate.getSdate(), surrogate.getEdate(),
surrogate.getState()};
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
Types.VARCHAR, Types.INTEGER};
saveOrUpdate(buildMap(SURROGATE_INSERT, args, type));
}
}
public void updateSurrogate(Surrogate surrogate) {
if(isORM()) {
saveOrUpdate(buildMap(surrogate, UPDATE));
} else {
Object[] args = new Object[]{surrogate.getProcessName(), surrogate.getSurrogate(), surrogate.getOdate(),
surrogate.getSdate(), surrogate.getEdate(), surrogate.getState(), surrogate.getId()};
int[] type = new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,Types.VARCHAR, Types.VARCHAR,
Types.INTEGER, Types.VARCHAR};
saveOrUpdate(buildMap(SURROGATE_UPDATE, args, type));
}
}
public void deleteSurrogate(Surrogate surrogate) {
if(!isORM()) {
Object[] args = new Object[]{surrogate.getId()};
int[] type = new int[]{Types.VARCHAR};
saveOrUpdate(buildMap(SURROGATE_DELETE, args, type));
}
}
public Surrogate getSurrogate(String id) {
String where = " where id = ?";
return queryObject(Surrogate.class, SURROGATE_QUERY + where, id);
}
public List getSurrogate(Page page, QueryFilter filter) {
StringBuffer sql = new StringBuffer(SURROGATE_QUERY);
sql.append(" where 1=1 and state = 1 ");
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy