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
.berlinModel
.in
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
20 import java
.util
.SortedSet
;
21 import java
.util
.TreeSet
;
22 import java
.util
.UUID
;
24 import org
.apache
.commons
.lang
.StringUtils
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.springframework
.stereotype
.Component
;
28 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
29 import eu
.etaxonomy
.cdm
.io
.berlinModel
.BerlinModelTransformer
;
30 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.validation
.BerlinModelCommonNamesImportValidator
;
31 import eu
.etaxonomy
.cdm
.io
.common
.IOValidator
;
32 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
33 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
35 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
36 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
37 import eu
.etaxonomy
.cdm
.model
.common
.DescriptionElementSource
;
38 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
39 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
40 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
41 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
42 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
43 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
44 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
45 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
46 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
47 import eu
.etaxonomy
.cdm
.model
.location
.TdwgArea
;
48 import eu
.etaxonomy
.cdm
.model
.location
.WaterbodyOrCountry
;
49 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
50 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
61 public class BerlinModelCommonNamesImport
extends BerlinModelImportBase
{
62 private static final Logger logger
= Logger
.getLogger(BerlinModelCommonNamesImport
.class);
64 public static final UUID REFERENCE_LANGUAGE_ISO639_2_UUID
= UUID
.fromString("40c4f8dd-3d9c-44a4-b77a-76e137a89a5f");
65 public static final UUID REFERENCE_LANGUAGE_STRING_UUID
= UUID
.fromString("2a1b678f-c27d-48c1-b43e-98fd0d426305");
66 public static final UUID STATUS_ANNOTATION_UUID
= UUID
.fromString("e3f7b80a-1286-458d-812c-5e818f731968");
68 public static final String NAMESPACE
= "common name";
71 private static final String pluralString
= "common names";
72 private static final String dbTableName
= "emCommonName";
75 //map that stores the regions (named areas) and makes them accessible via the regionFk
76 private Map
<String
, NamedArea
> regionMap
= new HashMap
<String
, NamedArea
>();
80 public BerlinModelCommonNamesImport(){
87 protected String
getIdQuery(BerlinModelImportState state
) {
88 String result
= " SELECT CommonNameId FROM emCommonName WHERE (1=1) ";
89 if (StringUtils
.isNotBlank(state
.getConfig().getCommonNameFilter())){
90 result
+= " AND " + state
.getConfig().getCommonNameFilter();
99 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
102 protected String
getRecordQuery(BerlinModelImportConfigurator config
) {
103 String recordQuery
= "";
105 " SELECT emCommonName.CommonNameId, emCommonName.CommonName, PTaxon.RIdentifier AS taxonId, emCommonName.PTNameFk, emCommonName.RefFk AS refId, emCommonName.Status, " +
106 " emCommonName.RegionFks, emCommonName.MisNameRefFk, emCommonName.NameInSourceFk , emCommonName.Created_When, emCommonName.Updated_When, emCommonName.Created_Who, emCommonName.Updated_Who, emCommonName.Note as Notes," +
107 " regionLanguage.Language AS regionLanguage, languageCommonName.Language, languageCommonName.LanguageOriginal, languageCommonName.ISO639_1, languageCommonName.ISO639_2, " +
108 " emLanguageRegion.Region, emLanguageReference.RefFk as languageRefRefFk, emLanguageReference.ReferenceShort, " +
109 " emLanguageReference.ReferenceLong, emLanguageReference.LanguageFk, languageReferenceLanguage.Language AS refLanguage, " +
110 " languageReferenceLanguage.ISO639_2 AS refLanguageIso639_2, regionLanguage.ISO639_2 AS regionLanguageIso, " +
111 " misappliedTaxon.RIdentifier AS misappliedTaxonId " +
112 " FROM emLanguage as regionLanguage RIGHT OUTER JOIN " +
113 " emLanguageRegion ON regionLanguage.LanguageId = emLanguageRegion.LanguageFk RIGHT OUTER JOIN " +
114 " emLanguage AS languageReferenceLanguage RIGHT OUTER JOIN " +
115 " emLanguageReference ON languageReferenceLanguage.LanguageId = emLanguageReference.LanguageFk RIGHT OUTER JOIN " +
116 " emCommonName INNER JOIN " +
117 " PTaxon ON emCommonName.PTNameFk = PTaxon.PTNameFk AND emCommonName.PTRefFk = PTaxon.PTRefFk ON " +
118 " emLanguageReference.ReferenceId = emCommonName.LanguageRefFk LEFT OUTER JOIN " +
119 " emLanguage AS languageCommonName ON emCommonName.LanguageFk = languageCommonName.LanguageId ON " +
120 " emLanguageRegion.RegionId = emCommonName.RegionFks LEFT OUTER JOIN " +
121 " PTaxon as misappliedTaxon ON emCommonName.NameInSourceFk = misappliedTaxon.PTNameFk AND emCommonName.MisNameRefFk = misappliedTaxon.PTRefFk " +
122 " WHERE emCommonName.CommonNameId IN (" + ID_LIST_TOKEN
+ ")";
129 protected void doInvoke(BerlinModelImportState state
) {
132 } catch (Exception e
) {
133 logger
.error("Error when creating common name regions:" + e
.getMessage());
135 state
.setUnsuccessfull();
137 super.doInvoke(state
);
145 private void makeRegions(BerlinModelImportState state
) {
147 SortedSet
<Integer
> regionFks
= new TreeSet
<Integer
>();
148 Source source
= state
.getConfig().getSource();
150 getRegionFks(state
, regionFks
, source
);
151 //concat filter string
152 String sqlWhere
= getSqlWhere(regionFks
);
154 //get E+M - TDWG Mapping
155 Map
<String
, String
> emTdwgMap
= getEmTdwgMap(source
);
157 fillRegionMap(state
, sqlWhere
, emTdwgMap
);
160 } catch (NumberFormatException e
) {
162 state
.setUnsuccessfull();
164 } catch (SQLException e
) {
166 state
.setUnsuccessfull();
173 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
175 public boolean doPartition(ResultSetPartitioner partitioner
, BerlinModelImportState state
) {
176 boolean success
= true ;
177 BerlinModelImportConfigurator config
= state
.getConfig();
178 Set
<TaxonBase
> taxaToSave
= new HashSet
<TaxonBase
>();
179 Map
<String
, Taxon
> taxonMap
= (Map
<String
, Taxon
>) partitioner
.getObjectMap(BerlinModelTaxonImport
.NAMESPACE
);
180 Map
<String
, TaxonNameBase
> taxonNameMap
= (Map
<String
, TaxonNameBase
>) partitioner
.getObjectMap(BerlinModelTaxonNameImport
.NAMESPACE
);
182 Map
<String
, Reference
> biblioRefMap
= (Map
<String
, Reference
>) partitioner
.getObjectMap(BerlinModelReferenceImport
.BIBLIO_REFERENCE_NAMESPACE
);
183 Map
<String
, Reference
> nomRefMap
= (Map
<String
, Reference
>) partitioner
.getObjectMap(BerlinModelReferenceImport
.NOM_REFERENCE_NAMESPACE
);
185 Map
<String
, Language
> iso6392Map
= new HashMap
<String
, Language
>();
187 // logger.warn("MisappliedNameRefFk not yet implemented for Common Names");
189 ResultSet rs
= partitioner
.getResultSet();
193 //create TaxonName element
194 Object commonNameId
= rs
.getObject("CommonNameId");
195 int taxonId
= rs
.getInt("taxonId");
196 Object refId
= rs
.getObject("refId");
197 Object ptNameFk
= rs
.getObject("PTNameFk");
198 String commonNameString
= rs
.getString("CommonName");
199 String iso639_2
= rs
.getString("ISO639_2");
200 String iso639_1
= rs
.getString("ISO639_1");
201 String languageString
= rs
.getString("Language");
202 String originalLanguageString
= rs
.getString("LanguageOriginal");
203 Object misNameRefFk
= rs
.getObject("MisNameRefFk");
204 Object languageRefRefFk
= rs
.getObject("languageRefRefFk");
205 String refLanguage
= rs
.getString("refLanguage");
206 String refLanguageIso639_2
= rs
.getString("refLanguageIso639_2");
207 String status
= rs
.getString("Status");
208 Object nameInSourceFk
= rs
.getObject("NameInSourceFk");
209 Object misappliedTaxonId
= rs
.getObject("misappliedTaxonId");
212 String region
= rs
.getString("Region");
213 String regionFks
= rs
.getString("RegionFks");
214 String
[] regionFkSplit
= regionFks
.split(",");
217 if (CdmUtils
.isEmpty(commonNameString
)){
218 String message
= "CommonName is empty or null. Do not import record for taxon " + taxonId
;
219 logger
.warn(message
);
225 TaxonBase taxonBase
= taxonMap
.get(String
.valueOf(taxonId
));
226 if (taxonBase
== null){
227 logger
.warn("Taxon (" + taxonId
+ ") could not be found. Common name " + commonNameString
+ "(" + commonNameId
+ ") not imported");
229 }else if (! taxonBase
.isInstanceOf(Taxon
.class)){
230 logger
.warn("taxon (" + taxonId
+ ") is not accepted. Can't import common name " + commonNameId
);
233 taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
237 Language language
= getAndHandleLanguage(iso6392Map
, iso639_2
, iso639_1
, languageString
, originalLanguageString
);
240 List
<CommonTaxonName
> commonTaxonNames
= new ArrayList
<CommonTaxonName
>();
241 for (String regionFk
: regionFkSplit
){ //
242 CommonTaxonName commonTaxonName
;
243 if (commonTaxonNames
.size() == 0){
244 commonTaxonName
= CommonTaxonName
.NewInstance(commonNameString
, language
);
246 commonTaxonName
= (CommonTaxonName
)commonTaxonNames
.get(0).clone();
248 commonTaxonNames
.add(commonTaxonName
);
249 regionFk
= regionFk
.trim();
250 NamedArea area
= regionMap
.get(regionFk
);
252 if (regionFkSplit
.length
> 1 && StringUtils
.isNotBlank(regionFk
)){
253 logger
.warn("Area for " + regionFk
+ " not defined.");
255 //no region is defined
258 commonTaxonName
.setArea(area
);
259 TaxonDescription description
= getDescription(taxon
);
260 description
.addElement(commonTaxonName
);
265 String strRefId
= String
.valueOf(refId
);
266 String languageRefFk
= String
.valueOf(languageRefRefFk
);
267 if (! CdmUtils
.nullSafeEqual(strRefId
, languageRefFk
)){
268 //use strRefId if languageRefFk is null
269 if (languageRefRefFk
== null){
270 languageRefFk
= strRefId
;
272 logger
.warn("CommonName.RefFk (" + CdmUtils
.Nz(strRefId
) + ") and LanguageReference.RefFk " + CdmUtils
.Nz(languageRefFk
) + " are not equal. I will import only languageReference.RefFk");
276 Reference reference
= getReferenceOnlyFromMaps(biblioRefMap
, nomRefMap
, String
.valueOf(languageRefRefFk
));
277 String microCitation
= null;
278 String originalNameString
= null;
280 TaxonNameBase nameUsedInSource
= taxonNameMap
.get(String
.valueOf(nameInSourceFk
));
281 if (nameInSourceFk
!= null && nameUsedInSource
== null){
282 logger
.warn("Name used in source (" + nameInSourceFk
+ ") was not found for common name " + commonNameId
);
284 DescriptionElementSource source
= DescriptionElementSource
.NewInstance(reference
, microCitation
, nameUsedInSource
, originalNameString
);
285 for (CommonTaxonName commonTaxonName
: commonTaxonNames
){
286 commonTaxonName
.addSource(source
);
291 if (misNameRefFk
!= null){
292 //Taxon misappliedName = getMisappliedName(biblioRefMap, nomRefMap, misNameRefFk, taxon);
293 Taxon misappliedNameTaxon
= null;
294 if (misappliedTaxonId
!= null){
295 misappliedNameTaxon
= taxonMap
.get(String
.valueOf(misappliedTaxonId
));
298 Reference
<?
> sec
= getReferenceOnlyFromMaps(biblioRefMap
, nomRefMap
, String
.valueOf(misNameRefFk
));
299 if (nameUsedInSource
== null || sec
== null){
300 logger
.warn("Taxon name or misapplied name reference is null for common name " + commonNameId
);
302 misappliedNameTaxon
= Taxon
.NewInstance(nameUsedInSource
, sec
);
303 MarkerType misCommonNameMarker
= getMarkerType(state
, BerlinModelTransformer
.uuidMisappliedCommonName
,"Misapplied Common Name in Berlin Model", "Misapplied taxon was automatically created by Berlin Model import for a common name with a misapplied name reference", "MCN");
304 Marker marker
= Marker
.NewInstance(misCommonNameMarker
, true);
305 misappliedNameTaxon
.addMarker(marker
);
306 taxaToSave
.add(misappliedNameTaxon
);
307 logger
.warn("Misapplied name taxon could not be found in database but misapplied name reference exists for common name. " +
308 "New misapplied name for misapplied reference common name was added. CommonNameId: " + commonNameId
);
311 if (misappliedNameTaxon
!= null){
313 if (! taxon
.getMisappliedNames().contains(misappliedNameTaxon
)){
314 taxon
.addMisappliedName(misappliedNameTaxon
, config
.getSourceReference(), null);
315 logger
.warn("Misapplied name for common name was not found related to the accepted taxon. Created new relationship. CommonNameId: " + commonNameId
);
318 TaxonDescription misappliedNameDescription
= getDescription(misappliedNameTaxon
);
319 for (CommonTaxonName commonTaxonName
: commonTaxonNames
){
320 CommonTaxonName commonNameClone
= (CommonTaxonName
)commonTaxonName
.clone();
321 misappliedNameDescription
.addElement(commonNameClone
);
324 logger
.warn("Misapplied name is null for common name " + commonNameId
);
330 //reference extensions
331 if (reference
!= null){
332 if (CdmUtils
.isNotEmpty(refLanguage
)){
333 ExtensionType refLanguageExtensionType
= getExtensionType( state
, REFERENCE_LANGUAGE_STRING_UUID
, "reference language","The language of the reference","ref. lang.");
334 Extension
.NewInstance(reference
, refLanguage
, refLanguageExtensionType
);
337 if (CdmUtils
.isNotEmpty(refLanguageIso639_2
)){
338 ExtensionType refLanguageIsoExtensionType
= getExtensionType( state
, REFERENCE_LANGUAGE_ISO639_2_UUID
, "reference language iso 639-2","The iso 639-2 code of the references language","ref. lang. 639-2");
339 Extension
.NewInstance(reference
, refLanguageIso639_2
, refLanguageIsoExtensionType
);
341 }else if (CdmUtils
.isNotEmpty(refLanguage
) || CdmUtils
.isNotEmpty(refLanguageIso639_2
)){
342 logger
.warn("Reference is null (" + languageRefRefFk
+ ") but refLanguage (" + CdmUtils
.Nz(refLanguage
) + ") or iso639_2 (" + CdmUtils
.Nz(refLanguageIso639_2
) + ") was not null for common name ("+ commonNameId
+")");
346 if (CdmUtils
.isNotEmpty(status
)){
347 AnnotationType statusAnnotationType
= getAnnotationType( state
, STATUS_ANNOTATION_UUID
, "status","The status of this object","status", null);
348 Annotation annotation
= Annotation
.NewInstance(status
, statusAnnotationType
, Language
.DEFAULT());
349 for (CommonTaxonName commonTaxonName
: commonTaxonNames
){
350 commonTaxonName
.addAnnotation(annotation
);
356 for (CommonTaxonName commonTaxonName
: commonTaxonNames
){
357 doIdCreatedUpdatedNotes(state
, commonTaxonName
, rs
, String
.valueOf(commonNameId
), NAMESPACE
);
359 partitioner
.startDoSave();
360 taxaToSave
.add(taxon
);
363 } catch (SQLException e
) {
364 logger
.error("SQLException:" + e
);
366 } catch (ClassCastException e
) {
370 // logger.info( i + " names handled");
371 getTaxonService().save(taxaToSave
);
379 * Not used anymore. Use MisappliedName RIdentifier instead
380 * @param biblioRefMap
382 * @param misNameRefFk
385 private boolean isFirstMisappliedName
= true;
386 private Taxon
getMisappliedName(Map
<String
, Reference
> biblioRefMap
, Map
<String
, Reference
> nomRefMap
, Object misNameRefFk
, Taxon taxon
) {
387 Taxon misappliedTaxon
= null;
388 Reference
<?
> misNameRef
= getReferenceOnlyFromMaps(biblioRefMap
, nomRefMap
, String
.valueOf(misNameRefFk
));
389 misappliedTaxon
= Taxon
.NewInstance(taxon
.getName(), misNameRef
);
390 Set
<String
> includeProperty
= new HashSet
<String
>();
392 // //IMatchStrategy matchStrategy = DefaultMatchStrategy.NewInstance(TaxonBase.class);
393 // //List<TaxonBase> misappliedList1 = getCommonService().findMatching(misappliedTaxon, matchStrategy);
394 List
<TaxonBase
> misappliedList
= getTaxonService().list(misappliedTaxon
, includeProperty
, null, null, null, null);
395 if (misappliedList
.size() > 0){
396 misappliedTaxon
= CdmBase
.deproxy(misappliedList
.get(0), Taxon
.class);
398 } catch (ClassCastException e
) {
399 logger
.error(e
.getMessage());
400 if (isFirstMisappliedName
){
402 isFirstMisappliedName
= false;
405 return misappliedTaxon
;
413 * @param languageString
414 * @param originalLanguageString
417 private Language
getAndHandleLanguage(Map
<String
, Language
> iso639Map
, String iso639_2
, String iso639_1
, String languageString
, String originalLanguageString
) {
419 if (CdmUtils
.isNotEmpty(iso639_2
)|| CdmUtils
.isNotEmpty(iso639_1
) ){
420 //TODO test performance, implement in state
421 language
= getLanguageFromIsoMap(iso639Map
, iso639_2
, iso639_1
);
423 if (language
== null){
424 language
= getTermService().getLanguageByIso(iso639_2
);
425 iso639Map
.put(iso639_2
, language
);
426 if (language
== null){
427 language
= getTermService().getLanguageByIso(iso639_1
);
428 iso639Map
.put(iso639_1
, language
);
430 if (language
== null){
431 logger
.warn("Language for code ISO693-2 '" + iso639_2
+ "' and ISO693-1 '" + iso639_1
+ "' was not found");
434 } else if ("unknown".equals(languageString
)){
435 language
= Language
.UNKNOWN_LANGUAGE();
437 logger
.warn("language ISO 639_1 and ISO 639_2 were empty for " + languageString
);
440 addOriginalLanguage(language
, originalLanguageString
);
451 private Language
getLanguageFromIsoMap(Map
<String
, Language
> iso639Map
, String iso639_2
, String iso639_1
) {
453 language
= iso639Map
.get(iso639_2
);
454 if (language
== null){
455 language
= iso639Map
.get(iso639_1
);
462 * @param originalLanguageString
464 private void addOriginalLanguage(Language language
, String originalLanguageString
) {
465 if (CdmUtils
.isEmpty(originalLanguageString
)){
467 }else if (language
== null){
468 logger
.warn("Language could not be defined, but originalLanguageString exists: " + originalLanguageString
);
470 Representation representation
= language
.getRepresentation(language
);
471 if (representation
== null){
472 language
.addRepresentation(Representation
.NewInstance(originalLanguageString
, originalLanguageString
, originalLanguageString
, language
));
473 getTermService().saveOrUpdate(language
);
486 * @throws SQLException
488 private void getRegionFks(BerlinModelImportState state
, SortedSet
<Integer
> regionFks
, Source source
) throws SQLException
{
489 String sql
= " SELECT DISTINCT RegionFks FROM emCommonName";
490 if (state
.getConfig().getCommonNameFilter() != null){
491 sql
+= " WHERE " + state
.getConfig().getCommonNameFilter();
494 ResultSet rs
= source
.getResultSet(sql
);
496 String strRegionFks
= rs
.getString("RegionFks");
497 String
[] regionFkArray
= strRegionFks
.split(",");
498 for (String regionFk
: regionFkArray
){
499 regionFk
= regionFk
.trim();
500 if (! StringUtils
.isNumeric(regionFk
) || "".equals(regionFk
) ){
501 state
.setUnsuccessfull();
502 logger
.warn("RegionFk is not numeric: " + regionFk
);
504 regionFks
.add(Integer
.valueOf(regionFk
));
517 * @throws SQLException
519 private void fillRegionMap(BerlinModelImportState state
, String sqlWhere
,
520 Map
<String
, String
> emTdwgMap
) throws SQLException
{
521 Source source
= state
.getConfig().getSource();
524 sql
= " SELECT RegionId, Region FROM emLanguageRegion WHERE RegionId IN ("+ sqlWhere
+ ") ";
525 rs
= source
.getResultSet(sql
);
527 Object regionId
= rs
.getObject("RegionId");
528 String region
= rs
.getString("Region");
529 String
[] splitRegion
= region
.split("-");
530 if (splitRegion
.length
<= 1){
531 NamedArea newArea
= getNamedArea(state
, null, region
, "Language region '" + region
+ "'", null, null, null);
532 getTermService().save(newArea
);
533 regionMap
.put(String
.valueOf(regionId
), newArea
);
534 logger
.warn("Found new area: " + region
);
535 }else if (splitRegion
.length
== 2){
536 String emCode
= splitRegion
[1].trim();
537 String tdwgCode
= emTdwgMap
.get(emCode
);
538 if (StringUtils
.isNotBlank(tdwgCode
) ){
539 NamedArea tdwgArea
= getNamedArea(state
, tdwgCode
);
540 regionMap
.put(String
.valueOf(regionId
), tdwgArea
);
542 logger
.warn("emCode did not map to valid tdwgCode: " + CdmUtils
.Nz(emCode
) + "->" + CdmUtils
.Nz(tdwgCode
));
553 private NamedArea
getNamedArea(BerlinModelImportState state
, String tdwgCode
) {
555 if (tdwgCode
.equalsIgnoreCase("Ab")){
556 area
= getNamedArea(state
, BerlinModelTransformer
.uuidAzerbaijanNakhichevan
, "Azerbaijan & Nakhichevan", "Azerbaijan (including Nakhichevan)", "Ab", null, null);
557 getTermService().save(area
);
558 }else if (tdwgCode
.equalsIgnoreCase("Rf")){
559 area
= WaterbodyOrCountry
.RUSSIANFEDERATION();
560 // getTermService().save(area);
561 }else if (tdwgCode
.equalsIgnoreCase("Uk")){
562 area
= getNamedArea(state
, BerlinModelTransformer
.uuidUkraineAndCrimea
, "Ukraine & Crimea", "Ukraine (including Crimea)", "Uk", null, null);
563 getTermService().save(area
);
565 area
= TdwgArea
.getAreaByTdwgAbbreviation(tdwgCode
);
568 logger
.warn("Area is null for " + tdwgCode
);
579 private String
getSqlWhere(SortedSet
<Integer
> regionFks
) {
580 String sqlWhere
= "";
581 for (Integer regionFk
: regionFks
){
582 sqlWhere
+= regionFk
+ ",";
584 sqlWhere
= sqlWhere
.substring(0, sqlWhere
.length()-1);
592 * @throws SQLException
594 private Map
<String
, String
> getEmTdwgMap(Source source
) throws SQLException
{
597 Map
<String
, String
> emTdwgMap
= new HashMap
<String
, String
>();
598 sql
= " SELECT EmCode, TDWGCode FROM emArea ";
599 rs
= source
.getResultSet(sql
);
601 String emCode
= rs
.getString("EMCode");
602 String TDWGCode
= rs
.getString("TDWGCode");
603 if (StringUtils
.isNotBlank(emCode
) ){
604 emCode
= emCode
.trim();
605 if (emCode
.equalsIgnoreCase("Ab") || emCode
.equalsIgnoreCase("Rf")|| emCode
.equalsIgnoreCase("Uk") ){
606 emTdwgMap
.put(emCode
, emCode
);
607 }else if (StringUtils
.isNotBlank(TDWGCode
)){
608 emTdwgMap
.put(emCode
, TDWGCode
.trim());
612 emTdwgMap
.put("Ab / Ab(A)", "Ab");
613 emTdwgMap
.put("Ga / Ga(F)", "FRA-FR");
614 emTdwgMap
.put("It / It(I)", "ITA");
615 emTdwgMap
.put("Uk / Uk(U)", "Uk");
616 emTdwgMap
.put("Ar / Ar(A)", "TCS-AR");
624 * Returns the first non-image gallery description. Creates a new one if no description exists.
628 private TaxonDescription
getDescription(Taxon taxon
) {
629 TaxonDescription result
= null;
630 for (TaxonDescription taxonDescription
: taxon
.getDescriptions()){
631 if (! taxonDescription
.isImageGallery()){
632 result
= taxonDescription
;
636 result
= TaxonDescription
.NewInstance(taxon
);
642 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
644 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
) {
648 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<Object
, Map
<String
, ?
extends CdmBase
>>();
651 Set
<String
> taxonIdSet
= new HashSet
<String
>();
652 Set
<String
> nameIdSet
= new HashSet
<String
>();
653 Set
<String
> referenceIdSet
= new HashSet
<String
>();
655 handleForeignKey(rs
, taxonIdSet
, "taxonId");
656 handleForeignKey(rs
, taxonIdSet
, "misappliedTaxonId");
657 handleForeignKey(rs
, referenceIdSet
, "refId");
658 handleForeignKey(rs
, referenceIdSet
, "languageRefRefFk");
659 handleForeignKey(rs
, nameIdSet
, "NameInSourceFk");
660 handleForeignKey(rs
, nameIdSet
, "PTNameFk");
661 handleForeignKey(rs
, referenceIdSet
, "MisNameRefFk");
665 nameSpace
= BerlinModelTaxonNameImport
.NAMESPACE
;
666 cdmClass
= TaxonNameBase
.class;
668 Map
<String
, TaxonNameBase
<?
,?
>> nameMap
= (Map
<String
, TaxonNameBase
<?
,?
>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
669 result
.put(nameSpace
, nameMap
);
672 nameSpace
= BerlinModelTaxonImport
.NAMESPACE
;
673 cdmClass
= TaxonBase
.class;
675 Map
<String
, TaxonBase
<?
>> taxonMap
= (Map
<String
, TaxonBase
<?
>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
676 result
.put(nameSpace
, taxonMap
);
679 nameSpace
= BerlinModelReferenceImport
.NOM_REFERENCE_NAMESPACE
;
680 cdmClass
= Reference
.class;
681 idSet
= referenceIdSet
;
682 Map
<String
, Reference
> nomReferenceMap
= (Map
<String
, Reference
>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
683 result
.put(nameSpace
, nomReferenceMap
);
685 //biblio reference map
686 nameSpace
= BerlinModelReferenceImport
.BIBLIO_REFERENCE_NAMESPACE
;
687 cdmClass
= Reference
.class;
688 idSet
= referenceIdSet
;
689 Map
<String
, Reference
<?
>> biblioReferenceMap
= (Map
<String
, Reference
<?
>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
690 result
.put(nameSpace
, biblioReferenceMap
);
692 } catch (SQLException e
) {
693 throw new RuntimeException(e
);
694 } catch (NullPointerException nep
){
695 logger
.error("NullPointerException in getRelatedObjectsForPartition()");
703 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
706 protected boolean doCheck(BerlinModelImportState state
){
707 IOValidator
<BerlinModelImportState
> validator
= new BerlinModelCommonNamesImportValidator();
708 return validator
.validate(state
);
713 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getTableName()
716 protected String
getTableName() {
721 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getPluralString()
724 public String
getPluralString() {
729 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
731 protected boolean isIgnore(BerlinModelImportState state
){
732 return ! state
.getConfig().isDoCommonNames();