ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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<ProcessCategory> getProcessCategories(final String parentId){
//        try {
//            String[] curCandidates = new ProcessTemplateService().getCurCandidates("", "", "");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return (List<ProcessCategory>)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<ProcessCategory> getProcessCategoriesByPage(final String parentId,final int pageSize,final int pageIndex){
        return (List<ProcessCategory>)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<ProcessInstance> pds = processEngine.getExecutionService().createProcessInstanceQuery().processDefinitionId(pdId).list();
            if(pds.size() > 0) {
                messCode = "410114";
                throw new VCIError("410114", new String[] {});
            }
            List<HistoryProcessInstance> 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) {
            }
        }
    }
 
}