Project

General

Profile

Download (9.39 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 name usedInSourceFk = -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 " +
168
					" WHERE (emCommonName.LanguageRefFk NOT IN " +
169
							"(SELECT ReferenceId FROM emLanguageReference)) AND " +
170
						"(emCommonName.LanguageRefFk is NOT NULL)";
171
			ResultSet rs = source.getResultSet(strQueryArticlesWithoutJournal);
172
			rs.next();
173
			int count = rs.getInt("n");
174
			if (count > 0){
175
				System.out.println("========================================================");
176
				logger.warn("There are " + count + " common names that have a languageRefFk which can not be found in the emLanguageRefernce table.");
177

    
178
				System.out.println("========================================================");
179
			}
180
			if (count > 0){
181
				System.out.println(" ");
182
			}
183

    
184

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

    
191
	}
192

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

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

    
209

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

    
225
				System.out.println("CommonNameId: " + commonNameId + "\n CommonName: " + commonName +
226
						"\n  Status: " + status +
227
						"\n  FullNameCache: " + fullNameCache +  "\n  ptRefFk: " + ptRefFk +
228
						"\n  sec: " + ptRef );
229

    
230
				result = firstRow = false;
231
			}
232

    
233
			return result;
234
		} catch (SQLException e) {
235
			e.printStackTrace();
236
			return false;
237
		}
238
	}
239

    
240

    
241
}
(3-3/19)