Project

General

Profile

Download (24.1 KB) Statistics
| Branch: | Revision:
1 5dd8e8b0 Andreas Müller
/**
2
* Copyright (C) 2007 EDIT
3 48014773 Andreas M��ller
* European Distributed Institute of Taxonomy
4 5dd8e8b0 Andreas Müller
* http://www.e-taxonomy.eu
5 48014773 Andreas M��ller
*
6 5dd8e8b0 Andreas Müller
* 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;
11
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19
20
import org.apache.log4j.Logger;
21
import org.springframework.stereotype.Component;
22
23
import eu.etaxonomy.cdm.common.CdmUtils;
24
import eu.etaxonomy.cdm.database.update.DatabaseTypeNotSupportedException;
25
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
26
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelTaxonNameImportValidator;
27
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
28
import eu.etaxonomy.cdm.io.common.IOValidator;
29
import eu.etaxonomy.cdm.io.common.ImportHelper;
30
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
31
import eu.etaxonomy.cdm.io.common.Source;
32
import eu.etaxonomy.cdm.model.agent.Person;
33
import eu.etaxonomy.cdm.model.agent.Team;
34
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
35 c385a1b7 Andreas Müller
import eu.etaxonomy.cdm.model.common.Annotation;
36 5dd8e8b0 Andreas Müller
import eu.etaxonomy.cdm.model.common.CdmBase;
37
import eu.etaxonomy.cdm.model.common.Extension;
38
import eu.etaxonomy.cdm.model.common.ExtensionType;
39
import eu.etaxonomy.cdm.model.common.Language;
40
import eu.etaxonomy.cdm.model.common.Representation;
41 7d882578 Andreas Müller
import eu.etaxonomy.cdm.model.name.IBotanicalName;
42 86536e03 Andreas Müller
import eu.etaxonomy.cdm.model.name.ICultivarPlantName;
43 7d882578 Andreas Müller
import eu.etaxonomy.cdm.model.name.INonViralName;
44
import eu.etaxonomy.cdm.model.name.IZoologicalName;
45 5dd8e8b0 Andreas Müller
import eu.etaxonomy.cdm.model.name.Rank;
46 86536e03 Andreas Müller
import eu.etaxonomy.cdm.model.name.TaxonName;
47 ded3de15 Andreas Müller
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
48 5dd8e8b0 Andreas Müller
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
49
import eu.etaxonomy.cdm.model.reference.Reference;
50
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
51
52
/**
53
 * @author a.mueller
54 a13538c8 Andreas Müller
 * @since 20.03.2008
55 5dd8e8b0 Andreas Müller
 */
56
@Component
57
public class BerlinModelTaxonNameImport extends BerlinModelImportBase {
58 97e3b5a9 Andreas Müller
    private static final long serialVersionUID = -8860800694286602016L;
59
60
    private static final boolean BLANK_TO_NULL = true;
61 5dd8e8b0 Andreas Müller
62
	private static final Logger logger = Logger.getLogger(BerlinModelTaxonNameImport.class);
63
64
	public static final String NAMESPACE = "TaxonName";
65 48014773 Andreas M��ller
66 5dd8e8b0 Andreas Müller
	public static final UUID SOURCE_ACC_UUID = UUID.fromString("c3959b4f-d876-4b7a-a739-9260f4cafd1c");
67 48014773 Andreas M��ller
68 5dd8e8b0 Andreas Müller
	private static int modCount = 5000;
69
	private static final String pluralString = "TaxonNames";
70
	private static final String dbTableName = "Name";
71
72
73
	public BerlinModelTaxonNameImport(){
74
		super(dbTableName, pluralString);
75
	}
76
77
78
	@Override
79
	protected String getIdQuery(BerlinModelImportState state) {
80
		if (state.getConfig().getNameIdTable()==null ){
81
			return super.getIdQuery(state);
82
		}else{
83 48014773 Andreas M��ller
			return "SELECT nameId FROM " + state.getConfig().getNameIdTable() + "";
84 5dd8e8b0 Andreas Müller
		}
85
	}
86 48014773 Andreas M��ller
87 5dd8e8b0 Andreas Müller
	@Override
88
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
89
		Source source = config.getSource();
90 48014773 Andreas M��ller
91 5dd8e8b0 Andreas Müller
			String facultativCols = "";
92
			String strFacTable = "RefDetail";
93
			String strFacColumn = "IdInSource";
94
			String strColAlias = null;
95
			if (checkSqlServerColumnExists(source, strFacTable, strFacColumn)){
96
				facultativCols +=  ", " + strFacTable + "." + strFacColumn ;
97
				if (! CdmUtils.Nz(strColAlias).equals("") ){
98
					facultativCols += " AS " + strColAlias;
99
				}
100
			}
101
102 48014773 Andreas M��ller
		String strRecordQuery =
103 5dd8e8b0 Andreas Müller
					"SELECT Name.* , RefDetail.RefDetailId, RefDetail.RefFk, " +
104 48014773 Andreas M��ller
                      		" RefDetail.FullRefCache, RefDetail.FullNomRefCache, RefDetail.PreliminaryFlag AS RefDetailPrelim, RefDetail.Details, " +
105 5dd8e8b0 Andreas Müller
                      		" RefDetail.SecondarySources, Rank.RankAbbrev, Rank.Rank " +
106
                      		facultativCols +
107
                    " FROM Name LEFT OUTER JOIN RefDetail ON Name.NomRefDetailFk = RefDetail.RefDetailId AND  " +
108
                    	" Name.NomRefFk = RefDetail.RefFk " +
109 48014773 Andreas M��ller
                    	" LEFT OUTER JOIN Rank ON Name.RankFk = Rank.rankID " +
110 5dd8e8b0 Andreas Müller
                " WHERE name.nameId IN ("+ID_LIST_TOKEN+") ";
111
					//strQuery += " AND RefDetail.PreliminaryFlag = 1 ";
112
					//strQuery += " AND Name.Created_When > '03.03.2004' ";
113
		return strRecordQuery +  "";
114
	}
