Project

General

Profile

Download (5.36 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 BerlinModelOccurrenceImportValidator implements IOValidator<BerlinModelImportState> {
28
	@SuppressWarnings("unused")
29
    private static final Logger logger = Logger.getLogger(BerlinModelOccurrenceImportValidator.class);
30

    
31
	@Override
32
	public boolean validate(BerlinModelImportState state) {
33
		boolean result = true;
34
		BerlinModelImportConfigurator config = state.getConfig();
35
		result &= checkTaxonIsAccepted(config);
36
		result &= checkSourcesWithWhitespace(config);
37
		return result;
38
	}
39

    
40

    
41
	//******************************** CHECK *************************************************
42

    
43
	private static boolean checkTaxonIsAccepted(BerlinModelImportConfigurator config){
44
		try {
45
			boolean result = true;
46
			Source source = config.getSource();
47
			String strQuery = "SELECT emOccurrence.OccurrenceId, PTaxon.StatusFk, Name.FullNameCache, "
48
			                + " Status.Status, PTaxon.PTRefFk, Reference.RefCache, emArea.EMCode, emOccurrence.* " +
49
						" FROM emOccurrence INNER JOIN " +
50
							" PTaxon ON emOccurrence.PTNameFk = PTaxon.PTNameFk AND emOccurrence.PTRefFk = PTaxon.PTRefFk INNER JOIN " +
51
			                " Name ON PTaxon.PTNameFk = Name.NameId INNER JOIN " +
52
			                " emArea ON emOccurrence.AreaFk = emArea.AreaId INNER JOIN " +
53
			                " Status ON PTaxon.StatusFk = Status.StatusId LEFT OUTER JOIN " +
54
			                " Reference ON PTaxon.PTRefFk = Reference.RefId " +
55
						" WHERE (PTaxon.StatusFk NOT IN ( 1, 5))  ";
56

    
57
			if (StringUtils.isNotBlank(config.getOccurrenceFilter())){
58
				strQuery += String.format(" AND (%s) ", config.getOccurrenceFilter()) ;
59
			}
60

    
61

    
62
			ResultSet resultSet = source.getResultSet(strQuery);
63
			boolean firstRow = true;
64
			while (resultSet.next()){
65
				if (firstRow){
66
					System.out.println("========================================================");
67
					System.out.println("There are Occurrences for a taxon that is not accepted!");
68
					System.out.println("--------------------------------------------------------");
69
					System.out.println(strQuery);
70
					System.out.println("========================================================");
71

    
72
				}
73
				int occurrenceId = resultSet.getInt("OccurrenceId");
74
//			    int statusFk = resulSet.getInt("StatusFk");
75
				String status = resultSet.getString("Status");
76
				String fullNameCache = resultSet.getString("FullNameCache");
77
				String ptRefFk = resultSet.getString("PTRefFk");
78
				String ptRef = resultSet.getString("RefCache");
79
				String area = resultSet.getString("EMCode");
80

    
81
				System.out.println("OccurrenceId:" + occurrenceId + "\n  Status: " + status +
82
						"\n  FullNameCache: " + fullNameCache +  "\n  ptRefFk: " + ptRefFk +
83
						"\n  sec: " + ptRef + "\n  area: " + area) ;
84

    
85
				result = firstRow = false;
86
			}
87

    
88
			return result;
89
		} catch (SQLException e) {
90
			e.printStackTrace();
91
			return false;
92
		}
93
	}
94

    
95
    private static boolean checkSourcesWithWhitespace(BerlinModelImportConfigurator config){
96
        try {
97
            boolean result = true;
98
            Source source = config.getSource();
99
            String strSelect = "SELECT OccurrenceId, PTNameFk, PTRefFk, AreaFk, Sources, Created_When, Created_Who, Updated_When, Updated_Who, Notes, Occurrence ";
100
            String strCount = " SELECT count(*) ";
101
            String strQueryBase =
102
                    " FROM emOccurrence " +
103
                    " WHERE (Sources LIKE '%  %') OR (Sources LIKE '% ') OR (Sources LIKE ' %') ";
104

    
105
            ResultSet rs = source.getResultSet(strCount + strQueryBase);
106
            rs.next();
107
            int n = rs.getInt("n");
108
            if (n > 0){
109
                System.out.println("=======================================================================");
110
                System.out.println("There are "+n+" occurrences with source attribute has unexpected whitespace!");
111
                System.out.println("---------------------------------------------------------------");
112
                System.out.println(strSelect + strQueryBase);
113
                System.out.println("=======================================================================");
114
            }
115

    
116
            rs = source.getResultSet(strSelect + strQueryBase);
117
            while (rs.next()){
118
                int occurrenceId = rs.getInt("OccurrenceId");
119
                String sources = rs.getString("Sources");
120

    
121
                System.out.println("OccurrenceSourceId:" + occurrenceId +
122
                        "\n  Sources: " + sources)
123
                        ;
124
            }
125
            return result;
126
        } catch (SQLException e) {
127
            e.printStackTrace();
128
            return false;
129
        }
130
    }
131
}
(8-8/19)