Project

General

Profile

Download (13.8 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2013 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.taxonx2013;
10

    
11
import java.util.Set;
12

    
13
import org.apache.log4j.Logger;
14

    
15
import eu.etaxonomy.cdm.model.common.CdmBase;
16
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
17
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
18
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
19
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
20
import eu.etaxonomy.cdm.model.description.TaxonDescription;
21
import eu.etaxonomy.cdm.model.description.TextData;
22
import eu.etaxonomy.cdm.model.name.ITaxonNameBase;
23
import eu.etaxonomy.cdm.model.name.TaxonName;
24
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
25
import eu.etaxonomy.cdm.model.reference.Reference;
26
import eu.etaxonomy.cdm.model.taxon.Synonym;
27
import eu.etaxonomy.cdm.model.taxon.Taxon;
28

    
29
/**
30
 * @author pkelbert
31
 * @date 20 déc. 2013
32
 *
33
 */
34
public class TaxonXAddSources {
35

    
36
    private Reference sourceUrlRef;
37
    private TaxonXImport importer;
38
    private TaxonXImportState configState;
39
    @SuppressWarnings("unused")
40
	private static final Logger logger = Logger.getLogger(TaxonXAddSources.class);
41

    
42
    /**
43
     * @param importer
44
     */
45
    public void setImporter(TaxonXImport importer) {
46
        this.importer=importer;
47
    }
48

    
49

    
50
    /**
51
     * @param configState the configState to set
52
     */
53
    public void setConfigState(TaxonXImportState configState) {
54
        this.configState = configState;
55
    }
56

    
57

    
58
    /**
59
     * @return the sourceUrlRef
60
     */
61
    public Reference getSourceUrlRef() {
62
        return sourceUrlRef;
63
    }
64

    
65
    /**
66
     * @param sourceUrlRef the sourceUrlRef to set
67
     */
68
    public void setSourceUrlRef(Reference sourceUrlRef) {
69
        this.sourceUrlRef = sourceUrlRef;
70
    }
71

    
72

    
73
    protected IdentifiableSource getIdentifiableSource(Reference reference, Set<IdentifiableSource> sources, boolean original){
74
//        logger.info("getIdentifiableSource");
75
        boolean sourceExists=false;
76
        IdentifiableSource source=null;
77
        for (IdentifiableSource src : sources){
78
            String micro = src.getCitationMicroReference();
79
            Reference r = src.getCitation();
80
            if (r.getTitleCache().equals(reference.getTitleCache())) {
81
                sourceExists=true;
82
                break;
83
            }
84
        }
85
        if(!sourceExists) {
86
            if(original) {
87
                source = IdentifiableSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource,null,null,reference,null);
88
            } else {
89
                source = IdentifiableSource.NewInstance(OriginalSourceType.Import,null,null,reference,null);
90
            }
91
        }
92
        return source;
93
    }
94

    
95
    protected DescriptionElementSource getDescriptionElementSource(Reference reference, Set<DescriptionElementSource> sources, boolean original){
96
        //logger.info("getDescriptionElementSource");
97
        boolean sourceExists=false;
98
        DescriptionElementSource source=null;
99
        for (DescriptionElementSource src : sources){
100
            String micro = src.getCitationMicroReference();
101
            Reference r = src.getCitation();
102
            if (r.getTitleCache().equals(reference.getTitleCache())) {
103
                sourceExists=true;
104
                break;
105
            }
106
        }
107
        if(!sourceExists) {
108
            if(original) {
109
                source = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource,null,null,reference,null);
110
            } else {
111
                source = DescriptionElementSource.NewInstance(OriginalSourceType.Import,null,null,reference,null);
112
            }
113
        }
114
        return source;
115

    
116
    }
117

    
118
    /**
119
     * @param refMods
120
     * @param synonym
121
     */
122
    protected void addSource(Reference refMods, Synonym synonym) {
123
        //logger.info("addSource");
124
        sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
125
        Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
126
        IdentifiableSource id = getIdentifiableSource(sourceUrlRef,synonym.getSources(), false);
127
        IdentifiableSource id2 = getIdentifiableSource(refMods,synonym.getSources(), true);
128
        IdentifiableSource id3 = getIdentifiableSource(sec,synonym.getSources(), false);
129
        if( id!=null) {
130
            synonym.addSource(id);
131
        }
132
        if( id2!=null) {
133
            synonym.addSource(id2);
134
        }
135
        if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
136
            synonym.addSource(id3);
137
        }
138
    }
