cleanup
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / greece / FloraHellenicaImportBase.java
1 /**
2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.io.greece;
10
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImport;
19 import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
20 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
21 import eu.etaxonomy.cdm.model.description.TaxonDescription;
22 import eu.etaxonomy.cdm.model.name.Rank;
23 import eu.etaxonomy.cdm.model.name.TaxonName;
24 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
25 import eu.etaxonomy.cdm.model.reference.Reference;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27
28 /**
29 * @author a.mueller
30 * @since 14.12.2016
31 */
32
33 public abstract class FloraHellenicaImportBase<CONFIG extends FloraHellenicaImportConfigurator>
34 extends SimpleExcelTaxonImport<CONFIG>{
35
36 private static final long serialVersionUID = 2593130403213346396L;
37 private static final Logger logger = Logger.getLogger(FloraHellenicaImportBase.class);
38
39 private Map<UUID, Taxon> acceptedTaxonMap = new HashMap<>();
40 private Reference sourceReference;
41 private Reference secReference;
42 private Reference secReference2;
43
44 protected TaxonDescription getTaxonDescription(Taxon taxon) {
45 if (!taxon.getDescriptions().isEmpty()){
46 return taxon.getDescriptions().iterator().next();
47 }else{
48 TaxonDescription desc = TaxonDescription.NewInstance(taxon);
49 desc.setDefault(true);
50 return desc;
51 }
52 }
53
54 protected Reference getSourceCitation(SimpleExcelTaxonImportState<CONFIG> state) {
55 if (this.sourceReference == null){
56 this.sourceReference = getPersistentReference(state.getConfig().getSourceReference());
57 }
58 return this.sourceReference;
59 }
60
61 protected Reference getSecReference(SimpleExcelTaxonImportState<CONFIG> state) {
62 if (this.secReference == null){
63 this.secReference = getPersistentReference(state.getConfig().getSecReference());
64 }
65 return this.secReference;
66 }
67
68 protected Reference getSecReference2(SimpleExcelTaxonImportState<CONFIG> state) {
69 if (this.secReference2 == null){
70 this.secReference2 = getPersistentReference(state.getConfig().getSecReference2());
71 }
72 return this.secReference2;
73 }
74
75 private Reference getPersistentReference(Reference reference) {
76 Reference result = getReferenceService().find(reference.getUuid());
77 if (result == null){
78 result = reference;
79 }
80 return result;
81 }
82
83 protected Taxon getAcceptedTaxon(Map<String, String> record,
84 SimpleExcelTaxonImportState<CONFIG> state, String key) {
85
86 String accStr = getValue(record, key);
87 if (accStr == null){
88 return null;
89 }
90 accStr = accStr.trim();
91
92 Taxon accTaxon = state.getTaxon(accStr);
93 if (accTaxon == null){
94 String message = state.getCurrentLine()+ ": Accepted taxon could not be found: " + accStr;
95 logger.warn(message);
96 return null;
97 }else{
98 initAcceptedTaxonMap();
99 // accTaxon = (Taxon)getTaxonService().find(accTaxon.getUuid());
100 accTaxon = acceptedTaxonMap.get(accTaxon.getUuid());
101 }
102 return accTaxon;
103 }
104
105 private void initAcceptedTaxonMap() {
106 if (acceptedTaxonMap.isEmpty()){
107 List<Taxon> list = getTaxonService().list(Taxon.class, null, null, null, null);
108 for (Taxon taxon : list){
109 acceptedTaxonMap.put(taxon.getUuid(), taxon);
110 }
111 }
112 }
113
114 /**
115 * @param record
116 * @param state
117 * @return
118 */
119 protected Taxon getHigherTaxon(Map<String, String> record,
120 SimpleExcelTaxonImportState<CONFIG> state, String key) {
121
122 String accStr = getValue(record, key);
123 if (accStr == null){
124 return null;
125 }
126 accStr = accStr.trim();
127
128 Taxon accTaxon = state.getHigherTaxon(accStr);
129 if (accTaxon == null){
130 String message = state.getCurrentLine()+ ": Higher taxon could not be found: " + accStr;
131 logger.info(message); //not critical
132 return null;
133 }else{
134 initAcceptedTaxonMap();
135 // accTaxon = (Taxon)getTaxonService().find(accTaxon.getUuid());
136 accTaxon = acceptedTaxonMap.get(accTaxon.getUuid());
137 }
138 return accTaxon;
139 }
140
141
142 protected TaxonName makeFamilyName(SimpleExcelTaxonImportState<CONFIG> state,
143 String famStr) {
144 TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.FAMILY());
145 famStr = famStr.substring(0,1).toUpperCase() + famStr.substring(1).toLowerCase();
146 name.setGenusOrUninomial(famStr);
147 name.addSource(makeOriginalSource(state));
148 return name;
149 }
150
151 protected TaxonName replaceNameAuthorsAndReferences(SimpleExcelTaxonImportState<CONFIG> state, TaxonName name) {
152 TaxonName result = state.getDeduplicationHelper().getExistingName(name);
153 state.getDeduplicationHelper().replaceAuthorNamesAndNomRef(result);
154 return result;
155 }
156
157 @Override
158 protected IdentifiableSource makeOriginalSource(SimpleExcelTaxonImportState<CONFIG> state) {
159 return IdentifiableSource.NewDataImportInstance("line: " + state.getCurrentLine(), null, getSourceCitation(state));
160 }
161
162 }