2 * Copyright (C) 2012 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.sdd
.ikeyplus
;
12 import java
.util
.Collection
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.codehaus
.plexus
.util
.StringUtils
;
21 import org
.springframework
.stereotype
.Component
;
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 fr
.lis
.ikeyplus
.IO
.SDDSaxParser
;
33 import fr
.lis
.ikeyplus
.model
.DataSet
;
34 import fr
.lis
.ikeyplus
.model
.ICharacter
;
35 import fr
.lis
.ikeyplus
.model
.QuantitativeCharacter
;
36 import fr
.lis
.ikeyplus
.model
.QuantitativeMeasure
;
37 import fr
.lis
.ikeyplus
.model
.SingleAccessKeyNode
;
38 import fr
.lis
.ikeyplus
.model
.SingleAccessKeyTree
;
39 import fr
.lis
.ikeyplus
.model
.Taxon
;
40 import fr
.lis
.ikeyplus
.services
.IdentificationKeyGenerator
;
41 import fr
.lis
.ikeyplus
.utils
.Utils
;
49 public class IkeyPlusImport
extends CdmImportBase
<IkeyPlusImportConfigurator
, IkeyPlusImportState
>{
51 public static final Logger logger
= Logger
.getLogger(IkeyPlusImport
.class);
53 private TermVocabulary
<Feature
> featureVoc
;
55 private PolytomousKey cdmKey
;
57 public PolytomousKey
getCdmKey() {
61 public void setCdmKey(PolytomousKey cdmKey
) {
65 private Map
<String
, Feature
>featureMap
= new HashMap
<String
, Feature
>();
67 public IkeyPlusImport() {
83 public PolytomousKey
getKey(URI sddUri
, Utils utils
) throws Exception
{
85 //TODO move below into configurator
87 // utils.setErrorMessage("foobar"); // Don't do it !!!! risk of NPE
88 utils
.setFewStatesCharacterFirst(false);
89 utils
.setMergeCharacterStatesIfSameDiscrimination(false);
90 utils
.setPruning(false);
91 utils
.setWeightContext("");
92 utils
.setWeightType(Utils
.GLOBAL_CHARACTER_WEIGHT
);
94 SDDSaxParser sDDSaxParser
= new SDDSaxParser(sddUri
.toString(), utils
);
97 DataSet data
= sDDSaxParser
.getDataset();
99 IdentificationKeyGenerator IkeyGenerator
;
101 IkeyGenerator
= new IdentificationKeyGenerator(data
, utils
);
102 } catch (Exception e
) {
103 logger
.error("could not generate key", e
);
104 throw new RuntimeException(e
);
107 IkeyGenerator
.createIdentificationKey();
108 } catch (Exception e
) {
109 //* IGNORE THIS TIME TO PREVENT FROM CREATING AN ERROR FILE */
111 SingleAccessKeyTree singleAccessKey
= IkeyGenerator
.getSingleAccessKeyTree();
113 // TODO idInSource for any cdm entity
115 cdmKey
= PolytomousKey
.NewTitledInstance(singleAccessKey
.getLabel() + "_1");
117 featureVoc
= TermVocabulary
.NewInstance(TermType
.Feature
, singleAccessKey
.getLabel(), singleAccessKey
.getLabel(), null, null);
119 Set
<PolytomousKeyNode
> rootNode
= recursivlyCreateKeyNodes(singleAccessKey
.getRoot(), null);
120 // Assert.assertEquals(1, rootNode.size());
121 // cdmKey.setRoot(rootNode.iterator().next());
123 persistNewEntities();
132 private void persistNewEntities() {
136 Collection features
= featureMap
.values();
137 getTermService().saveOrUpdate(features
);
138 getVocabularyService().saveOrUpdate(featureVoc
);
141 getPolytomousKeyService().saveOrUpdate(cdmKey
);
146 * @param parentNode may be null if node is the root node
149 private Set
<PolytomousKeyNode
> recursivlyCreateKeyNodes(SingleAccessKeyNode node
, SingleAccessKeyNode parentNode
) {
151 boolean isRootNode
= (parentNode
== null);
154 Set
<PolytomousKeyNode
> pkNodeSet
= new HashSet
<PolytomousKeyNode
>();
159 KeyStatement statement
= getKeyStatementFrom(node
);
162 //node.getNodeDescription(); // not needed here, contains warnings etc
164 // ---- do the children or taxa
165 List
<SingleAccessKeyNode
> childNodes
= node
.getChildren();
166 PolytomousKeyNode pkNode
;
167 if(childNodes
== null || childNodes
.size() == 0 ){
169 List
<Taxon
> taxa
= node
.getRemainingTaxa();
170 for(Taxon taxon
: taxa
){
172 pkNode
= createPkNode(null, statement
);
175 NonViralName
<?
> nonViralName
= NonViralName
.NewInstance(Rank
.UNKNOWN_RANK());
176 nonViralName
.setTitleCache(taxon
.getName(), true);
177 eu
.etaxonomy
.cdm
.model
.taxon
.Taxon cdmTaxon
= eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
.NewInstance(
178 nonViralName
, null); //FIXME !!!!!!
179 // TODO add taxon to covered taxa
180 // TODO media: get media from the parent node
182 pkNode
.setTaxon(cdmTaxon
);
183 pkNodeSet
.add(pkNode
);
187 Feature feature
= getFeatureFrom(childNodes
.iterator().next().getCharacter());
190 pkNode
= createPkNode(feature
, statement
);
191 for(SingleAccessKeyNode childNode
: childNodes
){
193 Set
<PolytomousKeyNode
> nodesToAdd
= recursivlyCreateKeyNodes(childNode
, node
);
194 for(PolytomousKeyNode nodeToAdd
: nodesToAdd
){
195 pkNode
.addChild(nodeToAdd
);
199 pkNodeSet
.add(pkNode
);
202 cdmKey
.setRoot(pkNode
);
215 public PolytomousKeyNode
createPkNode(Feature feature
, KeyStatement statement
) {
216 PolytomousKeyNode pkNode
;
217 pkNode
= PolytomousKeyNode
.NewInstance();
218 pkNode
.setKey(cdmKey
);
219 pkNode
.setFeature(feature
);
220 pkNode
.setStatement(statement
);
228 public KeyStatement
getKeyStatementFrom(SingleAccessKeyNode node
) {
230 if(node
.getCharacterState() instanceof QuantitativeMeasure
){
231 label
= ((QuantitativeMeasure
) node
.getCharacterState())
232 .toStringInterval(((QuantitativeCharacter
) node
.getCharacter())
233 .getMeasurementUnit());
235 label
= node
.getStringStates();
237 if (StringUtils
.isBlank(label
)){
240 return KeyStatement
.NewInstance(label
);
248 private Feature
getFeatureFrom(ICharacter character
) {
251 if(!featureMap
.containsKey(character
.getId())){
253 String featureLabel
= character
.getName();
255 Feature newFeature
= Feature
.NewInstance(featureLabel
, featureLabel
, null);
256 featureVoc
.addTerm(newFeature
);
257 featureMap
.put(character
.getId(),
261 return featureMap
.get(character
.getId());
265 protected void doInvoke(IkeyPlusImportState state
) {
268 this.getKey(state
.getConfig().getSource(), utils
);
269 } catch (Exception e
) {
276 protected boolean doCheck(IkeyPlusImportState state
) {
277 // TODO Auto-generated method stub
282 protected boolean isIgnore(IkeyPlusImportState state
) {
283 // TODO Auto-generated method stub