Project

General

Profile

Download (8.66 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.TaxonName;
37
import eu.etaxonomy.cdm.model.reference.Reference;
38
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
39
import eu.etaxonomy.cdm.model.taxon.Synonym;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
43

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

    
52
	private static final Logger logger = Logger.getLogger(GbifReferenceCsv2CdmConverter.class);
53

    
54
	private static final String CORE_ID = "coreId";
55

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

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

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

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

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

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

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

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

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

    
110

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

    
114

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

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

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

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

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

    
182
			}
183

    
184
		}
185

    
186

    
187
	}
188

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

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

    
218
	}
219

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

    
229

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

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

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

    
246

    
247
//********************** PARTITIONABLE **************************************/
248

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

    
260

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

    
268
//******************* TO STRING ******************************************/
269

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

    
275

    
276
}
(19-19/37)