115
116
117
118
	@Override
119
	protected void doInvoke(BerlinModelImportState state) {
120
		//update rank labels if necessary
121
		String strAbbrev = state.getConfig().getInfrGenericRankAbbrev();
122
		Rank rank = Rank.INFRAGENERICTAXON();
123
		testRankAbbrev(strAbbrev, rank);
124 48014773 Andreas M��ller
125 5dd8e8b0 Andreas Müller
		strAbbrev = state.getConfig().getInfrSpecificRankAbbrev();
126
		rank = Rank.INFRASPECIFICTAXON();
127
		testRankAbbrev(strAbbrev, rank);
128 48014773 Andreas M��ller
129 5dd8e8b0 Andreas Müller
		super.doInvoke(state);
130
	}
131
132
	private void testRankAbbrev(String strAbbrev, Rank rank) {
133
		if (strAbbrev != null){
134
			Representation rep = rank.getRepresentation(Language.ENGLISH());
135
			rep.setAbbreviatedLabel(strAbbrev);
136
			getTermService().saveOrUpdate(rank);
137
		}
138
	}
139
140 ed94068a Andreas Müller
	@Override
141 5dd8e8b0 Andreas Müller
	public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
142
		String dbAttrName;
143
		String cdmAttrName;
144
		boolean success = true ;
145
		BerlinModelImportConfigurator config = state.getConfig();
146 86536e03 Andreas Müller
		Set<TaxonName> namesToSave = new HashSet<>();
147 48014773 Andreas M��ller
		Map<String, Team> teamMap = partitioner.getObjectMap(BerlinModelAuthorTeamImport.NAMESPACE);
148
149 5dd8e8b0 Andreas Müller
		ResultSet rs = partitioner.getResultSet();
