Project

General

Profile

Download (9.74 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 &= checkNameUsedInSourceMinusOne(state.getConfig());
34
        result &= checkUnreferredNameUsedInSource(state.getConfig());
35
		result &= checkUnreferredLanguageRefFk(state.getConfig());
36

    
37
		result &= checkTaxonIsAccepted(state.getConfig());
38

    
39
		return result;
40
	}
41

    
42
    private boolean checkNameUsedInSourceMinusOne(BerlinModelImportConfigurator config){
43
        try {
44
            boolean result = true;
45
            Source source = config.getSource();
46
            String strQuery = "SELECT Count(*) as n " +
47
                    " FROM emCommonName " +
48
                    " WHERE (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 nameInSourceFk = -1.");
60
                System.out.println("========================================================");
61
            }
62
            String sql =
63
                " SELECT DISTINCT emCommonName.CommonNameId, emCommonName.NameInSourceFk, emCommonName.CommonName, PTaxon.PTNameFk, PTaxon.PTRefFk," +
64
                    " Name.FullNameCache, PTaxon.RIdentifier " +
65
                " FROM emCommonName INNER JOIN " +
66
                    " PTaxon ON emCommonName.PTNameFk = PTaxon.PTNameFk AND emCommonName.PTRefFk = PTaxon.PTRefFk INNER JOIN " +
67
                    " Name ON PTaxon.PTNameFk = Name.NameId " +
68
                " WHERE (emCommonName.NameInSourceFk = - 1) ";
69
            if (StringUtils.isNotBlank(config.getCommonNameFilter())){
70
                sql += String.format(" AND (%s) ", config.getCommonNameFilter()) ;
71
            }
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 checkUnreferredNameUsedInSource(BerlinModelImportConfigurator config){
102
		try {
103
			boolean result = true;
104
			Source source = config.getSource();
105
			String strQuery = "SELECT Count(*) as n " +
106
					" FROM emCommonName " +
107
					" WHERE (emCommonName.NameInSourceFk NOT IN " +
108
							"(SELECT NameId FROM Name AS Name_1)) AND " +
109
						"(emCommonName.NameInSourceFk <> - 1) ";
110

    
111
			if (StringUtils.isNotBlank(config.getCommonNameFilter())){
112
				strQuery += String.format(" AND (%s) ", config.getCommonNameFilter()) ;
113
			}
114

    
115
			ResultSet rs = source.getResultSet(strQuery);
116
			rs.next();
117
			int count = rs.getInt("n");
118
			if (count > 0){
119
				System.out.println("========================================================");
120
				System.out.println("There are " + count + " common names that have a name used in source which can not be found in the database.");
121

    
122
				System.out.println("========================================================");
123
			}
124
			String sql =
125
				" SELECT DISTINCT emCommonName.CommonNameId, emCommonName.NameInSourceFk, emCommonName.CommonName, PTaxon.PTNameFk, PTaxon.PTRefFk," +
126
					" Name.FullNameCache, PTaxon.RIdentifier " +
127
				" FROM emCommonName INNER JOIN " +
128
					" PTaxon ON emCommonName.PTNameFk = PTaxon.PTNameFk AND emCommonName.PTRefFk = PTaxon.PTRefFk INNER JOIN " +
129
					" Name ON PTaxon.PTNameFk = Name.NameId " +
130
				" WHERE (emCommonName.NameInSourceFk NOT IN " +
131
						"(SELECT NameId FROM Name AS Name_1)) AND " +
132
					"(emCommonName.NameInSourceFk <> - 1)";
133

    
134
			rs = source.getResultSet(sql);
135
			int i = 0;
136
			while (rs.next()){
137
				i++;
138
				int commonNameId = rs.getInt("CommonNameId");
139
				String fullNameCache = rs.getString("FullNameCache");
140
				String commonName = rs.getString("CommonName");
141
				int rIdentifier = rs.getInt("RIdentifier");
142
				int nameFk = rs.getInt("PTNameFk");
143
				int refFk = rs.getInt("PTRefFk");
144
				int nameInSourceFk = rs.getInt("NameInSourceFk");
145

    
146
				System.out.println("CommonName: " + commonName + "\n  CommonNameId: " + commonNameId + "\n Taxon Name:" + fullNameCache + "\n  TaxonNameFk: " + nameFk +
147
						"\n  TaxonRefFk: " + refFk + "\n  TaxonId" + rIdentifier + "\n NameInSourceFk: " + nameInSourceFk + "\n");
148
			}
149
			if (i > 0){
150
				System.out.println(" ");
151
			}
152

    
153

    
154
			return result;
155
		} catch (SQLException e) {
156
			e.printStackTrace();
157
			return false;
158
		}
159

    
160
	}