139

    
140

    
141
    /**
142
     * @param refMods
143
     * @param indAssociation
144
     */
145
    protected IndividualsAssociation addSource(Reference refMods, IndividualsAssociation indAssociation) {
146
        //logger.info("addSource");
147
        sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
148
        Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
149
        DescriptionElementSource id = getDescriptionElementSource(sourceUrlRef, indAssociation.getSources(), false);
150
        DescriptionElementSource id2 = getDescriptionElementSource(refMods, indAssociation.getSources(), true);
151
        DescriptionElementSource id3 = getDescriptionElementSource(sec, indAssociation.getSources(), false);
152

    
153
        if(id!=null) {
154
            indAssociation.addSource(id);
155
        }
156
        if( id2!=null) {
157
            indAssociation.addSource(id2);
158
        }
159
        if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
160
            indAssociation.addSource(id3);
161
        }
162
        return indAssociation;
163
    }
164

    
165
    /**
166
     * @param refMods
167
     * @param acceptedTaxon
168
     */
169
    protected void addSource(Reference refMods, Taxon acceptedTaxon) {
170
        //logger.info("addSource");
171
        sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
172
        Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
173
        IdentifiableSource id = getIdentifiableSource(sourceUrlRef, acceptedTaxon.getSources(), false);
174
        IdentifiableSource id2 = getIdentifiableSource(refMods, acceptedTaxon.getSources(), true);
175
        IdentifiableSource id3 = getIdentifiableSource(sec, acceptedTaxon.getSources(), false);
176
        if( id!=null) {
177
            acceptedTaxon.addSource(id);
178
        }
179
        if( id2!=null) {
180
            acceptedTaxon.addSource(id2);
181
        }
182
        if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
183
            acceptedTaxon.addSource(id3);
184
        }
185
    }
186

    
187
    /**
188
     * @param refMods
189
     * @param nameToBeFilled
190
     */
191
    protected void addSource(Reference refMods, ITaxonNameBase nameToBeFilled) {
192
        //logger.info("addSource");
193
        sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
194
        Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
195
        IdentifiableSource id = getIdentifiableSource(sourceUrlRef, nameToBeFilled.getSources(), false);
196
        IdentifiableSource id2 = getIdentifiableSource(refMods, nameToBeFilled.getSources(), true);
197
        IdentifiableSource id3 = getIdentifiableSource(sec, nameToBeFilled.getSources(), false);
198

    
199
        if( id!=null) {
200
            nameToBeFilled.addSource(id);
201
        }
202
        if( id2!=null) {
203
            nameToBeFilled.addSource(id2);
204
        }
205
        if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
206
            nameToBeFilled.addSource(id3);
207
        }
208

    
209
    }
210

    
211
    /**
212
     * @param refMods
213
     * @param textData
214
     */
215
    protected void addSource(Reference refMods, TextData textData) {
216
        //logger.info("addSource");
217
        sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
218
        Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
219
        DescriptionElementSource id = getDescriptionElementSource(sourceUrlRef, textData.getSources(), false);
220
        DescriptionElementSource id2 = getDescriptionElementSource(refMods, textData.getSources(), true);
221
        DescriptionElementSource id3 = getDescriptionElementSource(sec, textData.getSources(), false);
222

    
223
        if( id!=null) {
224
            textData.addSource(id);
225
        }
226
        if( id2!=null) {
227
            textData.addSource(id2);
228
        }
229
        if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
230
            textData.addSource(id3);
231
        }
232

    
233
    }
234

    
235
    /**
236
     * @param refMods
237
     * @param taxonDescription
238
     * @param currentRef
239
     */
240
    protected void addAndSaveSource(Reference refMods, TaxonDescription taxonDescription, Reference currentRef) {
241
        //logger.info("addAndSaveSource");
242
        sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
243
        Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
244
        IdentifiableSource id = getIdentifiableSource(sourceUrlRef, taxonDescription.getSources(), false);
245
        IdentifiableSource id2 = getIdentifiableSource(refMods, taxonDescription.getSources(), true);
246
        IdentifiableSource id3 = getIdentifiableSource(sec, taxonDescription.getSources(), false);
247

    
248
        if( id!=null) {
249
            taxonDescription.addSource(id);
250
        }
251
        if( id2!=null) {
252
            taxonDescription.addSource(id2);
253
        }
254
        if(!configState.getConfig().doKeepOriginalSecundum() && id3!=null) {
255
            taxonDescription.addSource(id3);
256
        }
257

    
258
        importer.getDescriptionService().saveOrUpdate(taxonDescription);
259
    }