150 48014773 Andreas M��ller
151 5dd8e8b0 Andreas Müller
		try {
152
			int i = 0;
153
			//for each reference
154
			while (rs.next()){
155 48014773 Andreas M��ller
156 5dd8e8b0 Andreas Müller
				if ((i++ % modCount) == 0 && i != 1 ){ logger.info("Names handled: " + (i-1));}
157 48014773 Andreas M��ller
158 5dd8e8b0 Andreas Müller
				//create TaxonName element
159
				int nameId = rs.getInt("nameId");
160
				Object authorFk = rs.getObject("AuthorTeamFk");
161
				Object exAuthorFk = rs.getObject("ExAuthorTeamFk");
162
				Object basAuthorFk = rs.getObject("BasAuthorTeamFk");
163
				Object exBasAuthorFk = rs.getObject("ExBasAuthorTeamFk");
164
				String strCultivarGroupName = rs.getString("CultivarGroupName");
165
				String strCultivarName = rs.getString("CultivarName");
166
				String nameCache = rs.getString("NameCache");
167
				String fullNameCache = rs.getString("FullNameCache");
168 ed94068a Andreas Müller
				String uuid = null;
169
				if (resultSetHasColumn(rs,"UUID")){
170
					uuid = rs.getString("UUID");
171
				}
172 48014773 Andreas M��ller
173 5dd8e8b0 Andreas Müller
				try {
174 48014773 Andreas M��ller
175 5dd8e8b0 Andreas Müller
					//define rank
176
					boolean useUnknownRank = true;
177
					Rank rank = BerlinModelTransformer.rankId2Rank(rs, useUnknownRank, config.isSwitchSpeciesGroup());
178 48014773 Andreas M��ller
179 5dd8e8b0 Andreas Müller
					boolean allowInfraSpecTaxonRank = state.getConfig().isAllowInfraSpecTaxonRank() ;
180
					if (rank == null || rank.equals(Rank.UNKNOWN_RANK()) || (rank.equals(Rank.INFRASPECIFICTAXON()) && ! allowInfraSpecTaxonRank)){
181
						rank = handleProlesAndRaceSublusus(state, rs, rank);
182
					}
183 48014773 Andreas M��ller
184 5dd8e8b0 Andreas Müller
					if (rank.getId() == 0){
185
						getTermService().save(rank);
186
						logger.warn("Rank did not yet exist: " +  rank.getTitleCache());
187
					}
188 48014773 Andreas M��ller
189 86536e03 Andreas Müller
					//create TaxonName
190 ea7deae0 Andreas Müller
					TaxonName taxonName;
191 5dd8e8b0 Andreas Müller
					if (config.getNomenclaturalCode() != null){
192 86536e03 Andreas Müller
						taxonName = config.getNomenclaturalCode().getNewTaxonNameInstance(rank);
193 5dd8e8b0 Andreas Müller
						//check cultivar
194 86536e03 Andreas Müller
						if (taxonName.isBotanical()){
195 5dd8e8b0 Andreas Müller
							if (isNotBlank(strCultivarGroupName) && isNotBlank(strCultivarName)){
196 86536e03 Andreas Müller
								taxonName = TaxonNameFactory.NewCultivarInstance(rank);
197 5dd8e8b0 Andreas Müller
							}
198
						}
199
					}else{
200 86536e03 Andreas Müller
						taxonName = TaxonNameFactory.NewNonViralInstance(rank);
201 5dd8e8b0 Andreas Müller
					}
202 ed94068a Andreas Müller
					if (uuid != null){
203 86536e03 Andreas Müller
						taxonName.setUuid(UUID.fromString(uuid));
204 ed94068a Andreas Müller
					}
205 48014773 Andreas M��ller
206 5dd8e8b0 Andreas Müller
					if (rank == null){
207
						//TODO rank should never be null or a more sophisticated algorithm has to be implemented for genus/supraGenericName
208 23336874 Andreas Müller
						logger.warn("Rank is null. Genus epithet was imported. May be wrong");
209 5dd8e8b0 Andreas Müller
						success = false;
210
					}
211 48014773 Andreas M��ller
212 5dd8e8b0 Andreas Müller
					//epithets
213
					if (rank != null && rank.isSupraGeneric()){
214
						dbAttrName = "supraGenericName";
215
					}else{
216
						dbAttrName = "genus";
217
					}
218
					cdmAttrName = "genusOrUninomial";
219 86536e03 Andreas Müller
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
220 48014773 Andreas M��ller
221 5dd8e8b0 Andreas Müller
					dbAttrName = "genusSubdivisionEpi";
222
					cdmAttrName = "infraGenericEpithet";
223 86536e03 Andreas Müller
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
224 48014773 Andreas M��ller
225 5dd8e8b0 Andreas Müller
					dbAttrName = "speciesEpi";
226
					cdmAttrName = "specificEpithet";
227 86536e03 Andreas Müller
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
228 48014773 Andreas M��ller
229
230 5dd8e8b0 Andreas Müller
					dbAttrName = "infraSpeciesEpi";
231
					cdmAttrName = "infraSpecificEpithet";
232 86536e03 Andreas Müller
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
233 48014773 Andreas M��ller
234 5dd8e8b0 Andreas Müller
					dbAttrName = "unnamedNamePhrase";
235
					cdmAttrName = "appendedPhrase";
236 86536e03 Andreas Müller
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
237 48014773 Andreas M��ller
238 5dd8e8b0 Andreas Müller
					//Details
239
					dbAttrName = "details";
240
					cdmAttrName = "nomenclaturalMicroReference";
241 86536e03 Andreas Müller
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
242 48014773 Andreas M��ller
243 5dd8e8b0 Andreas Müller
					//nomRef
244 86536e03 Andreas Müller
					success &= makeNomenclaturalReference(config, taxonName, nameId, rs, partitioner);
245 48014773 Andreas M��ller
246 5dd8e8b0 Andreas Müller
					//Source_Acc
247
					boolean colExists = true;
248
					try {
249
						colExists = state.getConfig().getSource().checkColumnExists("Name", "Source_Acc");
250
					} catch (DatabaseTypeNotSupportedException e) {
251
						logger.debug("Source does not support 'checkColumnExists'");
252
					}
253
					if (colExists){
254
						String sourceAcc = rs.getString("Source_Acc");
255 23b77c49 Andreas Müller
						if (isNotBlank(sourceAcc)){
256 5dd8e8b0 Andreas Müller
							ExtensionType sourceAccExtensionType = getExtensionType(state, SOURCE_ACC_UUID, "Source_Acc","Source_Acc","Source_Acc");
257 23b77c49 Andreas Müller
							Extension.NewInstance(taxonName, sourceAcc, sourceAccExtensionType);
258 5dd8e8b0 Andreas Müller
						}
259
					}
260 48014773 Andreas M��ller
261 5dd8e8b0 Andreas Müller
					//created, notes
262
					boolean excludeUpdated = true;
263 c385a1b7 Andreas Müller
					boolean excludeNotes = true;
264
					success &= doIdCreatedUpdatedNotes(state, taxonName, rs, nameId, NAMESPACE, excludeUpdated, excludeNotes);
265
					handleNameNotes(state, taxonName, rs, nameId);
266 48014773 Andreas M��ller
267 5dd8e8b0 Andreas Müller
					//NonViralName
268 86536e03 Andreas Müller
					if (taxonName.isNonViral()){
269
						INonViralName nonViralName = taxonName;
270 48014773 Andreas M��ller
271 5dd8e8b0 Andreas Müller
						//authorTeams
272
						if (teamMap != null ){
273 48014773 Andreas M��ller
							nonViralName.setCombinationAuthorship(getAuthorTeam(teamMap, authorFk, nameId, config));
274
							nonViralName.setExCombinationAuthorship(getAuthorTeam(teamMap, exAuthorFk, nameId, config));
275
							nonViralName.setBasionymAuthorship(getAuthorTeam(teamMap, basAuthorFk, nameId, config));
276
							nonViralName.setExBasionymAuthorship(getAuthorTeam(teamMap, exBasAuthorFk, nameId, config));
277 5dd8e8b0 Andreas Müller
						}else{
278
							logger.warn("TeamMap is null");
279
							success = false;
280
						}
281
					}//nonviralName
282 48014773 Andreas M��ller
283
284
285 5dd8e8b0 Andreas Müller
					//zoologicalName
286 86536e03 Andreas Müller
					if (taxonName.isZoological()){
287
						IZoologicalName zooName = taxonName;
288 5dd8e8b0 Andreas Müller
						makeZoologialName(rs, zooName, nameId);
289
					}
290 48014773 Andreas M��ller
					//botanicalName
291 86536e03 Andreas Müller
					else if (taxonName.isBotanical()){
292
						IBotanicalName botName = taxonName;
293 5dd8e8b0 Andreas Müller
						success &= makeBotanicalNamePart(rs, botName) ;
294 48014773 Andreas M��ller
295 5dd8e8b0 Andreas Müller
					}
296 48014773 Andreas M��ller
297 5dd8e8b0 Andreas Müller
	//				dbAttrName = "preliminaryFlag";
298
					Boolean preliminaryFlag = rs.getBoolean("PreliminaryFlag");
299
					Boolean hybridFormulaFlag = rs.getBoolean("HybridFormulaFlag");  //hybrid flag does not lead to cache update in Berlin Model
300
					if (preliminaryFlag == true || hybridFormulaFlag == true){
301 48014773 Andreas M��ller
						//Computes all caches and sets
302 86536e03 Andreas Müller
						taxonName.setTitleCache(fullNameCache, true);
303
						taxonName.setFullTitleCache(taxonName.getFullTitleCache(), true);
304
						if (taxonName.isNonViral()){
305
							INonViralName nvn = taxonName;
306 5dd8e8b0 Andreas Müller
							nvn.setNameCache(nameCache, true);
307
							nvn.setAuthorshipCache(nvn.getAuthorshipCache(), true);
308
						}
309
					}
310 86536e03 Andreas Müller
					namesToSave.add(taxonName);
311 48014773 Andreas M��ller
312 5dd8e8b0 Andreas Müller
				}
313
				catch (UnknownCdmTypeException e) {
314
					logger.warn("Name with id " + nameId + " has unknown rankId " + " and could not be saved.");
315 48014773 Andreas M��ller
					success = false;
316 5dd8e8b0 Andreas Müller
				}
317 48014773 Andreas M��ller
318 5dd8e8b0 Andreas Müller
			} //while rs.hasNext()
319
		} catch (SQLException e) {
320
			logger.error("SQLException:" +  e);
321
			return false;
322
		}
