package com.vci.server.workflow.server.template; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.jbpm.api.ProcessEngine; import org.jbpm.api.ProcessInstance; import org.jbpm.api.history.HistoryProcessInstance; import com.vci.common.log.ServerWithLog4j; import com.vci.common.objects.UserEntity; import com.vci.corba.common.VCIError; import com.vci.corba.workflow.data.FlowInstanceInfo; import com.vci.server.base.persistence.dao.BaseService; import com.vci.server.base.persistence.dao.HibernateCallback; import com.vci.server.base.persistence.dao.HibernateSessionFactory; import com.vci.server.base.persistence.dao.HibernateTemplate; import com.vci.server.workflow.dao.FlowInstanceDaoImpl; import com.vci.server.workflow.dao.ProcessCategoryDaoImpl; import com.vci.server.workflow.objects.FlowInstance; import com.vci.server.workflow.objects.ProcessCategory; import com.vci.server.workflow.server.JbpmEngine; public class ProcessCustomService extends BaseService { public ProcessCustomService(UserEntity userEntity) { super(userEntity); } public ProcessCustomService() { } public boolean saveProcessCategory(final ProcessCategory object){ return (Boolean)new HibernateTemplate().run(new HibernateCallback(){ public Object execute() throws HibernateException { ProcessCategoryDaoImpl impl = new ProcessCategoryDaoImpl(); object.setUserEntity(userEntity); impl.saveOrUpdate(object); return true; } }); } @SuppressWarnings("unchecked") public List getProcessCategories(final String parentId){ // try { // String[] curCandidates = new ProcessTemplateService().getCurCandidates("", "", ""); // } catch (Exception e) { // e.printStackTrace(); // } return (List)new HibernateTemplate().run(new HibernateCallback(){ public Object execute() throws HibernateException { ProcessCategoryDaoImpl impl = new ProcessCategoryDaoImpl(); String hql = "from ProcessCategory t where t.parentId = ?"; return impl.createQueryList(hql, new Object[]{parentId}); } }); } /** * 模板分类定义分页 * @param parentId * @param pageSize * @param pageIndex * @return */ @SuppressWarnings("unchecked") public List getProcessCategoriesByPage(final String parentId,final int pageSize,final int pageIndex){ return (List)new HibernateTemplate().run(new HibernateCallback(){ public Object execute() throws HibernateException { int start = 0; if(pageIndex<0){ start=1; }else{ start = pageIndex; } ProcessCategoryDaoImpl impl = new ProcessCategoryDaoImpl(); String hql = "from ProcessCategory t where t.parentId = ?"; return impl.findEntites(hql,(start-1)*pageSize ,pageSize,new Object[]{parentId}); } }); } public boolean updateProcessCategory(final ProcessCategory object){ return (Boolean)new HibernateTemplate().run(new HibernateCallback(){ public Object execute() throws HibernateException { ProcessCategoryDaoImpl impl = new ProcessCategoryDaoImpl(); object.setUserEntity(userEntity); ProcessCategory objGet = impl.getById(object.getId()); if(objGet == null){ impl.saveOrUpdate(object); }else{ objGet.setUserEntity(userEntity); objGet.setId(object.getId()); objGet.setParentId(object.getParentId()); objGet.setName(object.getName()); objGet.setDesc(object.getDesc()); objGet.setIcon(object.getIcon()); objGet.setModifyTime(object.getModifyTime()); objGet.setModifyUser(object.getModifyUser()); objGet.setModifyRole(object.getModifyRole()); objGet.setModifyOrg(object.getModifyOrg()); impl.saveOrUpdate(objGet); } return true; } }); } public boolean existProcessCategory(String id, String name) { Session session = null; try { session = HibernateSessionFactory.getSession(); StringBuilder hql = new StringBuilder("select count(*) from ProcessCategory p where p.name = :name"); boolean hasIdValue = (id != null && id.trim().length() > 0); if(hasIdValue){ hql.append(" and p.id <> :id"); } Query query = session.createQuery(hql.toString()); query.setString("name", name); if(hasIdValue){ query.setString("id", id); } Long count = (Long)query.uniqueResult(); if(count != null && ((Long)count).longValue() > 0){ return true; } } catch (Exception e) { e.printStackTrace(); ServerWithLog4j.logger.error(e); throw new RuntimeException(e); } return false; } public boolean deleteProcessCategory(final String id) { return (Boolean)new HibernateTemplate().run(new HibernateCallback(){ public Object execute() throws HibernateException { ProcessCategoryDaoImpl impl = new ProcessCategoryDaoImpl(); String hql = "delete ProcessCategory p where p.id = ?"; // impl.deleteQueryObject(hql, new String[]{id}, userEntity); impl.createQuery(hql, new String[]{id}); return true; } }); } /** * 移动流程定义模板 * @param deploymentId 流程部署id * @param categoryId 流程分类Id * @return */ public boolean moveDefinition( String deploymentId , String categoryId){ Session session = HibernateSessionFactory.getSession(); try { String sql = "update PLPROCESSTEMPLATE a set a.plcategoryoid = '"+categoryId+"' where a.pljbpmdeploymentid = '"+deploymentId+"'"; session.createSQLQuery(sql).executeUpdate(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } public boolean deleteProcessDefinition(final String deployId, final String pdId) throws VCIError { ProcessEngine processEngine = JbpmEngine.getProcessEngine(); // StringBuilder sql = new StringBuilder(); // sql.append("select count(*) from PLRMTEMPLATEPROCESS where PLJBPMDEPLOYMENTID = ?"); Session session = HibernateSessionFactory.getSession(); String messCode = ""; //Connection conn = null; //PreparedStatement pstmt = null; //ResultSet rs = null; //ConnectionProvider cp = ((SessionFactoryImplementor) session.getSessionFactory()).getConnectionProvider(); try { // Query query = session.createSQLQuery(sql.toString()); // query.setString(0, deployId); // int count = Integer.parseInt( query.list().get(0).toString() ); // if(count > 0) { // messCode = "410113"; // throw new VCIError("410113", new String[] {}); // } Query query; List pds = processEngine.getExecutionService().createProcessInstanceQuery().processDefinitionId(pdId).list(); if(pds.size() > 0) { messCode = "410114"; throw new VCIError("410114", new String[] {}); } List historyPds = processEngine.getHistoryService().createHistoryProcessInstanceQuery().processDefinitionId(pdId).list(); if(historyPds.size() > 0) { messCode = "410114"; throw new VCIError("410114", new String[] {}); } //删除任务描述信息表 String deleteTaskDescSql = "delete from pltaskdesc a where a.pljbpmdeploymentid = ?"; query = session.createSQLQuery(deleteTaskDescSql); query.setString(0, deployId); query.executeUpdate(); //删除PLPROCESSTEMPLATE中的数据 String deleteSql = "delete from PLPROCESSTEMPLATE WHERE PLJBPMDEPLOYMENTID = ?"; query = session.createSQLQuery(deleteSql); query.setString(0,deployId); query.executeUpdate(); //删除PLPROCESSTASKPROPERTY中的数据 deleteSql = "delete from PLPROCESSTASKPROPERTY p where p.PLTASKPROPERTY in (select t.PLTASKTYPEPROPERTY from PLPROCESSTASK t WHERE PLTASKTYPEPROPERTY is not null and t.PLJBPMDEPLOYMENTID = ?)"; query = session.createSQLQuery(deleteSql); query.setString(0,deployId); query.executeUpdate(); //删除PLPROCESSTASK中的数据 deleteSql = "delete from PLPROCESSTASK WHERE PLJBPMDEPLOYMENTID = ?"; query = session.createSQLQuery(deleteSql); query.setString(0,deployId); query.executeUpdate(); processEngine.getRepositoryService().deleteDeploymentCascade(deployId);//删除流程定义 } catch (Exception ex) { ex.printStackTrace(); throw new VCIError(messCode, new String[] {}); } finally { } return true; } public FlowInstanceInfo getFlowInstanceInfo(String executionid) { FlowInstanceDaoImpl impl = new FlowInstanceDaoImpl(); String hql = "from FlowInstance t where t.executionid = ?"; FlowInstance flowInstance = (FlowInstance)impl.findEntity(hql, new Object[]{executionid}); FlowInstanceInfo flowInstanceInfo = new FlowInstanceInfo(); flowInstanceInfo.id = flowInstance.getId() == null ? "" : flowInstance.getId(); flowInstanceInfo.executionid = flowInstance.getExecutionid() == null ? "" : flowInstance.getExecutionid(); flowInstanceInfo.applicant = flowInstance.getApplicant() == null ? "" : flowInstance.getApplicant(); flowInstanceInfo.creator = flowInstance.getCreator() == null ? "" : flowInstance.getCreator(); flowInstanceInfo.templatePuid = flowInstance.getTemplatePuid() == null ? "" : flowInstance.getTemplatePuid(); flowInstanceInfo.templateName = flowInstance.getTemplateName() == null ? "" : flowInstance.getTemplateName(); flowInstanceInfo.clsfOid = flowInstance.getClsfOid() == null ? "" : flowInstance.getClsfOid(); flowInstanceInfo.tableName = flowInstance.getTableName() == null ? "" : flowInstance.getTableName(); flowInstanceInfo.desc = flowInstance.getDesc() == null ? "" : flowInstance.getDesc(); flowInstanceInfo.processType = flowInstance.getProcessType() == null ? "" : flowInstance.getProcessType(); flowInstanceInfo.taskType = flowInstance.getTaskType() == null ? "" : flowInstance.getTaskType(); return flowInstanceInfo; } private static void close(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { } } } private static void close(Statement stmt) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { } } } private static void close(ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { } } } }