Project

General

Profile

Download (8.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.dwca.in;
10

    
11
import java.net.URI;
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17

    
18
import org.apache.commons.lang.StringUtils;
19
import org.apache.log4j.Logger;
20

    
21
import com.ibm.lsid.MalformedLSIDException;
22

    
23
import eu.etaxonomy.cdm.io.stream.IPartitionableConverter;
24
import eu.etaxonomy.cdm.io.stream.IReader;
25
import eu.etaxonomy.cdm.io.stream.ListReader;
26
import eu.etaxonomy.cdm.io.stream.MappedCdmBase;
27
import eu.etaxonomy.cdm.io.stream.PartitionableConverterBase;
28
import eu.etaxonomy.cdm.io.stream.StreamItem;
29
import eu.etaxonomy.cdm.io.stream.terms.TermUri;
30
import eu.etaxonomy.cdm.model.agent.Team;
31
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
32
import eu.etaxonomy.cdm.model.common.CdmBase;
33
import eu.etaxonomy.cdm.model.common.LSID;
34
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
35
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
36
import eu.etaxonomy.cdm.model.description.Feature;
37
import eu.etaxonomy.cdm.model.description.TaxonDescription;
38
import eu.etaxonomy.cdm.model.description.TextData;
39
import eu.etaxonomy.cdm.model.name.INonViralName;
40
import eu.etaxonomy.cdm.model.name.IZoologicalName;
41
import eu.etaxonomy.cdm.model.name.TaxonName;
42
import eu.etaxonomy.cdm.model.reference.Reference;
43
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
44
import eu.etaxonomy.cdm.model.taxon.Synonym;
45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
47
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
48

    
49
/**
50
 * @author a.mueller
51
 * @since 22.11.2011
52
 */
53
public class GbifReferenceCsv2CdmConverter extends PartitionableConverterBase<DwcaDataImportConfiguratorBase, DwcaDataImportStateBase<DwcaDataImportConfiguratorBase>>
54
						implements IPartitionableConverter<StreamItem, IReader<CdmBase>, String>{
55

    
56
	private static final Logger logger = Logger.getLogger(GbifReferenceCsv2CdmConverter.class);
57

    
58
	private static final String CORE_ID = "coreId";
59

    
60
	public GbifReferenceCsv2CdmConverter(DwcaDataImportStateBase state) {
61
		super(state);
62
	}
63

    
64
	@Override
65
    public IReader<MappedCdmBase<? extends CdmBase>> map(StreamItem item ){
66
		List<MappedCdmBase<? extends CdmBase>> resultList = new ArrayList<>();
67

    
68
		Map<String, String> csv = item.map;
69
		Reference sourceReference = state.getTransactionalSourceReference();
70
		String sourceReferecenDetail = null;
71

    
72
		String id = getSourceId(item);
73
		TaxonBase<?> taxon = getTaxonBase(id, item, TaxonBase.class, state);
74
		if (isNotBlank(id) && taxon == null){
75
			String message = "Taxon for id %s could not be found";
76
			message = String.format(message, id);
77
			fireWarningEvent(message, item, 8);
78
		}
79

    
80
		String strCreator = getValue(item, TermUri.DC_CREATOR);
81
		String strDate = getValue(item, TermUri.DC_DATE);
82
		String strTitle = getValue(item, TermUri.DC_TITLE);
83
		String strSource = getValue(item, TermUri.DC_SOURCE);
84
		String strIdentifier = getValue(item, TermUri.DC_IDENTIFIER);
85
		String strType = getValue(item, TermUri.DC_TYPE);
86

    
87
		Reference reference = ReferenceFactory.newGeneric();
88
		resultList.add(new MappedCdmBase<CdmBase>(reference));
89

    
90
		//author
91
		TeamOrPersonBase<?> author = handleCreator(strCreator);
92
		reference.setAuthorship(author);
93
		//date
94
		VerbatimTimePeriod publicationDate = handleDate(strDate);
95
		reference.setDatePublished(publicationDate);
96
		//title
97
		reference.setTitle(strTitle);
98
		//inreference
99
		Reference inRef = handleInRef(strSource);
100
		if (inRef != null){
101
			reference.setInReference(inRef);
102
			resultList.add(new MappedCdmBase<>(inRef));
103
		}
104

    
105
		//URI
106
		handleIdentifier(strIdentifier, reference);
107

    
108
		//type
109
		handleType(reference, strType, taxon, resultList, item);
110

    
111
		return new ListReader<>(resultList);
112
	}
113

    
114
	private void handleType(Reference reference, String strType, TaxonBase<?> taxon,
115
	        List<MappedCdmBase<? extends CdmBase>> resultList, StreamItem item) {
116
		// TODO handleType not yet implemented
117

    
118
		if (taxon == null){
119
			String message = "Taxon is null. Reference not imported.";
120
			fireWarningEvent(message,item, 4);
121
			//do nothing
122
		}else{
123
			boolean isNomRef = false;
124
			if (isNotBlank(strType)){
125
				if (strType.matches("Botanical Protologue")){
126
					if (taxon.getName() != null && reference != null && taxon.getName().isNonViral()){
127
						INonViralName nvn = taxon.getName();
128
						nvn.setNomenclaturalReference(reference);
129
						isNomRef = true;
130
					}else{
131
						//TODO
132
					}
133
				}
134
			}
135

    
136
			//guess a nom ref
137
			if (isNomRef == false && config.isGuessNomenclaturalReferences()){
138
				//if reference equals in author and year we assume that it is the nom ref
139
				//this information is usually only available for ICZN names
140
				if (taxon.getName() != null && reference != null && taxon.getName().isNonViral()){
141
					INonViralName nvn = taxon.getName();
142
					String taxonAuthor = nvn.getAuthorshipCache();
143
					String refAuthor = reference.getAuthorship().getNomenclaturalTitle();
144
					Integer combYear = null;
145
					Integer origYear = null;
146
					if (nvn.isZoological()){
147
						IZoologicalName zooName = (IZoologicalName)CdmBase.deproxy(nvn);
148
						combYear = zooName.getPublicationYear();
149
						origYear = zooName.getOriginalPublicationYear();
150
					}
151
					String refYear = reference.getYear();
152

    
153
					//combination compare
154
					if (taxonAuthor != null && taxonAuthor.equals(refAuthor)){
155
						if (combYear != null && String.valueOf(combYear).equals(refYear)){
156
							//is nom Ref
157
							isNomRef = true;
158
							nvn.setNomenclaturalReference(reference);
159
						}else if (origYear != null && String.valueOf(origYear).equals(refYear)){
160
							//TODO not yet handled by CDM
161
						}
162
					}
163
				}
164
			}
165
			if (config.isHandleAllRefsAsCitation()){
166
				if (taxon.isInstanceOf(Taxon.class)){
167
					TaxonDescription desc = getTaxonDescription(CdmBase.deproxy(taxon, Taxon.class), false);
168
					createCitation(desc, reference, taxon.getName());
169
					resultList.add(new MappedCdmBase<CdmBase>(desc));
170
				}else if (taxon.isInstanceOf(Synonym.class)){
171
					Synonym syn = CdmBase.deproxy(taxon, Synonym.class);
172
					Taxon tax = syn.getAcceptedTaxon();
173
					if (tax != null){
174
    					TaxonDescription desc = getTaxonDescription(tax, false);
175
    					createCitation(desc, reference, syn.getName());
176
    					resultList.add(new MappedCdmBase<CdmBase>(desc));
177
					}
178
				}
179
			}
180
		}
181
	}
182

    
183
	private void createCitation(TaxonDescription desc, Reference ref, TaxonName nameUsedInSource) {
184
		Feature feature = Feature.CITATION();
185
		TextData textData = TextData.NewInstance(feature);
186
		DescriptionElementSource source = DescriptionElementSource.NewPrimarySourceInstance(ref, null, nameUsedInSource, null);
187
		textData.addSource(source);
188
		desc.addElement(textData);
189
	}
190

    
191
	private void handleIdentifier(String strIdentifier, Reference reference) {
192
		if (StringUtils.isBlank(strIdentifier)){
193
			return;
194
		}else if (LSID.isLsid(strIdentifier)){
195
			LSID lsid;
196
			try {
197
				lsid = new LSID(strIdentifier);
198
				reference.setLsid(lsid);
199
			} catch (MalformedLSIDException e) {
200
				//TODO should not happen as we have checked before
201
				throw new RuntimeException(e);
202
			}
203
		}
204
		try {
205
			URI uri = URI.create(strIdentifier);
206
			reference.setUri(uri);
207
		} catch (Exception e) {
208
			logger.debug("Reference is not an URI");
209
		}
210
		//TODO further identifier types
211
	}
212

    
213
	private Reference handleInRef(String strSource) {
214
		if (StringUtils.isBlank(strSource)){
215
			return null;
216
		}else{
217
			Reference inRef = ReferenceFactory.newGeneric();
218
			return inRef;
219
		}
220
	}
221

    
222
	private VerbatimTimePeriod handleDate(String strDate) {
223
	    VerbatimTimePeriod tp = TimePeriodParser.parseStringVerbatim(strDate);
224
		return tp;
225
	}
226

    
227
	private TeamOrPersonBase<?> handleCreator(String strCreator) {
228
		Team team = Team.NewTitledInstance(strCreator, strCreator);
229
		return team;
230
	}
231

    
232
	@Override
233
	public String getSourceId(StreamItem item) {
234
		String id = item.get(CORE_ID);
235
		return id;
236
	}
237

    
238
//********************** PARTITIONABLE **************************************/
239

    
240
	@Override
241
	protected void makeForeignKeysForItem(StreamItem item, Map<String, Set<String>> fkMap) {
242
		String value;
243
		String key;
244
		if ( hasValue(value = item.get(CORE_ID))){
245
			key = TermUri.DWC_TAXON.toString();
246
			Set<String> keySet = getKeySet(key, fkMap);
247
			keySet.add(value);
248
		}
249
	}
250

    
251
	@Override
252
	public Set<String> requiredSourceNamespaces() {
253
		Set<String> result = new HashSet<>();
254
 		result.add(TermUri.DWC_TAXON.toString());
255
 		return result;
256
	}
257

    
258
//******************* TO STRING ******************************************/
259

    
260
	@Override
261
	public String toString(){
262
		return this.getClass().getName();
263
	}
264
}
(15-15/17)