1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.io.berlinModel.in.validation;
|
12
|
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.apache.log4j.Logger;
|
18
|
|
19
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
|
20
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
|
21
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
22
|
import eu.etaxonomy.cdm.io.common.Source;
|
23
|
|
24
|
/**
|
25
|
* @author a.mueller
|
26
|
* @created 17.02.2010
|
27
|
*/
|
28
|
public class BerlinModelFactsImportValidator implements IOValidator<BerlinModelImportState> {
|
29
|
private static final Logger logger = Logger.getLogger(BerlinModelFactsImportValidator.class);
|
30
|
|
31
|
@Override
|
32
|
public boolean validate(BerlinModelImportState state) {
|
33
|
boolean result = true;
|
34
|
result &= checkDesignationRefsExist(state);
|
35
|
result &= checkFactsForSynonyms(state);
|
36
|
return result;
|
37
|
}
|
38
|
|
39
|
|
40
|
private boolean checkDesignationRefsExist(BerlinModelImportState state){
|
41
|
try {
|
42
|
boolean result = true;
|
43
|
BerlinModelImportConfigurator config = state.getConfig();
|
44
|
Source source = state.getConfig().getSource();
|
45
|
String strQuery = "SELECT Count(*) as n " +
|
46
|
" FROM Fact " +
|
47
|
" WHERE (NOT (PTDesignationRefFk IS NULL) ) OR " +
|
48
|
" (NOT (PTDesignationRefDetailFk IS NULL) )";
|
49
|
|
50
|
if (StringUtils.isNotBlank(config.getFactFilter())){
|
51
|
strQuery += String.format(" AND (%s) ", config.getFactFilter()) ;
|
52
|
}
|
53
|
|
54
|
ResultSet rs = source.getResultSet(strQuery);
|
55
|
rs.next();
|
56
|
int count = rs.getInt("n");
|
57
|
if (count > 0){
|
58
|
System.out.println("========================================================");
|
59
|
logger.warn("There are "+count+" Facts with not empty designation references. Designation references are not imported.");
|
60
|
|
61
|
System.out.println("========================================================");
|
62
|
}
|
63
|
return result;
|
64
|
} catch (SQLException e) {
|
65
|
e.printStackTrace();
|
66
|
return false;
|
67
|
}
|
68
|
|
69
|
}
|
70
|
|
71
|
private boolean checkFactsForSynonyms(BerlinModelImportState state){
|
72
|
try {
|
73
|
boolean result = true;
|
74
|
BerlinModelImportConfigurator config = state.getConfig();
|
75
|
Source source = config.getSource();
|
76
|
String strQuery = "SELECT f.factId, f.fact, fc.factCategoryId, fc.FactCategory, pt.StatusFk, n.FullNameCache, s.Status, pt.PTRefFk, r.RefCache " +
|
77
|
" FROM Fact f " +
|
78
|
" INNER JOIN FactCategory fc ON fc.FactCategoryId = f.factCategoryFk " +
|
79
|
" INNER JOIN PTaxon pt ON f.PTNameFk = pt.PTNameFk AND f.PTRefFk = pt.PTRefFk" +
|
80
|
" INNER JOIN Name n ON pt.PTNameFk = n.NameId " +
|
81
|
" INNER JOIN Status s ON pt.StatusFk = s.StatusId " +
|
82
|
" LEFT OUTER JOIN Reference r ON pt.PTRefFk = r.RefId " +
|
83
|
" WHERE (pt.StatusFk NOT IN ( 1, 5)) ";
|
84
|
|
85
|
if (StringUtils.isNotBlank(config.getFactFilter())){
|
86
|
strQuery += String.format(" AND (%s) ", config.getFactFilter()) ;
|
87
|
}
|
88
|
|
89
|
ResultSet resulSet = source.getResultSet(strQuery);
|
90
|
boolean firstRow = true;
|
91
|
while (resulSet.next()){
|
92
|
if (firstRow){
|
93
|
System.out.println("========================================================");
|
94
|
System.out.println("There are facts for a taxon that is not accepted!");
|
95
|
System.out.println("========================================================");
|
96
|
}
|
97
|
int factId = resulSet.getInt("FactId");
|
98
|
String fact = resulSet.getString("Fact");
|
99
|
String factCategory = resulSet.getString("FactCategory");
|
100
|
int factCategoryId = resulSet.getInt("FactCategoryId");
|
101
|
String status = resulSet.getString("Status");
|
102
|
String fullNameCache = resulSet.getString("FullNameCache");
|
103
|
String ptRefFk = resulSet.getString("PTRefFk");
|
104
|
String ptRef = resulSet.getString("RefCache");
|
105
|
|
106
|
System.out.println("FactId: " + factId + "\n Fact: " + fact +
|
107
|
"\n FactCategory: " + factCategory + "\n FactCategoryId: " + factCategoryId +
|
108
|
"\n Status: " + status +
|
109
|
"\n FullNameCache: " + fullNameCache + "\n ptRefFk: " + ptRefFk +
|
110
|
"\n sec: " + ptRef );
|
111
|
|
112
|
result = firstRow = false;
|
113
|
}
|
114
|
return result;
|
115
|
} catch (SQLException e) {
|
116
|
e.printStackTrace();
|
117
|
return false;
|
118
|
}
|
119
|
|
120
|
}
|
121
|
}
|