Revision cd156f0a
Added by Andreas Müller about 2 years ago
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiTaxonExport.java | ||
---|---|---|
109 | 109 |
|
110 | 110 |
private static ExtensionType lastActionExtensionType; |
111 | 111 |
private static ExtensionType lastActionDateExtensionType; |
112 |
private static ExtensionType expertNameExtensionType; |
|
113 |
private static ExtensionType speciesExpertNameExtensionType; |
|
114 | 112 |
private static ExtensionType cacheCitationExtensionType; |
115 | 113 |
|
116 | 114 |
public static TaxonNameDefaultCacheStrategy zooNameStrategy = ZooNameNoMarkerCacheStrategy.NewInstance(); |
... | ... | |
160 | 158 |
// Find extensionTypes |
161 | 159 |
lastActionExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtLastAction); |
162 | 160 |
lastActionDateExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtLastActionDate); |
163 |
expertNameExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtExpertName); |
|
164 |
speciesExpertNameExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtSpeciesExpertName); |
|
165 | 161 |
cacheCitationExtensionType = (ExtensionType)getTermService().find(PesiTransformer.uuidExtCacheCitation); |
166 | 162 |
|
167 | 163 |
//Export Taxa.. |
... | ... | |
695 | 691 |
logger.info("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ..."); |
696 | 692 |
} |
697 | 693 |
|
698 |
List<TaxonBase> taxonList = null;
|
|
694 |
List<Taxon> taxonList = null; |
|
699 | 695 |
while ((taxonList = getTaxonService().listTaxaByName(Taxon.class, "*", "*", "*", "*", "*", Rank.SPECIES(), pageSize, pageNumber, null)).size() > 0) { |
700 | 696 |
|
701 | 697 |
Map<Integer, TaxonName> inferredSynonymsDataToBeSaved = new HashMap<>(); |
... | ... | |
775 | 771 |
} |
776 | 772 |
|
777 | 773 |
private Map<Integer, TaxonName> createInferredSynonymsForTaxonList(PesiExportState state, |
778 |
PesiExportMapping mapping, PesiExportMapping synRelMapping, List<TaxonBase> taxonList) {
|
|
774 |
PesiExportMapping mapping, PesiExportMapping synRelMapping, List<Taxon> taxonList) { |
|
779 | 775 |
|
780 |
Taxon acceptedTaxon; |
|
781 | 776 |
Classification classification = null; |
782 | 777 |
List<Synonym> inferredSynonyms = null; |
783 | 778 |
boolean localSuccess = true; |
784 | 779 |
|
785 | 780 |
Map<Integer, TaxonName> inferredSynonymsDataToBeSaved = new HashMap<>(); |
786 | 781 |
|
787 |
for (TaxonBase<?> taxonBase : taxonList) {
|
|
782 |
for (Taxon acceptedTaxon : taxonList) {
|
|
788 | 783 |
|
789 |
if (taxonBase.isInstanceOf(Taxon.class)) { // this should always be the case since we should have fetched accepted taxon only, but you never know... |
|
790 |
acceptedTaxon = CdmBase.deproxy(taxonBase, Taxon.class); |
|
791 |
TaxonName taxonName = acceptedTaxon.getName(); |
|
784 |
TaxonName taxonName = acceptedTaxon.getName(); |
|
792 | 785 |
|
793 |
if (taxonName.isZoological()) {
|
|
794 |
kingdomFk = findKingdomIdFromTreeIndex(taxonBase, state);
|
|
786 |
if (taxonName.isZoological()) { |
|
787 |
kingdomFk = findKingdomIdFromTreeIndex(acceptedTaxon, state);
|
|
795 | 788 |
|
796 |
Set<TaxonNode> taxonNodes = acceptedTaxon.getTaxonNodes();
|
|
797 |
TaxonNode singleNode = null;
|
|
789 |
Set<TaxonNode> taxonNodes = acceptedTaxon.getTaxonNodes(); |
|
790 |
TaxonNode singleNode = null; |
|
798 | 791 |
|
799 |
if (taxonNodes.size() > 0) {
|
|
800 |
// Determine the classification of the current TaxonNode
|
|
792 |
if (taxonNodes.size() > 0) { |
|
793 |
// Determine the classification of the current TaxonNode |
|
801 | 794 |
|
802 |
singleNode = taxonNodes.iterator().next(); |
|
803 |
if (singleNode != null) { |
|
804 |
classification = singleNode.getClassification(); |
|
805 |
} else { |
|
806 |
logger.error("A TaxonNode belonging to this accepted Taxon is NULL: " + acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache() +")"); |
|
807 |
} |
|
795 |
singleNode = taxonNodes.iterator().next(); |
|
796 |
if (singleNode != null) { |
|
797 |
classification = singleNode.getClassification(); |
|
808 | 798 |
} else { |
809 |
// Classification could not be determined directly from this TaxonNode |
|
810 |
// The stored classification from another TaxonNode is used. It's a simple, but not a failsafe fallback solution. |
|
811 |
if (taxonNodes.size() == 0) { |
|
812 |
//logger.error("Classification could not be determined directly from this Taxon: " + acceptedTaxon.getUuid() + " is misapplication? "+acceptedTaxon.isMisapplication()+ "). The classification of the last taxon is used"); |
|
813 |
} |
|
799 |
logger.error("A TaxonNode belonging to this accepted Taxon is NULL: " + acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache() +")"); |
|
814 | 800 |
} |
801 |
} else { |
|
802 |
// Classification could not be determined directly from this TaxonNode |
|
803 |
// The stored classification from another TaxonNode is used. It's a simple, but not a failsafe fallback solution. |
|
804 |
if (taxonNodes.size() == 0) { |
|
805 |
//logger.error("Classification could not be determined directly from this Taxon: " + acceptedTaxon.getUuid() + " is misapplication? "+acceptedTaxon.isMisapplication()+ "). The classification of the last taxon is used"); |
|
806 |
} |
|
807 |
} |
|
815 | 808 |
|
816 |
if (classification != null) {
|
|
817 |
try{
|
|
818 |
TaxonName name = acceptedTaxon.getName();
|
|
819 |
//if (name.isSpecies() || name.isInfraSpecific()){
|
|
820 |
inferredSynonyms = getTaxonService().createAllInferredSynonyms(acceptedTaxon, classification, true);
|
|
821 |
//}
|
|
809 |
if (classification != null) { |
|
810 |
try{ |
|
811 |
TaxonName name = acceptedTaxon.getName(); |
|
812 |
//if (name.isSpecies() || name.isInfraSpecific()){ |
|
813 |
inferredSynonyms = getTaxonService().createAllInferredSynonyms(acceptedTaxon, classification, true); |
|
814 |
//} |
|
822 | 815 |
// inferredSynonyms = getTaxonService().createInferredSynonyms(classification, acceptedTaxon, SynonymType.INFERRED_GENUS_OF()); |
823 |
if (inferredSynonyms != null) {
|
|
824 |
for (Synonym synonym : inferredSynonyms) {
|
|
816 |
if (inferredSynonyms != null) { |
|
817 |
for (Synonym synonym : inferredSynonyms) { |
|
825 | 818 |
// TaxonName synonymName = synonym.getName(); |
826 |
MarkerType markerType =getUuidMarkerType(PesiTransformer.uuidMarkerGuidIsMissing, state); |
|
827 |
synonym.addMarker(Marker.NewInstance(markerType, true)); |
|
828 |
// Both Synonym and its TaxonName have no valid Id yet |
|
829 |
synonym.setId(currentTaxonId++); |
|
830 |
|
|
831 |
|
|
832 |
localSuccess &= mapping.invoke(synonym); |
|
833 |
//get SynonymRelationship and export |
|
834 |
if (synonym.getAcceptedTaxon() == null ){ |
|
835 |
IdentifiableSource source = synonym.getSources().iterator().next(); |
|
836 |
if (source.getIdNamespace().contains("Potential combination")){ |
|
837 |
acceptedTaxon.addSynonym(synonym, SynonymType.POTENTIAL_COMBINATION_OF()); |
|
838 |
logger.error(synonym.getTitleCache() + " is not attached to " + acceptedTaxon.getTitleCache() + " type is set to potential combination"); |
|
839 |
} else if (source.getIdNamespace().contains("Inferred Genus")){ |
|
840 |
acceptedTaxon.addSynonym(synonym, SynonymType.INFERRED_GENUS_OF()); |
|
841 |
logger.error(synonym.getTitleCache() + " is not attached to " + acceptedTaxon.getTitleCache() + " type is set to inferred genus"); |
|
842 |
} else if (source.getIdNamespace().contains("Inferred Epithet")){ |
|
843 |
acceptedTaxon.addSynonym(synonym, SynonymType.INFERRED_EPITHET_OF()); |
|
844 |
logger.error(synonym.getTitleCache() + " is not attached to " + acceptedTaxon.getTitleCache() + " type is set to inferred epithet"); |
|
845 |
} else{ |
|
846 |
acceptedTaxon.addSynonym(synonym, SynonymType.INFERRED_SYNONYM_OF()); |
|
847 |
logger.error(synonym.getTitleCache() + " is not attached to " + acceptedTaxon.getTitleCache() + " type is set to inferred synonym"); |
|
848 |
} |
|
849 |
|
|
850 |
localSuccess &= synRelMapping.invoke(synonym); |
|
851 |
if (!localSuccess) { |
|
852 |
logger.error("Synonym relationship export failed " + synonym.getTitleCache() + " accepted taxon: " + acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache()+")"); |
|
853 |
} |
|
854 |
} else { |
|
855 |
localSuccess &= synRelMapping.invoke(synonym); |
|
856 |
if (!localSuccess) { |
|
857 |
logger.error("Synonym relationship export failed " + synonym.getTitleCache() + " accepted taxon: " + acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache()+")"); |
|
858 |
} else { |
|
859 |
logger.info("Synonym relationship successfully exported: " + synonym.getTitleCache() + " " +acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache()+")"); |
|
860 |
} |
|
819 |
MarkerType markerType =getUuidMarkerType(PesiTransformer.uuidMarkerGuidIsMissing, state); |
|
820 |
synonym.addMarker(Marker.NewInstance(markerType, true)); |
|
821 |
// Both Synonym and its TaxonName have no valid Id yet |
|
822 |
synonym.setId(currentTaxonId++); |
|
823 |
|
|
824 |
|
|
825 |
localSuccess &= mapping.invoke(synonym); |
|
826 |
//get SynonymRelationship and export |
|
827 |
if (synonym.getAcceptedTaxon() == null ){ |
|
828 |
IdentifiableSource source = synonym.getSources().iterator().next(); |
|
829 |
if (source.getIdNamespace().contains("Potential combination")){ |
|
830 |
acceptedTaxon.addSynonym(synonym, SynonymType.POTENTIAL_COMBINATION_OF()); |
|
831 |
logger.error(synonym.getTitleCache() + " is not attached to " + acceptedTaxon.getTitleCache() + " type is set to potential combination"); |
|
832 |
} else if (source.getIdNamespace().contains("Inferred Genus")){ |
|
833 |
acceptedTaxon.addSynonym(synonym, SynonymType.INFERRED_GENUS_OF()); |
|
834 |
logger.error(synonym.getTitleCache() + " is not attached to " + acceptedTaxon.getTitleCache() + " type is set to inferred genus"); |
|
835 |
} else if (source.getIdNamespace().contains("Inferred Epithet")){ |
|
836 |
acceptedTaxon.addSynonym(synonym, SynonymType.INFERRED_EPITHET_OF()); |
|
837 |
logger.error(synonym.getTitleCache() + " is not attached to " + acceptedTaxon.getTitleCache() + " type is set to inferred epithet"); |
|
838 |
} else{ |
|
839 |
acceptedTaxon.addSynonym(synonym, SynonymType.INFERRED_SYNONYM_OF()); |
|
840 |
logger.error(synonym.getTitleCache() + " is not attached to " + acceptedTaxon.getTitleCache() + " type is set to inferred synonym"); |
|
861 | 841 |
} |
862 | 842 |
|
863 |
inferredSynonymsDataToBeSaved.put(synonym.getId(), synonym.getName()); |
|
843 |
localSuccess &= synRelMapping.invoke(synonym); |
|
844 |
if (!localSuccess) { |
|
845 |
logger.error("Synonym relationship export failed " + synonym.getTitleCache() + " accepted taxon: " + acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache()+")"); |
|
846 |
} |
|
847 |
} else { |
|
848 |
localSuccess &= synRelMapping.invoke(synonym); |
|
849 |
if (!localSuccess) { |
|
850 |
logger.error("Synonym relationship export failed " + synonym.getTitleCache() + " accepted taxon: " + acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache()+")"); |
|
851 |
} else { |
|
852 |
logger.info("Synonym relationship successfully exported: " + synonym.getTitleCache() + " " +acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache()+")"); |
|
853 |
} |
|
864 | 854 |
} |
855 |
|
|
856 |
inferredSynonymsDataToBeSaved.put(synonym.getId(), synonym.getName()); |
|
865 | 857 |
} |
866 |
}catch(Exception e){ |
|
867 |
logger.error(e.getMessage()); |
|
868 |
e.printStackTrace(); |
|
869 | 858 |
} |
870 |
} else { |
|
871 |
logger.error("Classification is NULL. Inferred Synonyms could not be created for this Taxon: " + acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache() + ")"); |
|
859 |
}catch(Exception e){ |
|
860 |
logger.error(e.getMessage()); |
|
861 |
e.printStackTrace(); |
|
872 | 862 |
} |
873 | 863 |
} else { |
874 |
// logger.error("TaxonName is not a ZoologicalName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
|
|
864 |
logger.error("Classification is NULL. Inferred Synonyms could not be created for this Taxon: " + acceptedTaxon.getUuid() + " (" + acceptedTaxon.getTitleCache() + ")");
|
|
875 | 865 |
} |
876 | 866 |
} else { |
877 |
logger.error("This TaxonBase is not a Taxon even though it should be: " + taxonBase.getUuid() + " (" + taxonBase.getTitleCache() + ")");
|
|
867 |
// logger.error("TaxonName is not a ZoologicalName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
|
|
878 | 868 |
} |
879 | 869 |
} |
870 |
|
|
880 | 871 |
return inferredSynonymsDataToBeSaved; |
881 | 872 |
} |
882 | 873 |
|
... | ... | |
2143 | 2134 |
} |
2144 | 2135 |
|
2145 | 2136 |
/** |
2146 |
* Returns the <code>ExpertName</code> attribute. |
|
2147 |
* @param taxonName The {@link TaxonNameBase TaxonName}. |
|
2137 |
* Returns the <code>ExpertName</code> attribute. For ERMS this is |
|
2138 |
* the last action editor, for E+M the former last scrutiny, now sec.-reference, |
|
2139 |
* for FauEu it still needs to be investigated and for IF it does not exist.<BR> |
|
2140 |
* For merged taxa the result might be pipe separated. |
|
2141 |
* |
|
2142 |
* @param taxon The {@link TaxonBase taxonBase}. |
|
2148 | 2143 |
* @return The <code>ExpertName</code> attribute. |
2149 | 2144 |
* @see MethodMapper |
2150 | 2145 |
*/ |
2151 | 2146 |
//@SuppressWarnings("unused") //for some reason it is also called by getCacheCitation |
2152 | 2147 |
private static String getExpertName(TaxonBase<?> taxon) { |
2153 | 2148 |
try { |
2154 |
String result = null; |
|
2155 |
if(expertNameExtensionType!=null){ //some databases do not have this extension type |
|
2156 |
Set<Extension> extensions = taxon.getExtensions(); |
|
2157 |
for (Extension extension : extensions) { |
|
2158 |
if (extension.getType().equals(expertNameExtensionType)) { |
|
2159 |
result = extension.getValue(); |
|
2160 |
} |
|
2149 |
List<String> result = new ArrayList<>(); |
|
2150 |
EnumSet<PesiSource> sources = getSources(taxon); |
|
2151 |
|
|
2152 |
//EM |
|
2153 |
if (sources.contains(PesiSource.EM)){ |
|
2154 |
String expertName = getEuroMedExport(taxon.getSec()); |
|
2155 |
//TODO handle misapplications |
|
2156 |
//TODO think about using the author only |
|
2157 |
if (isNotBlank(expertName) && !result.contains(expertName)) { |
|
2158 |
result.add(expertName); |
|
2161 | 2159 |
} |
2162 | 2160 |
} |
2163 |
if (getPesiSources(taxon).contains(PesiSource.EM)){ |
|
2164 |
return taxon.getSec().getTitleCache(); |
|
2161 |
//ERMS |
|
2162 |
if (sources.contains(PesiSource.ERMS)){} //nothing to do, ERMS does not have expert names |
|
2163 |
|
|
2164 |
//FauEu |
|
2165 |
if (sources.contains(PesiSource.FE)){ |
|
2166 |
//TODO handle FauEu, not sure if the below is correct |
|
2167 |
|
|
2168 |
Set<String> expertNamesExtensions = taxon.getExtensions(PesiTransformer.uuidExtExpertName); |
|
2169 |
for (String extension : expertNamesExtensions) { |
|
2170 |
if (isNotBlank(extension) && !result.contains(extension)) { |
|
2171 |
result.add(extension); |
|
2172 |
} |
|
2173 |
} |
|
2165 | 2174 |
} |
2166 |
return null; |
|
2175 |
//IF |
|
2176 |
if (sources.contains(PesiSource.IF)){} //nothing to do, IF does not have expert name |
|
2177 |
|
|
2178 |
return CdmUtils.concat(" | ", result.toArray(new String[0])); |
|
2167 | 2179 |
} catch (Exception e) { |
2168 | 2180 |
e.printStackTrace(); |
2169 | 2181 |
return null; |
... | ... | |
2177 | 2189 |
} |
2178 | 2190 |
|
2179 | 2191 |
/** |
2180 |
* Returns the <code>SpeciesExpertName</code> attribute. |
|
2192 |
* Returns the <code>SpeciesExpertName</code> attribute. For ERMS this is |
|
2193 |
* the last action editor, for E+M the former last scrutiny, now sec.-reference, |
|
2194 |
* for FauEu it still needs to be investigated and for IF it does not exist.<BR> |
|
2195 |
* For merged taxa the result might be pipe separated. |
|
2196 |
* |
|
2181 | 2197 |
* @param taxonName The {@link TaxonNameBase TaxonName}. |
2182 | 2198 |
* @return The <code>SpeciesExpertName</code> attribute. |
2183 | 2199 |
* @see MethodMapper |
... | ... | |
2185 | 2201 |
@SuppressWarnings("unused") |
2186 | 2202 |
private static String getSpeciesExpertName(TaxonBase<?> taxon) { |
2187 | 2203 |
try { |
2188 |
Set<Extension> extensions = taxon.getExtensions(); |
|
2189 |
if(speciesExpertNameExtensionType != null){ //some databases do not have this extension type |
|
2190 |
for (Extension extension : extensions) { |
|
2191 |
if (extension.getType().equals(speciesExpertNameExtensionType)) { |
|
2192 |
return extension.getValue(); |
|
2193 |
} |
|
2194 |
} |
|
2195 |
} |
|
2196 |
if (getPesiSources(taxon).contains(PesiSource.EM)){ |
|
2197 |
return taxon.getSec().getTitleCache(); |
|
2204 |
List<String> result = new ArrayList<>(); |
|
2205 |
EnumSet<PesiSource> sources = getSources(taxon); |
|
2206 |
|
|
2207 |
//EM |
|
2208 |
if (sources.contains(PesiSource.EM)){ |
|
2209 |
String expertName = getEuroMedExport(taxon.getSec()); |
|
2210 |
//TODO handle misapplications |
|
2211 |
//TODO think about using the author only |
|
2212 |
if (isNotBlank(expertName) && !result.contains(expertName)) { |
|
2213 |
result.add(expertName); |
|
2214 |
} |
|
2198 | 2215 |
} |
2199 |
return null; |
|
2216 |
//ERMS |
|
2217 |
if (sources.contains(PesiSource.ERMS)){ |
|
2218 |
Set<String> expertNamesExtensions = taxon.getExtensions(PesiTransformer.uuidExtSpeciesExpertName); |
|
2219 |
for (String extension : expertNamesExtensions) { |
|
2220 |
if (isNotBlank(extension) && !result.contains(extension)) { |
|
2221 |
result.add(extension); |
|
2222 |
} |
|
2223 |
} |
|
2224 |
} |
|
2225 |
//FauEu |
|
2226 |
if (sources.contains(PesiSource.FE)){ |
|
2227 |
//TODO handle FauEu, not sure if the below is correct |
|
2228 |
|
|
2229 |
Set<String> expertNamesExtensions = taxon.getExtensions(PesiTransformer.uuidExtSpeciesExpertName); |
|
2230 |
for (String extension : expertNamesExtensions) { |
|
2231 |
if (isNotBlank(extension) && !result.contains(extension)) { |
|
2232 |
result.add(extension); |
|
2233 |
} |
|
2234 |
} |
|
2235 |
} |
|
2236 |
//IF |
|
2237 |
if (sources.contains(PesiSource.IF)){} //nothing to do, IF does not have expert name |
|
2238 |
|
|
2239 |
return CdmUtils.concat(" | ", result.toArray(new String[0])); |
|
2200 | 2240 |
} catch (Exception e) { |
2201 | 2241 |
e.printStackTrace(); |
2202 | 2242 |
return null; |
... | ... | |
2204 | 2244 |
} |
2205 | 2245 |
|
2206 | 2246 |
/** |
2247 |
* Computes the expert or species expert string for an Euro+Med |
|
2248 |
* taxon by it's sec reference by either using the references |
|
2249 |
* author or, if not available, using the reference titlecache |
|
2250 |
*/ |
|
2251 |
private static String getEuroMedExport(Reference sec) { |
|
2252 |
if (sec == null){ |
|
2253 |
return null; |
|
2254 |
}else if (sec.getAuthorship() != null && isNotBlank(sec.getAuthorship().getTitleCache())){ |
|
2255 |
return sec.getAuthorship().getTitleCache(); |
|
2256 |
}else{ |
|
2257 |
return sec.getTitleCache(); |
|
2258 |
} |
|
2259 |
} |
|
2260 |
|
|
2261 |
/** |
|
2207 | 2262 |
* Returns the <code>SpeciesExpertFk</code> attribute. |
2208 | 2263 |
* @param reference The {@link Reference Reference}. |
2209 | 2264 |
* @param state The {@link PesiExportState PesiExportState}. |
Also available in: Unified diff
latest changes to PesiTaxonExport