Project

General

Profile

« Previous | Next » 

Revision 247d3114

Added by Andreas Müller almost 5 years ago

Add missing explicit sources check to E+M import validation

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/io/berlinModel/in/validation/BerlinModelOccurrenceImportValidator.java
11 11

  
12 12
import java.sql.ResultSet;
13 13
import java.sql.SQLException;
14
import java.util.HashSet;
15
import java.util.Set;
14 16

  
15 17
import org.apache.commons.lang.StringUtils;
16 18
import org.apache.log4j.Logger;
......
34 36
		BerlinModelImportConfigurator config = state.getConfig();
35 37
		result &= checkTaxonIsAccepted(config);
36 38
		result &= checkSourcesWithWhitespace(config);
39
		result &= checkMissingExplicitSources(config);
37 40
		return result;
38 41
	}
39 42

  
40 43

  
41
	//******************************** CHECK *************************************************
44

  
45

  
46
    //******************************** CHECK *************************************************
42 47

  
43 48
	private static boolean checkTaxonIsAccepted(BerlinModelImportConfigurator config){
44 49
		try {
......
128 133
            return false;
129 134
        }
130 135
    }
136

  
137

  
138
    /**
139
     * @param config
140
     * @return
141
     */
142
    private boolean checkMissingExplicitSources(BerlinModelImportConfigurator config) {
143
        try {
144
             boolean result = true;
145
            Source source = config.getSource();
146
            String sql = " SELECT occ.OccurrenceId, occ.Sources, ocs.OccurrenceSourceId, ocs.SourceNumber, ar.EMCode, ar.Unit, n.FullNameCache, occ.PTRefFk  " +
147
                " FROM emOccurrence occ " +
148
                    " INNER JOIN emArea ar ON occ.AreaFk = ar.AreaId " +
149
                    " INNER JOIN PTaxon pt ON occ.PTNameFk = pt.PTNameFk AND occ.PTRefFk = pt.PTRefFk " +
150
                    " INNER JOIN Name n ON occ.PTNameFk = n.NameId  " +
151
                    " LEFT OUTER JOIN emOccurSumCat sumcat ON occ.SummaryStatus = sumcat.emOccurSumCatId " +
152
                    " LEFT OUTER JOIN emOccurrenceSource ocs ON occ.OccurrenceId = ocs.OccurrenceFk " +
153
                " WHERE ( occurrenceId IN ( SELECT occurrenceId FROM v_cdm_exp_occurrenceAll ))" +
154
                " ORDER BY occ.PTRefFk, n.fullNameCache, occ.occurrenceId";
155
            ResultSet rs = source.getResultSet(sql);
156
            int oldOccurrenceId = -1;
157
            Set<Integer> sources = new HashSet<>();
158
            int unmatched = 0;
159
            while (rs.next()){
160
                int occurrenceId = rs.getInt("OccurrenceId");
161
                if (occurrenceId != oldOccurrenceId){
162
                    checkExistingSources(sources, oldOccurrenceId, rs);
163
                    String sourcesStr = rs.getString("Sources");
164
                    sources = makeSources(sourcesStr);
165
                    oldOccurrenceId = occurrenceId;
166
                }
167
                String sourceIdStr = rs.getString("SourceNumber");
168
                Integer sourceId = StringUtils.isBlank(sourceIdStr) ? null: Integer.valueOf(sourceIdStr);
169
                unmatched = removeSource(sources, sourceId, occurrenceId, unmatched);
170
            }
171
            return result;
172
        } catch (Exception e) {
173
            e.printStackTrace();
174
            return false;
175
        }
176
    }
177

  
178
    /**
179
     * @param sources
180
     * @param sourceId
181
     * @param unmatched
182
     */
183
    private int removeSource(Set<Integer> sources, Integer sourceId, int occurrenceId, int unmatched) {
184
        boolean contained = sources.remove(sourceId);
185
        if (sourceId != null && !contained){
186
//            System.out.println("OccurrenceId(" + occurrenceId + "): sourceId " + sourceId + " not found in sources field.");
187
            unmatched++;
188
        }
189
        return unmatched;
190
    }
191

  
192
    /**
193
     * @param sources
194
     * @param occurrenceId
195
     * @param rs
196
     * @throws SQLException
197
     */
198
    private void checkExistingSources(Set<Integer> sources, int occurrenceId, ResultSet rs) throws SQLException {
199
        sources.remove(27133);
200
        sources.remove(0);
201
        if (!sources.isEmpty()){
202
            String emCode = rs.getString("EMCode").trim();
203
            String unit = rs.getString("Unit");
204
            String name = rs.getString("FullNameCache");
205
            String ref = rs.getString("PTRefFk");
206
            System.out.println(name + " ("+ ref + " (occId: " + occurrenceId + ", " + emCode + ", " + unit + "): The following sources are not matched: " + sources);
207
        }
208
    }
209

  
210
    private Set<Integer> makeSources(String sourcesStr) {
211
        Set<Integer> result = new HashSet<>();
212
        if (sourcesStr != null){
213
            String[] splits = sourcesStr.split("\\|");
214
            for (String split : splits){
215
                split = split.trim();
216
                if (StringUtils.isNotBlank(split)){
217
                    Integer number;
218
                    try {
219
                        number = Integer.valueOf(split);
220
                        result.add(number);
221
                    } catch (NumberFormatException e) {
222
                        e.printStackTrace();
223
                    }
224
                }
225
            }
226
        }
227

  
228
        return result;
229
    }
131 230
}

Also available in: Unified diff