260

    
261
    /**
262
     * @param refMods
263
     * @param derivedUnitBase
264
     */
265
    protected void addAndSaveSource(Reference refMods, DerivedUnit derivedUnitBase) {
266
        //logger.info("addAndSaveSource");
267
        sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
268
        Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
269
        IdentifiableSource id = getIdentifiableSource(sourceUrlRef, derivedUnitBase.getSources(), false);
270
        IdentifiableSource id2 = getIdentifiableSource(refMods, derivedUnitBase.getSources(), true);
271
        IdentifiableSource id3 = getIdentifiableSource(sec, derivedUnitBase.getSources(), false);
272

    
273
        if( id!=null) {
274
            derivedUnitBase.addSource(id);
275
        }
276
        if( id2!=null) {
277
            derivedUnitBase.addSource(id2);
278
        }
279
        if(!configState.getConfig().doKeepOriginalSecundum() &&  id3!=null) {
280
            derivedUnitBase.addSource(id3);
281
        }
282
        importer.getOccurrenceService().saveOrUpdate(derivedUnitBase);
283
        derivedUnitBase= CdmBase.deproxy(derivedUnitBase, DerivedUnit.class);
284
    }
285

    
286

    
287
    /**
288
     * @param refMods
289
     * @param taxonDescription
290
     */
291
    public void addSource(Reference refMods, TaxonDescription taxonDescription) {
292
        //logger.info("addSource");
293
        sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
294
        Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
295
        IdentifiableSource id = getIdentifiableSource(sourceUrlRef, taxonDescription.getSources(), false);
296
        IdentifiableSource id2 = getIdentifiableSource(refMods, taxonDescription.getSources(), true);
297
        IdentifiableSource id3 = getIdentifiableSource(sec, taxonDescription.getSources(), false);
298

    
299
        if( id!=null) {
300
            taxonDescription.addSource(id);
301
        }
302
        if( id2!=null) {
303
            taxonDescription.addSource(id2);
304
        }
305
        if(!configState.getConfig().doKeepOriginalSecundum() &&  id3!=null) {
306
            taxonDescription.addSource(id3);
307
        }
308
        importer.getDescriptionService().saveOrUpdate(taxonDescription);
309

    
310
    }
311

    
312

    
313
    /**
314
     * @param reference
315
     * @param textData
316
     * @param name
317
     * @param ref
318
     */
319
    public void addSource(Reference reference, TextData textData, TaxonName name, Reference refMods) {
320
        //logger.info("addSource");
321
        sourceUrlRef=CdmBase.deproxy(sourceUrlRef, Reference.class);
322
        Reference sec = CdmBase.deproxy(configState.getConfig().getSecundum(), Reference.class);
323
        DescriptionElementSource id1 = getDescriptionElementSource(refMods, textData.getSources(),name, true);
324
        DescriptionElementSource id2 = getDescriptionElementSource(reference, textData.getSources(),name, false);
325
        if( id1!=null) {
326
            textData.addSource(id1);
327
        }
328

    
329
        if( id2!=null) {
330
            textData.addSource(id2);
331
        }
332

    
333
    }
334

    
335

    
336
    @SuppressWarnings({ "unused", "rawtypes" })
337
    private DescriptionElementSource getDescriptionElementSource(Reference reference, Set<DescriptionElementSource> sources,
338
            TaxonName originalname, boolean original){
339
        //logger.info("getDescriptionElementSource");
340
        boolean sourceExists=false;
341
        DescriptionElementSource source=null;
342
        for (DescriptionElementSource src : sources){
343
            String micro = src.getCitationMicroReference();
344
            Reference r = src.getCitation();
345
            TaxonName oname = src.getNameUsedInSource();
346
            try {
347
                if (r.getTitleCache().equals(reference.getTitleCache())) {
348
                    if (oname.getTitleCache().equalsIgnoreCase(originalname.getTitleCache())) {
349
                            sourceExists=true;
350
                            break;
351
                    }
352
                }
353
            } catch (Exception e) {
354
                // TODO Auto-generated catch block
355
                e.printStackTrace();
356
            }
357
        }
358
        if(!sourceExists) {
359
            if(original) {
360
                source = DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource, null,null, reference, null, originalname, null);
361
            } else {
362
                source = DescriptionElementSource.NewInstance(OriginalSourceType.Import, null,null, reference, null, originalname, null);
363
            }
364
        }
365
        return source;
366
    }
367

    
368

    
369

    
370
}
(1-1/9)