Remove generics from Reference in cdmlib (except for cdmlib-model) #5830
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / taxonx2013 / TaxonXAddSources.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.io.taxonx2013;
11
12 import java.util.Set;
13
14 import org.apache.log4j.Logger;
15
16 import eu.etaxonomy.cdm.model.common.CdmBase;
17 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
18 import eu.etaxonomy.cdm.model.common.OriginalSourceType;
19 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
20 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
21 import eu.etaxonomy.cdm.model.description.TaxonDescription;
22 import eu.etaxonomy.cdm.model.description.TextData;
23 import eu.etaxonomy.cdm.model.name.NonViralName;
24 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
26 import eu.etaxonomy.cdm.model.reference.Reference;
27 import eu.etaxonomy.cdm.model.taxon.Synonym;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29
30 /**
31 * @author pkelbert
32 * @date 20 déc. 2013
33 *
34 */
35 public class TaxonXAddSources {
36
37 private Reference sourceUrlRef;
38 private TaxonXImport importer;
39 private TaxonXImportState configState;
40 @SuppressWarnings("unused")
41 private static final Logger logger = Logger.getLogger(TaxonXAddSources.class);
42
43 /**
44 * @param importer
45 */
46 public void setImporter(TaxonXImport importer) {
47 this.importer=importer;
48 }
49
50
51 /**
52 * @param configState the configState to set
53 */
54 public void setConfigState(TaxonXImportState configState) {
55 this.configState = configState;
56 }
57
58
59 /**
60 * @return the sourceUrlRef
61 */
62 public Reference getSourceUrlRef() {
63 return sourceUrlRef;
64 }
65
66 /**
67 * @param sourceUrlRef the sourceUrlRef to set
68 */
69 public void setSourceUrlRef(Reference sourceUrlRef) {
70 this.sourceUrlRef = sourceUrlRef;
71 }
72
73
74 protected IdentifiableSource getIdentifiableSource(Reference reference, Set<IdentifiableSource> sources, boolean original){
75 // logger.info("getIdentifiableSource");
76 boolean sourceExists=false;
77 IdentifiableSource source=null;
78 for (IdentifiableSource src : sources){
79 String micro = src.getCitationMicroReference();
80 Reference r = src.getCitation();
81 if (r.getTitleCache().equals(reference.getTitleCache())) {
82 sourceExists=true;
83 break;
84 }
85 }
86 if(!sourceExists) {
87 if(original) {
88 source = IdentifiableSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource,null,null,reference,null);
89 } else {
90 source = IdentifiableSource.NewInstance(OriginalSourceType.Import,null,null,reference,null);
91 }
92 }
93 return source;
94 }
95
96 protected DescriptionElementSource getDescriptionElementSource(Reference reference, Set<DescriptionElementSource> sources, boolean original){
97 //logger.info("getDescriptionElementSource");
98 boolean sourceExists=false;
99 DescriptionElementSource source=null;
100 for (DescriptionElementSource src : sources){
101 String micro = src.getCitationMicroReference();
102 Reference r = src.getCitation();
103 if (r.getTitleCache().equals(reference.getTitleCache())) {
104 sourceExists=true;
105 break;
106 }
107 }
108 if(!sourceExists) {
109 if(original) {
110 source = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource,null,null,reference,null);
111 } else {
112 source = DescriptionElementSource.NewInstance(OriginalSourceType.Import,null,null,reference,null);
113 }
114 }
115 return source;
116
117 }
118
119 /**
120 * @param refMods
121 * @param synonym
122 */
123 protected void addSource(Reference refMods, Synonym synonym) {
124 //logger.info("addSource");
125 sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
126 Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
127 IdentifiableSource id = getIdentifiableSource(sourceUrlRef,synonym.getSources(), false);
128 IdentifiableSource id2 = getIdentifiableSource(refMods,synonym.getSources(), true);
129 IdentifiableSource id3 = getIdentifiableSource(sec,synonym.getSources(), false);
130 if( id!=null) {
131 synonym.addSource(id);
132 }
133 if( id2!=null) {
134 synonym.addSource(id2);
135 }
136 if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
137 synonym.addSource(id3);
138 }
139 }
140
141
142 /**
143 * @param refMods
144 * @param indAssociation
145 */
146 protected IndividualsAssociation addSource(Reference refMods, IndividualsAssociation indAssociation) {
147 //logger.info("addSource");
148 sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
149 Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
150 DescriptionElementSource id = getDescriptionElementSource(sourceUrlRef, indAssociation.getSources(), false);
151 DescriptionElementSource id2 = getDescriptionElementSource(refMods, indAssociation.getSources(), true);
152 DescriptionElementSource id3 = getDescriptionElementSource(sec, indAssociation.getSources(), false);
153
154 if(id!=null) {
155 indAssociation.addSource(id);
156 }
157 if( id2!=null) {
158 indAssociation.addSource(id2);
159 }
160 if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
161 indAssociation.addSource(id3);
162 }
163 return indAssociation;
164 }
165
166 /**
167 * @param refMods
168 * @param acceptedTaxon
169 */
170 protected void addSource(Reference refMods, Taxon acceptedTaxon) {
171 //logger.info("addSource");
172 sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
173 Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
174 IdentifiableSource id = getIdentifiableSource(sourceUrlRef, acceptedTaxon.getSources(), false);
175 IdentifiableSource id2 = getIdentifiableSource(refMods, acceptedTaxon.getSources(), true);
176 IdentifiableSource id3 = getIdentifiableSource(sec, acceptedTaxon.getSources(), false);
177 if( id!=null) {
178 acceptedTaxon.addSource(id);
179 }
180 if( id2!=null) {
181 acceptedTaxon.addSource(id2);
182 }
183 if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
184 acceptedTaxon.addSource(id3);
185 }
186 }
187
188 /**
189 * @param refMods
190 * @param nameToBeFilled
191 */
192 protected void addSource(Reference refMods, NonViralName<?> nameToBeFilled) {
193 //logger.info("addSource");
194 sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
195 Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
196 IdentifiableSource id = getIdentifiableSource(sourceUrlRef, nameToBeFilled.getSources(), false);
197 IdentifiableSource id2 = getIdentifiableSource(refMods, nameToBeFilled.getSources(), true);
198 IdentifiableSource id3 = getIdentifiableSource(sec, nameToBeFilled.getSources(), false);
199
200 if( id!=null) {
201 nameToBeFilled.addSource(id);
202 }
203 if( id2!=null) {
204 nameToBeFilled.addSource(id2);
205 }
206 if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
207 nameToBeFilled.addSource(id3);
208 }
209
210 }
211
212 /**
213 * @param refMods
214 * @param textData
215 */
216 protected void addSource(Reference refMods, TextData textData) {
217 //logger.info("addSource");
218 sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
219 Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
220 DescriptionElementSource id = getDescriptionElementSource(sourceUrlRef, textData.getSources(), false);
221 DescriptionElementSource id2 = getDescriptionElementSource(refMods, textData.getSources(), true);
222 DescriptionElementSource id3 = getDescriptionElementSource(sec, textData.getSources(), false);
223
224 if( id!=null) {
225 textData.addSource(id);
226 }
227 if( id2!=null) {
228 textData.addSource(id2);
229 }
230 if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
231 textData.addSource(id3);
232 }
233
234 }
235
236 /**
237 * @param refMods
238 * @param taxonDescription
239 * @param currentRef
240 */
241 protected void addAndSaveSource(Reference refMods, TaxonDescription taxonDescription, Reference currentRef) {
242 //logger.info("addAndSaveSource");
243 sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
244 Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
245 IdentifiableSource id = getIdentifiableSource(sourceUrlRef, taxonDescription.getSources(), false);
246 IdentifiableSource id2 = getIdentifiableSource(refMods, taxonDescription.getSources(), true);
247 IdentifiableSource id3 = getIdentifiableSource(sec, taxonDescription.getSources(), false);
248
249 if( id!=null) {
250 taxonDescription.addSource(id);
251 }
252 if( id2!=null) {
253 taxonDescription.addSource(id2);
254 }
255 if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
256 taxonDescription.addSource(id3);
257 }
258
259 importer.getDescriptionService().saveOrUpdate(taxonDescription);
260 }
261
262 /**
263 * @param refMods
264 * @param derivedUnitBase
265 */
266 protected void addAndSaveSource(Reference refMods, DerivedUnit derivedUnitBase) {
267 //logger.info("addAndSaveSource");
268 sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
269 Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
270 IdentifiableSource id = getIdentifiableSource(sourceUrlRef, derivedUnitBase.getSources(), false);
271 IdentifiableSource id2 = getIdentifiableSource(refMods, derivedUnitBase.getSources(), true);
272 IdentifiableSource id3 = getIdentifiableSource(sec, derivedUnitBase.getSources(), false);
273
274 if( id!=null) {
275 derivedUnitBase.addSource(id);
276 }
277 if( id2!=null) {
278 derivedUnitBase.addSource(id2);
279 }
280 if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
281 derivedUnitBase.addSource(id3);
282 }
283 importer.getOccurrenceService().saveOrUpdate(derivedUnitBase);
284 derivedUnitBase= CdmBase.deproxy(derivedUnitBase, DerivedUnit.class);
285 }
286
287
288 /**
289 * @param refMods
290 * @param taxonDescription
291 */
292 public void addSource(Reference refMods, TaxonDescription taxonDescription) {
293 //logger.info("addSource");
294 sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
295 Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
296 IdentifiableSource id = getIdentifiableSource(sourceUrlRef, taxonDescription.getSources(), false);
297 IdentifiableSource id2 = getIdentifiableSource(refMods, taxonDescription.getSources(), true);
298 IdentifiableSource id3 = getIdentifiableSource(sec, taxonDescription.getSources(), false);
299
300 if( id!=null) {
301 taxonDescription.addSource(id);
302 }
303 if( id2!=null) {
304 taxonDescription.addSource(id2);
305 }
306 if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
307 taxonDescription.addSource(id3);
308 }
309 importer.getDescriptionService().saveOrUpdate(taxonDescription);
310
311 }
312
313
314 /**
315 * @param reference
316 * @param textData
317 * @param name
318 * @param ref
319 */
320 public void addSource(Reference reference, TextData textData, TaxonNameBase name, Reference refMods) {
321 //logger.info("addSource");
322 sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
323 Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
324 DescriptionElementSource id1 = getDescriptionElementSource(refMods, textData.getSources(),name, true);
325 DescriptionElementSource id2 = getDescriptionElementSource(reference, textData.getSources(),name, false);
326 if( id1!=null) {
327 textData.addSource(id1);
328 }
329
330 if( id2!=null) {
331 textData.addSource(id2);
332 }
333
334 }
335
336
337 @SuppressWarnings({ "unused", "rawtypes" })
338 private DescriptionElementSource getDescriptionElementSource(Reference reference, Set<DescriptionElementSource> sources,
339 TaxonNameBase originalname, boolean original){
340 //logger.info("getDescriptionElementSource");
341 boolean sourceExists=false;
342 DescriptionElementSource source=null;
343 for (DescriptionElementSource src : sources){
344 String micro = src.getCitationMicroReference();
345 Reference r = src.getCitation();
346 TaxonNameBase oname = src.getNameUsedInSource();
347 try {
348 if (r.getTitleCache().equals(reference.getTitleCache())) {
349 if (oname.getTitleCache().equalsIgnoreCase(originalname.getTitleCache())) {
350 sourceExists=true;
351 break;
352 }
353 }
354 } catch (Exception e) {
355 // TODO Auto-generated catch block
356 e.printStackTrace();
357 }
358 }
359 if(!sourceExists) {
360 if(original) {
361 source = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource, null,null, reference, null, originalname, null);
362 } else {
363 source = DescriptionElementSource.NewInstance(OriginalSourceType.Import, null,null, reference, null, originalname, null);
364 }
365 }
366 return source;
367 }
368
369
370
371 }