Project

General

Profile

Download (24.2 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;
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.commons.lang.StringUtils;
21
import org.apache.log4j.Logger;
22
import org.springframework.stereotype.Component;
23

    
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.database.update.DatabaseTypeNotSupportedException;
26
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
27
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelTaxonNameImportValidator;
28
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
29
import eu.etaxonomy.cdm.io.common.IOValidator;
30
import eu.etaxonomy.cdm.io.common.ImportHelper;
31
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
32
import eu.etaxonomy.cdm.io.common.Source;
33
import eu.etaxonomy.cdm.model.agent.Person;
34
import eu.etaxonomy.cdm.model.agent.Team;
35
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
36
import eu.etaxonomy.cdm.model.common.Annotation;
37
import eu.etaxonomy.cdm.model.common.CdmBase;
38
import eu.etaxonomy.cdm.model.common.Extension;
39
import eu.etaxonomy.cdm.model.common.ExtensionType;
40
import eu.etaxonomy.cdm.model.common.Language;
41
import eu.etaxonomy.cdm.model.common.Representation;
42
import eu.etaxonomy.cdm.model.name.IBotanicalName;
43
import eu.etaxonomy.cdm.model.name.ICultivarPlantName;
44
import eu.etaxonomy.cdm.model.name.INonViralName;
45
import eu.etaxonomy.cdm.model.name.IZoologicalName;
46
import eu.etaxonomy.cdm.model.name.Rank;
47
import eu.etaxonomy.cdm.model.name.TaxonName;
48
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
49
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
50
import eu.etaxonomy.cdm.model.reference.Reference;
51
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
52

    
53
/**
54
 * @author a.mueller
55
 * @since 20.03.2008
56
 */
57
@Component
58
public class BerlinModelTaxonNameImport extends BerlinModelImportBase {
59
    private static final long serialVersionUID = -8860800694286602016L;
60

    
61
    private static final boolean BLANK_TO_NULL = true;
62

    
63
	private static final Logger logger = Logger.getLogger(BerlinModelTaxonNameImport.class);
64

    
65
	public static final String NAMESPACE = "TaxonName";
66

    
67
	public static final UUID SOURCE_ACC_UUID = UUID.fromString("c3959b4f-d876-4b7a-a739-9260f4cafd1c");
68

    
69
	private static int modCount = 5000;
70
	private static final String pluralString = "TaxonNames";
71
	private static final String dbTableName = "Name";
72

    
73

    
74
	public BerlinModelTaxonNameImport(){
75
		super(dbTableName, pluralString);
76
	}
77

    
78

    
79
	@Override
80
	protected String getIdQuery(BerlinModelImportState state) {
81
		if (state.getConfig().getNameIdTable()==null ){
82
			return super.getIdQuery(state);
83
		}else{
84
			return "SELECT nameId FROM " + state.getConfig().getNameIdTable() + "";
85
		}
86
	}
87

    
88
	@Override
89
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
90
		Source source = config.getSource();
91

    
92
			String facultativCols = "";
93
			String strFacTable = "RefDetail";
94
			String strFacColumn = "IdInSource";
95
			String strColAlias = null;
96
			if (checkSqlServerColumnExists(source, strFacTable, strFacColumn)){
97
				facultativCols +=  ", " + strFacTable + "." + strFacColumn ;
98
				if (! CdmUtils.Nz(strColAlias).equals("") ){
99
					facultativCols += " AS " + strColAlias;
100
				}
101
			}
102

    
103
		String strRecordQuery =
104
					"SELECT Name.* , RefDetail.RefDetailId, RefDetail.RefFk, " +
105
                      		" RefDetail.FullRefCache, RefDetail.FullNomRefCache, RefDetail.PreliminaryFlag AS RefDetailPrelim, RefDetail.Details, " +
106
                      		" RefDetail.SecondarySources, Rank.RankAbbrev, Rank.Rank " +
107
                      		facultativCols +
108
                    " FROM Name LEFT OUTER JOIN RefDetail ON Name.NomRefDetailFk = RefDetail.RefDetailId AND  " +
109
                    	" Name.NomRefFk = RefDetail.RefFk " +
110
                    	" LEFT OUTER JOIN Rank ON Name.RankFk = Rank.rankID " +
111
                " WHERE name.nameId IN ("+ID_LIST_TOKEN+") ";
112
					//strQuery += " AND RefDetail.PreliminaryFlag = 1 ";
113
					//strQuery += " AND Name.Created_When > '03.03.2004' ";
114
		return strRecordQuery +  "";
115
	}
116

    
117

    
118

    
119
	@Override
120
	protected void doInvoke(BerlinModelImportState state) {
121
		//update rank labels if necessary
122
		String strAbbrev = state.getConfig().getInfrGenericRankAbbrev();
123
		Rank rank = Rank.INFRAGENERICTAXON();
124
		testRankAbbrev(strAbbrev, rank);
125

    
126
		strAbbrev = state.getConfig().getInfrSpecificRankAbbrev();
127
		rank = Rank.INFRASPECIFICTAXON();
128
		testRankAbbrev(strAbbrev, rank);
129

    
130
		super.doInvoke(state);
131
	}
132

    
133
	private void testRankAbbrev(String strAbbrev, Rank rank) {
134
		if (strAbbrev != null){
135
			Representation rep = rank.getRepresentation(Language.ENGLISH());
136
			rep.setAbbreviatedLabel(strAbbrev);
137
			getTermService().saveOrUpdate(rank);
138
		}
139
	}
140

    
141
	@Override
142
	public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
143
		String dbAttrName;
144
		String cdmAttrName;
145
		boolean success = true ;
146
		BerlinModelImportConfigurator config = state.getConfig();
147
		Set<TaxonName> namesToSave = new HashSet<>();
148
		Map<String, Team> teamMap = partitioner.getObjectMap(BerlinModelAuthorTeamImport.NAMESPACE);
149

    
150
		ResultSet rs = partitioner.getResultSet();
151

    
152
		try {
153
			int i = 0;
154
			//for each reference
155
			while (rs.next()){
156

    
157
				if ((i++ % modCount) == 0 && i != 1 ){ logger.info("Names handled: " + (i-1));}
158

    
159
				//create TaxonName element
160
				int nameId = rs.getInt("nameId");
161
				Object authorFk = rs.getObject("AuthorTeamFk");
162
				Object exAuthorFk = rs.getObject("ExAuthorTeamFk");
163
				Object basAuthorFk = rs.getObject("BasAuthorTeamFk");
164
				Object exBasAuthorFk = rs.getObject("ExBasAuthorTeamFk");
165
				String strCultivarGroupName = rs.getString("CultivarGroupName");
166
				String strCultivarName = rs.getString("CultivarName");
167
				String nameCache = rs.getString("NameCache");
168
				String fullNameCache = rs.getString("FullNameCache");
169
				String uuid = null;
170
				if (resultSetHasColumn(rs,"UUID")){
171
					uuid = rs.getString("UUID");
172
				}
173

    
174
				try {
175

    
176
					//define rank
177
					boolean useUnknownRank = true;
178
					Rank rank = BerlinModelTransformer.rankId2Rank(rs, useUnknownRank, config.isSwitchSpeciesGroup());
179

    
180
					boolean allowInfraSpecTaxonRank = state.getConfig().isAllowInfraSpecTaxonRank() ;
181
					if (rank == null || rank.equals(Rank.UNKNOWN_RANK()) || (rank.equals(Rank.INFRASPECIFICTAXON()) && ! allowInfraSpecTaxonRank)){
182
						rank = handleProlesAndRaceSublusus(state, rs, rank);
183
					}
184

    
185
					if (rank.getId() == 0){
186
						getTermService().save(rank);
187
						logger.warn("Rank did not yet exist: " +  rank.getTitleCache());
188
					}
189

    
190
					//create TaxonName
191
					TaxonName taxonName;
192
					if (config.getNomenclaturalCode() != null){
193
						taxonName = config.getNomenclaturalCode().getNewTaxonNameInstance(rank);
194
						//check cultivar
195
						if (taxonName.isBotanical()){
196
							if (isNotBlank(strCultivarGroupName) && isNotBlank(strCultivarName)){
197
								taxonName = TaxonNameFactory.NewCultivarInstance(rank);
198
							}
199
						}
200
					}else{
201
						taxonName = TaxonNameFactory.NewNonViralInstance(rank);
202
					}
203
					if (uuid != null){
204
						taxonName.setUuid(UUID.fromString(uuid));
205
					}
206

    
207
					if (rank == null){
208
						//TODO rank should never be null or a more sophisticated algorithm has to be implemented for genus/supraGenericName
209
						logger.warn("Rank is null. Genus epithet was imported. May be wrong");
210
						success = false;
211
					}
212

    
213
					//epithets
214
					if (rank != null && rank.isSupraGeneric()){
215
						dbAttrName = "supraGenericName";
216
					}else{
217
						dbAttrName = "genus";
218
					}
219
					cdmAttrName = "genusOrUninomial";
220
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
221

    
222
					dbAttrName = "genusSubdivisionEpi";
223
					cdmAttrName = "infraGenericEpithet";
224
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
225

    
226
					dbAttrName = "speciesEpi";
227
					cdmAttrName = "specificEpithet";
228
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
229

    
230

    
231
					dbAttrName = "infraSpeciesEpi";
232
					cdmAttrName = "infraSpecificEpithet";
233
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
234

    
235
					dbAttrName = "unnamedNamePhrase";
236
					cdmAttrName = "appendedPhrase";
237
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
238

    
239
					//Details
240
					dbAttrName = "details";
241
					cdmAttrName = "nomenclaturalMicroReference";
242
					success &= ImportHelper.addStringValue(rs, taxonName, dbAttrName, cdmAttrName, BLANK_TO_NULL);
243

    
244
					//nomRef
245
					success &= makeNomenclaturalReference(config, taxonName, nameId, rs, partitioner);
246

    
247
					//Source_Acc
248
					boolean colExists = true;
249
					try {
250
						colExists = state.getConfig().getSource().checkColumnExists("Name", "Source_Acc");
251
					} catch (DatabaseTypeNotSupportedException e) {
252
						logger.debug("Source does not support 'checkColumnExists'");
253
					}
254
					if (colExists){
255
						String sourceAcc = rs.getString("Source_Acc");
256
						if (StringUtils.isNotBlank(sourceAcc)){
257
							ExtensionType sourceAccExtensionType = getExtensionType(state, SOURCE_ACC_UUID, "Source_Acc","Source_Acc","Source_Acc");
258
							Extension datesExtension = Extension.NewInstance(taxonName, sourceAcc, sourceAccExtensionType);
259
						}
260
					}
261

    
262
					//created, notes
263
					boolean excludeUpdated = true;
264
					boolean excludeNotes = true;
265
					success &= doIdCreatedUpdatedNotes(state, taxonName, rs, nameId, NAMESPACE, excludeUpdated, excludeNotes);
266
					handleNameNotes(state, taxonName, rs, nameId);
267

    
268
					//NonViralName
269
					if (taxonName.isNonViral()){
270
						INonViralName nonViralName = taxonName;
271

    
272
						//authorTeams
273
						if (teamMap != null ){
274
							nonViralName.setCombinationAuthorship(getAuthorTeam(teamMap, authorFk, nameId, config));
275
							nonViralName.setExCombinationAuthorship(getAuthorTeam(teamMap, exAuthorFk, nameId, config));
276
							nonViralName.setBasionymAuthorship(getAuthorTeam(teamMap, basAuthorFk, nameId, config));
277
							nonViralName.setExBasionymAuthorship(getAuthorTeam(teamMap, exBasAuthorFk, nameId, config));
278
						}else{
279
							logger.warn("TeamMap is null");
280
							success = false;
281
						}
282
					}//nonviralName
283

    
284

    
285

    
286
					//zoologicalName
287
					if (taxonName.isZoological()){
288
						IZoologicalName zooName = taxonName;
289
						makeZoologialName(rs, zooName, nameId);
290
					}
291
					//botanicalName
292
					else if (taxonName.isBotanical()){
293
						IBotanicalName botName = taxonName;
294
						success &= makeBotanicalNamePart(rs, botName) ;
295

    
296
					}
297

    
298
	//				dbAttrName = "preliminaryFlag";
299
					Boolean preliminaryFlag = rs.getBoolean("PreliminaryFlag");
300
					Boolean hybridFormulaFlag = rs.getBoolean("HybridFormulaFlag");  //hybrid flag does not lead to cache update in Berlin Model
301
					if (preliminaryFlag == true || hybridFormulaFlag == true){
302
						//Computes all caches and sets
303
						taxonName.setTitleCache(fullNameCache, true);
304
						taxonName.setFullTitleCache(taxonName.getFullTitleCache(), true);
305
						if (taxonName.isNonViral()){
306
							INonViralName nvn = taxonName;
307
							nvn.setNameCache(nameCache, true);
308
							nvn.setAuthorshipCache(nvn.getAuthorshipCache(), true);
309
						}
310
					}
311
					namesToSave.add(taxonName);
312

    
313
				}
314
				catch (UnknownCdmTypeException e) {
315
					logger.warn("Name with id " + nameId + " has unknown rankId " + " and could not be saved.");
316
					success = false;
317
				}
318

    
319
			} //while rs.hasNext()
320
		} catch (SQLException e) {
321
			logger.error("SQLException:" +  e);
322
			return false;
323
		}
324

    
325

    
326
//		logger.info( i + " names handled");
327
		getNameService().save(namesToSave);
328
		return success;
329
	}
