1
|
/**
|
2
|
* Copyright (C) 2008 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*/
|
6
|
|
7
|
package eu.etaxonomy.cdm.test.integration;
|
8
|
|
9
|
import java.sql.ResultSet;
|
10
|
import java.sql.SQLException;
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.HashMap;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.springframework.transaction.TransactionStatus;
|
18
|
|
19
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
20
|
import eu.etaxonomy.cdm.common.AccountStore;
|
21
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
22
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
23
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
24
|
import eu.etaxonomy.cdm.io.common.Source;
|
25
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
28
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
29
|
import eu.etaxonomy.cdm.model.common.Representation;
|
30
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
31
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
|
32
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
33
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
34
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
35
|
|
36
|
/**
|
37
|
* @author a.babadshanjan
|
38
|
* @created 15.09.2008
|
39
|
*/
|
40
|
public class TestCdmDbComparator {
|
41
|
|
42
|
private static final String sourceDbOne = "cdm_test_jaxb";
|
43
|
private static final String sourceDbTwo = "cdm_test_jaxb2";
|
44
|
|
45
|
private static final ICdmDataSource sourceOne = TestCdmDbComparator.CDM_DB(sourceDbOne);
|
46
|
private static final ICdmDataSource sourceTwo = TestCdmDbComparator.CDM_DB(sourceDbTwo);
|
47
|
|
48
|
private static final String server = "192.168.2.10";
|
49
|
private static final String username = "edit";
|
50
|
|
51
|
public static ICdmDataSource CDM_DB(String dbname) {
|
52
|
|
53
|
String password = AccountStore.readOrStorePassword(dbname, server, username, null);
|
54
|
ICdmDataSource datasource = CdmDataSource.NewMySqlInstance(server, dbname, username, password);
|
55
|
return datasource;
|
56
|
}
|
57
|
|
58
|
private final int MAX_ROWS = 60000;
|
59
|
private final int MAX_TABLES = 150;
|
60
|
|
61
|
private static final Logger logger = Logger.getLogger(TestCdmDbComparator.class);
|
62
|
|
63
|
private static final String[] table_list = {
|
64
|
// "Address",
|
65
|
"Agent",
|
66
|
// "Agent_Agent",
|
67
|
// "Agent_Annotation",
|
68
|
// "Agent_DefinedTermBase",
|
69
|
// "Agent_Extension",
|
70
|
// "Agent_InstitutionalMembership",
|
71
|
// "Agent_Marker",
|
72
|
// "Agent_Media",
|
73
|
// "Agent_OriginalSource",
|
74
|
// "Agent_Rights",
|
75
|
"Annotation",
|
76
|
// "CDM_VIEW",
|
77
|
// "CDM_VIEW_CDM_VIEW",
|
78
|
// "Collection",
|
79
|
// "Collection_Annotation",
|
80
|
// "Collection_Extension",
|
81
|
// "Collection_Marker",
|
82
|
// "Collection_Media",
|
83
|
// "Collection_OriginalSource",
|
84
|
// "Collection_Rights",
|
85
|
// "Contact",
|
86
|
"DefinedTermBase",
|
87
|
// "DefinedTermBase_DefinedTermBase",
|
88
|
// "DefinedTermBase_Media",
|
89
|
// "DefinedTermBase_Representation",
|
90
|
// "DefinedTermBase_TermVocabulary",
|
91
|
// "DerivationEvent",
|
92
|
// "DerivationEvent_Annotation",
|
93
|
// "DerivationEvent_Marker",
|
94
|
"DescriptionBase",
|
95
|
// "DescriptionBase_Annotation",
|
96
|
// "DescriptionBase_DefinedTermBase",
|
97
|
// "DescriptionBase_DescriptionElementBase",
|
98
|
// "DescriptionBase_Extension",
|
99
|
// "DescriptionBase_Marker",
|
100
|
// "DescriptionBase_OriginalSource",
|
101
|
// "DescriptionBase_Rights",
|
102
|
"DescriptionElementBase",
|
103
|
// "DescriptionElementBase_Annotation",
|
104
|
// "DescriptionElementBase_DefinedTermBase",
|
105
|
// "DescriptionElementBase_LanguageString",
|
106
|
// "DescriptionElementBase_Marker",
|
107
|
// "DescriptionElementBase_Media",
|
108
|
// "DescriptionElementBase_StatisticalMeasurementValue",
|
109
|
// "DeterminationEvent",
|
110
|
// "DeterminationEvent_Annotation",
|
111
|
// "DeterminationEvent_Marker",
|
112
|
// "Extension",
|
113
|
"FeatureNode",
|
114
|
"FeatureTree",
|
115
|
// "FeatureTree_Representation",
|
116
|
// "GatheringEvent",
|
117
|
// "GatheringEvent_Annotation",
|
118
|
// "GatheringEvent_Marker",
|
119
|
"HomotypicalGroup",
|
120
|
// "HomotypicalGroup_Annotation",
|
121
|
// "HomotypicalGroup_Marker",
|
122
|
// "HybridRelationship",
|
123
|
// "HybridRelationship_Annotation",
|
124
|
// "HybridRelationship_Marker",
|
125
|
// "InstitutionalMembership",
|
126
|
"LanguageString",
|
127
|
// "Locus",
|
128
|
"Marker",
|
129
|
"Media",
|
130
|
"MediaRepresentation",
|
131
|
"MediaRepresentationPart",
|
132
|
// "Media_Annotation",
|
133
|
// "Media_Marker",
|
134
|
// "Media_Rights",
|
135
|
// "Media_Sequence",
|
136
|
// "MediaKey_CoveredTaxon",
|
137
|
"NameRelationship",
|
138
|
// "NameRelationship_Annotation",
|
139
|
// "NameRelationship_Marker",
|
140
|
"NomenclaturalStatus",
|
141
|
// "NomenclaturalStatus_Annotation",
|
142
|
// "NomenclaturalStatus_Marker",
|
143
|
"OriginalSource",
|
144
|
// "OriginalSource_Annotation",
|
145
|
// "OriginalSource_Marker",
|
146
|
// "Person_Keyword",
|
147
|
"Reference",
|
148
|
// "ReferenceBase_Annotation",
|
149
|
// "ReferenceBase_Extension",
|
150
|
// "ReferenceBase_Marker",
|
151
|
// "ReferenceBase_Media",
|
152
|
// "ReferenceBase_OriginalSource",
|
153
|
// "ReferenceBase_Rights",
|
154
|
// "RelationshipTermBase_inverseRepresentation",
|
155
|
"Representation",
|
156
|
// "Rights",
|
157
|
// "Sequence",
|
158
|
// "Sequence_Annotation",
|
159
|
// "Sequence_Extension",
|
160
|
// "Sequence_Marker",
|
161
|
// "Sequence_Media",
|
162
|
// "Sequence_OriginalSource",
|
163
|
// "Sequence_ReferenceBase",
|
164
|
// "Sequence_Rights",
|
165
|
"SpecimenOrObservationBase",
|
166
|
// "SpecimenOrObservationBase_Annotation",
|
167
|
// "SpecimenOrObservationBase_DerivationEvent",
|
168
|
// "SpecimenOrObservationBase_Extension",
|
169
|
// "SpecimenOrObservationBase_Marker",
|
170
|
// "SpecimenOrObservationBase_Media",
|
171
|
// "SpecimenOrObservationBase_OriginalSource",
|
172
|
// "SpecimenOrObservationBase_Rights",
|
173
|
// "StateData",
|
174
|
// "StateData_DefinedTermBase",
|
175
|
// "StatisticalMeasurementValue",
|
176
|
// "StatisticalMeasurementValue_DefinedTermBase",
|
177
|
"SynonymRelationship",
|
178
|
// "SynonymRelationship_Annotation",
|
179
|
// "SynonymRelationship_Marker",
|
180
|
"TaxonBase",
|
181
|
// "TaxonBase_Annotation",
|
182
|
// "TaxonBase_Extension",
|
183
|
// "TaxonBase_Marker",
|
184
|
// "TaxonBase_OriginalSource",
|
185
|
// "TaxonBase_Rights",
|
186
|
"TaxonNameBase",
|
187
|
// "TaxonNameBase_Annotation",
|
188
|
// "TaxonNameBase_Extension",
|
189
|
// "TaxonNameBase_HybridRelationship",
|
190
|
// "TaxonNameBase_Marker",
|
191
|
// "TaxonNameBase_NomenclaturalStatus",
|
192
|
// "TaxonNameBase_OriginalSource",
|
193
|
// "TaxonNameBase_Rights",
|
194
|
// "TaxonNameBase_TypeDesignationBase",
|
195
|
"TaxonRelationship",
|
196
|
// "TaxonRelationship_Annotation",
|
197
|
// "TaxonRelationship_Marker",
|
198
|
"TermVocabulary",
|
199
|
// "TermVocabulary_Representation",
|
200
|
"TypeDesignationBase",
|
201
|
// "TypeDesignationBase_Annotation",
|
202
|
// "TypeDesignationBase_Marker",
|
203
|
};
|
204
|
|
205
|
// @Autowired
|
206
|
// @Qualifier("cdmDao")
|
207
|
// private ICdmEntityDao cdmDao;
|
208
|
|
209
|
// @Autowired
|
210
|
// protected void setDao(ICdmEntityDao dao) {
|
211
|
// logger.debug("setting DAO");
|
212
|
// this.cdmDao = dao;
|
213
|
// }
|
214
|
|
215
|
// @Autowired
|
216
|
// private SessionFactory factory;
|
217
|
//
|
218
|
// protected Session getSession(){
|
219
|
// Session session = factory.getCurrentSession();
|
220
|
// return session;
|
221
|
//}
|
222
|
|
223
|
// public ResultSet getResultSet (String query){
|
224
|
// ResultSet rs;
|
225
|
// try {
|
226
|
// this.getConnection(); //establish connection
|
227
|
// if (query == null){
|
228
|
// return null;
|
229
|
// }
|
230
|
// mStmt = mConn.createStatement();
|
231
|
// rs = mStmt.executeQuery(query);
|
232
|
// return rs;
|
233
|
// }catch(SQLException e){
|
234
|
// logger.error("Problems when creating Resultset for query \n " + query + " \n" + "Exception: " + e);
|
235
|
// return null;
|
236
|
// }
|
237
|
// }
|
238
|
|
239
|
private Map<String, List<String>> doLoadDataFromDb(String dbname, Source source) {
|
240
|
|
241
|
Map<String, List<String>> dbTables = new HashMap<String, List<String>>();
|
242
|
|
243
|
logger.info("Loading data from DB " + dbname);
|
244
|
|
245
|
CdmApplicationController appCtr = null;
|
246
|
|
247
|
|
248
|
String password = AccountStore.readOrStorePassword(dbname, server, username, null);
|
249
|
|
250
|
DbSchemaValidation dbSchemaValidation = DbSchemaValidation.VALIDATE;
|
251
|
ICdmDataSource datasource = CdmDataSource.NewMySqlInstance(server, dbname, username, password);
|
252
|
appCtr = CdmApplicationController.NewInstance(datasource, dbSchemaValidation, true);
|
253
|
|
254
|
|
255
|
TransactionStatus txStatus = appCtr.startTransaction(true);
|
256
|
|
257
|
// get data from DB
|
258
|
|
259
|
try {
|
260
|
|
261
|
dbTables = retrieveAllTables(appCtr);
|
262
|
|
263
|
} catch (Exception e) {
|
264
|
logger.error("error setting data");
|
265
|
e.printStackTrace();
|
266
|
}
|
267
|
appCtr.commitTransaction(txStatus);
|
268
|
appCtr.close();
|
269
|
|
270
|
return dbTables;
|
271
|
|
272
|
}
|
273
|
|
274
|
private Map<String, List<CdmBase>> doLoadDataFromDb_(String dbname) {
|
275
|
|
276
|
Map<String, List<CdmBase>> dbTables = new HashMap<String, List<CdmBase>>();
|
277
|
|
278
|
logger.info("Loading data from DB " + dbname);
|
279
|
|
280
|
CdmApplicationController appCtr = null;
|
281
|
|
282
|
|
283
|
String password = AccountStore.readOrStorePassword(dbname, server, username, null);
|
284
|
|
285
|
DbSchemaValidation dbSchemaValidation = DbSchemaValidation.VALIDATE;
|
286
|
ICdmDataSource datasource = CdmDataSource.NewMySqlInstance(server, dbname, username, password);
|
287
|
appCtr = CdmApplicationController.NewInstance(datasource, dbSchemaValidation, true);
|
288
|
|
289
|
|
290
|
TransactionStatus txStatus = appCtr.startTransaction(true);
|
291
|
|
292
|
// get data from DB
|
293
|
|
294
|
try {
|
295
|
|
296
|
dbTables = retrieveAllTables_(appCtr);
|
297
|
|
298
|
} catch (Exception e) {
|
299
|
logger.error("error setting data");
|
300
|
e.printStackTrace();
|
301
|
}
|
302
|
appCtr.commitTransaction(txStatus);
|
303
|
appCtr.close();
|
304
|
|
305
|
return dbTables;
|
306
|
|
307
|
}
|
308
|
|
309
|
private Map<String, List<String>> doLoadDataFromDb__(String dbname, Source source) {
|
310
|
|
311
|
Map<String, List<String>> dbTables = new HashMap<String, List<String>>();
|
312
|
|
313
|
logger.info("Loading data from DB " + dbname);
|
314
|
|
315
|
try {
|
316
|
|
317
|
dbTables = retrieveAllTables__(source);
|
318
|
|
319
|
} catch (Exception e) {
|
320
|
logger.error("error setting data");
|
321
|
e.printStackTrace();
|
322
|
}
|
323
|
return dbTables;
|
324
|
|
325
|
}
|
326
|
|
327
|
private Map<String, List<CdmBase>> retrieveAllTables_(CdmApplicationController appCtr) {
|
328
|
|
329
|
Map<String, List<CdmBase>> tables_ = new HashMap<String, List<CdmBase>>(table_list.length);
|
330
|
|
331
|
List<String> tableRows = new ArrayList<String>(MAX_ROWS);
|
332
|
|
333
|
//List<Agent> agents = appCtr.getAgentService().getAllAgents(MAX_ROWS, 0);
|
334
|
|
335
|
try {
|
336
|
//get data from database
|
337
|
for (int i = 0; i < table_list.length; i++) {
|
338
|
|
339
|
logger.debug("Retrieving table '" + table_list[i] + "'");
|
340
|
System.out.println("Retrieving table '" + table_list[i] + "'");
|
341
|
|
342
|
List<CdmBase> rows = new ArrayList<CdmBase>(MAX_ROWS);
|
343
|
|
344
|
rows = appCtr.getMainService().rows(table_list[i], MAX_ROWS, 0);
|
345
|
|
346
|
tables_.put(table_list[i], rows);
|
347
|
|
348
|
}
|
349
|
|
350
|
} catch (Exception e) {
|
351
|
logger.error("error retrieving data");
|
352
|
e.printStackTrace();
|
353
|
}
|
354
|
// return tables;
|
355
|
return tables_;
|
356
|
}
|
357
|
|
358
|
private Map<String, List<String>> retrieveAllTables___(Source source) {
|
359
|
|
360
|
Map<String, List<String>> tables = new HashMap<String, List<String>>(table_list.length);
|
361
|
List<String> tableRows = new ArrayList<String>(MAX_ROWS);
|
362
|
|
363
|
try {
|
364
|
//get data from database
|
365
|
for (int i = 0; i < table_list.length; i++) {
|
366
|
|
367
|
List<String> rows = new ArrayList<String>(MAX_ROWS);
|
368
|
|
369
|
// Session session = factory.getCurrentSession();
|
370
|
|
371
|
// if ( sessionObject != null ) {
|
372
|
// session.update(sessionObject);
|
373
|
// }
|
374
|
// Query query = session.createQuery("select term from DefinedTermBase term join fetch term.representations representation where representation.label = :label");
|
375
|
// query.setParameter("label", queryString);
|
376
|
|
377
|
// Query query = session.createQuery("select from " + table_list[i]);
|
378
|
// rows = query.list();
|
379
|
|
380
|
//FIXME: NullPointerException (cdmDao is null)
|
381
|
//rows = cdmDao.list(MAX_ROWS, 0);
|
382
|
tables.put(table_list[i], rows);
|
383
|
|
384
|
}
|
385
|
|
386
|
} catch (Exception e) {
|
387
|
logger.error("error retrieving data");
|
388
|
e.printStackTrace();
|
389
|
}
|
390
|
return tables;
|
391
|
}
|
392
|
|
393
|
private Map<String, List<String>> retrieveAllTables__(Source source) {
|
394
|
|
395
|
// IImportConfigurator config = new BerlinModelImportConfigurator;
|
396
|
// BerlinModelImportConfigurator bmiConfig = (BerlinModelImportConfigurator)config;
|
397
|
// Source source = bmiConfig.getSource();
|
398
|
// ResultSet rs = berlinModelSource.getResultSet();
|
399
|
|
400
|
Map<String, List<String>> tables = new HashMap<String, List<String>>(table_list.length);
|
401
|
List<String> tableRows = new ArrayList<String>(MAX_ROWS);
|
402
|
|
403
|
try {
|
404
|
//get data from database
|
405
|
for (int i = 0; i < table_list.length; i++) {
|
406
|
String strQuery =
|
407
|
" SELECT * FROM " + table_list[i];
|
408
|
logger.debug("SQL Statement: " + strQuery);
|
409
|
//ResultSet rs = berlinModelSource.getResultSet();
|
410
|
//ResultSet rs = source.getResultSet(strQuery) ;
|
411
|
ResultSet rs = source.getResultSet(strQuery) ;
|
412
|
List<String> rows = new ArrayList<String>(MAX_ROWS);
|
413
|
|
414
|
while (rs.next()) {
|
415
|
rows.add(rs.toString());
|
416
|
}
|
417
|
tables.put(table_list[i], rows);
|
418
|
}
|
419
|
} catch (SQLException e) {
|
420
|
logger.error("SQLException:" + e);
|
421
|
}
|
422
|
return tables;
|
423
|
}
|
424
|
|
425
|
private Map<String, List<String>> retrieveAllTables(CdmApplicationController appCtr) {
|
426
|
|
427
|
Map<String, List<String>> tables = new HashMap<String, List<String>>(table_list.length);
|
428
|
|
429
|
List<String> agentTableContent = new ArrayList<String>(MAX_ROWS);
|
430
|
List<? extends AgentBase> agents = appCtr.getAgentService().list(null,MAX_ROWS, 0,null,null);
|
431
|
for (AgentBase agent: agents ) {
|
432
|
//TODO: Want the entire row as string not just toString() of the object.
|
433
|
agentTableContent.add(agent.toString());
|
434
|
}
|
435
|
tables.put("agents", agentTableContent);
|
436
|
|
437
|
//List<Annotation> annotations = appCtr.getTermService().getAllAnnotations(MAX_ROWS, 0);
|
438
|
|
439
|
List<String> definedTermBaseTableContent = new ArrayList<String>(MAX_ROWS);
|
440
|
List<DefinedTermBase> definedTermBases = appCtr.getTermService().list(null,MAX_ROWS, 0,null,null);
|
441
|
for (DefinedTermBase definedTermBase: definedTermBases ) {
|
442
|
definedTermBaseTableContent.add(definedTermBase.toString());
|
443
|
}
|
444
|
tables.put("definedTermBases", definedTermBaseTableContent);
|
445
|
|
446
|
//List<DescriptionBase> descriptionBases = appCtr.getDescriptionService().getAllDescriptionBases(MAX_ROWS, 0);
|
447
|
//List<DescriptionElementBase> descriptionElementBases = appCtr.getDescriptionService().getAllDescriptionElementBases(MAX_ROWS, 0);
|
448
|
//List<HomotypicalGroup> homotypicalGroups = appCtr.getNameService().getAllHomotypicalGroups(MAX_ROWS, 0);
|
449
|
List<LanguageString> languageStrings = appCtr.getTermService().getAllLanguageStrings(MAX_ROWS, 0);
|
450
|
//List<Marker> markers = appCtr.getTermService().getAllMarkers(MAX_ROWS, 0);
|
451
|
//List<NameRelationship> nameRelationships = appCtr.getNameService().getAllNameRelationships(MAX_ROWS, 0);
|
452
|
List<NomenclaturalStatus> nomenclaturalStatus = appCtr.getNameService().getAllNomenclaturalStatus(MAX_ROWS, 0);
|
453
|
//List<OriginalSource> originalSources = appCtr.getNameService().getAllOriginalSources(MAX_ROWS, 0);
|
454
|
List<Reference> references = appCtr.getReferenceService().list(null,MAX_ROWS, 0,null,null);
|
455
|
List<Representation> representations = appCtr.getTermService().getAllRepresentations(MAX_ROWS, 0);
|
456
|
List<SpecimenOrObservationBase> specimenOrObservationBases = appCtr.getOccurrenceService().list(null,MAX_ROWS, 0,null,null);
|
457
|
//List<SynonymRelationship> synonymRelationships = appCtr.getTaxonService().getAllSynonymRelationships(MAX_ROWS, 0);
|
458
|
// List<TaxonBase> taxonBases = appCtr.getTaxonService().getAllTaxa(MAX_ROWS, 0);
|
459
|
// List<TaxonNameBase> taxonNameBases = appCtr.getNameService().getAllNames(MAX_ROWS, 0);
|
460
|
//List<TaxonRelationship> taxonRelationships = appCtr.getTaxonService().getAllTaxonRelationships(MAX_ROWS, 0);
|
461
|
List<TermVocabulary> termVocabularies = appCtr.getVocabularyService().list(null,MAX_ROWS, 0,null,null);
|
462
|
List<TypeDesignationBase> typeDesignationBases = appCtr.getNameService().getAllTypeDesignations(MAX_ROWS, 0);
|
463
|
|
464
|
return tables;
|
465
|
}
|
466
|
|
467
|
private void compareTables(String tableName, List<CdmBase> tablesDbOne, List<CdmBase> tablesDbTwo) {
|
468
|
|
469
|
int tableOneSize = tablesDbOne.size();
|
470
|
int tableTwoSize = tablesDbTwo.size();
|
471
|
int tableMinSize = 0;
|
472
|
int tableMaxSize = 0;
|
473
|
|
474
|
if (tableOneSize != tableTwoSize) {
|
475
|
logger.warn("Table '" + tableName + "', Rows differ: " + tablesDbOne.size() + ", " + tablesDbTwo.size());
|
476
|
tableMinSize = Math.min(tableOneSize, tableTwoSize);
|
477
|
tableMaxSize = Math.max(tableOneSize, tableTwoSize);
|
478
|
} else {
|
479
|
logger.info("Table '" + tableName + "': " + tablesDbOne.size());
|
480
|
}
|
481
|
|
482
|
int different = 0;
|
483
|
|
484
|
try {
|
485
|
for (int i = 0; i < tableMinSize; i++) {
|
486
|
|
487
|
CdmBase obj1 = tablesDbOne.get(i);
|
488
|
CdmBase obj2 = tablesDbTwo.get(i);
|
489
|
|
490
|
// This compares only whether both tables contain the same objects.
|
491
|
// It doesn't check whether all field values are the same.
|
492
|
logger.debug("Row # " + i + ":");
|
493
|
if (obj1.equals(obj2) != true) {
|
494
|
different++;
|
495
|
logger.debug("Table 1 = " + obj1);
|
496
|
logger.debug("Table 2 = " + obj2);
|
497
|
} else {
|
498
|
logger.debug("Entry = " + obj1);
|
499
|
}
|
500
|
}
|
501
|
if (different > 0) {
|
502
|
logger.info("# Rows identical: " + (tableMaxSize - different));
|
503
|
logger.warn("# Rows different: " + different);
|
504
|
}
|
505
|
} catch (org.hibernate.LazyInitializationException e){
|
506
|
logger.error("LazyInitializationException");
|
507
|
}
|
508
|
}
|
509
|
|
510
|
private void doCompareDatabases(Map<String, List<CdmBase>> tablesDbOne, Map<String, List<CdmBase>> tablesDbTwo) {
|
511
|
// public void doCompareDatabases(Map<String, List<String>> tablesDbOne, Map<String, List<String>> tablesDbTwo) {
|
512
|
|
513
|
logger.debug("# Tables in DB 1: " + tablesDbOne.size());
|
514
|
logger.debug("# Tables in DB 2: " + tablesDbTwo.size());
|
515
|
|
516
|
for (String tableName: tablesDbOne.keySet()) {
|
517
|
|
518
|
logger.info("Comparing table '" + tableName + "'");
|
519
|
|
520
|
// List<String> dbOneTableRows = new ArrayList<String>();
|
521
|
// List<String> dbTwoTableRows = new ArrayList<String>();
|
522
|
List<CdmBase> dbOneTableRows = new ArrayList<CdmBase>();
|
523
|
List<CdmBase> dbTwoTableRows = new ArrayList<CdmBase>();
|
524
|
|
525
|
dbOneTableRows = tablesDbOne.get(tableName);
|
526
|
dbTwoTableRows = tablesDbTwo.get(tableName);
|
527
|
|
528
|
// Collections.sort(dbOneTableRows);
|
529
|
// Collections.sort(dbTwoTableRows);
|
530
|
|
531
|
int different = 0;
|
532
|
int tableSize = dbOneTableRows.size();
|
533
|
|
534
|
for (int i = 0; i < tableSize; i++) {
|
535
|
|
536
|
// String str1 = dbOneTableRows.get(i);
|
537
|
// String str2 = dbTwoTableRows.get(i);
|
538
|
CdmBase str1 = dbOneTableRows.get(i);
|
539
|
CdmBase str2 = dbTwoTableRows.get(i);
|
540
|
|
541
|
if (str1.equals(str2) != true) {
|
542
|
|
543
|
different++;
|
544
|
logger.debug("Rows differ:");
|
545
|
logger.debug("Table 1 Row = " + str1);
|
546
|
logger.debug("Table 2 Row = " + str2);
|
547
|
|
548
|
}
|
549
|
i++;
|
550
|
}
|
551
|
if (different > 0) {
|
552
|
logger.info("Compared table '" + tableName + "':");
|
553
|
logger.info("# Rows total: " + tableSize);
|
554
|
logger.info("# Rows identical: " + (tableSize - different));
|
555
|
logger.warn("# Rows different: " + different);
|
556
|
}
|
557
|
}
|
558
|
logger.info("End database comparison");
|
559
|
}
|
560
|
|
561
|
// private void test_(){
|
562
|
//
|
563
|
// Map<String, List<CdmBase>> tablesDbOne = doLoadDataFromDb_(sourceDbOne);
|
564
|
// Map<String, List<CdmBase>> tablesDbTwo = doLoadDataFromDb_(sourceDbTwo);
|
565
|
// doCompareDatabases(tablesDbOne, tablesDbTwo);
|
566
|
//
|
567
|
// }
|
568
|
|
569
|
private void test(){
|
570
|
|
571
|
CdmApplicationController appCtrOne = null;
|
572
|
CdmApplicationController appCtrTwo = null;
|
573
|
logger.info("Comparing '" + sourceDbOne + "' and '" + sourceDbTwo + "'");
|
574
|
|
575
|
try {
|
576
|
appCtrOne = CdmApplicationController.NewInstance(sourceOne, DbSchemaValidation.VALIDATE, true);
|
577
|
appCtrTwo = CdmApplicationController.NewInstance(sourceTwo, DbSchemaValidation.VALIDATE, true);
|
578
|
|
579
|
} catch (Exception e) {
|
580
|
logger.error("Error creating application controller");
|
581
|
e.printStackTrace();
|
582
|
System.exit(1);
|
583
|
}
|
584
|
|
585
|
try {
|
586
|
//get data from database
|
587
|
TransactionStatus txStatOne = appCtrOne.startTransaction(true);
|
588
|
TransactionStatus txStatTwo = appCtrTwo.startTransaction(true);
|
589
|
for (int i = 0; i < table_list.length; i++) {
|
590
|
|
591
|
List<CdmBase> rowsDbOne = new ArrayList<CdmBase>(MAX_ROWS);
|
592
|
List<CdmBase> rowsDbTwo = new ArrayList<CdmBase>(MAX_ROWS);
|
593
|
rowsDbOne = appCtrOne.getMainService().rows(table_list[i], MAX_ROWS, 0);
|
594
|
rowsDbTwo = appCtrTwo.getMainService().rows(table_list[i], MAX_ROWS, 0);
|
595
|
compareTables(table_list[i], rowsDbOne, rowsDbTwo);
|
596
|
}
|
597
|
appCtrTwo.commitTransaction(txStatTwo);
|
598
|
appCtrOne.commitTransaction(txStatOne);
|
599
|
//java.lang.IllegalStateException: Cannot deactivate transaction synchronization - not active
|
600
|
appCtrOne.close();
|
601
|
appCtrTwo.close();
|
602
|
logger.info("End database comparison");
|
603
|
|
604
|
} catch (Exception e) {
|
605
|
logger.error("Error retrieving or comparing data");
|
606
|
e.printStackTrace();
|
607
|
}
|
608
|
}
|
609
|
|
610
|
/**
|
611
|
* @param args
|
612
|
*/
|
613
|
public static void main(String[] args) {
|
614
|
TestCdmDbComparator diff = new TestCdmDbComparator();
|
615
|
diff.test();
|
616
|
}
|
617
|
}
|