323
324 48014773 Andreas M��ller
325 5dd8e8b0 Andreas Müller
//		logger.info( i + " names handled");
326
		getNameService().save(namesToSave);
327
		return success;
328
	}
329
330
331 c385a1b7 Andreas Müller
	/**
332
     * @param state
333
     * @param taxonName
334
     * @param rs
335
     * @param nameId
336
	 * @throws SQLException
337
     */
338
    private void handleNameNotes(BerlinModelImportState state, TaxonName taxonName, ResultSet rs, int nameId) throws SQLException {
339
        String notesOrig = rs.getString("notes");
340
        String notes = filterNotes(notesOrig, nameId);
341
        if (isNotBlank(notes) && taxonName != null ){
342
            String notesString = String.valueOf(notes);
343
            if (notesString.length() > 65530 ){
344
                notesString = notesString.substring(0, 65530) + "...";
345
                logger.warn("Notes string is longer than 65530 and was truncated: " + taxonName);
346
            }
347
            Annotation notesAnnotation = Annotation.NewInstance(notesString, Language.DEFAULT());
348
            //notesAnnotation.setAnnotationType(AnnotationType.EDITORIAL());
349
            //notes.setCommentator(bmiConfig.getCommentator());
350
            taxonName.addAnnotation(notesAnnotation);
351
        }
352
353
    }