330

    
331

    
332
	/**
333
     * @param state
334
     * @param taxonName
335
     * @param rs
336
     * @param nameId
337
	 * @throws SQLException
338
     */
339
    private void handleNameNotes(BerlinModelImportState state, TaxonName taxonName, ResultSet rs, int nameId) throws SQLException {
340
        String notesOrig = rs.getString("notes");
341
        String notes = filterNotes(notesOrig, nameId);
342
        if (isNotBlank(notes) && taxonName != null ){
343
            String notesString = String.valueOf(notes);
344
            if (notesString.length() > 65530 ){
345
                notesString = notesString.substring(0, 65530) + "...";
346
                logger.warn("Notes string is longer than 65530 and was truncated: " + taxonName);
347
            }
348
            Annotation notesAnnotation = Annotation.NewInstance(notesString, Language.DEFAULT());
349
            //notesAnnotation.setAnnotationType(AnnotationType.EDITORIAL());
350
            //notes.setCommentator(bmiConfig.getCommentator());
351
            taxonName.addAnnotation(notesAnnotation);
352
        }
353

    
354
    }
355

    
356
    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})?)?)?)?";
357
    /**
358
     * @param notes
359
     */
360
    private String filterNotes(String notes, int nameId) {
361
        String result;
362
        if (isBlank(notes)){
363
            result = null;
364
        }else if (notes.matches("Acc:.*")){
365
            if (notes.matches("Acc: .*\\$$") || (notes.matches("Acc: .*"+MCL))){
366
                result = null;
367
            }else if (notes.matches("Acc: .*(\\$|"+MCL+")\\s*\\{.*\\}")){
368
                notes = notes.substring(notes.indexOf("{")+1, notes.length()-1);
369
                result = notes;
370
            }else if (notes.matches("Acc: .*(\\$|"+MCL+")\\s*\\[.*\\]")){
371
                notes = notes.substring(notes.indexOf("[")+1, notes.length()-1);
372
                result = notes;
373
            }else{
374
                logger.warn("Name id: " + nameId + ". Namenote: " + notes);
375
                result = notes;
376
            }
377
        }else if (notes.matches("Syn:.*")){
378
            if (notes.matches("Syn: .*\\$$") || (notes.matches("Syn: .*"+MCL))){
379
                result = null;
380
            }else if (notes.matches("Syn: .*(\\$|"+MCL+")\\s*\\{.*\\}")){
381
                notes = notes.substring(notes.indexOf("{")+1, notes.length()-1);
382
                result = notes;
383
            }else if (notes.matches("Syn: .*(\\$|"+MCL+")\\s*\\[.*\\]")){
384
                notes = notes.substring(notes.indexOf("[")+1, notes.length()-1);
385
                result = notes;
386
            }else{
387
                logger.warn("Name id: " + nameId + ". Namenote: " + notes);
388
                result = notes;
389
            }
390
        }else{
391
            result = notes;
392
        }
393
        return result;
394
    }
