Project

General

Profile

Download (8.45 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 java.net.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.log4j.Logger;
20
import org.codehaus.plexus.util.StringUtils;
21
import org.springframework.stereotype.Component;
22

    
23
import eu.etaxonomy.cdm.io.common.CdmImportBase;
24
import eu.etaxonomy.cdm.model.common.TermType;
25
import eu.etaxonomy.cdm.model.common.TermVocabulary;
26
import eu.etaxonomy.cdm.model.description.Feature;
27
import eu.etaxonomy.cdm.model.description.KeyStatement;
28
import eu.etaxonomy.cdm.model.description.PolytomousKey;
29
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
30
import eu.etaxonomy.cdm.model.name.NonViralName;
31
import eu.etaxonomy.cdm.model.name.Rank;
32
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
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
 * @date Sep 18, 2012
47
 *
48
 */
49
@Component
50
public class IkeyPlusImport extends CdmImportBase<IkeyPlusImportConfigurator, IkeyPlusImportState>{
51

    
52
    public static final Logger logger = Logger.getLogger(IkeyPlusImport.class);
53

    
54
    private TermVocabulary<Feature> featureVoc;
55

    
56
    private PolytomousKey cdmKey;
57

    
58
    public PolytomousKey getCdmKey() {
59
        return cdmKey;
60
    }
61

    
62
    public void setCdmKey(PolytomousKey cdmKey) {
63
        this.cdmKey = cdmKey;
64
    }
65

    
66
    private Map<String, Feature>featureMap = new HashMap<String, Feature>();
67

    
68
    public IkeyPlusImport() {
69

    
70
    }
71

    
72
    /**
73
     * @param sddUri
74
     * @param utils
75
     * @return
76
     * @throws Exception
77
     */
78
    /**
79
     * @param sddUri
80
     * @param utils
81
     * @return
82
     * @throws Exception
83
     */
84
    public PolytomousKey getKey(URI sddUri, Utils utils) throws Exception {
85

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

    
95
        SDDSaxParser sDDSaxParser = new SDDSaxParser(sddUri.toString(), utils);
96

    
97

    
98
        DataSet data = sDDSaxParser.getDataset();
99

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

    
114
        // TODO idInSource for any cdm entity
115

    
116
        cdmKey = PolytomousKey.NewTitledInstance(singleAccessKey.getLabel() + "_1");
117

    
118
        featureVoc = TermVocabulary.NewInstance(TermType.Feature, singleAccessKey.getLabel(), singleAccessKey.getLabel(), null, null);
119

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

    
124
        persistNewEntities();
125

    
126

    
127

    
128
        return null;
129

    
130

    
131
    }
132

    
133
    private void persistNewEntities() {
134

    
135

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

    
141
        // persist the rest
142
        getPolytomousKeyService().saveOrUpdate(cdmKey);
143
    }
144

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

    
152
        boolean isRootNode = (parentNode == null);
153

    
154

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

    
160
        KeyStatement statement = getKeyStatementFrom(node);
161

    
162

    
163
        //node.getNodeDescription(); // not needed here, contains warnings etc
164

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

    
173
                pkNode = createPkNode(null, statement);
174

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

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

    
190

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

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

    
199
            }
200
            pkNodeSet.add(pkNode);
201

    
202
            if(isRootNode) {
203
                cdmKey.setRoot(pkNode);
204
            }
205
        }
206

    
207

    
208
        return pkNodeSet;
209
    }
210

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

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

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

    
251

    
252
        if(!featureMap.containsKey(character.getId())){
253

    
254
            String featureLabel = character.getName();
255

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

    
261
        }
262
        return featureMap.get(character.getId());
263
    }
264

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

    
275

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

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

    
288
}
(1-1/3)