3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.io
.pesi
.erms
;
13 import java
.sql
.ResultSet
;
14 import java
.sql
.SQLException
;
15 import java
.util
.HashMap
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.springframework
.stereotype
.Component
;
21 import eu
.etaxonomy
.cdm
.io
.common
.IOValidator
;
22 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
23 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportMapping
;
24 import eu
.etaxonomy
.cdm
.io
.pesi
.erms
.validation
.ErmsRankImportValidator
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.OrderedTermVocabulary
;
27 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
28 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
29 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
37 public class ErmsImportRankMap
extends ErmsImportBase
<Rank
>{
38 private static final Logger logger
= Logger
.getLogger(ErmsImportRankMap
.class);
40 private Map
<Integer
, Map
<Integer
,Rank
>> rankMap
;
46 public ErmsImportRankMap() {
47 super(null, null, null);
52 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#invoke(eu.etaxonomy.cdm.io.common.IoStateBase)
54 public boolean invoke (ErmsImportState state
){
55 rankMap
= new HashMap
<Integer
, Map
<Integer
,Rank
>>();
56 Source source
= state
.getConfig().getSource() ;
57 String strSQL
= " SELECT * FROM ranks ";
58 ResultSet rs
= source
.getResultSet(strSQL
);
61 Integer kingdomId
= rs
.getInt("kingdom_id");
62 Integer rankId
= rs
.getInt("rank_id");
63 String rankName
= rs
.getString("rank_name");
64 NomenclaturalCode nc
= ErmsTransformer
.kingdomId2NomCode(kingdomId
);
66 Map
<Integer
, Rank
> kingdomMap
= makeKingdomMap(rankMap
, rankId
);
68 rankName
= rankName
.replace("Forma", "Form").replace("Subforma", "Subform");
70 if (nc
== null && kingdomId
== 1){
71 rank
= getRank(state
, ErmsTransformer
.uuidRankSuperdomain
, "Superdomain", "Superdomain", null, CdmBase
.deproxy(Rank
.GENUS().getVocabulary(), OrderedTermVocabulary
.class), Rank
.KINGDOM());
73 rank
= Rank
.getRankByEnglishName(rankName
, nc
, false);
76 logger
.warn("Rank is null: " + rankName
);
78 kingdomMap
.put(kingdomId
, rank
);
79 } catch (UnknownCdmTypeException e
) {
80 String errorMessage
= "Rank '" + rankName
+ "' is not well mapped for code " + nc
+ ", kingdom_id = " + kingdomId
+ ". Rank is ignored!";
81 logger
.warn(errorMessage
);
84 } catch (SQLException e
) {
85 logger
.error(e
.getMessage());
86 throw new RuntimeException(e
);
88 state
.setRankMap(rankMap
);
93 * Retrieves or creates the kingdom map (mapping kingdom to rank for a defined rank_id) and
94 * adds it to the rank map.
99 private Map
<Integer
, Rank
> makeKingdomMap(Map
<Integer
, Map
<Integer
, Rank
>> rankMap
, Integer rankId
) {
100 Map
<Integer
, Rank
> result
= rankMap
.get(rankId
);
102 result
= new HashMap
<Integer
, Rank
>();
103 rankMap
.put(rankId
, result
);
110 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
113 protected boolean doCheck(ErmsImportState state
) {
114 IOValidator rankImport
= new ErmsRankImportValidator();
115 return rankImport
.validate(state
);
120 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getRecordQuery(eu.etaxonomy.cdm.io.erms.ErmsImportConfigurator)
123 protected String
getRecordQuery(ErmsImportConfigurator config
) {
124 // TODO Auto-generated method stub
130 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
133 protected boolean isIgnore(ErmsImportState state
) {
134 return false; //should always be called
139 * @see eu.etaxonomy.cdm.io.common.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
141 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
) {
142 return null; // not needed
146 * @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase)
148 public Rank
createObject(ResultSet rs
, ErmsImportState state
)
149 throws SQLException
{
150 return null; // not needed
155 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getMapping()
158 protected DbImportMapping
<?
, ?
> getMapping() {
159 return null; //not needed