1
|
package eu.etaxonomy.cdm.io.pesi.indexFungorum;
|
2
|
|
3
|
import java.sql.ResultSet;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.UUID;
|
8
|
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.stereotype.Component;
|
11
|
import org.springframework.transaction.TransactionStatus;
|
12
|
|
13
|
import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
|
14
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
15
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
16
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
17
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
18
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
19
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
20
|
|
21
|
/**
|
22
|
* @author k.luther
|
23
|
* @since 19.08.2014
|
24
|
*/
|
25
|
@Component
|
26
|
public class IndexFungorumSubSpeciesImport extends IndexFungorumImportBase {
|
27
|
|
28
|
private static final long serialVersionUID = -2877755674188760685L;
|
29
|
private static final Logger logger = Logger.getLogger(IndexFungorumSpeciesImport.class);
|
30
|
|
31
|
private static final String pluralString = "subSpecies";
|
32
|
|
33
|
public IndexFungorumSubSpeciesImport(){
|
34
|
super(pluralString, null);
|
35
|
}
|
36
|
|
37
|
@Override
|
38
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(
|
39
|
ResultSet rs, IndexFungorumImportState state) {
|
40
|
return null; //not used here
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
protected String getRecordQuery(IndexFungorumImportConfigurator config) {
|
45
|
return null; //not used here
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
protected void doInvoke(IndexFungorumImportState state){
|
50
|
|
51
|
logger.info("create infraspecific - specific relationship: " + state.getInfraspecificTaxaUUIDs().size() + " taxa");
|
52
|
|
53
|
List<String> propertyPaths = new ArrayList<>();
|
54
|
propertyPaths.add("taxonNodes.*");
|
55
|
propertyPaths.add("taxonNodes.classification");
|
56
|
propertyPaths.add("taxonNodes.childNodes.*");
|
57
|
propertyPaths.add("taxonNodes.childNodes.taxon.*");
|
58
|
propertyPaths.add("taxonNodes.parent.*");
|
59
|
propertyPaths.add("taxonNodes.parent.taxon.*");
|
60
|
Classification classification = getClassification(state);
|
61
|
for (UUID infraspecificTaxonUUID: state.getInfraspecificTaxaUUIDs().keySet()){
|
62
|
TransactionStatus txStatus = startTransaction();
|
63
|
Taxon infraspecificTaxon = (Taxon)getTaxonService().load(infraspecificTaxonUUID, propertyPaths);
|
64
|
|
65
|
TaxonName name = infraspecificTaxon.getName();
|
66
|
|
67
|
// getNameService().saveOrUpdate(name);
|
68
|
String parentNameString = getParentNameInfraSpecific(name);
|
69
|
// System.out.println("Parent name string: " + parentNameString);
|
70
|
MatchingTaxonConfigurator matchingConfig = new MatchingTaxonConfigurator();
|
71
|
matchingConfig.setTaxonNameTitle(parentNameString);
|
72
|
|
73
|
matchingConfig.setPropertyPath(propertyPaths);
|
74
|
@SuppressWarnings("rawtypes")
|
75
|
List<TaxonBase> potentialParents = getTaxonService().findTaxaByName(matchingConfig);
|
76
|
//Taxon.class, parentNameString + "sec. ", MatchMode.BEGINNING, , pageSize, pageNumber, orderHints, propertyPaths)
|
77
|
//.searchNames(String uninomial,String infraGenericEpithet, String specificEpithet, String infraspecificEpithet, Rank rank, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
|
78
|
boolean matched = false;
|
79
|
if (potentialParents.size()>1){
|
80
|
for (@SuppressWarnings("rawtypes") TaxonBase potentialParent : potentialParents){
|
81
|
if (potentialParent.getTitleCache().equals(parentNameString + " sec*")){
|
82
|
classification.addParentChild((Taxon)potentialParent, infraspecificTaxon, null, null);
|
83
|
matched = true;
|
84
|
break;
|
85
|
}
|
86
|
}
|
87
|
if (matched == false){
|
88
|
logger.warn("Multiple match candidates but no match for " + name.getTitleCache());
|
89
|
}
|
90
|
}else if (!potentialParents.isEmpty()){
|
91
|
Taxon parent = HibernateProxyHelper.deproxy(potentialParents.get(0), Taxon.class);
|
92
|
classification.addParentChild(parent, infraspecificTaxon, null, null);
|
93
|
matched = true;
|
94
|
} else{
|
95
|
Integer genusId = state.getInfraspecificTaxaUUIDs().get(infraspecificTaxonUUID);
|
96
|
Taxon genusParent = getParentGenus(state, genusId);
|
97
|
// Taxon genusParent = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
|
98
|
classification.addParentChild(genusParent, infraspecificTaxon, null, null);
|
99
|
logger.warn("Added infraspecific taxon to genus because species does not exist: " + infraspecificTaxon.getTitleCache());
|
100
|
matched = true;
|
101
|
}
|
102
|
if (!matched){
|
103
|
System.out.println("No parent for: " + name.getTitleCache());
|
104
|
}
|
105
|
getTaxonService().saveOrUpdate(infraspecificTaxon);
|
106
|
commitTransaction(txStatus);
|
107
|
}
|
108
|
}
|
109
|
|
110
|
private Taxon getParentGenus(IndexFungorumImportState state, Integer genusId) {
|
111
|
Taxon result = getCommonService().getSourcedObjectByIdInSource(Taxon.class, String.valueOf(genusId), NAMESPACE_GENERA);
|
112
|
return result;
|
113
|
}
|
114
|
|
115
|
private String getParentNameInfraSpecific(TaxonName taxonName){
|
116
|
String parentName = taxonName.getGenusOrUninomial() + " " + taxonName.getSpecificEpithet();
|
117
|
return parentName;
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
protected boolean doCheck(IndexFungorumImportState state) {
|
122
|
return false;
|
123
|
}
|
124
|
|
125
|
@Override
|
126
|
protected boolean isIgnore(IndexFungorumImportState state) {
|
127
|
return false;
|
128
|
}
|
129
|
|
130
|
}
|