354
355
    private static final String MCL = "MCL\\s?[0-9]{1,3}(\\-[0-9]{1,4}(\\-[0-9]{1,4}(\\-[0-9]{1,4}(\\-[0-9]{1,3})?)?)?)?";
356
    /**
357
     * @param notes
358
     */
359
    private String filterNotes(String notes, int nameId) {
360
        String result;
361
        if (isBlank(notes)){
362
            result = null;
363
        }else if (notes.matches("Acc:.*")){
364
            if (notes.matches("Acc: .*\\$$") || (notes.matches("Acc: .*"+MCL))){
365
                result = null;
366
            }else if (notes.matches("Acc: .*(\\$|"+MCL+")\\s*\\{.*\\}")){
367
                notes = notes.substring(notes.indexOf("{")+1, notes.length()-1);
368
                result = notes;
369
            }else if (notes.matches("Acc: .*(\\$|"+MCL+")\\s*\\[.*\\]")){
370
                notes = notes.substring(notes.indexOf("[")+1, notes.length()-1);
371
                result = notes;
372
            }else{
373
                logger.warn("Name id: " + nameId + ". Namenote: " + notes);
374
                result = notes;
375
            }
376
        }else if (notes.matches("Syn:.*")){
377
            if (notes.matches("Syn: .*\\$$") || (notes.matches("Syn: .*"+MCL))){
378
                result = null;
379
            }else if (notes.matches("Syn: .*(\\$|"+MCL+")\\s*\\{.*\\}")){
380
                notes = notes.substring(notes.indexOf("{")+1, notes.length()-1);
381
                result = notes;
382
            }else if (notes.matches("Syn: .*(\\$|"+MCL+")\\s*\\[.*\\]")){
383
                notes = notes.substring(notes.indexOf("[")+1, notes.length()-1);
384
                result = notes;
385
            }else{
386
                logger.warn("Name id: " + nameId + ". Namenote: " + notes);
387
                result = notes;
388
            }
389
        }else{
390
            result = notes;
391
        }
392
        return result;
393
    }