395

    
396

    
397
    private Rank handleProlesAndRaceSublusus(BerlinModelImportState state, ResultSet rs, Rank rank) throws SQLException {
398
		Rank result;
399
		String rankAbbrev = rs.getString("RankAbbrev");
400
//		String rankStr = rs.getString("Rank");
401
		if (CdmUtils.nullSafeEqual(rankAbbrev, "prol.") ){
402
			result = Rank.PROLES();
403
		}else if(CdmUtils.nullSafeEqual(rankAbbrev, "race")){
404
			result = Rank.RACE();
405
		}else if(CdmUtils.nullSafeEqual(rankAbbrev, "sublusus")){
406
			result = Rank.SUBLUSUS();
407
		}else{
408
			result = rank;
409
			logger.warn("Unhandled rank: " + rankAbbrev);
410
		}
411
		return result;
412
	}
413

    
414
	@Override
415
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
416
		String nameSpace;
417
		Class<?> cdmClass;
418
		Set<String> idSet;
419

    
420
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
421

    
422
		try{
423
			Set<String> teamIdSet = new HashSet<String>();
424
			Set<String> referenceIdSet = new HashSet<String>();
425
			Set<String> refDetailIdSet = new HashSet<String>();
426
			while (rs.next()){
427
				handleForeignKey(rs, teamIdSet, "AuthorTeamFk");
428
				handleForeignKey(rs, teamIdSet, "ExAuthorTeamFk");
429
				handleForeignKey(rs, teamIdSet, "BasAuthorTeamFk");
430
				handleForeignKey(rs, teamIdSet, "ExBasAuthorTeamFk");
431
				handleForeignKey(rs, referenceIdSet, "nomRefFk");
432
				handleForeignKey(rs, refDetailIdSet, "nomRefDetailFk");
433
			}
434

    
435
			//team map
436
			nameSpace = BerlinModelAuthorTeamImport.NAMESPACE;
437
			cdmClass = Team.class;
438
			idSet = teamIdSet;
439
			Map<String, Person> teamMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
440
			result.put(nameSpace, teamMap);
441

    
442
			//reference map
443
			nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
444
			cdmClass = Reference.class;
445
			idSet = referenceIdSet;
446
			Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
447
			result.put(nameSpace, referenceMap);
448

    
449
			//refDetail map
450
			nameSpace = BerlinModelRefDetailImport.REFDETAIL_NAMESPACE;
451
			cdmClass = Reference.class;
452
			idSet = refDetailIdSet;
453
			Map<String, Reference> refDetailMap= (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
454
			result.put(nameSpace, refDetailMap);
455

    
456
		} catch (SQLException e) {
457
			throw new RuntimeException(e);
458
		}
459
		return result;
460
	}
