2 * Copyright (C) 2007 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.
10 package eu
.etaxonomy
.cdm
.io
.pesi
.erms
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
18 import java
.util
.UUID
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
23 import eu
.etaxonomy
.cdm
.io
.common
.IOValidator
;
24 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportAnnotationMapper
;
25 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportCommonNameCreationMapper
;
26 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportMapping
;
27 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportObjectMapper
;
28 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportStringMapper
;
29 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.UndefinedTransformerMethodException
;
30 import eu
.etaxonomy
.cdm
.io
.pesi
.erms
.validation
.ErmsVernacularImportValidator
;
31 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
34 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
43 public class ErmsVernacularImport
extends ErmsImportBase
<CommonTaxonName
> {
44 @SuppressWarnings("unused")
45 private static final Logger logger
= Logger
.getLogger(ErmsVernacularImport
.class);
47 private DbImportMapping
<ErmsImportState
, ErmsImportConfigurator
> mapping
;
48 private ErmsImportState state
; //import instance is never used more than once for Erms ; dirty
51 private static final String pluralString
= "vernaculars";
52 private static final String dbTableName
= "vernaculars";
53 private static final Class
<?
> cdmTargetClass
= CommonTaxonName
.class;
55 public ErmsVernacularImport(){
56 super(pluralString
, dbTableName
, cdmTargetClass
);
60 protected String
getRecordQuery(ErmsImportConfigurator config
) {
61 String strRecordQuery
=
62 " SELECT vernaculars.*, tu.tu_acctaxon, tu.id " +
63 " FROM vernaculars INNER JOIN tu ON vernaculars.tu_id = tu.id " +
64 " WHERE ( vernaculars.id IN (" + ID_LIST_TOKEN
+ ") )";
65 return strRecordQuery
;
69 protected DbImportMapping
<ErmsImportState
, ErmsImportConfigurator
> getMapping() {
71 mapping
= new DbImportMapping
<ErmsImportState
, ErmsImportConfigurator
>();
73 mapping
.addMapper(DbImportCommonNameCreationMapper
.NewInstance("id", VERNACULAR_NAMESPACE
, "tu_id", ErmsTaxonImport
.TAXON_NAMESPACE
));
75 mapping
.addMapper(DbImportObjectMapper
.NewInstance("lan_id", "language", LANGUAGE_NAMESPACE
));
76 mapping
.addMapper(DbImportStringMapper
.NewInstance("vername", "name"));
77 mapping
.addMapper(DbImportAnnotationMapper
.NewInstance("note", AnnotationType
.EDITORIAL(), Language
.DEFAULT()));
83 protected void doInvoke(ErmsImportState state
) {
85 super.doInvoke(state
);
89 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, ErmsImportState state
) {
93 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<Object
, Map
<String
, ?
extends CdmBase
>>();
96 Set
<String
> taxonIdSet
= new HashSet
<String
>();
97 Set
<String
> languageIdSet
= new HashSet
<String
>();
99 handleForeignKey(rs
, taxonIdSet
, "tu_id");
100 handleForeignKey(rs
, languageIdSet
, "lan_id");
104 nameSpace
= ErmsTaxonImport
.TAXON_NAMESPACE
;
105 cdmClass
= TaxonBase
.class;
107 Map
<String
, TaxonBase
<?
>> taxonMap
= (Map
<String
, TaxonBase
<?
>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
108 result
.put(nameSpace
, taxonMap
);
111 nameSpace
= LANGUAGE_NAMESPACE
;
112 Map
<String
, Language
> languageMap
= new HashMap
<String
, Language
>();
113 ErmsTransformer transformer
= new ErmsTransformer();
114 for (String lanAbbrev
: languageIdSet
){
115 Language language
= null;
117 language
= transformer
.getLanguageByKey(lanAbbrev
);
118 if (language
== null || language
.equals(Language
.UNDETERMINED())){
119 UUID uuidLang
= transformer
.getLanguageUuid(lanAbbrev
);
120 if (uuidLang
!= null){
121 language
= getLanguage(state
, uuidLang
, lanAbbrev
, lanAbbrev
, lanAbbrev
);
124 if (language
== null || language
.equals(Language
.UNDETERMINED() )){
125 logger
.warn("Langauge undefined: " + lanAbbrev
);
129 } catch (IllegalArgumentException e
) {
131 } catch (UndefinedTransformerMethodException e
) {
134 languageMap
.put(lanAbbrev
, language
);
136 result
.put(nameSpace
, languageMap
);
138 } catch (SQLException e
) {
139 throw new RuntimeException(e
);
145 protected boolean doCheck(ErmsImportState state
){
146 IOValidator
<ErmsImportState
> validator
= new ErmsVernacularImportValidator();
147 return validator
.validate(state
);
152 protected boolean isIgnore(ErmsImportState state
){
153 return ! state
.getConfig().isDoVernaculars();