394
395
396
    private Rank handleProlesAndRaceSublusus(BerlinModelImportState state, ResultSet rs, Rank rank) throws SQLException {
397 5dd8e8b0 Andreas Müller
		Rank result;
398
		String rankAbbrev = rs.getString("RankAbbrev");
399
//		String rankStr = rs.getString("Rank");
400
		if (CdmUtils.nullSafeEqual(rankAbbrev, "prol.") ){
401
			result = Rank.PROLES();
402
		}else if(CdmUtils.nullSafeEqual(rankAbbrev, "race")){
403
			result = Rank.RACE();
404
		}else if(CdmUtils.nullSafeEqual(rankAbbrev, "sublusus")){
405
			result = Rank.SUBLUSUS();
406
		}else{
407
			result = rank;
408
			logger.warn("Unhandled rank: " + rankAbbrev);
409
		}
410
		return result;
411
	}
412 48014773 Andreas M��ller
413 7c778a7e Andreas Müller
	@Override
414
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
415 5dd8e8b0 Andreas Müller
		String nameSpace;
416 7c778a7e Andreas Müller
		Class<?> cdmClass;
417 5dd8e8b0 Andreas Müller
		Set<String> idSet;
418 48014773 Andreas M��ller
419 5dd8e8b0 Andreas Müller
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
420 48014773 Andreas M��ller
421 5dd8e8b0 Andreas Müller
		try{
422
			Set<String> teamIdSet = new HashSet<String>();
423
			Set<String> referenceIdSet = new HashSet<String>();
424
			Set<String> refDetailIdSet = new HashSet<String>();
425
			while (rs.next()){
426
				handleForeignKey(rs, teamIdSet, "AuthorTeamFk");
427
				handleForeignKey(rs, teamIdSet, "ExAuthorTeamFk");
428
				handleForeignKey(rs, teamIdSet, "BasAuthorTeamFk");
429
				handleForeignKey(rs, teamIdSet, "ExBasAuthorTeamFk");
430
				handleForeignKey(rs, referenceIdSet, "nomRefFk");
431
				handleForeignKey(rs, refDetailIdSet, "nomRefDetailFk");
432
			}
433 48014773 Andreas M��ller
434 5dd8e8b0 Andreas Müller
			//team map
435
			nameSpace = BerlinModelAuthorTeamImport.NAMESPACE;
436
			cdmClass = Team.class;
437
			idSet = teamIdSet;
438
			Map<String, Person> teamMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
439
			result.put(nameSpace, teamMap);
440
441
			//reference map
442
			nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
443
			cdmClass = Reference.class;
444
			idSet = referenceIdSet;
445
			Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
446
			result.put(nameSpace, referenceMap);
447 48014773 Andreas M��ller
448 5dd8e8b0 Andreas Müller
			//refDetail map
449
			nameSpace = BerlinModelRefDetailImport.REFDETAIL_NAMESPACE;
450
			cdmClass = Reference.class;
451
			idSet = refDetailIdSet;
452
			Map<String, Reference> refDetailMap= (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
453
			result.put(nameSpace, refDetailMap);
454 48014773 Andreas M��ller
455 5dd8e8b0 Andreas Müller
		} catch (SQLException e) {
456
			throw new RuntimeException(e);
457
		}
458
		return result;
459
	}
460 48014773 Andreas M��ller
461 7d882578 Andreas Müller
	private boolean makeZoologialName(ResultSet rs, IZoologicalName zooName, int nameId)
462 5dd8e8b0 Andreas Müller
					throws SQLException{
463
		boolean success = true;
464
		//publicationYear
465
		String authorTeamYear = rs.getString("authorTeamYear");
466
		try {
467
			if (! "".equals(CdmUtils.Nz(authorTeamYear).trim())){
468
				Integer publicationYear  = Integer.valueOf(authorTeamYear.trim());
469
				zooName.setPublicationYear(publicationYear);
470
			}
471
		} catch (NumberFormatException e) {
472
			logger.warn("authorTeamYear could not be parsed for taxonName: "+ nameId);
473
		}
474
		//original publication year
475
		String basAuthorTeamYear = rs.getString("basAuthorTeamYear");
476
		try {
477
			if (! "".equals(CdmUtils.Nz(basAuthorTeamYear).trim())){
478
				Integer OriginalPublicationYear  = Integer.valueOf(basAuthorTeamYear.trim());
479
				zooName.setOriginalPublicationYear(OriginalPublicationYear);
480
			}
481
		} catch (NumberFormatException e) {
482
			logger.warn("basAuthorTeamYear could not be parsed for taxonName: "+ nameId);
483
		}
484
		return success;
485
	}
