Project

General

Profile

Download (8.79 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.dwca.TermUri;
24
import eu.etaxonomy.cdm.io.stream.StreamItem;
25
import eu.etaxonomy.cdm.model.agent.Team;
26
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.LSID;
29
import eu.etaxonomy.cdm.model.common.TimePeriod;
30
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
31
import eu.etaxonomy.cdm.model.description.Feature;
32
import eu.etaxonomy.cdm.model.description.TaxonDescription;
33
import eu.etaxonomy.cdm.model.description.TextData;
34
import eu.etaxonomy.cdm.model.name.INonViralName;
35
import eu.etaxonomy.cdm.model.name.IZoologicalName;
36
import eu.etaxonomy.cdm.model.name.NonViralName;
37
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
38
import eu.etaxonomy.cdm.model.name.ZoologicalName;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
41
import eu.etaxonomy.cdm.model.taxon.Synonym;
42
import eu.etaxonomy.cdm.model.taxon.Taxon;
43
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
44
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
45

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

    
54
	private static final Logger logger = Logger.getLogger(GbifReferenceCsv2CdmConverter.class);
55

    
56
	private static final String CORE_ID = "coreId";
57

    
58
	/**
59
	 * @param state
60
	 */
61
	public GbifReferenceCsv2CdmConverter(DwcaDataImportStateBase state) {
62
		super(state);
63
	}
64

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

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

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

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

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

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

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

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

    
112

    
113
		return new ListReader<MappedCdmBase>(resultList);
114
	}
115

    
116

    
117
	private void handleType(Reference reference, String strType, TaxonBase<?> taxon, List<MappedCdmBase> resultList, StreamItem item) {
118
		// TODO handleType not yet implemented
119

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

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

    
155
					//combination compare
156
					if (taxonAuthor != null && taxonAuthor.equals(refAuthor)){
157
						if (combYear != null && String.valueOf(combYear).equals(refYear)){
158
							//is nom Ref
159
							isNomRef = true;
160
							nvn.setNomenclaturalReference(reference);
161
						}else if (origYear != null && String.valueOf(origYear).equals(refYear)){
162
							//TODO not yet handled by CDM
163
						}
164
					}
165

    
166
				}
167
			}
168
			if (config.isHandleAllRefsAsCitation()){
169
				if (taxon.isInstanceOf(Taxon.class)){
170
					TaxonDescription desc = getTaxonDescription(CdmBase.deproxy(taxon, Taxon.class), false);
171
					createCitation(desc, reference, taxon.getName());
172
					resultList.add(new MappedCdmBase<CdmBase>(desc));
173
				}else if (taxon.isInstanceOf(Synonym.class)){
174
					Synonym syn = CdmBase.deproxy(taxon, Synonym.class);
175
					Taxon tax = syn.getAcceptedTaxon();
176
					if (tax != null){
177
    					TaxonDescription desc = getTaxonDescription(tax, false);
178
    					createCitation(desc, reference, syn.getName());
179
    					resultList.add(new MappedCdmBase<CdmBase>(desc));
180
					}
181
				}
182

    
183
			}
184

    
185
		}
186

    
187

    
188
	}
189

    
190
	private void createCitation(TaxonDescription desc, Reference ref, TaxonNameBase nameUsedInSource) {
191
		Feature feature = Feature.CITATION();
192
		TextData textData = TextData.NewInstance(feature);
193
		DescriptionElementSource source = DescriptionElementSource.NewPrimarySourceInstance(ref, null, nameUsedInSource, null);
194
		textData.addSource(source);
195
		desc.addElement(textData);
196
	}
197

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

    
219
	}
220

    
221
	private Reference handleInRef(String strSource) {
222
		if (StringUtils.isBlank(strSource)){
223
			return null;
224
		}else{
225
			Reference inRef = ReferenceFactory.newGeneric();
226
			return inRef;
227
		}
228
	}
229

    
230

    
231
	private TimePeriod handleDate(String strDate) {
232
		TimePeriod tp = TimePeriodParser.parseString(strDate);
233
		return tp;
234
	}
235

    
236
	private TeamOrPersonBase handleCreator(String strCreator) {
237
		Team team = Team.NewTitledInstance(strCreator, strCreator);
238
		return team;
239
	}
240

    
241
	@Override
242
	public String getSourceId(StreamItem item) {
243
		String id = item.get(CORE_ID);
244
		return id;
245
	}
246

    
247

    
248
//********************** PARTITIONABLE **************************************/
249

    
250
	@Override
251
	protected void makeForeignKeysForItem(StreamItem item, Map<String, Set<String>> fkMap) {
252
		String value;
253
		String key;
254
		if ( hasValue(value = item.get(CORE_ID))){
255
			key = TermUri.DWC_TAXON.toString();
256
			Set<String> keySet = getKeySet(key, fkMap);
257
			keySet.add(value);
258
		}
259
	}
260

    
261

    
262
	@Override
263
	public Set<String> requiredSourceNamespaces() {
264
		Set<String> result = new HashSet<String>();
265
 		result.add(TermUri.DWC_TAXON.toString());
266
 		return result;
267
	}
268

    
269
//******************* TO STRING ******************************************/
270

    
271
	@Override
272
	public String toString(){
273
		return this.getClass().getName();
274
	}
275

    
276

    
277
}
(19-19/37)