1
|
/**
|
2
|
* Copyright (C) 2012 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
|
package eu.etaxonomy.cdm.io.sdd.ikeyplus;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.HashMap;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.logging.log4j.LogManager;
|
19
|
import org.apache.logging.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
|
22
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
23
|
import eu.etaxonomy.cdm.common.URI;
|
24
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
25
|
import eu.etaxonomy.cdm.model.description.Feature;
|
26
|
import eu.etaxonomy.cdm.model.description.KeyStatement;
|
27
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
28
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
29
|
import eu.etaxonomy.cdm.model.name.INonViralName;
|
30
|
import eu.etaxonomy.cdm.model.name.Rank;
|
31
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
32
|
import eu.etaxonomy.cdm.model.term.TermType;
|
33
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
34
|
import fr.lis.ikeyplus.IO.SDDSaxParser;
|
35
|
import fr.lis.ikeyplus.model.DataSet;
|
36
|
import fr.lis.ikeyplus.model.ICharacter;
|
37
|
import fr.lis.ikeyplus.model.QuantitativeCharacter;
|
38
|
import fr.lis.ikeyplus.model.QuantitativeMeasure;
|
39
|
import fr.lis.ikeyplus.model.SingleAccessKeyNode;
|
40
|
import fr.lis.ikeyplus.model.SingleAccessKeyTree;
|
41
|
import fr.lis.ikeyplus.model.Taxon;
|
42
|
import fr.lis.ikeyplus.services.IdentificationKeyGenerator;
|
43
|
import fr.lis.ikeyplus.utils.Utils;
|
44
|
|
45
|
/**
|
46
|
* @author andreas
|
47
|
* @since Sep 18, 2012
|
48
|
*/
|
49
|
@Component
|
50
|
public class IkeyPlusImport extends CdmImportBase<IkeyPlusImportConfigurator, IkeyPlusImportState>{
|
51
|
|
52
|
private static final long serialVersionUID = -6817762818458834785L;
|
53
|
|
54
|
public static final Logger logger = LogManager.getLogger(IkeyPlusImport.class);
|
55
|
|
56
|
private TermVocabulary<Feature> featureVoc;
|
57
|
|
58
|
private PolytomousKey cdmKey;
|
59
|
|
60
|
public PolytomousKey getCdmKey() {
|
61
|
return cdmKey;
|
62
|
}
|
63
|
|
64
|
public void setCdmKey(PolytomousKey cdmKey) {
|
65
|
this.cdmKey = cdmKey;
|
66
|
}
|
67
|
|
68
|
private Map<String, Feature>featureMap = new HashMap<>();
|
69
|
|
70
|
public IkeyPlusImport() {
|
71
|
|
72
|
}
|
73
|
|
74
|
public PolytomousKey getKey(URI sddUri, Utils utils) throws Exception {
|
75
|
|
76
|
//TODO move below into configurator
|
77
|
utils = new Utils();
|
78
|
// utils.setErrorMessage("foobar"); // Don't do it !!!! risk of NPE
|
79
|
utils.setFewStatesCharacterFirst(false);
|
80
|
utils.setMergeCharacterStatesIfSameDiscrimination(false);
|
81
|
utils.setPruning(false);
|
82
|
utils.setWeightContext("");
|
83
|
utils.setWeightType(Utils.GLOBAL_CHARACTER_WEIGHT);
|
84
|
|
85
|
SDDSaxParser sDDSaxParser = new SDDSaxParser(sddUri.toString(), utils);
|
86
|
|
87
|
DataSet data = sDDSaxParser.getDataset();
|
88
|
|
89
|
IdentificationKeyGenerator IkeyGenerator;
|
90
|
try {
|
91
|
IkeyGenerator = new IdentificationKeyGenerator(data, utils);
|
92
|
} catch (Exception e) {
|
93
|
logger.error("could not generate key", e);
|
94
|
throw new RuntimeException(e);
|
95
|
}
|
96
|
try {
|
97
|
IkeyGenerator.createIdentificationKey();
|
98
|
} catch (Exception e) {
|
99
|
//* IGNORE THIS TIME TO PREVENT FROM CREATING AN ERROR FILE */
|
100
|
}
|
101
|
SingleAccessKeyTree singleAccessKey = IkeyGenerator.getSingleAccessKeyTree();
|
102
|
|
103
|
// TODO idInSource for any cdm entity
|
104
|
|
105
|
cdmKey = PolytomousKey.NewTitledInstance(singleAccessKey.getLabel() + "_1");
|
106
|
|
107
|
featureVoc = TermVocabulary.NewInstance(TermType.Feature, Feature.class,
|
108
|
singleAccessKey.getLabel(), singleAccessKey.getLabel(), null, null);
|
109
|
|
110
|
Set<PolytomousKeyNode> rootNode = recursivlyCreateKeyNodes(singleAccessKey.getRoot(), null);
|
111
|
// Assert.assertEquals(1, rootNode.size());
|
112
|
// cdmKey.setRoot(rootNode.iterator().next());
|
113
|
|
114
|
persistNewEntities();
|
115
|
|
116
|
return null;
|
117
|
}
|
118
|
|
119
|
private void persistNewEntities() {
|
120
|
|
121
|
// persist features
|
122
|
Collection features = featureMap.values();
|
123
|
getTermService().saveOrUpdate(features);
|
124
|
getVocabularyService().saveOrUpdate(featureVoc);
|
125
|
|
126
|
// persist the rest
|
127
|
getPolytomousKeyService().saveOrUpdate(cdmKey);
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* @param node
|
132
|
* @param parentNode may be null if node is the root node
|
133
|
* @return
|
134
|
*/
|
135
|
private Set<PolytomousKeyNode> recursivlyCreateKeyNodes(SingleAccessKeyNode node, SingleAccessKeyNode parentNode) {
|
136
|
|
137
|
boolean isRootNode = (parentNode == null);
|
138
|
|
139
|
|
140
|
Set<PolytomousKeyNode> pkNodeSet = new HashSet<>();
|
141
|
if(node == null){
|
142
|
return pkNodeSet;
|
143
|
}
|
144
|
|
145
|
KeyStatement statement = getKeyStatementFrom(node);
|
146
|
|
147
|
//node.getNodeDescription(); // not needed here, contains warnings etc
|
148
|
|
149
|
// ---- do the children or taxa
|
150
|
List<SingleAccessKeyNode> childNodes = node.getChildren();
|
151
|
PolytomousKeyNode pkNode;
|
152
|
if(childNodes == null || childNodes.size() == 0 ){
|
153
|
// do the taxa
|
154
|
List<Taxon> taxa = node.getRemainingTaxa();
|
155
|
for(Taxon taxon : taxa){
|
156
|
|
157
|
pkNode = createPkNode(null, statement);
|
158
|
|
159
|
//TODO handle rank
|
160
|
INonViralName nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.UNKNOWN_RANK());
|
161
|
nonViralName.setTitleCache(taxon.getName(), true);
|
162
|
eu.etaxonomy.cdm.model.taxon.Taxon cdmTaxon = eu.etaxonomy.cdm.model.taxon.Taxon.NewInstance(
|
163
|
nonViralName, null); //FIXME !!!!!!
|
164
|
// TODO add taxon to covered taxa
|
165
|
// TODO media: get media from the parent node
|
166
|
|
167
|
pkNode.setTaxon(cdmTaxon);
|
168
|
pkNodeSet.add(pkNode);
|
169
|
}
|
170
|
} else {
|
171
|
// do the children
|
172
|
Feature feature = getFeatureFrom(childNodes.iterator().next().getCharacter());
|
173
|
|
174
|
|
175
|
pkNode = createPkNode(feature, statement);
|
176
|
for(SingleAccessKeyNode childNode : childNodes){
|
177
|
|
178
|
Set<PolytomousKeyNode> nodesToAdd = recursivlyCreateKeyNodes(childNode, node);
|
179
|
for(PolytomousKeyNode nodeToAdd : nodesToAdd){
|
180
|
pkNode.addChild(nodeToAdd);
|
181
|
}
|
182
|
|
183
|
}
|
184
|
pkNodeSet.add(pkNode);
|
185
|
|
186
|
if(isRootNode) {
|
187
|
cdmKey.setRoot(pkNode);
|
188
|
}
|
189
|
}
|
190
|
|
191
|
return pkNodeSet;
|
192
|
}
|
193
|
|
194
|
public PolytomousKeyNode createPkNode(Feature feature, KeyStatement statement) {
|
195
|
PolytomousKeyNode pkNode;
|
196
|
pkNode = PolytomousKeyNode.NewInstance();
|
197
|
pkNode.setKey(cdmKey);
|
198
|
pkNode.setFeature(feature);
|
199
|
pkNode.setStatement(statement);
|
200
|
return pkNode;
|
201
|
}
|
202
|
|
203
|
public KeyStatement getKeyStatementFrom(SingleAccessKeyNode node) {
|
204
|
String label;
|
205
|
if(node.getCharacterState() instanceof QuantitativeMeasure){
|
206
|
label = ((QuantitativeMeasure) node.getCharacterState())
|
207
|
.toStringInterval(((QuantitativeCharacter) node.getCharacter())
|
208
|
.getMeasurementUnit());
|
209
|
} else {
|
210
|
label = node.getStringStates();
|
211
|
}
|
212
|
if (CdmUtils.isBlank(label)){
|
213
|
return null;
|
214
|
}else{
|
215
|
return KeyStatement.NewInstance(label);
|
216
|
}
|
217
|
}
|
218
|
|
219
|
private Feature getFeatureFrom(ICharacter character) {
|
220
|
|
221
|
if(!featureMap.containsKey(character.getId())){
|
222
|
|
223
|
String featureLabel = character.getName();
|
224
|
|
225
|
Feature newFeature = Feature.NewInstance(featureLabel, featureLabel, null);
|
226
|
featureVoc.addTerm(newFeature);
|
227
|
featureMap.put(character.getId(),
|
228
|
newFeature);
|
229
|
|
230
|
}
|
231
|
return featureMap.get(character.getId());
|
232
|
}
|
233
|
|
234
|
@Override
|
235
|
protected void doInvoke(IkeyPlusImportState state) {
|
236
|
Utils utils = null;
|
237
|
try {
|
238
|
this.getKey(state.getConfig().getSource(), utils);
|
239
|
} catch (Exception e) {
|
240
|
e.printStackTrace();
|
241
|
}
|
242
|
}
|
243
|
|
244
|
@Override
|
245
|
protected boolean doCheck(IkeyPlusImportState state) {
|
246
|
// TODO Auto-generated method stub
|
247
|
return false;
|
248
|
}
|
249
|
|
250
|
@Override
|
251
|
protected boolean isIgnore(IkeyPlusImportState state) {
|
252
|
// TODO Auto-generated method stub
|
253
|
return false;
|
254
|
}
|
255
|
}
|