lyh
2025-01-13 137d008bd9b7d932160436a3a560b24512f6d1db
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
package org.activiti.engine.impl.db;
 
import org.activiti.engine.ActivitiException;
import org.activiti.engine.ActivitiOptimisticLockingException;
import org.activiti.engine.ActivitiWrongDbException;
import org.activiti.engine.impl.*;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.db.upgrade.DbUpgradeStep;
import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.persistence.cache.CachedEntity;
import org.activiti.engine.impl.persistence.cache.EntityCache;
import org.activiti.engine.impl.persistence.entity.Entity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.PropertyEntity;
import org.activiti.engine.impl.util.IoUtil;
import org.activiti.engine.impl.util.ReflectUtil;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class DbSqlSession implements Session {
    private static final Logger log = LoggerFactory.getLogger(DbSqlSession.class);
    protected static final Pattern CLEAN_VERSION_REGEX = Pattern.compile("\\d\\.\\d*");
    protected static final String LAST_V5_VERSION = "5.99.0.0";
    protected static final List<ActivitiVersion> ACTIVITI_VERSIONS = new ArrayList();
    protected SqlSession sqlSession;
    protected DbSqlSessionFactory dbSqlSessionFactory;
    protected EntityCache entityCache;
    protected Map<Class<? extends Entity>, Map<String, Entity>> insertedObjects = new HashMap();
    protected Map<Class<? extends Entity>, Map<String, Entity>> deletedObjects = new HashMap();
    protected Map<Class<? extends Entity>, List<BulkDeleteOperation>> bulkDeleteOperations = new HashMap();
    protected List<Entity> updatedObjects = new ArrayList();
    protected String connectionMetadataDefaultCatalog;
    protected String connectionMetadataDefaultSchema;
    public static String[] JDBC_METADATA_TABLE_TYPES;
 
    public DbSqlSession(DbSqlSessionFactory dbSqlSessionFactory, EntityCache entityCache) {
        this.dbSqlSessionFactory = dbSqlSessionFactory;
        this.sqlSession = dbSqlSessionFactory.getSqlSessionFactory().openSession();
        this.entityCache = entityCache;
        this.connectionMetadataDefaultCatalog = dbSqlSessionFactory.getDatabaseCatalog();
        this.connectionMetadataDefaultSchema = dbSqlSessionFactory.getDatabaseSchema();
    }
 
    public DbSqlSession(DbSqlSessionFactory dbSqlSessionFactory, EntityCache entityCache, Connection connection, String catalog, String schema) {
        this.dbSqlSessionFactory = dbSqlSessionFactory;
        this.sqlSession = dbSqlSessionFactory.getSqlSessionFactory().openSession(connection);
        this.entityCache = entityCache;
        this.connectionMetadataDefaultCatalog = catalog;
        this.connectionMetadataDefaultSchema = schema;
    }
 
    public void insert(Entity entity) {
        if (entity.getId() == null) {
            String id = this.dbSqlSessionFactory.getIdGenerator().getNextId();
            entity.setId(id);
        }
 
        Class<? extends Entity> clazz = entity.getClass();
        if (!this.insertedObjects.containsKey(clazz)) {
            this.insertedObjects.put(clazz, new LinkedHashMap());
        }
 
        ((Map)this.insertedObjects.get(clazz)).put(entity.getId(), entity);
        this.entityCache.put(entity, false);
        entity.setInserted(true);
    }
 
    public void update(Entity entity) {
        this.entityCache.put(entity, false);
        entity.setUpdated(true);
    }
 
    public int update(String statement, Object parameters) {
        String updateStatement = this.dbSqlSessionFactory.mapStatement(statement);
        return this.getSqlSession().update(updateStatement, parameters);
    }
 
    public void delete(String statement, Object parameter, Class<? extends Entity> entityClass) {
        if (!this.bulkDeleteOperations.containsKey(entityClass)) {
            this.bulkDeleteOperations.put(entityClass, new ArrayList(1));
        }
 
        ((List)this.bulkDeleteOperations.get(entityClass)).add(new BulkDeleteOperation(this.dbSqlSessionFactory.mapStatement(statement), parameter));
    }
 
    public void delete(Entity entity) {
        Class<? extends Entity> clazz = entity.getClass();
        if (!this.deletedObjects.containsKey(clazz)) {
            this.deletedObjects.put(clazz, new LinkedHashMap());
        }
 
        ((Map)this.deletedObjects.get(clazz)).put(entity.getId(), entity);
        entity.setDeleted(true);
    }
 
    public List selectList(String statement) {
        return this.selectList(statement, (Object)null, 0, Integer.MAX_VALUE);
    }
 
    public List selectList(String statement, Object parameter) {
        return this.selectList(statement, parameter, 0, Integer.MAX_VALUE);
    }
 
    public List selectList(String statement, Object parameter, boolean useCache) {
        return this.selectList(statement, parameter, 0, Integer.MAX_VALUE, useCache);
    }
 
    public List selectList(String statement, Object parameter, Page page) {
        return this.selectList(statement, parameter, page, true);
    }
 
    public List selectList(String statement, Object parameter, Page page, boolean useCache) {
        return page != null ? this.selectList(statement, parameter, page.getFirstResult(), page.getMaxResults(), useCache) : this.selectList(statement, parameter, 0, Integer.MAX_VALUE, useCache);
    }
 
    public List selectList(String statement, ListQueryParameterObject parameter, Page page) {
        return this.selectList(statement, parameter, page, true);
    }
 
    public List selectList(String statement, ListQueryParameterObject parameter, Page page, boolean useCache) {
        ListQueryParameterObject parameterToUse = parameter;
        if (parameterToUse == null) {
            parameterToUse = new ListQueryParameterObject();
        }
 
        if (page != null) {
            parameterToUse.setFirstResult(page.getFirstResult());
            parameterToUse.setMaxResults(page.getMaxResults());
        }
 
        return this.selectList(statement, parameterToUse, useCache);
    }
 
    public List selectList(String statement, Object parameter, int firstResult, int maxResults) {
        return this.selectList(statement, parameter, firstResult, maxResults, true);
    }
 
    public List selectList(String statement, Object parameter, int firstResult, int maxResults, boolean useCache) {
        return this.selectList(statement, new ListQueryParameterObject(parameter, firstResult, maxResults), useCache);
    }
 
    public List selectList(String statement, ListQueryParameterObject parameter) {
        return this.selectList(statement, parameter, true);
    }
 
    public List selectList(String statement, ListQueryParameterObject parameter, boolean useCache) {
        return this.selectListWithRawParameter(statement, parameter, parameter.getFirstResult(), parameter.getMaxResults(), useCache);
    }
 
    public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults) {
        return this.selectListWithRawParameter(statement, parameter, firstResult, maxResults, true);
    }
 
    public List selectListWithRawParameter(String statement, Object parameter, int firstResult, int maxResults, boolean useCache) {
        statement = this.dbSqlSessionFactory.mapStatement(statement);
        if (firstResult != -1 && maxResults != -1) {
            List loadedObjects = this.sqlSession.selectList(statement, parameter);
            return useCache ? this.cacheLoadOrStore(loadedObjects) : loadedObjects;
        } else {
            return Collections.EMPTY_LIST;
        }
    }
 
    public List selectListWithRawParameterWithoutFilter(String statement, Object parameter, int firstResult, int maxResults) {
        statement = this.dbSqlSessionFactory.mapStatement(statement);
        return firstResult != -1 && maxResults != -1 ? this.sqlSession.selectList(statement, parameter) : Collections.EMPTY_LIST;
    }
 
    public Object selectOne(String statement, Object parameter) {
        statement = this.dbSqlSessionFactory.mapStatement(statement);
        Object result = this.sqlSession.selectOne(statement, parameter);
        if (result instanceof Entity) {
            Entity loadedObject = (Entity)result;
            result = this.cacheLoadOrStore(loadedObject);
        }
 
        return result;
    }
 
    public <T extends Entity> T selectById(Class<T> entityClass, String id) {
        return this.selectById(entityClass, id, true);
    }
 
    public <T extends Entity> T selectById(Class<T> entityClass, String id, boolean useCache) {
        T entity = null;
        if (useCache) {
            entity = (T) this.entityCache.findInCache(entityClass, id);
            if (entity != null) {
                return entity;
            }
        }
 
        String selectStatement = this.dbSqlSessionFactory.getSelectStatement(entityClass);
        selectStatement = this.dbSqlSessionFactory.mapStatement(selectStatement);
        entity = (T) this.sqlSession.selectOne(selectStatement, id);
        if (entity == null) {
            return null;
        } else {
            this.entityCache.put(entity, true);
            return entity;
        }
    }
 
    protected List cacheLoadOrStore(List<Object> loadedObjects) {
        if (loadedObjects.isEmpty()) {
            return loadedObjects;
        } else if (!(loadedObjects.get(0) instanceof Entity)) {
            return loadedObjects;
        } else {
            List<Entity> filteredObjects = new ArrayList(loadedObjects.size());
            Iterator var3 = loadedObjects.iterator();
 
            while(var3.hasNext()) {
                Object loadedObject = var3.next();
                Entity cachedEntity = this.cacheLoadOrStore((Entity)loadedObject);
                filteredObjects.add(cachedEntity);
            }
 
            return filteredObjects;
        }
    }
 
    protected Entity cacheLoadOrStore(Entity entity) {
        Entity cachedEntity = (Entity)this.entityCache.findInCache(entity.getClass(), entity.getId());
        if (cachedEntity != null) {
            return cachedEntity;
        } else {
            this.entityCache.put(entity, true);
            return entity;
        }
    }
 
    public void flush() {
        this.determineUpdatedObjects();
        this.removeUnnecessaryOperations();
        if (log.isDebugEnabled()) {
            this.debugFlush();
        }
 
        this.flushInserts();
        this.flushUpdates();
        this.flushDeletes();
    }
 
    protected void removeUnnecessaryOperations() {
        Iterator var1 = this.deletedObjects.keySet().iterator();
 
        while(var1.hasNext()) {
            Class<? extends Entity> entityClass = (Class)var1.next();
            Set<String> ids = new HashSet();
            Iterator<Entity> entitiesToDeleteIterator = ((Map)this.deletedObjects.get(entityClass)).values().iterator();
 
            while(entitiesToDeleteIterator.hasNext()) {
                Entity entityToDelete = (Entity)entitiesToDeleteIterator.next();
                if (!ids.contains(entityToDelete.getId())) {
                    ids.add(entityToDelete.getId());
                } else {
                    entitiesToDeleteIterator.remove();
                }
            }
 
            Iterator var7 = ids.iterator();
 
            while(var7.hasNext()) {
                String id = (String)var7.next();
                if (this.insertedObjects.containsKey(entityClass) && ((Map)this.insertedObjects.get(entityClass)).containsKey(id)) {
                    ((Map)this.insertedObjects.get(entityClass)).remove(id);
                    ((Map)this.deletedObjects.get(entityClass)).remove(id);
                }
            }
        }
 
    }
 
    public void determineUpdatedObjects() {
        this.updatedObjects = new ArrayList();
        Map<Class<?>, Map<String, CachedEntity>> cachedObjects = this.entityCache.getAllCachedEntities();
        Iterator var2 = cachedObjects.keySet().iterator();
 
        label34:
        while(var2.hasNext()) {
            Class<?> clazz = (Class)var2.next();
            Map<String, CachedEntity> classCache = (Map)cachedObjects.get(clazz);
            Iterator var5 = classCache.values().iterator();
 
            while(true) {
                CachedEntity cachedObject;
                Entity cachedEntity;
                do {
                    do {
                        if (!var5.hasNext()) {
                            continue label34;
                        }
 
                        cachedObject = (CachedEntity)var5.next();
                        cachedEntity = cachedObject.getEntity();
                    } while(this.isEntityInserted(cachedEntity));
                } while(!ExecutionEntity.class.isAssignableFrom(cachedEntity.getClass()) && this.isEntityToBeDeleted(cachedEntity));
 
                if (cachedObject.hasChanged()) {
                    this.updatedObjects.add(cachedEntity);
                }
            }
        }
 
    }
 
    protected void debugFlush() {
        log.debug("Flushing dbSqlSession");
        int nrOfInserts = 0;
        int nrOfUpdates = 0;
        int nrOfDeletes = 0;
        Iterator var4 = this.insertedObjects.values().iterator();
 
        Map deletedObjectMap;
        Iterator var6;
        Entity deletedObject;
        while(var4.hasNext()) {
            deletedObjectMap = (Map)var4.next();
 
            for(var6 = deletedObjectMap.values().iterator(); var6.hasNext(); ++nrOfInserts) {
                deletedObject = (Entity)var6.next();
                log.debug("  insert {}", deletedObject);
            }
        }
 
        for(var4 = this.updatedObjects.iterator(); var4.hasNext(); ++nrOfUpdates) {
            Entity updatedObject = (Entity)var4.next();
            log.debug("  update {}", updatedObject);
        }
 
        var4 = this.deletedObjects.values().iterator();
 
        while(var4.hasNext()) {
            deletedObjectMap = (Map)var4.next();
 
            for(var6 = deletedObjectMap.values().iterator(); var6.hasNext(); ++nrOfDeletes) {
                deletedObject = (Entity)var6.next();
                log.debug("  delete {} with id {}", deletedObject, deletedObject.getId());
            }
        }
 
        var4 = this.bulkDeleteOperations.values().iterator();
 
        while(var4.hasNext()) {
            Collection<BulkDeleteOperation> bulkDeleteOperationList = (Collection)var4.next();
 
            for(var6 = bulkDeleteOperationList.iterator(); var6.hasNext(); ++nrOfDeletes) {
                BulkDeleteOperation bulkDeleteOperation = (BulkDeleteOperation)var6.next();
                log.debug("  {}", bulkDeleteOperation);
            }
        }
 
        log.debug("flush summary: {} insert, {} update, {} delete.", new Object[]{nrOfInserts, nrOfUpdates, nrOfDeletes});
        log.debug("now executing flush...");
    }
 
    public boolean isEntityInserted(Entity entity) {
        return this.insertedObjects.containsKey(entity.getClass()) && ((Map)this.insertedObjects.get(entity.getClass())).containsKey(entity.getId());
    }
 
    public boolean isEntityToBeDeleted(Entity entity) {
        return this.deletedObjects.containsKey(entity.getClass()) && ((Map)this.deletedObjects.get(entity.getClass())).containsKey(entity.getId());
    }
 
    protected void flushInserts() {
        if (this.insertedObjects.size() != 0) {
            Iterator var1 = EntityDependencyOrder.INSERT_ORDER.iterator();
 
            Class entityClass;
            while(var1.hasNext()) {
                entityClass = (Class)var1.next();
                if (this.insertedObjects.containsKey(entityClass)) {
                    this.flushInsertEntities(entityClass, ((Map)this.insertedObjects.get(entityClass)).values());
                    this.insertedObjects.remove(entityClass);
                }
            }
 
            if (this.insertedObjects.size() > 0) {
                var1 = this.insertedObjects.keySet().iterator();
 
                while(var1.hasNext()) {
                    entityClass = (Class)var1.next();
                    this.flushInsertEntities(entityClass, ((Map)this.insertedObjects.get(entityClass)).values());
                }
            }
 
            this.insertedObjects.clear();
        }
    }
 
    protected void flushInsertEntities(Class<? extends Entity> entityClass, Collection<Entity> entitiesToInsert) {
        if (entitiesToInsert.size() == 1) {
            this.flushRegularInsert((Entity)entitiesToInsert.iterator().next(), entityClass);
        } else if (Boolean.FALSE.equals(this.dbSqlSessionFactory.isBulkInsertable(entityClass))) {
            Iterator var3 = entitiesToInsert.iterator();
 
            while(var3.hasNext()) {
                Entity entity = (Entity)var3.next();
                this.flushRegularInsert(entity, entityClass);
            }
        } else {
            this.flushBulkInsert(entitiesToInsert, entityClass);
        }
 
    }
 
    protected Collection<Entity> orderExecutionEntities(Map<String, Entity> executionEntities, boolean parentBeforeChildExecution) {
        List<Entity> result = new ArrayList(executionEntities.size());
        Map<String, String> childToParentExecutionMapping = new HashMap();
        Map<String, List<ExecutionEntity>> parentToChildrenMapping = new HashMap();
        Collection<Entity> executionCollection = executionEntities.values();
 
        Iterator executionIterator;
        ExecutionEntity currentExecutionEntity;
        String executionId;
        String parentId;
        for(executionIterator = executionCollection.iterator(); executionIterator.hasNext(); ((List)parentToChildrenMapping.get(parentId)).add(currentExecutionEntity)) {
            currentExecutionEntity = (ExecutionEntity)executionIterator.next();
            parentId = currentExecutionEntity.getParentId();
            executionId = currentExecutionEntity.getSuperExecutionId();
            parentId = parentId != null ? parentId : executionId;
            childToParentExecutionMapping.put(currentExecutionEntity.getId(), parentId);
            if (!parentToChildrenMapping.containsKey(parentId)) {
                parentToChildrenMapping.put(parentId, new ArrayList());
            }
        }
 
        Set<String> handledExecutionIds = new HashSet(executionEntities.size());
        executionIterator = executionCollection.iterator();
 
        while(true) {
            do {
                if (!executionIterator.hasNext()) {
                    return result;
                }
 
                currentExecutionEntity = (ExecutionEntity)executionIterator.next();
                executionId = currentExecutionEntity.getId();
            } while(handledExecutionIds.contains(executionId));
 
            parentId = (String)childToParentExecutionMapping.get(executionId);
            if (parentId != null) {
                while(parentId != null) {
                    String newParentId = (String)childToParentExecutionMapping.get(parentId);
                    if (newParentId == null) {
                        break;
                    }
 
                    parentId = newParentId;
                }
            }
 
            if (parentId == null) {
                parentId = executionId;
            }
 
            if (executionEntities.containsKey(parentId) && !handledExecutionIds.contains(parentId)) {
                handledExecutionIds.add(parentId);
                if (parentBeforeChildExecution) {
                    result.add(executionEntities.get(parentId));
                } else {
                    result.add(0, executionEntities.get(parentId));
                }
            }
 
            this.collectChildExecutionsForInsertion(result, parentToChildrenMapping, handledExecutionIds, parentId, parentBeforeChildExecution);
        }
    }
 
    protected void collectChildExecutionsForInsertion(List<Entity> result, Map<String, List<ExecutionEntity>> parentToChildrenMapping, Set<String> handledExecutionIds, String parentId, boolean parentBeforeChildExecution) {
        List<ExecutionEntity> childExecutionEntities = (List)parentToChildrenMapping.get(parentId);
        if (childExecutionEntities != null) {
            ExecutionEntity childExecutionEntity;
            for(Iterator var7 = childExecutionEntities.iterator(); var7.hasNext(); this.collectChildExecutionsForInsertion(result, parentToChildrenMapping, handledExecutionIds, childExecutionEntity.getId(), parentBeforeChildExecution)) {
                childExecutionEntity = (ExecutionEntity)var7.next();
                handledExecutionIds.add(childExecutionEntity.getId());
                if (parentBeforeChildExecution) {
                    result.add(childExecutionEntity);
                } else {
                    result.add(0, childExecutionEntity);
                }
            }
 
        }
    }
 
    protected void flushRegularInsert(Entity entity, Class<? extends Entity> clazz) {
        String insertStatement = this.dbSqlSessionFactory.getInsertStatement(entity);
        insertStatement = this.dbSqlSessionFactory.mapStatement(insertStatement);
        if (insertStatement == null) {
            throw new ActivitiException("no insert statement for " + entity.getClass() + " in the ibatis mapping files");
        } else {
            log.debug("inserting: {}", entity);
            this.sqlSession.insert(insertStatement, entity);
            if (entity instanceof HasRevision) {
                this.incrementRevision(entity);
            }
 
        }
    }
 
    protected void flushBulkInsert(Collection<Entity> entities, Class<? extends Entity> clazz) {
        String insertStatement = this.dbSqlSessionFactory.getBulkInsertStatement(clazz);
        insertStatement = this.dbSqlSessionFactory.mapStatement(insertStatement);
        if (insertStatement == null) {
            throw new ActivitiException("no insert statement for " + ((Entity)entities.iterator().next()).getClass() + " in the ibatis mapping files");
        } else {
            Iterator<Entity> entityIterator = entities.iterator();
            Boolean hasRevision = null;
 
            while(entityIterator.hasNext()) {
                List<Entity> subList = new ArrayList();
 
                for(int index = 0; entityIterator.hasNext() && index < this.dbSqlSessionFactory.getMaxNrOfStatementsInBulkInsert(); ++index) {
                    Entity entity = (Entity)entityIterator.next();
                    subList.add(entity);
                    if (hasRevision == null) {
                        hasRevision = entity instanceof HasRevision;
                    }
                }
 
                this.sqlSession.insert(insertStatement, subList);
            }
 
            if (hasRevision != null && hasRevision) {
                entityIterator = entities.iterator();
 
                while(entityIterator.hasNext()) {
                    this.incrementRevision((Entity)entityIterator.next());
                }
            }
 
        }
    }
 
    protected void incrementRevision(Entity insertedObject) {
        HasRevision revisionEntity = (HasRevision)insertedObject;
        if (revisionEntity.getRevision() == 0) {
            revisionEntity.setRevision(revisionEntity.getRevisionNext());
        }
 
    }
 
    protected void flushUpdates() {
        Iterator var1 = this.updatedObjects.iterator();
 
        while(var1.hasNext()) {
            Entity updatedObject = (Entity)var1.next();
            String updateStatement = this.dbSqlSessionFactory.getUpdateStatement(updatedObject);
            updateStatement = this.dbSqlSessionFactory.mapStatement(updateStatement);
            if (updateStatement == null) {
                throw new ActivitiException("no update statement for " + updatedObject.getClass() + " in the ibatis mapping files");
            }
 
            log.debug("updating: {}", updatedObject);
            int updatedRecords = this.sqlSession.update(updateStatement, updatedObject);
            if (updatedRecords == 0) {
                throw new ActivitiOptimisticLockingException(updatedObject + " was updated by another transaction concurrently");
            }
 
            if (updatedObject instanceof HasRevision) {
                ((HasRevision)updatedObject).setRevision(((HasRevision)updatedObject).getRevisionNext());
            }
        }
 
        this.updatedObjects.clear();
    }
 
    protected void flushDeletes() {
        if (this.deletedObjects.size() != 0 || this.bulkDeleteOperations.size() != 0) {
            Iterator var1;
            Class entityClass;
            for(var1 = EntityDependencyOrder.DELETE_ORDER.iterator(); var1.hasNext(); this.flushBulkDeletes(entityClass)) {
                entityClass = (Class)var1.next();
                if (this.deletedObjects.containsKey(entityClass)) {
                    this.flushDeleteEntities(entityClass, ((Map)this.deletedObjects.get(entityClass)).values());
                    this.deletedObjects.remove(entityClass);
                }
            }
 
            if (this.deletedObjects.size() > 0) {
                var1 = this.deletedObjects.keySet().iterator();
 
                while(var1.hasNext()) {
                    entityClass = (Class)var1.next();
                    this.flushDeleteEntities(entityClass, ((Map)this.deletedObjects.get(entityClass)).values());
                    this.flushBulkDeletes(entityClass);
                }
            }
 
            this.deletedObjects.clear();
        }
    }
 
    protected void flushBulkDeletes(Class<? extends Entity> entityClass) {
        if (this.bulkDeleteOperations.containsKey(entityClass)) {
            Iterator var2 = ((List)this.bulkDeleteOperations.get(entityClass)).iterator();
 
            while(var2.hasNext()) {
                BulkDeleteOperation bulkDeleteOperation = (BulkDeleteOperation)var2.next();
                bulkDeleteOperation.execute(this.sqlSession);
            }
        }
 
    }
 
    protected void flushDeleteEntities(Class<? extends Entity> entityClass, Collection<Entity> entitiesToDelete) {
        Iterator var3 = entitiesToDelete.iterator();
 
        while(var3.hasNext()) {
            Entity entity = (Entity)var3.next();
            String deleteStatement = this.dbSqlSessionFactory.getDeleteStatement(entity.getClass());
            deleteStatement = this.dbSqlSessionFactory.mapStatement(deleteStatement);
            if (deleteStatement == null) {
                throw new ActivitiException("no delete statement for " + entity.getClass() + " in the ibatis mapping files");
            }
 
            if (entity instanceof HasRevision) {
                int nrOfRowsDeleted = this.sqlSession.delete(deleteStatement, entity);
                if (nrOfRowsDeleted == 0) {
                    throw new ActivitiOptimisticLockingException(entity + " was updated by another transaction concurrently");
                }
            } else {
                this.sqlSession.delete(deleteStatement, entity);
            }
        }
 
    }
 
    public void close() {
        this.sqlSession.close();
    }
 
    public void commit() {
        this.sqlSession.commit();
    }
 
    public void rollback() {
        this.sqlSession.rollback();
    }
 
    public void dbSchemaCheckVersion() {
        try {
            String dbVersion = this.getDbVersion();
            if (!"6.0.0.4".equals(dbVersion)) {
                throw new ActivitiWrongDbException("6.0.0.4", dbVersion);
            }
 
            String errorMessage = null;
            if (!this.isEngineTablePresent()) {
                errorMessage = this.addMissingComponent(errorMessage, "engine");
            }
 
            if (this.dbSqlSessionFactory.isDbHistoryUsed() && !this.isHistoryTablePresent()) {
                errorMessage = this.addMissingComponent(errorMessage, "history");
            }
 
            if (this.dbSqlSessionFactory.isDbIdentityUsed() && !this.isIdentityTablePresent()) {
                errorMessage = this.addMissingComponent(errorMessage, "identity");
            }
 
            if (errorMessage != null) {
                throw new ActivitiException("Activiti database problem: " + errorMessage);
            }
        } catch (Exception var3) {
            Exception e = var3;
            if (this.isMissingTablesException(e)) {
                throw new ActivitiException("no activiti tables in db. set <property name=\"databaseSchemaUpdate\" to value=\"true\" or value=\"create-drop\" (use create-drop for testing only!) in bean processEngineConfiguration in activiti.cfg.xml for automatic schema creation", e);
            }
 
            if (e instanceof RuntimeException) {
                throw (RuntimeException)e;
            }
 
            throw new ActivitiException("couldn't get db schema version", e);
        }
 
        log.debug("activiti db schema check successful");
    }
 
    protected String addMissingComponent(String missingComponents, String component) {
        return missingComponents == null ? "Tables missing for component(s) " + component : missingComponents + ", " + component;
    }
 
    protected String getDbVersion() {
        String selectSchemaVersionStatement = this.dbSqlSessionFactory.mapStatement("selectDbSchemaVersion");
        return (String)this.sqlSession.selectOne(selectSchemaVersionStatement);
    }
 
    public void dbSchemaCreate() {
        if (this.isEngineTablePresent()) {
            String dbVersion = this.getDbVersion();
            if (!"6.0.0.4".equals(dbVersion)) {
                throw new ActivitiWrongDbException("6.0.0.4", dbVersion);
            }
        } else {
            this.dbSchemaCreateEngine();
        }
 
        if (this.dbSqlSessionFactory.isDbHistoryUsed()) {
            this.dbSchemaCreateHistory();
        }
 
        if (this.dbSqlSessionFactory.isDbIdentityUsed()) {
            this.dbSchemaCreateIdentity();
        }
 
    }
 
    protected void dbSchemaCreateIdentity() {
        this.executeMandatorySchemaResource("create", "identity");
    }
 
    protected void dbSchemaCreateHistory() {
        this.executeMandatorySchemaResource("create", "history");
    }
 
    protected void dbSchemaCreateEngine() {
        this.executeMandatorySchemaResource("create", "engine");
    }
 
    public void dbSchemaDrop() {
        this.executeMandatorySchemaResource("drop", "engine");
        if (this.dbSqlSessionFactory.isDbHistoryUsed()) {
            this.executeMandatorySchemaResource("drop", "history");
        }
 
        if (this.dbSqlSessionFactory.isDbIdentityUsed()) {
            this.executeMandatorySchemaResource("drop", "identity");
        }
 
    }
 
    public void dbSchemaPrune() {
        if (this.isHistoryTablePresent() && !this.dbSqlSessionFactory.isDbHistoryUsed()) {
            this.executeMandatorySchemaResource("drop", "history");
        }
 
        if (this.isIdentityTablePresent() && this.dbSqlSessionFactory.isDbIdentityUsed()) {
            this.executeMandatorySchemaResource("drop", "identity");
        }
 
    }
 
    public void executeMandatorySchemaResource(String operation, String component) {
        this.executeSchemaResource(operation, component, this.getResourceForDbOperation(operation, operation, component), false);
    }
 
    public String dbSchemaUpdate() {
        String feedback = null;
        boolean isUpgradeNeeded = false;
        int matchingVersionIndex = -1;
        if (this.isEngineTablePresent()) {
            PropertyEntity dbVersionProperty = (PropertyEntity)this.selectById(PropertyEntity.class, "schema.version");
            String dbVersion = dbVersionProperty.getValue();
            matchingVersionIndex = this.findMatchingVersionIndex(dbVersion);
            if (matchingVersionIndex < 0 && dbVersion != null && dbVersion.startsWith("5.")) {
                matchingVersionIndex = this.findMatchingVersionIndex("5.99.0.0");
            }
 
            if (matchingVersionIndex < 0) {
                throw new ActivitiException("Could not update Activiti database schema: unknown version from database: '" + dbVersion + "'");
            }
 
            isUpgradeNeeded = matchingVersionIndex != ACTIVITI_VERSIONS.size() - 1;
            if (isUpgradeNeeded) {
                dbVersionProperty.setValue("6.0.0.4");
                PropertyEntity dbHistoryProperty;
                if ("5.0".equals(dbVersion)) {
                    dbHistoryProperty = (PropertyEntity)Context.getCommandContext().getPropertyEntityManager().create();
                    dbHistoryProperty.setName("schema.history");
                    dbHistoryProperty.setValue("create(5.0)");
                    this.insert(dbHistoryProperty);
                } else {
                    dbHistoryProperty = (PropertyEntity)this.selectById(PropertyEntity.class, "schema.history");
                }
 
                String dbHistoryValue = dbHistoryProperty.getValue() + " upgrade(" + dbVersion + "->" + "6.0.0.4" + ")";
                dbHistoryProperty.setValue(dbHistoryValue);
                this.dbSchemaUpgrade("engine", matchingVersionIndex);
                feedback = "upgraded Activiti from " + dbVersion + " to " + "6.0.0.4";
            }
        } else {
            this.dbSchemaCreateEngine();
        }
 
        if (this.isHistoryTablePresent()) {
            if (isUpgradeNeeded) {
                this.dbSchemaUpgrade("history", matchingVersionIndex);
            }
        } else if (this.dbSqlSessionFactory.isDbHistoryUsed()) {
            this.dbSchemaCreateHistory();
        }
 
        if (this.isIdentityTablePresent()) {
            if (isUpgradeNeeded) {
                this.dbSchemaUpgrade("identity", matchingVersionIndex);
            }
        } else if (this.dbSqlSessionFactory.isDbIdentityUsed()) {
            this.dbSchemaCreateIdentity();
        }
 
        return feedback;
    }
 
    protected int findMatchingVersionIndex(String dbVersion) {
        int index = 0;
        int matchingVersionIndex = -1;
 
        while(matchingVersionIndex < 0 && index < ACTIVITI_VERSIONS.size()) {
            if (((ActivitiVersion)ACTIVITI_VERSIONS.get(index)).matches(dbVersion)) {
                matchingVersionIndex = index;
            } else {
                ++index;
            }
        }
 
        return matchingVersionIndex;
    }
 
    public boolean isEngineTablePresent() {
        return this.isTablePresent("ACT_RU_EXECUTION");
    }
 
    public boolean isHistoryTablePresent() {
        return this.isTablePresent("ACT_HI_PROCINST");
    }
 
    public boolean isIdentityTablePresent() {
        return this.isTablePresent("ACT_ID_USER");
    }
 
    public boolean isTablePresent(String tableName) {
        if (!this.dbSqlSessionFactory.isTablePrefixIsSchema()) {
            tableName = this.prependDatabaseTablePrefix(tableName);
        }
 
        Connection connection = null;
 
        try {
            connection = this.sqlSession.getConnection();
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            ResultSet tables = null;
            String catalog = this.connectionMetadataDefaultCatalog;
            if (this.dbSqlSessionFactory.getDatabaseCatalog() != null && this.dbSqlSessionFactory.getDatabaseCatalog().length() > 0) {
                catalog = this.dbSqlSessionFactory.getDatabaseCatalog();
            }
 
            String schema = this.connectionMetadataDefaultSchema;
            if (this.dbSqlSessionFactory.getDatabaseSchema() != null && this.dbSqlSessionFactory.getDatabaseSchema().length() > 0) {
                schema = this.dbSqlSessionFactory.getDatabaseSchema();
            }
 
            String databaseType = this.dbSqlSessionFactory.getDatabaseType();
            if ("postgres".equals(databaseType)) {
                tableName = tableName.toLowerCase();
            }
 
            if (schema != null && "oracle".equals(databaseType)) {
                schema = schema.toUpperCase();
            }
 
            if (catalog != null && catalog.length() == 0) {
                catalog = null;
            }
 
            boolean var8;
            try {
                tables = databaseMetaData.getTables(catalog, schema, tableName, JDBC_METADATA_TABLE_TYPES);
                var8 = tables.next();
            } finally {
                try {
                    tables.close();
                } catch (Exception var16) {
                    Exception e = var16;
                    log.error("Error closing meta data tables", e);
                }
 
            }
 
            return var8;
        } catch (Exception var18) {
            Exception e = var18;
            throw new ActivitiException("couldn't check if tables are already present using metadata: " + e.getMessage(), e);
        }
    }
 
    protected boolean isUpgradeNeeded(String versionInDatabase) {
        if ("6.0.0.4".equals(versionInDatabase)) {
            return false;
        } else {
            String cleanDbVersion = this.getCleanVersion(versionInDatabase);
            String[] cleanDbVersionSplitted = cleanDbVersion.split("\\.");
            int dbMajorVersion = Integer.valueOf(cleanDbVersionSplitted[0]);
            int dbMinorVersion = Integer.valueOf(cleanDbVersionSplitted[1]);
            String cleanEngineVersion = this.getCleanVersion("6.0.0.4");
            String[] cleanEngineVersionSplitted = cleanEngineVersion.split("\\.");
            int engineMajorVersion = Integer.valueOf(cleanEngineVersionSplitted[0]);
            int engineMinorVersion = Integer.valueOf(cleanEngineVersionSplitted[1]);
            if (dbMajorVersion <= engineMajorVersion && (dbMajorVersion > engineMajorVersion || dbMinorVersion <= engineMinorVersion)) {
                if (cleanDbVersion.compareTo(cleanEngineVersion) == 0) {
                    log.warn("Engine-version is the same, but not an exact match: {} vs. {}. Not performing database-upgrade.", versionInDatabase, "6.0.0.4");
                    return false;
                } else {
                    return true;
                }
            } else {
                throw new ActivitiException("Version of activiti database (" + versionInDatabase + ") is more recent than the engine (" + "6.0.0.4" + ")");
            }
        }
    }
 
    protected String getCleanVersion(String versionString) {
        Matcher matcher = CLEAN_VERSION_REGEX.matcher(versionString);
        if (!matcher.find()) {
            throw new ActivitiException("Illegal format for version: " + versionString);
        } else {
            String cleanString = matcher.group();
 
            try {
                Double.parseDouble(cleanString);
                return cleanString;
            } catch (NumberFormatException var5) {
                throw new ActivitiException("Illegal format for version: " + versionString);
            }
        }
    }
 
    protected String prependDatabaseTablePrefix(String tableName) {
        return this.dbSqlSessionFactory.getDatabaseTablePrefix() + tableName;
    }
 
    protected void dbSchemaUpgrade(String component, int currentDatabaseVersionsIndex) {
        ActivitiVersion activitiVersion = (ActivitiVersion)ACTIVITI_VERSIONS.get(currentDatabaseVersionsIndex);
        String dbVersion = activitiVersion.getMainVersion();
        log.info("upgrading activiti {} schema from {} to {}", new Object[]{component, dbVersion, "6.0.0.4"});
 
        for(int i = currentDatabaseVersionsIndex + 1; i < ACTIVITI_VERSIONS.size(); ++i) {
            String nextVersion = ((ActivitiVersion)ACTIVITI_VERSIONS.get(i)).getMainVersion();
            if (nextVersion.endsWith("-SNAPSHOT")) {
                nextVersion = nextVersion.substring(0, nextVersion.length() - "-SNAPSHOT".length());
            }
 
            dbVersion = dbVersion.replace(".", "");
            nextVersion = nextVersion.replace(".", "");
            log.info("Upgrade needed: {} -> {}. Looking for schema update resource for component '{}'", new Object[]{dbVersion, nextVersion, component});
            this.executeSchemaResource("upgrade", component, this.getResourceForDbOperation("upgrade", "upgradestep." + dbVersion + ".to." + nextVersion, component), true);
            dbVersion = nextVersion;
        }
 
    }
 
    public String getResourceForDbOperation(String directory, String operation, String component) {
        String databaseType = this.dbSqlSessionFactory.getDatabaseType();
        // 当databaseType 为dm时,借用oracle的sql文件来代替执行
        if (ProcessEngineConfigurationImpl.DATABASE_TYPE_DM.equals(databaseType)) {
            databaseType = ProcessEngineConfigurationImpl.DATABASE_TYPE_ORACLE;
        }
        return "org/activiti/db/" + directory + "/activiti." + databaseType + "." + operation + "." + component + ".sql";
    }
 
    public void executeSchemaResource(String operation, String component, String resourceName, boolean isOptional) {
        InputStream inputStream = null;
 
        try {
            inputStream = ReflectUtil.getResourceAsStream(resourceName);
            if (inputStream == null) {
                if (!isOptional) {
                    throw new ActivitiException("resource '" + resourceName + "' is not available");
                }
 
                log.info("no schema resource {} for {}", resourceName, operation);
            } else {
                this.executeSchemaResource(operation, component, resourceName, inputStream);
            }
        } finally {
            IoUtil.closeSilently(inputStream);
        }
 
    }
 
    private void executeSchemaResource(String operation, String component, String resourceName, InputStream inputStream) {
        log.info("performing {} on {} with resource {}", new Object[]{operation, component, resourceName});
        String sqlStatement = null;
        String exceptionSqlStatement = null;
 
        try {
            Connection connection = this.sqlSession.getConnection();
            Exception exception = null;
            byte[] bytes = IoUtil.readInputStream(inputStream, resourceName);
            String ddlStatements = new String(bytes);
 
            try {
                if (this.isMysql()) {
                    DatabaseMetaData databaseMetaData = connection.getMetaData();
                    int majorVersion = databaseMetaData.getDatabaseMajorVersion();
                    int minorVersion = databaseMetaData.getDatabaseMinorVersion();
                    log.info("Found MySQL: majorVersion=" + majorVersion + " minorVersion=" + minorVersion);
                    if (majorVersion <= 5 && minorVersion < 6) {
                        ddlStatements = this.updateDdlForMySqlVersionLowerThan56(ddlStatements);
                    }
                }
            } catch (Exception var26) {
                Exception e = var26;
                log.info("Could not get database metadata", e);
            }
 
            BufferedReader reader = new BufferedReader(new StringReader(ddlStatements));
            String line = this.readNextTrimmedLine(reader);
 
            for(boolean inOraclePlsqlBlock = false; line != null; line = this.readNextTrimmedLine(reader)) {
                if (line.startsWith("# ")) {
                    log.debug(line.substring(2));
                } else if (line.startsWith("-- ")) {
                    log.debug(line.substring(3));
                } else {
                    Exception e;
                    if (line.startsWith("execute java ")) {
                        String upgradestepClassName = line.substring(13).trim();
                        e = null;
 
                        DbUpgradeStep dbUpgradeStep;
                        try {
                            dbUpgradeStep = (DbUpgradeStep)ReflectUtil.instantiate(upgradestepClassName);
                        } catch (ActivitiException var25) {
                            ActivitiException ex = var25;
                            throw new ActivitiException("database update java class '" + upgradestepClassName + "' can't be instantiated: " + ex.getMessage(), ex);
                        }
 
                        try {
                            log.debug("executing upgrade step java class {}", upgradestepClassName);
                            dbUpgradeStep.execute(this);
                        } catch (Exception var24) {
                            Exception ex = var24;
                            throw new ActivitiException("error while executing database update java class '" + upgradestepClassName + "': " + ex.getMessage(), ex);
                        }
                    } else if (line.length() > 0) {
                        if (this.isOracle() && line.startsWith("begin")) {
                            inOraclePlsqlBlock = true;
                            sqlStatement = this.addSqlStatementPiece(sqlStatement, line);
                        } else if ((!line.endsWith(";") || inOraclePlsqlBlock) && (!line.startsWith("/") || !inOraclePlsqlBlock)) {
                            sqlStatement = this.addSqlStatementPiece(sqlStatement, line);
                        } else {
                            if (inOraclePlsqlBlock) {
                                inOraclePlsqlBlock = false;
                            } else {
                                sqlStatement = this.addSqlStatementPiece(sqlStatement, line.substring(0, line.length() - 1));
                            }
 
                            Statement jdbcStatement = connection.createStatement();
 
                            try {
                                log.debug("SQL: {}", sqlStatement);
                                jdbcStatement.execute(sqlStatement);
                                jdbcStatement.close();
                            } catch (Exception var27) {
                                e = var27;
                                if (exception == null) {
                                    exception = e;
                                }
 
                                log.error("problem during schema {}, statement {}", new Object[]{operation, sqlStatement, e});
                            } finally {
                                sqlStatement = null;
                            }
                        }
                    }
                }
            }
 
            if (exception != null) {
                throw exception;
            } else {
                log.debug("activiti db schema {} for component {} successful", operation, component);
            }
        } catch (Exception var29) {
            Exception e = var29;
            throw new ActivitiException("couldn't " + operation + " db schema: " + exceptionSqlStatement, e);
        }
    }
 
    protected String updateDdlForMySqlVersionLowerThan56(String ddlStatements) {
        return ddlStatements.replace("timestamp(3)", "timestamp").replace("datetime(3)", "datetime").replace("TIMESTAMP(3)", "TIMESTAMP").replace("DATETIME(3)", "DATETIME");
    }
 
    protected String addSqlStatementPiece(String sqlStatement, String line) {
        return sqlStatement == null ? line : sqlStatement + " \n" + line;
    }
 
    protected String readNextTrimmedLine(BufferedReader reader) throws IOException {
        String line = reader.readLine();
        if (line != null) {
            line = line.trim();
        }
 
        return line;
    }
 
    protected boolean isMissingTablesException(Exception e) {
        String exceptionMessage = e.getMessage();
        if (e.getMessage() != null) {
            if (exceptionMessage.indexOf("Table") != -1 && exceptionMessage.indexOf("not found") != -1) {
                return true;
            }
 
            if ((exceptionMessage.indexOf("Table") != -1 || exceptionMessage.indexOf("table") != -1) && exceptionMessage.indexOf("doesn't exist") != -1) {
                return true;
            }
 
            if ((exceptionMessage.indexOf("relation") != -1 || exceptionMessage.indexOf("table") != -1) && exceptionMessage.indexOf("does not exist") != -1) {
                return true;
            }
        }
 
        return false;
    }
 
    public void performSchemaOperationsProcessEngineBuild() {
        String databaseSchemaUpdate = Context.getProcessEngineConfiguration().getDatabaseSchemaUpdate();
        log.debug("Executing performSchemaOperationsProcessEngineBuild with setting " + databaseSchemaUpdate);
        if ("drop-create".equals(databaseSchemaUpdate)) {
            try {
                this.dbSchemaDrop();
            } catch (RuntimeException var3) {
            }
        }
 
        if (!"create-drop".equals(databaseSchemaUpdate) && !"drop-create".equals(databaseSchemaUpdate) && !"create".equals(databaseSchemaUpdate)) {
            if ("false".equals(databaseSchemaUpdate)) {
                this.dbSchemaCheckVersion();
            } else if ("true".equals(databaseSchemaUpdate)) {
                this.dbSchemaUpdate();
            }
        } else {
            this.dbSchemaCreate();
        }
 
    }
 
    public void performSchemaOperationsProcessEngineClose() {
        String databaseSchemaUpdate = Context.getProcessEngineConfiguration().getDatabaseSchemaUpdate();
        if ("create-drop".equals(databaseSchemaUpdate)) {
            this.dbSchemaDrop();
        }
 
    }
 
    public <T> T getCustomMapper(Class<T> type) {
        return this.sqlSession.getMapper(type);
    }
 
    public boolean isMysql() {
        return this.dbSqlSessionFactory.getDatabaseType().equals("mysql");
    }
 
    public boolean isOracle() {
        return this.dbSqlSessionFactory.getDatabaseType().equals("oracle");
    }
 
    public DeploymentQueryImpl createDeploymentQuery() {
        return new DeploymentQueryImpl();
    }
 
    public ModelQueryImpl createModelQueryImpl() {
        return new ModelQueryImpl();
    }
 
    public ProcessDefinitionQueryImpl createProcessDefinitionQuery() {
        return new ProcessDefinitionQueryImpl();
    }
 
    public ProcessInstanceQueryImpl createProcessInstanceQuery() {
        return new ProcessInstanceQueryImpl();
    }
 
    public ExecutionQueryImpl createExecutionQuery() {
        return new ExecutionQueryImpl();
    }
 
    public TaskQueryImpl createTaskQuery() {
        return new TaskQueryImpl();
    }
 
    public JobQueryImpl createJobQuery() {
        return new JobQueryImpl();
    }
 
    public HistoricProcessInstanceQueryImpl createHistoricProcessInstanceQuery() {
        return new HistoricProcessInstanceQueryImpl();
    }
 
    public HistoricActivityInstanceQueryImpl createHistoricActivityInstanceQuery() {
        return new HistoricActivityInstanceQueryImpl();
    }
 
    public HistoricTaskInstanceQueryImpl createHistoricTaskInstanceQuery() {
        return new HistoricTaskInstanceQueryImpl();
    }
 
    public HistoricDetailQueryImpl createHistoricDetailQuery() {
        return new HistoricDetailQueryImpl();
    }
 
    public HistoricVariableInstanceQueryImpl createHistoricVariableInstanceQuery() {
        return new HistoricVariableInstanceQueryImpl();
    }
 
    public UserQueryImpl createUserQuery() {
        return new UserQueryImpl();
    }
 
    public GroupQueryImpl createGroupQuery() {
        return new GroupQueryImpl();
    }
 
    public SqlSession getSqlSession() {
        return this.sqlSession;
    }
 
    public DbSqlSessionFactory getDbSqlSessionFactory() {
        return this.dbSqlSessionFactory;
    }
 
    static {
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.7"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.8"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.9"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.10"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.11"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.12", Arrays.asList("5.12.1", "5.12T")));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.13"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.14"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.15"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.15.1"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.1"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.2-SNAPSHOT"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.2"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.3.0"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.16.4.0"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.17.0.0"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.17.0.1"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.17.0.2"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.18.0.0"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.18.0.1"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.20.0.0"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.20.0.1"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.20.0.2"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.21.0.0"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.22.0.0"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("5.99.0.0"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("6.0.0.0"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("6.0.0.1"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("6.0.0.2"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("6.0.0.3"));
        ACTIVITI_VERSIONS.add(new ActivitiVersion("6.0.0.4"));
        JDBC_METADATA_TABLE_TYPES = new String[]{"TABLE"};
    }
}