2 * Copyright (C) 2016 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
.mexico
;
12 import java
.net
.URISyntaxException
;
13 import java
.util
.Arrays
;
14 import java
.util
.HashMap
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
22 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
23 import eu
.etaxonomy
.cdm
.common
.UTF8
;
24 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
25 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
26 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Identifier
;
28 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
29 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
31 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
33 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
34 import eu
.etaxonomy
.cdm
.model
.name
.IBotanicalName
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
36 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
37 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
38 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameFactory
;
39 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
40 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
41 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
44 import eu
.etaxonomy
.cdm
.strategy
.parser
.TimePeriodParser
;
52 public class MexicoBorhidiExcelImport
<CONFIG
extends MexicoBorhidiImportConfigurator
>
53 extends SimpleExcelTaxonImport
<CONFIG
>{
54 private static final Logger logger
= Logger
.getLogger(MexicoBorhidiExcelImport
.class);
55 private static final long serialVersionUID
= -3607776356577606657L;
57 private static List
<String
> expectedKeys
= Arrays
.asList(new String
[]{
58 "FullnameNoAuthors","OutputNameID","OutputFullNameWithAuthors","RefType"
59 ,"OutputAbbreviatedTitle","OutputCollation","OutputVolume",
60 "OutputIssue","OutputPage","OutputTitlePageYear","OutputYearPublished",
65 protected void firstPass(SimpleExcelTaxonImportState
<CONFIG
> state
) {
66 String line
= state
.getCurrentLine() + ": ";
67 HashMap
<String
, String
> record
= state
.getOriginalRecord();
69 Set
<String
> keys
= record
.keySet();
70 checkAllKeysExist(line
, keys
, expectedKeys
);
72 if (record
.get("FullnameNoAuthors") == null ){
73 logger
.warn("No FullnameNoAuthors given: " + line
);
78 IBotanicalName speciesName
= makeName(record
, state
);
81 Reference sec
= state
.getConfig().getSecReference();
82 Taxon taxon
= Taxon
.NewInstance(speciesName
, sec
);
83 TaxonNode rubiaceae
= getHighestNode(state
);
85 taxon
.addSource(makeOriginalSource(state
));
89 makeGenus(state
, speciesName
, sec
, taxon
, rubiaceae
);
93 private Classification classification
;
94 private TaxonNode rubiaceaeNode
;
98 private TaxonNode
getHighestNode(SimpleExcelTaxonImportState
<CONFIG
> state
) {
99 if (rubiaceaeNode
== null){
100 MexicoBorhidiImportConfigurator config
= state
.getConfig();
101 classification
= Classification
.NewInstance(state
.getConfig().getClassificationName());
102 classification
.setUuid(config
.getClassificationUuid());
103 classification
.setReference(config
.getSecReference());
104 IBotanicalName nameRubiaceae
= TaxonNameFactory
.NewBotanicalInstance(Rank
.FAMILY());
105 nameRubiaceae
.setGenusOrUninomial("Rubiaceae");
106 Taxon rubiaceaeTaxon
= Taxon
.NewInstance(nameRubiaceae
, classification
.getReference());
107 rubiaceaeNode
= classification
.addChildTaxon(rubiaceaeTaxon
, null, null);
108 getClassificationService().save(classification
);
110 return rubiaceaeNode
;
118 private IBotanicalName
makeName(HashMap
<String
, String
> record
, SimpleExcelTaxonImportState
<CONFIG
> state
) {
119 String line
= state
.getCurrentLine() + ": ";
121 String fullNameStr
= getValue(record
, "OutputFullNameWithAuthors");
122 // String volume = getValue(record, "OutputVolume");
123 // String issue = getValue(record, "OutputIssue");
124 // String page = getValue(record, "OutputPage");
125 String titleYear
= getValue(record
, "OutputTitlePageYear");
126 String publishedYear
= getValue(record
, "OutputYearPublished");
127 String refAbbrevTitle
= getValue(record
, "OutputAbbreviatedTitle");
128 String outputCollation
= getValue(record
, "OutputCollation");
129 String refType
= getValue(record
, "RefType");
132 TaxonName name
= (TaxonName
)nameParser
.parseFullName(fullNameStr
, NomenclaturalCode
.ICNAFP
, Rank
.SPECIES());
133 if (name
.isProtectedTitleCache()){
134 //for the 2 ined. names
135 name
= (TaxonName
)nameParser
.parseReferencedName(fullNameStr
, NomenclaturalCode
.ICNAFP
, Rank
.SPECIES());
137 if (name
.isProtectedTitleCache()){
138 logger
.warn(line
+ "Name could not be parsed: " + fullNameStr
);
140 replaceAuthorNamesAndNomRef(state
, name
);
143 if (refAbbrevTitle
!= null){
144 String
[] volumeDetail
= makeVolumeDetail(outputCollation
);
146 String volume
= null;
147 if (volumeDetail
.length
> 1){
148 volume
= volumeDetail
[0].trim();
149 detail
= volumeDetail
[1].trim();
151 detail
= volumeDetail
[0].trim();
154 refAbbrevTitle
= refAbbrevTitle
.trim();
155 boolean isArticle
= "A".equalsIgnoreCase(refType
);
158 if (! "A".equalsIgnoreCase(refType
)){
159 logger
.warn(line
+ "RefType problem with article " + refType
);
162 Reference journal
= state
.getReference(refAbbrevTitle
);
163 if (journal
== null){
164 journal
= ReferenceFactory
.newJournal();
165 journal
.setAbbrevTitle(refAbbrevTitle
);
166 state
.putReference(refAbbrevTitle
, journal
);
167 journal
.addSource(makeOriginalSource(state
));
170 Reference article
= ReferenceFactory
.newArticle();
173 // String detail = page;
174 name
.setNomenclaturalMicroReference(detail
);
176 article
.setVolume(CdmUtils
.Ne(volume
));
177 article
.setInReference(journal
);
179 titleYear
= (isBlank(publishedYear
)? titleYear
: UTF8
.QUOT_DBL_LOW9
+ titleYear
+ UTF8
.QUOT_DBL_HIGH_REV9
+ "[" + publishedYear
+ "]");
180 article
.setDatePublished(TimePeriodParser
.parseString(titleYear
));
182 article
.setAuthorship(name
.getCombinationAuthorship());
184 Reference existingArticle
= state
.getReference(article
.getTitleCache());
185 if (existingArticle
!= null){
186 name
.setNomenclaturalReference(existingArticle
);
188 name
.setNomenclaturalReference(article
);
189 state
.putReference(article
.getTitleCache(), article
);
190 article
.addSource(makeOriginalSource(state
));
193 if (! "B".equalsIgnoreCase(refType
)){
194 logger
.warn(line
+ "RefType problem with book" + refType
);
197 Reference book
= ReferenceFactory
.newBook();
198 book
.setAbbrevTitle(refAbbrevTitle
);
201 titleYear
= (isBlank(publishedYear
)? titleYear
: UTF8
.QUOT_DBL_LOW9
+ titleYear
+ UTF8
.QUOT_DBL_HIGH_REV9
+ "[" + publishedYear
+ "]");
202 book
.setDatePublished(TimePeriodParser
.parseString(titleYear
));
204 book
.setAuthorship(name
.getCombinationAuthorship());
207 Reference existingBook
= state
.getReference(book
.getTitleCache());
208 if (existingBook
!= null){
209 name
.setNomenclaturalReference(existingBook
);
211 name
.setNomenclaturalReference(book
);
212 state
.putReference(book
.getTitleCache(), book
);
215 book
.setVolume(volume
);
217 //String detail = page;
218 name
.setNomenclaturalMicroReference(detail
);
222 addNomRefExtension(state
, name
);
225 String bhlLink
= record
.get("OutputBHLLink");
226 if (isNotBlank(bhlLink
)){
229 uri
= new URI(bhlLink
);
230 Media media
= Media
.NewInstance(uri
, null, null, null);
231 TaxonNameDescription desc
= TaxonNameDescription
.NewInstance(name
);
232 desc
.setTitleCache("Protologue for " + name
.getNameCache(), true);
233 DescriptionElementBase elem
= TextData
.NewInstance(Feature
.PROTOLOGUE());
234 elem
.addMedia(media
);
235 desc
.addElement(elem
);
236 } catch (URISyntaxException e
) {
237 logger
.warn(line
+ "URI could not be parsed: " + e
.getMessage());
241 //add tropicos identifier
242 String tropicosId
= record
.get("OutputNameID");
243 if (isNotBlank(tropicosId
)){
244 String tropicosIdTypeLabel
= "Tropicos Name Identifier";
245 UUID uuid
= DefinedTerm
.uuidTropicosNameIdentifier
;
246 TermVocabulary
<DefinedTerm
> voc
= null; //for now it goes to user defined voc
247 DefinedTerm identifierType
= this.getIdentiferType(state
, uuid
, tropicosIdTypeLabel
, tropicosIdTypeLabel
, null, voc
);
248 Identifier
<Taxon
> identifier
= Identifier
.NewInstance(tropicosId
, identifierType
);
249 name
.addIdentifier(identifier
);
252 name
.addSource(makeOriginalSource(state
));
259 * @param outputCollation
262 private String
[] makeVolumeDetail(String outputCollation
) {
263 if (outputCollation
== null){
264 return new String
[0];
266 String
[] split
= outputCollation
.split(":");
273 * @param referencedName
275 private void addNomRefExtension(SimpleExcelTaxonImportState
<CONFIG
> state
, IBotanicalName name
) {
276 String newExtensionStr
= name
.getFullTitleCache() + " - BORHIDI";
277 UUID uuidNomRefExtension
= MexicoConabioTransformer
.uuidNomRefExtension
;
278 for (Extension extension
: name
.getExtensions()){
279 if (extension
.getType().getUuid().equals(uuidNomRefExtension
)){
280 extension
.setValue(extension
.getValue() + "\n" + newExtensionStr
);
284 String label
= "Nomenclatural reference in Sources";
285 String abbrev
= "Nom. ref. src.";
286 ExtensionType extensionType
= getExtensionType(state
, uuidNomRefExtension
, label
, label
, abbrev
);
287 Extension
.NewInstance((TaxonName
)name
, newExtensionStr
, extensionType
);