461

    
462
	private boolean makeZoologialName(ResultSet rs, IZoologicalName zooName, int nameId)
463
					throws SQLException{
464
		boolean success = true;
465
		//publicationYear
466
		String authorTeamYear = rs.getString("authorTeamYear");
467
		try {
468
			if (! "".equals(CdmUtils.Nz(authorTeamYear).trim())){
469
				Integer publicationYear  = Integer.valueOf(authorTeamYear.trim());
470
				zooName.setPublicationYear(publicationYear);
471
			}
472
		} catch (NumberFormatException e) {
473
			logger.warn("authorTeamYear could not be parsed for taxonName: "+ nameId);
474
		}
475
		//original publication year
476
		String basAuthorTeamYear = rs.getString("basAuthorTeamYear");
477
		try {
478
			if (! "".equals(CdmUtils.Nz(basAuthorTeamYear).trim())){
479
				Integer OriginalPublicationYear  = Integer.valueOf(basAuthorTeamYear.trim());
480
				zooName.setOriginalPublicationYear(OriginalPublicationYear);
481
			}
482
		} catch (NumberFormatException e) {
483
			logger.warn("basAuthorTeamYear could not be parsed for taxonName: "+ nameId);
484
		}
485
		return success;
486
	}
487

    
488
	private boolean makeBotanicalNamePart(ResultSet rs, IBotanicalName botanicalName)throws SQLException{
489
		boolean success = true;
490
		String dbAttrName;
491
		String cdmAttrName;
492

    
493
		dbAttrName = "HybridFormulaFlag";
494
		cdmAttrName = "isHybridFormula";
495
		success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
496

    
497
		dbAttrName = "MonomHybFlag";
498
		cdmAttrName = "isMonomHybrid";
499
		success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
500

    
501
		dbAttrName = "BinomHybFlag";
502
		cdmAttrName = "isBinomHybrid";
503
		success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
504

    
505
		dbAttrName = "TrinomHybFlag";
506
		cdmAttrName = "isTrinomHybrid";
507
		success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
508

    
509
		try {
510
			String strCultivarGroupName = rs.getString("CultivarGroupName");
511
			String strCultivarName = rs.getString("CultivarName");
512
			if (botanicalName.isCultivar()){
513
				ICultivarPlantName cultivarName = (ICultivarPlantName)botanicalName;
514
				String concatCultivarName = CdmUtils.concat("-", strCultivarName, strCultivarGroupName);
515
				if (isNotBlank(strCultivarGroupName) && isNotBlank(strCultivarName)){
516
					logger.warn("CDM does not support cultivarGroupName and CultivarName together: " + concatCultivarName);
517
				}
518
				cultivarName.setCultivarName(strCultivarGroupName);
519
			}
520
		} catch (SQLException e) {
521
			throw e;
522
		}
523
		return success;
524
	}
