Project

General

Profile

« Previous | Next » 

Revision 58cd368c

Added by Andreas Müller over 4 years ago

ref #8509 use kingdomId to define rank for PESI export

View differences:

cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiTaxonExport.java
46 46
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
47 47
import eu.etaxonomy.cdm.io.common.mapping.out.ObjectChangeMapper;
48 48
import eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer;
49
import eu.etaxonomy.cdm.model.common.Annotation;
50 49
import eu.etaxonomy.cdm.model.common.AnnotationType;
51 50
import eu.etaxonomy.cdm.model.common.CdmBase;
52 51
import eu.etaxonomy.cdm.model.common.Extension;
53 52
import eu.etaxonomy.cdm.model.common.ExtensionType;
54 53
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
55 54
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
56
import eu.etaxonomy.cdm.model.common.Language;
57 55
import eu.etaxonomy.cdm.model.common.Marker;
58 56
import eu.etaxonomy.cdm.model.common.MarkerType;
59 57
import eu.etaxonomy.cdm.model.common.RelationshipBase;
......
309 307
				}
310 308

  
311 309
				//TODO switch on again, leads to some warnings in ERMS for taxa of not correctly handled kingdoms
312
//				validatePhaseOne(taxon, nvn);
310
				validatePhaseOne(taxon, nvn);
313 311
			}
314 312

  
315 313
			// Commit transaction
......
336 334
	private void validatePhaseOne(TaxonBase<?> taxon, TaxonName taxonName) {
337 335

  
338 336
	    // Check whether some rules are violated
339
		NomenclaturalCode nomenclaturalCode = taxonName.getNameType();
340 337
		String genusOrUninomial = taxonName.getGenusOrUninomial();
341 338
		String specificEpithet = taxonName.getSpecificEpithet();
342 339
		String infraSpecificEpithet = taxonName.getInfraSpecificEpithet();
343 340
		String infraGenericEpithet = taxonName.getInfraGenericEpithet();
344
		Integer rankFk = getRankFk(taxonName, nomenclaturalCode);
341
		Rank rank =  taxonName.getRank();
345 342

  
346
		if (rankFk == null) {
347
			logger.error("Rank was not determined: " + taxon.getUuid() + " (" + taxon.getTitleCache() + ")");
348
		} else {
343
		//as kingdomFk can not be defined in Phase 01 the below code was switched to use the CDM rank. This may be changed if we move validation to Phase03 or later
344
//		Integer rankFk = getRankFk(taxonName, taxonName.getNameType());
345
//		if (rankFk == null) {
346
//			logger.error("Rank was not determined: " + taxon.getUuid() + " (" + taxon.getTitleCache() + ")");
347
//		} else {
349 348

  
350 349
			// Check whether infraGenericEpithet is set correctly
351 350
			// 1. Childs of an accepted taxon of rank subgenus that are accepted taxa of rank species have to have an infraGenericEpithet
......
370 369
				}
371 370
			}
