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
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.test;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.Table;
import org.jbpm.api.ProcessEngine;
import org.jbpm.internal.log.Log;
 
 
/**
 * @author Tom Baeyens
 */
public class Db {
  
  private static final Log log = Log.getLog(Db.class.getName());
 
  static Map<ProcessEngine, String[]> cleanSqlCache = new HashMap<ProcessEngine, String[]>();
  static Map<ProcessEngine, String[]> tableNamesCache = new HashMap<ProcessEngine, String[]>();
  
  public static void clean(ProcessEngine processEngine) {
    SessionFactory sessionFactory = processEngine.get(SessionFactory.class);
    // when running this with a remote ejb invocation configuration, there is no
    // session factory and no cleanup needs to be done
    if (sessionFactory==null) {
      return;
    }
    
    String[] cleanSql = cleanSqlCache.get(processEngine);
 
    if (cleanSql == null) {
      Configuration configuration = processEngine.get(Configuration.class);
      
      SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
      Dialect dialect = sessionFactoryImplementor.getDialect();
 
      // loop over all foreign key constraints
      List<String> dropForeignKeysSql = new ArrayList<String>();
      List<String> createForeignKeysSql = new ArrayList<String>();
      Iterator<Table> iter = configuration.getTableMappings();
      
      //if no session-factory is build, the configuration is not fully initialized.
      //Hence, the ForeignKey's won't have a referenced table. This is calculated on 
      //second pass.
      configuration.buildMappings();
      
      
      while (iter.hasNext()) {
        Table table = (Table) iter.next();
        if (table.isPhysicalTable()) {
          String catalog = table.getCatalog();
          String schema = table.getSchema();
          Iterator<ForeignKey> subIter = table.getForeignKeyIterator();
          while (subIter.hasNext()) {
            ForeignKey fk = (ForeignKey) subIter.next();
            if (fk.isPhysicalConstraint()) {
              // collect the drop foreign key constraint sql
              dropForeignKeysSql.add(fk.sqlDropString(dialect, catalog, schema));
              // MySQLDialect creates an index for each foreign key.
              // see
              // http://opensource.atlassian.com/projects/hibernate/browse/HHH-2155
              // This index should be dropped or an error will be thrown during
              // the creation phase
              if (dialect instanceof MySQLDialect) {
                dropForeignKeysSql.add("alter table " + table.getName() + " drop key " + fk.getName());
              }
              // and collect the create foreign key constraint sql
              createForeignKeysSql.add(fk.sqlCreateString(dialect, sessionFactoryImplementor, catalog, schema));
            }
          }
        }
      }
 
      List<String> deleteSql = new ArrayList<String>();
      iter = configuration.getTableMappings();
      while (iter.hasNext()) {
        Table table = (Table) iter.next();
        if (table.isPhysicalTable()) {
          deleteSql.add("delete from " + table.getName());
        }
      }
 
      // glue
      // - drop foreign key constraints
      // - delete contents of all tables
      // - create foreign key constraints
      // together to form the clean script
      List<String> cleanSqlList = new ArrayList<String>();
      cleanSqlList.addAll(dropForeignKeysSql);
      cleanSqlList.addAll(deleteSql);
      cleanSqlList.addAll(createForeignKeysSql);
 
      cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]);
      
      cleanSqlCache.put(processEngine, cleanSql);
    }
 
    Session session = sessionFactory.openSession();
    try {
      for (String query : cleanSql) {
        // log.trace(query);
        session.createSQLQuery(query).executeUpdate();
      }
    } finally {
      session.close();
    }
  }
 
  public static String verifyClean(ProcessEngine processEngine) {
    SessionFactory sessionFactory = processEngine.get(SessionFactory.class);
    // when running this with a remote ejb invocation configuration, there is no
    // session factory and no cleanup needs to be done
    if (sessionFactory==null) {
      return null;
    }
    
    String[] tableNames = tableNamesCache.get(processEngine);
 
    if (tableNames == null) {
      Configuration configuration = processEngine.get(Configuration.class);
      
      // loop over all foreign key constraints
      List<String> tableNamesList = new ArrayList<String>();
      Iterator iter = configuration.getTableMappings();
      while (iter.hasNext()) {
        Table table = (Table) iter.next();
        if (table.isPhysicalTable()) {
          tableNamesList.add(table.getName());
        }
      }
 
      tableNames = tableNamesList.toArray(new String[tableNamesList.size()]);
      
      tableNamesCache.put(processEngine, tableNames);
    }
 
    String recordsLeftMsg = "";
    Session session = sessionFactory.openSession();
    try {
      for (String tableName : tableNames) {
        if (!"JBPM4_PROPERTY".equals(tableName)) {
          String countSql = "select count(*) as RECORD_COUNT_ from "+tableName;
          SQLQuery sqlQuery = session.createSQLQuery(countSql);
          sqlQuery.addScalar("RECORD_COUNT_", Hibernate.LONG);
          Long recordCount = (Long) sqlQuery.uniqueResult();
          if (recordCount>0L) {
            recordsLeftMsg += tableName+":"+recordCount+", ";
          }
        }
      }
    } finally {
      session.close();
    }
    
    if (recordsLeftMsg.length()>0) {
      clean(processEngine);
    }
    
    return recordsLeftMsg;
  }
}