Project

General

Profile

Download (6.6 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.io.berlinModel.in.validation;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14

    
15
import org.apache.commons.lang.StringUtils;
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
19
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
20
import eu.etaxonomy.cdm.io.common.IOValidator;
21
import eu.etaxonomy.cdm.io.common.Source;
22

    
23
/**
24
 * @author a.mueller
25
 * @since 17.02.2010
26
 */
27
public class BerlinModelCommonNamesImportValidator implements IOValidator<BerlinModelImportState> {
28
	private static final Logger logger = Logger.getLogger(BerlinModelCommonNamesImportValidator.class);
29

    
30
	@Override
31
	public boolean validate(BerlinModelImportState state) {
32
		boolean result = true;
33
		result &= checkUnreferredNameUsedInSource(state.getConfig());
34
		result &= checkUnreferredLanguageRefFk(state.getConfig());
35
		result &= checkTaxonIsAccepted(state.getConfig());
36
		
37
		return result;
38
	}
39
	
40
	private boolean checkUnreferredNameUsedInSource(BerlinModelImportConfigurator config){
41
		try {
42
			boolean result = true;
43
			Source source = config.getSource();
44
			String strQuery = "SELECT Count(*) as n " +
45
					" FROM emCommonName " +
46
					" WHERE (emCommonName.NameInSourceFk NOT IN " + 
47
							"(SELECT NameId FROM Name AS Name_1)) AND " + 
48
						"(emCommonName.NameInSourceFk <> - 1) ";
49
			
50
			if (StringUtils.isNotBlank(config.getCommonNameFilter())){
51
				strQuery += String.format(" AND (%s) ", config.getCommonNameFilter()) ; 
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
				System.out.println("There are " + count + " common names that have a name used in source which can not be found in the database.");
60
				
61
				System.out.println("========================================================");
62
			}
63
			String sql = 
64
				" SELECT DISTINCT emCommonName.CommonNameId, emCommonName.NameInSourceFk, emCommonName.CommonName, PTaxon.PTNameFk, PTaxon.PTRefFk," + 
65
					" Name.FullNameCache, PTaxon.RIdentifier " +
66
				" FROM emCommonName INNER JOIN " +
67
					" PTaxon ON emCommonName.PTNameFk = PTaxon.PTNameFk AND emCommonName.PTRefFk = PTaxon.PTRefFk INNER JOIN " +
68
					" Name ON PTaxon.PTNameFk = Name.NameId " +
69
				" WHERE (emCommonName.NameInSourceFk NOT IN " + 
70
						"(SELECT NameId FROM Name AS Name_1)) AND " + 
71
					"(emCommonName.NameInSourceFk <> - 1)";
72
			
73
			rs = source.getResultSet(sql);
74
			int i = 0;
75
			while (rs.next()){
76
				i++;
77
				int commonNameId = rs.getInt("CommonNameId");
78
				String fullNameCache = rs.getString("FullNameCache");
79
				String commonName = rs.getString("CommonName");
80
				int rIdentifier = rs.getInt("RIdentifier");
81
				int nameFk = rs.getInt("PTNameFk");
82
				int refFk = rs.getInt("PTRefFk");
83
				int nameInSourceFk = rs.getInt("NameInSourceFk");
84
				
85
				System.out.println("CommonName: " + commonName + "\n  CommonNameId: " + commonNameId + "\n Taxon Name:" + fullNameCache + "\n  TaxonNameFk: " + nameFk + 
86
						"\n  TaxonRefFk: " + refFk + "\n  TaxonId" + rIdentifier + "\n NameInSourceFk: " + nameInSourceFk + "\n");
87
			}
88
			if (i > 0){
89
				System.out.println(" ");
90
			}
91
			
92
			
93
			return result;
94
		} catch (SQLException e) {
95
			e.printStackTrace();
96
			return false;
97
		}
98

    
99
	}
100
	
101
	private boolean checkUnreferredLanguageRefFk(BerlinModelImportConfigurator config){
102
		try {
103
			boolean result = true;
104
			Source source = config.getSource();
105
			String strQueryArticlesWithoutJournal = "SELECT Count(*) as n " +
106
					" FROM emCommonName " +
107
					" WHERE (emCommonName.LanguageRefFk NOT IN " + 
108
							"(SELECT ReferenceId FROM emLanguageReference)) AND " + 
109
						"(emCommonName.LanguageRefFk is NOT NULL)";
110
			ResultSet rs = source.getResultSet(strQueryArticlesWithoutJournal);
111
			rs.next();
112
			int count = rs.getInt("n");
113
			if (count > 0){
114
				System.out.println("========================================================");
115
				logger.warn("There are " + count + " common names that have a languageRefFk which can not be found in the emLanguageRefernce table.");
116

    
117
				System.out.println("========================================================");
118
			}
119
			if (count > 0){
120
				System.out.println(" ");
121
			}
122
			
123
			
124
			return result;
125
		} catch (SQLException e) {
126
			e.printStackTrace();
127
			return false;
128
		}
129

    
130
	}
131
	
132
	private static boolean checkTaxonIsAccepted(BerlinModelImportConfigurator config){
133
		try {
134
			boolean result = true;
135
			Source source = config.getSource();
136
			String strQuery = "SELECT cn.CommonNameId, cn.CommonName, pt.StatusFk, n.FullNameCache, s.Status, pt.PTRefFk, r.RefCache " + 
137
						" FROM emCommonName cn " +
138
							" INNER JOIN PTaxon pt ON cn.PTNameFk = pt.PTNameFk AND cn.PTRefFk = pt.PTRefFk " + 
139
			                " INNER JOIN Name n ON pt.PTNameFk = n.NameId " +
140
			                " INNER JOIN Status s ON pt.StatusFk = s.StatusId " +
141
			                " LEFT OUTER JOIN Reference r ON pt.PTRefFk = r.RefId " + 
142
						" WHERE (pt.StatusFk NOT IN ( 1, 5))  ";
143

    
144
			if (StringUtils.isNotBlank(config.getOccurrenceFilter())){
145
				strQuery += String.format(" AND (%s) ", config.getCommonNameFilter()) ; 
146
			}
147

    
148
			
149
			ResultSet resulSet = source.getResultSet(strQuery);
150
			boolean firstRow = true;
151
			while (resulSet.next()){
152
				if (firstRow){
153
					System.out.println("========================================================");
154
					System.out.println("There are Common Names for a taxon that is not accepted!");
155
					System.out.println("========================================================");
156
				}
157
				int commonNameId = resulSet.getInt("CommonNameId");
158
				String commonName = resulSet.getString("CommonName");
159
				String status = resulSet.getString("Status");
160
				String fullNameCache = resulSet.getString("FullNameCache");
161
				String ptRefFk = resulSet.getString("PTRefFk");
162
				String ptRef = resulSet.getString("RefCache");
163
				
164
				System.out.println("CommonNameId: " + commonNameId + "\n CommonName: " + commonName + 
165
						"\n  Status: " + status + 
166
						"\n  FullNameCache: " + fullNameCache +  "\n  ptRefFk: " + ptRefFk +
167
						"\n  sec: " + ptRef );
168
				
169
				result = firstRow = false;
170
			}
171
			
172
			return result;
173
		} catch (SQLException e) {
174
			e.printStackTrace();
175
			return false;
176
		}
177
	}
178
	
179

    
180
}
(3-3/19)