525

    
526

    
527
	private boolean makeNomenclaturalReference(IImportConfigurator config, TaxonName taxonNameBase,
528
					int nameId, ResultSet rs, ResultSetPartitioner partitioner) throws SQLException{
529
		Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
530
		Map<String, Reference> refDetailMap = partitioner.getObjectMap(BerlinModelRefDetailImport.REFDETAIL_NAMESPACE);
531

    
532
		Object nomRefFkObj = rs.getObject("NomRefFk");
533
		Object nomRefDetailFkObj = rs.getObject("NomRefDetailFk");
534
		boolean refDetailPrelim = rs.getBoolean("RefDetailPrelim");
535

    
536
		boolean success = true;
537
		//nomenclatural Reference
538
		if (refMap != null){
539
			if (nomRefFkObj != null){
540
				String nomRefFk = String.valueOf(nomRefFkObj);
541
				String nomRefDetailFk = String.valueOf(nomRefDetailFkObj);
542
				//get nomRef
543
				Reference nomReference =
544
					getReferenceFromMaps(refDetailMap, refMap, nomRefDetailFk, nomRefFk);
545

    
546

    
547
				//setNomRef
548
				if (nomReference == null ){
549
					//TODO
550
					if (! config.isIgnoreNull()){
551
						logger.warn("Nomenclatural reference (nomRefFk = " + nomRefFk + ") for TaxonName (nameId = " + nameId + ")"+
552
							" was not found in reference store. Nomenclatural reference was not set!!");
553
					}
554
				}else{
555
					if (! INomenclaturalReference.class.isAssignableFrom(nomReference.getClass())){
556
						logger.warn("Nomenclatural reference (nomRefFk = " + nomRefFk + ") for TaxonName (nameId = " + nameId + ")"+
557
								" is not assignable from INomenclaturalReference. (Class = " + nomReference.getClass()+ ")");
558
					}
559
					nomReference.setNomenclaturallyRelevant(true);
560
					taxonNameBase.setNomenclaturalReference(nomReference);
561
				}
562
			}
563
		}
564
		return success;
565
	}