486 48014773 Andreas M��ller
487 7d882578 Andreas Müller
	private boolean makeBotanicalNamePart(ResultSet rs, IBotanicalName botanicalName)throws SQLException{
488 5dd8e8b0 Andreas Müller
		boolean success = true;
489
		String dbAttrName;
490
		String cdmAttrName;
491 48014773 Andreas M��ller
492 5dd8e8b0 Andreas Müller
		dbAttrName = "HybridFormulaFlag";
493
		cdmAttrName = "isHybridFormula";
494
		success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
495
496
		dbAttrName = "MonomHybFlag";
497
		cdmAttrName = "isMonomHybrid";
498
		success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
499
500
		dbAttrName = "BinomHybFlag";
501
		cdmAttrName = "isBinomHybrid";
502
		success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
503
504
		dbAttrName = "TrinomHybFlag";
505
		cdmAttrName = "isTrinomHybrid";
506
		success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
507
508
		try {
509
			String strCultivarGroupName = rs.getString("CultivarGroupName");
510
			String strCultivarName = rs.getString("CultivarName");
511 86536e03 Andreas Müller
			if (botanicalName.isCultivar()){
512
				ICultivarPlantName cultivarName = (ICultivarPlantName)botanicalName;
513 5dd8e8b0 Andreas Müller
				String concatCultivarName = CdmUtils.concat("-", strCultivarName, strCultivarGroupName);
514 86536e03 Andreas Müller
				if (isNotBlank(strCultivarGroupName) && isNotBlank(strCultivarName)){
515 5dd8e8b0 Andreas Müller
					logger.warn("CDM does not support cultivarGroupName and CultivarName together: " + concatCultivarName);
516
				}
517
				cultivarName.setCultivarName(strCultivarGroupName);
518
			}
519
		} catch (SQLException e) {
520
			throw e;
521
		}
522
		return success;
523
	}
524 48014773 Andreas M��ller
525
526 86536e03 Andreas Müller
	private boolean makeNomenclaturalReference(IImportConfigurator config, TaxonName taxonNameBase,
527 5dd8e8b0 Andreas Müller
					int nameId, ResultSet rs, ResultSetPartitioner partitioner) throws SQLException{
528
		Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
529
		Map<String, Reference> refDetailMap = partitioner.getObjectMap(BerlinModelRefDetailImport.REFDETAIL_NAMESPACE);
530 48014773 Andreas M��ller
531 5dd8e8b0 Andreas Müller
		Object nomRefFkObj = rs.getObject("NomRefFk");
532
		Object nomRefDetailFkObj = rs.getObject("NomRefDetailFk");
533
		boolean refDetailPrelim = rs.getBoolean("RefDetailPrelim");
534 48014773 Andreas M��ller
535 5dd8e8b0 Andreas Müller
		boolean success = true;
536
		//nomenclatural Reference
537
		if (refMap != null){
538
			if (nomRefFkObj != null){
539
				String nomRefFk = String.valueOf(nomRefFkObj);
540
				String nomRefDetailFk = String.valueOf(nomRefDetailFkObj);
541
				//get nomRef
542 8422c0cd Andreas Müller
				Reference nomReference =
543 5dd8e8b0 Andreas Müller
					getReferenceFromMaps(refDetailMap, refMap, nomRefDetailFk, nomRefFk);
544 48014773 Andreas M��ller
545
546 5dd8e8b0 Andreas Müller
				//setNomRef
547
				if (nomReference == null ){
548
					//TODO
549
					if (! config.isIgnoreNull()){
550
						logger.warn("Nomenclatural reference (nomRefFk = " + nomRefFk + ") for TaxonName (nameId = " + nameId + ")"+
551
							" was not found in reference store. Nomenclatural reference was not set!!");
552
					}
553
				}else{
554
					if (! INomenclaturalReference.class.isAssignableFrom(nomReference.getClass())){
555
						logger.warn("Nomenclatural reference (nomRefFk = " + nomRefFk + ") for TaxonName (nameId = " + nameId + ")"+
556
								" is not assignable from INomenclaturalReference. (Class = " + nomReference.getClass()+ ")");
557
					}
558
					nomReference.setNomenclaturallyRelevant(true);
559
					taxonNameBase.setNomenclaturalReference(nomReference);
560
				}
561
			}
562
		}
563
		return success;
564
	}
