1 |
1 |
/**
|
2 |
2 |
* Copyright (C) 2007 EDIT
|
3 |
|
* European Distributed Institute of Taxonomy
|
|
3 |
* European Distributed Institute of Taxonomy
|
4 |
4 |
* http://www.e-taxonomy.eu
|
5 |
|
*
|
|
5 |
*
|
6 |
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7 |
7 |
* See LICENSE.TXT at the top of this package for the full license terms.
|
8 |
8 |
*/
|
... | ... | |
11 |
11 |
|
12 |
12 |
import java.sql.ResultSet;
|
13 |
13 |
import java.sql.SQLException;
|
|
14 |
import java.util.ArrayList;
|
14 |
15 |
import java.util.HashMap;
|
15 |
16 |
import java.util.HashSet;
|
|
17 |
import java.util.List;
|
16 |
18 |
import java.util.Map;
|
17 |
19 |
import java.util.Set;
|
|
20 |
import java.util.UUID;
|
18 |
21 |
|
19 |
22 |
import org.apache.commons.lang.StringUtils;
|
20 |
23 |
import org.apache.log4j.Logger;
|
21 |
24 |
import org.springframework.stereotype.Component;
|
22 |
25 |
|
|
26 |
import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
|
23 |
27 |
import eu.etaxonomy.cdm.common.CdmUtils;
|
|
28 |
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
24 |
29 |
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
|
30 |
import eu.etaxonomy.cdm.io.common.Source;
|
25 |
31 |
import eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer;
|
26 |
32 |
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
|
27 |
33 |
import eu.etaxonomy.cdm.model.common.CdmBase;
|
28 |
34 |
import eu.etaxonomy.cdm.model.common.Extension;
|
29 |
35 |
import eu.etaxonomy.cdm.model.common.ExtensionType;
|
30 |
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
31 |
36 |
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
32 |
37 |
import eu.etaxonomy.cdm.model.name.NonViralName;
|
33 |
|
import eu.etaxonomy.cdm.model.name.Rank;
|
|
38 |
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
34 |
39 |
import eu.etaxonomy.cdm.model.reference.Reference;
|
35 |
40 |
import eu.etaxonomy.cdm.model.taxon.Classification;
|
36 |
41 |
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
... | ... | |
45 |
50 |
@Component
|
46 |
51 |
public class IndexFungorumSpeciesImport extends IndexFungorumImportBase {
|
47 |
52 |
private static final Logger logger = Logger.getLogger(IndexFungorumSpeciesImport.class);
|
48 |
|
|
|
53 |
|
49 |
54 |
private static final String pluralString = "species";
|
50 |
55 |
private static final String dbTableName = "[tblPESIfungi-IFdata]";
|
|
56 |
private final Set<UUID> infraspecificTaxaUUIDs = new HashSet<UUID>();
|
51 |
57 |
|
52 |
58 |
public IndexFungorumSpeciesImport(){
|
53 |
59 |
super(pluralString, dbTableName, null);
|
|
60 |
|
54 |
61 |
}
|
55 |
62 |
|
56 |
63 |
|
57 |
|
|
58 |
|
|
|
64 |
|
|
65 |
|
59 |
66 |
@Override
|
60 |
67 |
protected String getIdQuery() {
|
61 |
68 |
String result = " SELECT PreferredNameIFnumber FROM " + getTableName() +
|
... | ... | |
71 |
78 |
*/
|
72 |
79 |
@Override
|
73 |
80 |
protected String getRecordQuery(IndexFungorumImportConfigurator config) {
|
74 |
|
String strRecordQuery =
|
|
81 |
String strRecordQuery =
|
75 |
82 |
" SELECT DISTINCT distribution.PreferredNameFDCnumber, species.* , cl.[Phylum name]" +
|
76 |
83 |
" FROM tblPESIfungi AS distribution RIGHT OUTER JOIN dbo.[tblPESIfungi-IFdata] AS species ON distribution.PreferredNameIFnumber = species.PreferredNameIFnumber " +
|
77 |
84 |
" LEFT OUTER JOIN [tblPESIfungi-Classification] cl ON species.PreferredName = cl.PreferredName " +
|
78 |
85 |
" WHERE ( species.PreferredNameIFnumber IN (" + ID_LIST_TOKEN + ") )" +
|
79 |
86 |
"";
|
80 |
87 |
return strRecordQuery;
|
|
88 |
}
|
|
89 |
@Override
|
|
90 |
protected void doInvoke(IndexFungorumImportState state){
|
|
91 |
System.out.println("start make " + getPluralString() + " ...");
|
|
92 |
IndexFungorumImportConfigurator config = state.getConfig();
|
|
93 |
Source source = config.getSource();
|
|
94 |
|
|
95 |
super.doInvoke(state);
|
|
96 |
Classification classification = getClassification(state);
|
|
97 |
List<TaxonBase> infraspecificTaxa = new ArrayList<TaxonBase>();
|
|
98 |
for (UUID uuid: infraspecificTaxaUUIDs){
|
|
99 |
infraspecificTaxa.add(getTaxonService().load(uuid));
|
|
100 |
}
|
|
101 |
|
|
102 |
System.out.println("create infraspecific - specific relationship: " + infraspecificTaxa.size() + " taxa");
|
|
103 |
for (TaxonBase infraspecificTaxon: infraspecificTaxa){
|
|
104 |
HibernateProxyHelper.deproxy(infraspecificTaxon);
|
|
105 |
TaxonNameBase name = infraspecificTaxon.getName();
|
|
106 |
|
|
107 |
UUID uuid = getNameService().saveOrUpdate(name);
|
|
108 |
String parentNameString = getParentNameInfraSpecific(name);
|
|
109 |
System.out.println("Parent name string: " + parentNameString);
|
|
110 |
MatchingTaxonConfigurator matchingConfig = new MatchingTaxonConfigurator();
|
|
111 |
matchingConfig.setTaxonNameTitle(parentNameString);
|
|
112 |
List<String> propertyPaths = new ArrayList<String>();
|
|
113 |
propertyPaths.add("taxonNodes.*");
|
|
114 |
propertyPaths.add("taxonNodes.classification");
|
|
115 |
propertyPaths.add("taxonNodes.childNodes.*");
|
|
116 |
propertyPaths.add("taxonNodes.childNodes.taxon.*");
|
|
117 |
propertyPaths.add("taxonNodes.parent.*");
|
|
118 |
propertyPaths.add("taxonNodes.parent.taxon.*");
|
|
119 |
matchingConfig.setPropertyPath(propertyPaths);
|
|
120 |
List<TaxonBase> potentialParents = getTaxonService().findTaxaByName(matchingConfig);
|
|
121 |
//Taxon.class, parentNameString + "sec. ", MatchMode.BEGINNING, , pageSize, pageNumber, orderHints, propertyPaths)
|
|
122 |
//.searchNames(String uninomial,String infraGenericEpithet, String specificEpithet, String infraspecificEpithet, Rank rank, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
|
|
123 |
if (potentialParents.size()>1){
|
|
124 |
for (TaxonBase potentialParent:potentialParents){
|
|
125 |
if (potentialParent.getTitleCache().equals(parentNameString + " sec*")){
|
|
126 |
classification.addParentChild((Taxon)potentialParent, (Taxon)infraspecificTaxon, null, null);
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}else if (!potentialParents.isEmpty()){
|
|
130 |
Taxon parent = HibernateProxyHelper.deproxy(potentialParents.get(0), Taxon.class);
|
|
131 |
Taxon child = (Taxon)getTaxonService().load(infraspecificTaxon.getUuid(), propertyPaths);
|
|
132 |
classification.addParentChild(parent, child, null, null);
|
|
133 |
} else{
|
|
134 |
System.out.println("No parent for: " + name.getTitleCache());
|
|
135 |
}
|
|
136 |
getTaxonService().saveOrUpdate(infraspecificTaxon);
|
|
137 |
}
|
|
138 |
|
|
139 |
|
81 |
140 |
}
|
82 |
141 |
|
83 |
|
|
84 |
142 |
@Override
|
85 |
143 |
public boolean doPartition(ResultSetPartitioner partitioner, IndexFungorumImportState state) {
|
86 |
144 |
boolean success = true;
|
87 |
145 |
Reference<?> sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
|
88 |
146 |
ResultSet rs = partitioner.getResultSet();
|
89 |
|
|
90 |
147 |
Classification classification = getClassification(state);
|
|
148 |
|
91 |
149 |
try {
|
92 |
150 |
while (rs.next()){
|
93 |
151 |
|
... | ... | |
95 |
153 |
|
96 |
154 |
Integer id = (Integer)rs.getObject("PreferredNameIFnumber");
|
97 |
155 |
String phylumName = rs.getString("Phylum name");
|
98 |
|
|
|
156 |
|
99 |
157 |
String preferredName = rs.getString("PreferredName");
|
100 |
158 |
if (StringUtils.isBlank(preferredName)){
|
101 |
159 |
logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD UMBER" + CdmUtils.Nz(id));
|
102 |
160 |
}
|
103 |
|
|
104 |
|
Rank rank = Rank.SPECIES();
|
105 |
|
|
|
161 |
|
|
162 |
//Rank rank = Rank.SPECIES();
|
|
163 |
|
106 |
164 |
NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
|
107 |
|
NonViralName<?> name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICNAFP, rank);
|
108 |
|
|
|
165 |
NonViralName<?> name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICNAFP, null);
|
|
166 |
|
109 |
167 |
Taxon taxon = Taxon.NewInstance(name, sourceReference);
|
110 |
|
Taxon parent = getParentTaxon(state, rs);
|
111 |
|
if (parent == null){
|
112 |
|
logger.warn("parent not found for name:" +preferredName);
|
|
168 |
//if name is infraspecific the parent should be the species not the genus
|
|
169 |
Taxon parent;
|
|
170 |
if (!name.isInfraSpecific()){
|
|
171 |
parent = getParentTaxon(state, rs);
|
|
172 |
if (parent == null){
|
|
173 |
logger.warn("parent not found for name:" +preferredName);
|
|
174 |
}
|
|
175 |
classification.addParentChild(parent, taxon, null, null);
|
113 |
176 |
}
|
114 |
|
classification.addParentChild(parent, taxon, null, null);
|
115 |
|
|
|
177 |
|
116 |
178 |
//author + publication
|
117 |
179 |
makeAuthorAndPublication(state, rs, name);
|
118 |
180 |
//source
|
119 |
181 |
makeSource(state, taxon, id, NAMESPACE_SPECIES );
|
120 |
|
|
|
182 |
|
121 |
183 |
//fossil
|
122 |
184 |
if (FOSSIL_FUNGI.equalsIgnoreCase(phylumName)){
|
123 |
185 |
ExtensionType fossilExtType = getExtensionType(state, ErmsTransformer.uuidFossilStatus, "fossil status", "fossil status", "fos. stat.");
|
124 |
|
Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType);
|
|
186 |
Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType);
|
125 |
187 |
}
|
126 |
188 |
//save
|
127 |
|
getTaxonService().saveOrUpdate(taxon);
|
|
189 |
|
|
190 |
UUID uuidTaxon = getTaxonService().saveOrUpdate(taxon);
|
|
191 |
getNameService().saveOrUpdate(name);
|
|
192 |
if (name.isInfraSpecific()){
|
|
193 |
infraspecificTaxaUUIDs.add(uuidTaxon);
|
|
194 |
}
|
|
195 |
|
128 |
196 |
}
|
129 |
197 |
|
130 |
|
|
|
198 |
|
131 |
199 |
} catch (Exception e) {
|
132 |
200 |
e.printStackTrace();
|
133 |
201 |
logger.error(e.getMessage());
|
... | ... | |
137 |
205 |
return success;
|
138 |
206 |
}
|
139 |
207 |
|
140 |
|
|
141 |
|
private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
|
|
208 |
|
|
209 |
/**
|
|
210 |
* @param taxon
|
|
211 |
* @return
|
|
212 |
*/
|
|
213 |
private String getParentNameInfraSpecific(TaxonNameBase taxonName){
|
|
214 |
NonViralName<NonViralName> name = HibernateProxyHelper.deproxy(taxonName, NonViralName.class);
|
|
215 |
String parentName = name.getGenusOrUninomial() + " " + name.getSpecificEpithet();
|
|
216 |
|
|
217 |
return parentName;
|
|
218 |
}
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
|
142 |
224 |
Integer genusId = rs.getInt("PreferredNameFDCnumber");
|
143 |
|
|
|
225 |
|
144 |
226 |
Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
|
145 |
227 |
if (taxon == null){
|
146 |
228 |
logger.warn("Taxon not found for " + genusId);
|
... | ... | |
155 |
237 |
Class<?> cdmClass;
|
156 |
238 |
Set<String> idSet;
|
157 |
239 |
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
158 |
|
|
|
240 |
|
159 |
241 |
try{
|
160 |
242 |
Set<String> taxonIdSet = new HashSet<String>();
|
161 |
|
while (rs.next()){
|
|
243 |
Set<String> taxonSpeciesNames = new HashSet<String>();
|
|
244 |
while (rs.next()){
|
162 |
245 |
handleForeignKey(rs, taxonIdSet,"PreferredNameFDCnumber" );
|
|
246 |
handleForeignKey(rs, taxonSpeciesNames, "PreferredName");
|
163 |
247 |
}
|
164 |
|
|
|
248 |
|
165 |
249 |
//taxon map
|
166 |
250 |
nameSpace = NAMESPACE_GENERA;
|
167 |
251 |
cdmClass = TaxonBase.class;
|
168 |
252 |
idSet = taxonIdSet;
|
169 |
253 |
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
170 |
254 |
result.put(nameSpace, taxonMap);
|
171 |
|
|
|
255 |
|
|
256 |
|
172 |
257 |
//sourceReference
|
173 |
258 |
Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
|
174 |
259 |
Map<String, Reference> referenceMap = new HashMap<String, Reference>();
|
175 |
260 |
referenceMap.put(SOURCE_REFERENCE, sourceReference);
|
176 |
261 |
result.put(NAMESPACE_REFERENCE, referenceMap);
|
177 |
|
|
|
262 |
|
178 |
263 |
} catch (SQLException e) {
|
179 |
264 |
throw new RuntimeException(e);
|
180 |
265 |
}
|
181 |
266 |
return result;
|
182 |
267 |
}
|
183 |
|
|
|
268 |
|
184 |
269 |
@Override
|
185 |
270 |
protected boolean doCheck(IndexFungorumImportState state){
|
186 |
271 |
return true;
|
187 |
272 |
}
|
188 |
|
|
|
273 |
|
189 |
274 |
@Override
|
190 |
275 |
protected boolean isIgnore(IndexFungorumImportState state){
|
191 |
276 |
return ! state.getConfig().isDoTaxa();
|
index fungorum import of infraspecific taxa