Project

General

Profile

Download (5.67 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.util.ArrayList;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16

    
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.io.common.TdwgAreaProvider;
20
import eu.etaxonomy.cdm.io.dwca.TermUri;
21
import eu.etaxonomy.cdm.io.stream.StreamItem;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.description.Distribution;
24
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
25
import eu.etaxonomy.cdm.model.description.TaxonDescription;
26
import eu.etaxonomy.cdm.model.location.NamedArea;
27
import eu.etaxonomy.cdm.model.reference.Reference;
28
import eu.etaxonomy.cdm.model.taxon.Taxon;
29

    
30
/**
31
 * @author a.mueller
32
 * @date 22.11.2011
33
 *
34
 */
35
public class GbifDistributionCsv2CdmConverter extends PartitionableConverterBase<DwcaDataImportConfiguratorBase, DwcaDataImportStateBase<DwcaDataImportConfiguratorBase>>
36
						implements IPartitionableConverter<StreamItem, IReader<CdmBase>, String>{
37

    
38
	@SuppressWarnings("unused")
39
	private static final Logger logger = Logger.getLogger(GbifDistributionCsv2CdmConverter.class);
40

    
41
	private static final String CORE_ID = "coreId";
42

    
43
	/**
44
	 * @param state
45
	 */
46
	public GbifDistributionCsv2CdmConverter(DwcaDataImportStateBase state) {
47
		super(state);
48
	}
49

    
50
	@Override
51
    public IReader<MappedCdmBase> map(StreamItem item ){
52
		List<MappedCdmBase> resultList = new ArrayList<MappedCdmBase>();
53

    
54
		Map<String, String> csv = item.map;
55
		Reference sourceReference = state.getTransactionalSourceReference();
56
		String sourceReferecenDetail = null;
57

    
58
		String id = getSourceId(item);
59
		Taxon taxon = getTaxonBase(id, item, Taxon.class, state);
60
		if (taxon != null){
61

    
62
			String locationId = item.get(TermUri.DWC_LOCATION_ID);
63
			NamedArea area = getAreaByLocationId(item, locationId);
64
			if (area != null){
65
				MappedCdmBase  mcb = new MappedCdmBase(item.term, csv.get(TermUri.DWC_LOCATION_ID), area);
66
				resultList.add(mcb);
67
			}else if (! config.isExcludeLocality()){
68
				String locality = item.get(TermUri.DWC_LOCALITY);
69
				area = getAreaByLocality(item, locality);
70
				MappedCdmBase  mcb = new MappedCdmBase(item.term, csv.get(TermUri.DWC_LOCALITY), area);
71
				resultList.add(mcb);
72
			}
73

    
74
			if (area != null){
75

    
76
				//TODO language, area,
77
				TaxonDescription desc = getTaxonDescription(taxon, false);
78

    
79
				//TODO
80
				PresenceAbsenceTerm status = null;
81
				Distribution distribution = Distribution.NewInstance(area, status);
82
				desc.addElement(distribution);
83

    
84
				//save taxon
85
				MappedCdmBase  mcb = new MappedCdmBase(item.term, csv.get(CORE_ID), taxon);
86
				resultList.add(mcb);
87
			}
88

    
89
		}else{
90
			String message = "Can't retrieve taxon from database for id '%s'";
91
			fireWarningEvent(String.format(message, id), item, 12);
92
		}
93

    
94
		//return
95
		return new ListReader<MappedCdmBase>(resultList);
96
	}
97

    
98

    
99

    
100
	private NamedArea getAreaByLocality(StreamItem item, String locality) {
101
		String namespace = TermUri.DWC_LOCALITY.toString();
102
		List<NamedArea> result = state.get(namespace, locality, NamedArea.class);
103
		if (result.isEmpty()){
104
			NamedArea newArea = NamedArea.NewInstance(locality, locality, locality);
105
			newArea.setTitleCache(locality, true);
106
			return newArea;
107
		}
108
		if (result.size() > 1){
109
			String message = "There is more than 1 cdm entity matching given locality '%s'. I take an arbitrary one.";
110
			fireWarningEvent(String.format(message, locality), item, 4);
111
		}
112
		return result.iterator().next();
113
	}
114

    
115
	private NamedArea getAreaByLocationId(StreamItem item, String locationId) {
116
		String namespace = TermUri.DWC_LOCATION_ID.toString();
117
		List<NamedArea> result = state.get(namespace, locationId, NamedArea.class);
118
		if (result.isEmpty()){
119
			//try to find in cdm
120
			NamedArea newArea = TdwgAreaProvider.getAreaByTdwgAbbreviation(locationId);
121
			if (newArea == null){
122
//				state.getCurrentIO().getTermService().findByAreaCode
123
			}
124
			if (newArea == null){
125
				newArea = NamedArea.NewInstance(locationId, locationId, locationId);
126
			}
127

    
128
			return newArea;
129
		}
130
		if (result.size() > 1){
131
			String message = "There is more than 1 cdm entity matching given locationId '%s'. I take an arbitrary one.";
132
			fireWarningEvent(String.format(message, locationId), item, 4);
133
		}
134
		return result.iterator().next();
135
	}
136

    
137
	@Override
138
	public String getSourceId(StreamItem item) {
139
		String id = item.get(CORE_ID);
140
		return id;
141
	}
142

    
143

    
144
//********************** PARTITIONABLE **************************************/
145

    
146
	@Override
147
	protected void makeForeignKeysForItem(StreamItem item, Map<String, Set<String>> fkMap) {
148
		String value;
149
		String key;
150
		//taxon
151
		if ( hasValue(value = item.get(CORE_ID))){
152
			key = TermUri.DWC_TAXON.toString();
153
			Set<String> keySet = getKeySet(key, fkMap);
154
			keySet.add(value);
155
		}
156

    
157
		//areaId
158

    
159
		String locationId = item.get(TermUri.DWC_LOCATION_ID);
160
		if ( hasValue(value = locationId)){
161
			key = TermUri.DWC_LOCATION_ID.toString();
162
			Set<String> keySet = getKeySet(key, fkMap);
163
			keySet.add(value);
164
		}
165

    
166
	}
167

    
168

    
169
	@Override
170
	public Set<String> requiredSourceNamespaces() {
171
		Set<String> result = new HashSet<String>();
172
 		result.add(TermUri.DWC_TAXON.toString());
173
 		result.add(TermUri.DWC_LOCATION_ID.toString());
174
 		return result;
175
	}
176

    
177
//******************* TO STRING ******************************************/
178

    
179
	@Override
180
	public String toString(){
181
		return this.getClass().getName();
182
	}
183

    
184

    
185
}
(17-17/37)