372 371

  
373
			if (infraGenericEpithet == null && rankFk.intValue() == 190) {
374
				logger.warn("InfraGenericEpithet was not determined although it should exist for rank 190: " + taxon.getUuid() + " (" + taxon.getTitleCache() + ")");
372
			if (infraGenericEpithet == null && rank.isInfraGenericButNotSpeciesGroup()) {
373
				logger.warn("InfraGenericEpithet was not determined although it should exist for infra generic names: " + taxon.getUuid() + " (" + taxon.getTitleCache() + ")");
375 374
			}
376
			if (specificEpithet != null && rankFk.intValue() < 216) {
377
				logger.warn("SpecificEpithet was determined for rank " + rankFk + " although it should only exist for ranks higher or equal to 220: TaxonName " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
375
			if (specificEpithet != null && (rank.isInfraGenericButNotSpeciesGroup()||rank.isGenus()||rank.isSupraGeneric())) {
376
				logger.warn("SpecificEpithet was determined for rank " + rank.getTitleCache() + " although it should only exist for species aggregates, species or infraspecific taxa: TaxonName " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
378 377
			}
379
			if (infraSpecificEpithet != null && rankFk.intValue() < 225) {
380
				String message = "InfraSpecificEpithet '" +infraSpecificEpithet + "' was determined for rank " + rankFk + " although it should only exist for ranks higher or equal to 230: "  + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")";
378
			if (infraSpecificEpithet != null && !rank.isInfraSpecific()) {
379
				String message = "InfraSpecificEpithet '" +infraSpecificEpithet + "' was determined for rank " + rank.getTitleCache() + " although it should only exist for rank species and higher: "  + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")";
381 380
				if (StringUtils.isNotBlank(infraSpecificEpithet)){
382 381
					logger.warn(message);
383 382
				}else{
384 383
					logger.warn(message);
385 384
				}
386 385
			}
387
		}
386
//		}
388 387
		if (infraSpecificEpithet != null && specificEpithet == null) {
389 388
			logger.warn("An infraSpecificEpithet was determined, but a specificEpithet was not determined: "  + taxon.getUuid() + " (" + taxon.getTitleCache() + ")");
390 389
		}
......
508 507
				doCount(count++, modCount, pluralString);
509 508
				Integer typeNameFk = getTypeNameFk(taxonName, state);
510 509
				Integer kingdomFk = findKingdomIdFromTreeIndex(taxon, state);
511
				 //       PesiTransformer.nomenClaturalCode2Kingdom(nomenclaturalCode);
512

  
513
				//TODO why are expertFks needed? (Andreas M.)
514
//				if (expertFk != null || speciesExpertFk != null) {
515
				    NomenclaturalCode nomCode = taxonName.getNameType();
516
				    //is there a reason why we do pass nomCode separately? Before nomCode was class variable, but not clear why and when it was set
517
					invokeRankDataAndTypeNameFkAndKingdomFk(taxonName, nomCode, state.getDbId(taxon),
518
							typeNameFk, kingdomFk, state);
519
//				}
510

  
511
			    invokeRankDataAndTypeNameFkAndKingdomFk(taxonName, state.getDbId(taxon),
512
						typeNameFk, kingdomFk, state);
520 513
			}
521 514

  
522 515
			// Commit transaction
......
712 705
			// Save Rank Data and KingdomFk for inferred synonyms
713 706
			for (Integer taxonFk : inferredSynonymsDataToBeSaved.keySet()) {
714 707
			    TaxonName taxonName = inferredSynonymsDataToBeSaved.get(taxonFk);
715
                NomenclaturalCode nomCode = taxonName.getNameType(); //nomCode was class variable before, not sure if this was important
716
                invokeRankDataAndKingdomFk(inferredSynonymsDataToBeSaved.get(taxonFk), nomCode, taxonFk, kingdomFk, state);
708
                invokeRankDataAndKingdomFk(inferredSynonymsDataToBeSaved.get(taxonFk), taxonFk, kingdomFk, state);
717 709
			}
718 710

  
719 711
			// Start transaction
......
743 735
			// Save Rank Data and KingdomFk for inferred synonyms
744 736
			for (Integer taxonFk : inferredSynonymsDataToBeSaved.keySet()) {
745 737
			    TaxonName taxonName = inferredSynonymsDataToBeSaved.get(taxonFk);
746
			    NomenclaturalCode nomCode = taxonName.getNameType(); //nomCode was class variable before, not sure if this was important
747
				invokeRankDataAndKingdomFk(inferredSynonymsDataToBeSaved.get(taxonFk), nomCode, taxonFk, kingdomFk, state);
738
			    invokeRankDataAndKingdomFk(taxonName, taxonFk, kingdomFk, state);
748 739
			}
749 740

  
750 741
			// Start transaction
......
1057 1048
	 * @param kindomFk The KingdomFk.
1058 1049
	 * @return Whether save was successful or not.
1059 1050
	 */
1060
	private boolean invokeRankDataAndKingdomFk(TaxonName taxonName, NomenclaturalCode nomenclaturalCode, Integer taxonFk, Integer kingdomFk, PesiExportState state) {
1061
		try {
1062
			Integer rankFk = getRankFk(taxonName, nomenclaturalCode);
1051
	private boolean invokeRankDataAndKingdomFk(TaxonName taxonName,
1052
	        Integer taxonFk, Integer kingdomFk, PesiExportState state) {
1053

  
1054
	    try {
1055
			Integer rankFk = getRankFk(taxonName, kingdomFk);
1063 1056
			if (rankFk != null) {
1064 1057
				rankUpdateStmt.setInt(1, rankFk);
1065 1058
			} else {
1066 1059
				rankUpdateStmt.setObject(1, null);
1067 1060
			}
1068 1061

  
1069
			String rankCache = getRankCache(taxonName, nomenclaturalCode, state);
1062
			String rankCache = getRankCache(taxonName, kingdomFk, state);
1070 1063
			if (rankCache != null) {
1071 1064
				rankUpdateStmt.setString(2, rankCache);
1072 1065
			} else {
......
1107 1100
	 * @param speciesExpertFk The SpeciesExpertFk.
1108 1101
	 * @return Whether save was successful or not.
1109 1102
	 */
1110
	private boolean invokeRankDataAndTypeNameFkAndKingdomFk(TaxonName taxonName, NomenclaturalCode nomenclaturalCode,
1103
	private boolean invokeRankDataAndTypeNameFkAndKingdomFk(TaxonName taxonName,
1111 1104
			Integer taxonFk, Integer typeNameFk, Integer kingdomFk, PesiExportState state) {
1112 1105

  
1106
	    NomenclaturalCode nomenclaturalCodes = taxonName.getNameType();
1107

  
1113 1108
	    Integer rankFk = null;
1114 1109
	    try {
1115 1110
			int index = 1;
1116
			rankFk = getRankFk(taxonName, nomenclaturalCode);
1111
			rankFk = getRankFk(taxonName, kingdomFk);
1117 1112
			if (rankFk != null) {
1118 1113
				rankTypeExpertsUpdateStmt.setInt(index++, rankFk);
1119 1114
			} else {
1120 1115
				rankTypeExpertsUpdateStmt.setObject(index++, null);
1121 1116
			}
1122 1117

  
1123
			String rankCache = getRankCache(taxonName, nomenclaturalCode, state);
1118
			String rankCache = getRankCache(taxonName, kingdomFk, state);
1124 1119
			if (rankCache != null) {
1125 1120
				rankTypeExpertsUpdateStmt.setString(index++, rankCache);
1126 1121
			} else {
......
1191 1186
		return true;
1192 1187
	}
1193 1188

  
1194
	/**
1195
	 * Returns the rankFk for the taxon name based on the names nomenclatural code.
1196
	 * You may not use this method for kingdoms other then Animalia, Plantae and Bacteria.
1197
	 * @param taxonName
1198
	 * @return
1199
	 */
1200
	@SuppressWarnings("unused")  //used by mapper
1201
	private static Integer getRankFk(TaxonName taxonName) {
1202
		return getRankFk(taxonName, taxonName.getNameType());
1189
	private static Integer getRankFk(TaxonName taxonName, NomenclaturalCode nomenclaturalCode) {
1190
	    Integer kingdomId = PesiTransformer.nomenclaturalCode2Kingdom(nomenclaturalCode);
1191
	    return getRankFk(taxonName, kingdomId);
1203 1192
	}
1204 1193

  
1205 1194
	/**
......
1209 1198
	 * @return The <code>RankFk</code> attribute.
1210 1199
	 * @see MethodMapper
1211 1200
	 */
1212
	private static Integer getRankFk(TaxonName taxonName, NomenclaturalCode nomenclaturalCode) {
1201
	private static Integer getRankFk(TaxonName taxonName, Integer kingdomId) {
1213 1202
		Integer result = null;
1214 1203
		try {
1215
			if (nomenclaturalCode != null) {
1216
				if (taxonName != null) {
1217
					if (taxonName.getRank() == null) {
1218
						logger.warn("Rank is null: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
1219
					} else {
1220
						result = PesiTransformer.rank2RankId(taxonName.getRank(), PesiTransformer.nomenclaturalCode2Kingdom(nomenclaturalCode));
1221
					}
1222
					if (result == null) {
1223
						logger.warn("Rank could not be determined for PESI-Kingdom-Id " + PesiTransformer.nomenclaturalCode2Kingdom(nomenclaturalCode) + " and TaxonName " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
1224
					}
1204
			if (taxonName != null) {
1205
				if (taxonName.getRank() == null) {
1206
					logger.warn("Rank is null: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
1207
				} else {
1208
					result = PesiTransformer.rank2RankId(taxonName.getRank(), kingdomId);
1209
				}
1210
				if (result == null) {
1211
					logger.warn("Rank could not be determined for PESI-Kingdom-Id " + kingdomId + " and TaxonName " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
1225 1212
				}
1226 1213
			}
1227 1214
		} catch (Exception e) {
......
1231 1218
	}
1232 1219

  
1233 1220
	private static String getRankCache(TaxonName taxonName, PesiExportState state) {
1234
		return getRankCache(taxonName, taxonName.getNameType(), state);
1221
	    List<TaxonNode> nodes = getTaxonNodes(taxonName);
1222
        Integer kingdomId = findKingdomIdFromTreeIndex(nodes.iterator().next().getTaxon(), state);
1223
        return getRankCache(taxonName, kingdomId, state);
1235 1224
	}
1236 1225

  
1237 1226

  
......
1243 1232
	 * @return The <code>RankCache</code> attribute.
1244 1233
	 * @see MethodMapper
1245 1234
	 */
1246
	private static String getRankCache(TaxonName taxonName, NomenclaturalCode nomenclaturalCode, PesiExportState state) {
1247
	    List<TaxonNode> nodes = getTaxonNodes(taxonName);
1235
	private static String getRankCache(TaxonName taxonName, Integer kingdomFk, PesiExportState state) {
1248 1236
	    if (Rank.DOMAIN().equals(taxonName.getRank())){
1249 1237
            return state.getTransformer().getCacheByRankAndKingdom(Rank.DOMAIN(), null);
1250
        }else if (!nodes.isEmpty()) {
1251
            return state.getTransformer().getCacheByRankAndKingdom(taxonName.getRank(), findKingdomIdFromTreeIndex(nodes.iterator().next().getTaxon(), state)); //PesiTransformer.nomenclaturalCode2Kingdom(nomenclaturalCode));
1252
        }else if (nomenclaturalCode != null){
1253
            return state.getTransformer().getCacheByRankAndKingdom(taxonName.getRank(), PesiTransformer.nomenclaturalCode2Kingdom(nomenclaturalCode));
1238
        }else if (kingdomFk != null) {
1239
            return state.getTransformer().getCacheByRankAndKingdom(taxonName.getRank(), kingdomFk);
1240
        }else if (taxonName.getNameType() != null){
1241
            return state.getTransformer().getCacheByRankAndKingdom(taxonName.getRank(), PesiTransformer.nomenclaturalCode2Kingdom(taxonName.getNameType()));
1254 1242
        }else{
1255
			logger.warn("No nomenclatural code defined for name " + taxonName.getUuid());
1243
			logger.warn("No kingdom ID could be defined for name " + taxonName.getUuid());
1256 1244
			return null;
1257 1245
		}
1258 1246
	}

Also available in: Unified diff