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