Project

General

Profile

Download (8.6 KB) Statistics
| Branch: | Tag: | Revision:
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 eu.etaxonomy.cdm.common.URI;
12
import java.util.Collection;
13
import java.util.HashMap;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18

    
19
import org.apache.logging.log4j.LogManager;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.io.common.CdmImportBase;
24
import eu.etaxonomy.cdm.model.description.Feature;
25
import eu.etaxonomy.cdm.model.description.KeyStatement;
26
import eu.etaxonomy.cdm.model.description.PolytomousKey;
27
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
28
import eu.etaxonomy.cdm.model.name.INonViralName;
29
import eu.etaxonomy.cdm.model.name.Rank;
30
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
31
import eu.etaxonomy.cdm.model.term.TermType;
32
import eu.etaxonomy.cdm.model.term.TermVocabulary;
33
import fr.lis.ikeyplus.IO.SDDSaxParser;
34
import fr.lis.ikeyplus.model.DataSet;
35
import fr.lis.ikeyplus.model.ICharacter;
36
import fr.lis.ikeyplus.model.QuantitativeCharacter;
37
import fr.lis.ikeyplus.model.QuantitativeMeasure;
38
import fr.lis.ikeyplus.model.SingleAccessKeyNode;
39
import fr.lis.ikeyplus.model.SingleAccessKeyTree;
40
import fr.lis.ikeyplus.model.Taxon;
41
import fr.lis.ikeyplus.services.IdentificationKeyGenerator;
42
import fr.lis.ikeyplus.utils.Utils;
43

    
44
/**
45
 * @author andreas
46
 * @since Sep 18, 2012
47
 *
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
    /**
75
     * @param sddUri
76
     * @param utils
77
     * @return
78
     * @throws Exception
79
     */
80
    /**
81
     * @param sddUri
82
     * @param utils
83
     * @return
84
     * @throws Exception
85
     */
86
    public PolytomousKey getKey(URI sddUri, Utils utils) throws Exception {
87

    
88
        //TODO move below into configurator
89
        utils = new Utils();
90
//        utils.setErrorMessage("foobar"); // Don't do it !!!! risk of NPE
91
        utils.setFewStatesCharacterFirst(false);
92
        utils.setMergeCharacterStatesIfSameDiscrimination(false);
93
        utils.setPruning(false);
94
        utils.setWeightContext("");
95
        utils.setWeightType(Utils.GLOBAL_CHARACTER_WEIGHT);
96

    
97
        SDDSaxParser sDDSaxParser = new SDDSaxParser(sddUri.toString(), utils);
98

    
99

    
100
        DataSet data = sDDSaxParser.getDataset();
101

    
102
        IdentificationKeyGenerator IkeyGenerator;
103
        try {
104
            IkeyGenerator = new IdentificationKeyGenerator(data, utils);
105
        } catch (Exception e) {
106
            logger.error("could not generate key", e);
107
            throw new RuntimeException(e);
108
        }
109
        try {
110
            IkeyGenerator.createIdentificationKey();
111
        } catch (Exception e) {
112
            //* IGNORE THIS TIME TO PREVENT FROM CREATING AN ERROR FILE */
113
        }
114
        SingleAccessKeyTree singleAccessKey = IkeyGenerator.getSingleAccessKeyTree();
115

    
116
        // TODO idInSource for any cdm entity
117

    
118
        cdmKey = PolytomousKey.NewTitledInstance(singleAccessKey.getLabel() + "_1");
119

    
120
        featureVoc = TermVocabulary.NewInstance(TermType.Feature, Feature.class,
121
                singleAccessKey.getLabel(), singleAccessKey.getLabel(), null, null);
122

    
123
        Set<PolytomousKeyNode> rootNode = recursivlyCreateKeyNodes(singleAccessKey.getRoot(), null);
124
//        Assert.assertEquals(1, rootNode.size());
125
//        cdmKey.setRoot(rootNode.iterator().next());
126

    
127
        persistNewEntities();
128

    
129

    
130
        return null;
131

    
132

    
133
    }
134

    
135
    private void persistNewEntities() {
136

    
137

    
138
        // persist features
139
        Collection features = featureMap.values();
140
        getTermService().saveOrUpdate(features);
141
        getVocabularyService().saveOrUpdate(featureVoc);
142

    
143
        // persist the rest
144
        getPolytomousKeyService().saveOrUpdate(cdmKey);
145
    }
146

    
147
    /**
148
     * @param node
149
     * @param parentNode may be null if node is the root node
150
     * @return
151
     */
