Project

General

Profile

Download (13.7 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.TaxonName;
23
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
24
import eu.etaxonomy.cdm.model.reference.Reference;
25
import eu.etaxonomy.cdm.model.taxon.Synonym;
26
import eu.etaxonomy.cdm.model.taxon.Taxon;
27

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

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

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

    
48

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

    
56

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

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

    
71

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

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

    
115
    }
116

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

    
139

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

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

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

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

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

    
208
    }
209

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

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

    
232
    }
233

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

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

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

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

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

    
285

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

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

    
309
    }
310

    
311

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

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

    
332
    }
333

    
334

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

    
367

    
368

    
369
}
(1-1/9)