161

    
162
	private boolean checkUnreferredLanguageRefFk(BerlinModelImportConfigurator config){
163
		try {
164
			boolean result = true;
165
			Source source = config.getSource();
166
			String strQueryArticlesWithoutJournal = "SELECT Count(*) as n " +
167
					" FROM emCommonName cn INNER JOIN PTaxon pt ON pt.PTNameFk = cn.PTNameFk AND pt.PTRefFk = cn.PTRefFk " +
168
					" WHERE (cn.LanguageRefFk NOT IN " +
169
							"(SELECT ReferenceId FROM emLanguageReference)) AND " +
170
						"(cn.LanguageRefFk is NOT NULL) AND "
171
						+ " cn.LanguageRefFk <> cn.RefFk "
172
						+ " AND pt.statusFk NOT IN (6) ";
173
			ResultSet rs = source.getResultSet(strQueryArticlesWithoutJournal);
174
			rs.next();
175
			int count = rs.getInt("n");
176
			if (count > 0){
177
				System.out.println("============================================================================");
178
				System.out.println("There are " + count + " common names that have a languageRefFk which can not be found in the emLanguageReference table AND are not equal to RefFk.");
179
				System.out.println("============================================================================");
180
			}
181
			if (count > 0){
182
				System.out.println(" ");
183
			}
184

    
185

    
186
			return result;
187
		} catch (SQLException e) {
188
			e.printStackTrace();
189
			return false;
190
		}
191

    
192
	}
193

    
194
	private static boolean checkTaxonIsAccepted(BerlinModelImportConfigurator config){
195
		try {
196
			boolean result = true;
197
			Source source = config.getSource();
198
			String strQuery = "SELECT cn.CommonNameId, cn.CommonName, pt.StatusFk, n.FullNameCache, s.Status, pt.PTRefFk, pt.PTNameFk, r.RefCache " +
199
						" FROM emCommonName cn " +
200
							" INNER JOIN PTaxon pt ON cn.PTNameFk = pt.PTNameFk AND cn.PTRefFk = pt.PTRefFk " +
201
			                " INNER JOIN Name n ON pt.PTNameFk = n.NameId " +
202
			                " INNER JOIN Status s ON pt.StatusFk = s.StatusId " +
203
			                " LEFT OUTER JOIN Reference r ON pt.PTRefFk = r.RefId " +
204
						" WHERE (pt.StatusFk NOT IN ( 1, 5))  ";
205

    
206
			if (StringUtils.isNotBlank(config.getOccurrenceFilter())){
207
				strQuery += String.format(" AND (%s) ", config.getCommonNameFilter()) ;
208
			}
209

    
210

    
211
			ResultSet resulSet = source.getResultSet(strQuery);
212
			boolean firstRow = true;
213
			while (resulSet.next()){
214
				if (firstRow){
215
					System.out.println("========================================================");
216
					System.out.println("There are Common Names for a taxon that is not accepted!");
217
					System.out.println("========================================================");
218
				}
219
				int commonNameId = resulSet.getInt("CommonNameId");
220
				String commonName = resulSet.getString("CommonName");
221
				String status = resulSet.getString("Status");
222
				String fullNameCache = resulSet.getString("FullNameCache");
223
				String ptRefFk = resulSet.getString("PTRefFk");
224
				String ptNameFk = resulSet.getString("PTNameFk");
225
                String ptRef = resulSet.getString("RefCache");
226

    
227
				System.out.println("CommonNameId: " + commonNameId + "\n CommonName: " + commonName +
228
						"\n  Status: " + status +
229
						"\n  FullNameCache: " + fullNameCache +
230
						"\n  ptRefFk: " + ptRefFk +
231
						"\n  ptNameFk: " + ptNameFk +
232
                        "\n  sec: " + ptRef );
233

    
234
				result = firstRow = false;
235
			}
236

    
237
			return result;
238
		} catch (SQLException e) {
239
			e.printStackTrace();
240
			return false;
241
		}
242
	}
243

    
244

    
245
}
(4-4/20)