Project

General

Profile

Download (6.61 KB) Statistics
| Branch: | Revision:
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 BerlinModelCommonNamesImportValidator implements IOValidator<BerlinModelImportState> {
29
	private static final Logger logger = Logger.getLogger(BerlinModelCommonNamesImportValidator.class);
30

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

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

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

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

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

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

    
181
}
(3-3/19)