566

    
567
	private static TeamOrPersonBase getAuthorTeam(Map<String, Team> teamMap, Object teamIdObject, int nameId, BerlinModelImportConfigurator bmiConfig){
568
		if (teamIdObject == null){
569
			return null;
570
		}else {
571
			String teamId = String.valueOf(teamIdObject);
572
			TeamOrPersonBase author = teamMap.get(teamId);
573
			if (author == null){
574
				//TODO
575
				if (!bmiConfig.isIgnoreNull() && ! (teamId.equals(0) && bmiConfig.isIgnore0AuthorTeam()) ){
576
					logger.warn("AuthorTeam (teamId = " + teamId + ") for TaxonName (nameId = " + nameId + ")"+
577
				" was not found in authorTeam store. Relation was not set!!");}
578
				return null;
579
			}else{
580
				return author;
581
			}
582
		}
583
	}
584

    
585
	@Override
586
	protected boolean doCheck(BerlinModelImportState state){
587
		IOValidator<BerlinModelImportState> validator = new BerlinModelTaxonNameImportValidator();
588
		return validator.validate(state);
589
	}
590

    
591
	@Override
592
    protected boolean isIgnore(BerlinModelImportState state){
593
		return ! state.getConfig().isDoTaxonNames();
594
	}
595

    
596

    
597

    
598

    
599

    
600

    
601
//FOR FUTURE USE , DONT DELETE
602
//	new CdmStringMapper("nameId", "nameId"),
603
//	new CdmStringMapper("rankFk", "rankFk"),
604
//	new CdmStringMapper("nameCache", "nameCache"),
605
//	new CdmStringMapper("unnamedNamePhrase", "unnamedNamePhrase"),
606
//	new CdmStringMapper("fullNameCache", "fullNameCache"),
607
//	new CdmStringMapper("preliminaryFlag", "preliminaryFlag"),
608
//	new CdmStringMapper("supragenericName", "supragenericName"),
609
//	new CdmStringMapper("genus", "genus"),
610
//	new CdmStringMapper("genusSubdivisionEpi", "genusSubdivisionEpi"),
611
//	new CdmStringMapper("speciesEpi", "speciesEpi"),
612
//	new CdmStringMapper("infraSpeciesEpi", "infraSpeciesEpi"),
613
//	new CdmStringMapper("authorTeamFk", "authorTeamFk"),
614
//	new CdmStringMapper("exAuthorTeamFk", "exAuthorTeamFk"),
615
//	new CdmStringMapper("basAuthorTeamFk", "basAuthorTeamFk"),
616
//	new CdmStringMapper("exBasAuthorTeamFk", "exBasAuthorTeamFk"),
617
//	new CdmStringMapper("hybridFormulaFlag", "hybridFormulaFlag"),
618
//	new CdmStringMapper("monomHybFlag", "monomHybFlag"),
619
//	new CdmStringMapper("binomHybFlag", "binomHybFlag"),
620
//	new CdmStringMapper("trinomHybFlag", "trinomHybFlag"),
621
//	new CdmStringMapper("cultivarGroupName", "cultivarGroupName"),
622
//	new CdmStringMapper("cultivarName", "cultivarName"),
623
//	new CdmStringMapper("nomRefFk", "nomRefFk"),
624
//	new CdmStringMapper("nomRefDetailFk", "nomRefDetailFk"),
625
//	new CdmStringMapper("nameSourceRefFk", "nameSourceRefFk"),
626
//	new CdmStringMapper("source_Acc", "source_Acc"),
627
//	new CdmStringMapper("created_When", "created_When"),
628
//	new CdmStringMapper("created_Who", "created_Who"),
629
//	new CdmStringMapper("notes", "notes"),
630
//	new CdmStringMapper("parsingComments", "parsingComments"),
631
//	new CdmStringMapper("oldNomRefFk", "oldNomRefFk"),
632
//	new CdmStringMapper("oldNomRefDetailFk", "oldNomRefDetailFk"),
633
//	new CdmStringMapper("updated_Who", "updated_Who"),
634
//	new CdmStringMapper("orthoProjection", "orthoProjection"),
635

    
636

    
637
}
(15-15/21)