152
    private Set<PolytomousKeyNode> recursivlyCreateKeyNodes(SingleAccessKeyNode node, SingleAccessKeyNode parentNode) {
153

    
154
        boolean isRootNode = (parentNode == null);
155

    
156

    
157
        Set<PolytomousKeyNode> pkNodeSet = new HashSet<PolytomousKeyNode>();
158
        if(node == null){
159
            return pkNodeSet;
160
        }
161

    
162
        KeyStatement statement = getKeyStatementFrom(node);
163

    
164

    
165
        //node.getNodeDescription(); // not needed here, contains warnings etc
166

    
167
        // ---- do the children or taxa
168
        List<SingleAccessKeyNode> childNodes = node.getChildren();
169
        PolytomousKeyNode pkNode;
170
        if(childNodes == null || childNodes.size() == 0 ){
171
            // do the taxa
172
            List<Taxon> taxa = node.getRemainingTaxa();
173
            for(Taxon taxon : taxa){
174

    
175
                pkNode = createPkNode(null, statement);
176

    
177
                //TODO handle rank
178
                INonViralName nonViralName = TaxonNameFactory.NewNonViralInstance(Rank.UNKNOWN_RANK());
179
                nonViralName.setTitleCache(taxon.getName(), true);
180
                eu.etaxonomy.cdm.model.taxon.Taxon cdmTaxon = eu.etaxonomy.cdm.model.taxon.Taxon.NewInstance(
181
                        nonViralName, null); //FIXME !!!!!!
182
                // TODO add taxon to covered taxa
183
                // TODO media: get media from the parent node
184

    
185
                pkNode.setTaxon(cdmTaxon);
186
                pkNodeSet.add(pkNode);
187
            }
188
        } else {
189
            // do the children
190
            Feature feature = getFeatureFrom(childNodes.iterator().next().getCharacter());
191

    
192

    
193
            pkNode = createPkNode(feature, statement);
194
            for(SingleAccessKeyNode childNode : childNodes){
195

    
196
                Set<PolytomousKeyNode> nodesToAdd = recursivlyCreateKeyNodes(childNode, node);
197
                for(PolytomousKeyNode nodeToAdd : nodesToAdd){
198
                    pkNode.addChild(nodeToAdd);
199
                }
200

    
201
            }
202
            pkNodeSet.add(pkNode);
203

    
204
            if(isRootNode) {
205
                cdmKey.setRoot(pkNode);
206
            }
207
        }
208

    
209

    
210
        return pkNodeSet;
211
    }
212

    
213
    /**
214
     * @param feature
215
     * @param statement
216
     * @return
217
     */
218
    public PolytomousKeyNode createPkNode(Feature feature, KeyStatement statement) {
219
        PolytomousKeyNode pkNode;
220
        pkNode = PolytomousKeyNode.NewInstance();
221
        pkNode.setKey(cdmKey);
222
        pkNode.setFeature(feature);
223
        pkNode.setStatement(statement);
224
        return pkNode;
225
    }
226

    
227
    /**
228
     * @param node
229
     * @return
230
     */
231
    public KeyStatement getKeyStatementFrom(SingleAccessKeyNode node) {
232
        String label;
233
        if(node.getCharacterState() instanceof QuantitativeMeasure){
234
            label = ((QuantitativeMeasure) node.getCharacterState())
235
                    .toStringInterval(((QuantitativeCharacter) node.getCharacter())
236
                            .getMeasurementUnit());
237
        } else {
238
            label = node.getStringStates();
239
        }
240
        if (CdmUtils.isBlank(label)){
241
            return null;
242
        }else{
243
            return KeyStatement.NewInstance(label);
244
        }
245
    }
246

    
247
    /**
248
     * @param character
249
     * @return
250
     */
251
    private Feature getFeatureFrom(ICharacter character) {
252

    
253

    
254
        if(!featureMap.containsKey(character.getId())){
255

    
256
            String featureLabel = character.getName();
257

    
258
            Feature newFeature = Feature.NewInstance(featureLabel, featureLabel, null);
259
            featureVoc.addTerm(newFeature);
260
            featureMap.put(character.getId(),
261
                    newFeature);
262

    
263
        }
264
        return featureMap.get(character.getId());
265
    }
266

    
267
    @Override
268
    protected void doInvoke(IkeyPlusImportState state) {
269
        Utils utils = null;
270
        try {
271
            this.getKey(state.getConfig().getSource(), utils);
272
        } catch (Exception e) {
273
            e.printStackTrace();
274
        }
275
    }
276

    
277

    
278
    @Override
279
    protected boolean doCheck(IkeyPlusImportState state) {
280
        // TODO Auto-generated method stub
281
        return false;
282
    }
283

    
284
    @Override
285
    protected boolean isIgnore(IkeyPlusImportState state) {
286
        // TODO Auto-generated method stub
287
        return false;
288
    }
289

    
290
}
(1-1/3)