565 48014773 Andreas M��ller
566 5dd8e8b0 Andreas Müller
	private static TeamOrPersonBase getAuthorTeam(Map<String, Team> teamMap, Object teamIdObject, int nameId, BerlinModelImportConfigurator bmiConfig){
567
		if (teamIdObject == null){
568
			return null;
569
		}else {
570
			String teamId = String.valueOf(teamIdObject);
571
			TeamOrPersonBase author = teamMap.get(teamId);
572
			if (author == null){
573
				//TODO
574 48014773 Andreas M��ller
				if (!bmiConfig.isIgnoreNull() && ! (teamId.equals(0) && bmiConfig.isIgnore0AuthorTeam()) ){
575 5dd8e8b0 Andreas Müller
					logger.warn("AuthorTeam (teamId = " + teamId + ") for TaxonName (nameId = " + nameId + ")"+
576
				" was not found in authorTeam store. Relation was not set!!");}
577
				return null;
578
			}else{
579
				return author;
580
			}
581
		}
582
	}
583 48014773 Andreas M��ller
584 5dd8e8b0 Andreas Müller
	@Override
585
	protected boolean doCheck(BerlinModelImportState state){
586
		IOValidator<BerlinModelImportState> validator = new BerlinModelTaxonNameImportValidator();
587
		return validator.validate(state);
588
	}
589 48014773 Andreas M��ller
590
	@Override
591
    protected boolean isIgnore(BerlinModelImportState state){
592 5dd8e8b0 Andreas Müller
		return ! state.getConfig().isDoTaxonNames();
593
	}
594
595
596
597
598
599 48014773 Andreas M��ller
600
//FOR FUTURE USE , DONT DELETE
601 5dd8e8b0 Andreas Müller
//	new CdmStringMapper("nameId", "nameId"),
602
//	new CdmStringMapper("rankFk", "rankFk"),
603
//	new CdmStringMapper("nameCache", "nameCache"),
604
//	new CdmStringMapper("unnamedNamePhrase", "unnamedNamePhrase"),
605
//	new CdmStringMapper("fullNameCache", "fullNameCache"),
606
//	new CdmStringMapper("preliminaryFlag", "preliminaryFlag"),
607
//	new CdmStringMapper("supragenericName", "supragenericName"),
608
//	new CdmStringMapper("genus", "genus"),
609
//	new CdmStringMapper("genusSubdivisionEpi", "genusSubdivisionEpi"),
610
//	new CdmStringMapper("speciesEpi", "speciesEpi"),
611
//	new CdmStringMapper("infraSpeciesEpi", "infraSpeciesEpi"),
612
//	new CdmStringMapper("authorTeamFk", "authorTeamFk"),
613
//	new CdmStringMapper("exAuthorTeamFk", "exAuthorTeamFk"),
614
//	new CdmStringMapper("basAuthorTeamFk", "basAuthorTeamFk"),
615
//	new CdmStringMapper("exBasAuthorTeamFk", "exBasAuthorTeamFk"),
616
//	new CdmStringMapper("hybridFormulaFlag", "hybridFormulaFlag"),
617
//	new CdmStringMapper("monomHybFlag", "monomHybFlag"),
618
//	new CdmStringMapper("binomHybFlag", "binomHybFlag"),
619
//	new CdmStringMapper("trinomHybFlag", "trinomHybFlag"),
620
//	new CdmStringMapper("cultivarGroupName", "cultivarGroupName"),
621
//	new CdmStringMapper("cultivarName", "cultivarName"),
622
//	new CdmStringMapper("nomRefFk", "nomRefFk"),
623
//	new CdmStringMapper("nomRefDetailFk", "nomRefDetailFk"),
624
//	new CdmStringMapper("nameSourceRefFk", "nameSourceRefFk"),
625
//	new CdmStringMapper("source_Acc", "source_Acc"),
626
//	new CdmStringMapper("created_When", "created_When"),
627
//	new CdmStringMapper("created_Who", "created_Who"),
628
//	new CdmStringMapper("notes", "notes"),
629
//	new CdmStringMapper("parsingComments", "parsingComments"),
630
//	new CdmStringMapper("oldNomRefFk", "oldNomRefFk"),
631
//	new CdmStringMapper("oldNomRefDetailFk", "oldNomRefDetailFk"),
632
//	new CdmStringMapper("updated_Who", "updated_Who"),
633
//	new CdmStringMapper("orthoProjection", "orthoProjection"),
634
635 48014773 Andreas M��ller
636 5dd8e8b0 Andreas Müller
}