Project

General

Profile

Download (6.49 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
import java.util.UUID;
17

    
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
21
import eu.etaxonomy.cdm.io.stream.IPartitionableConverter;
22
import eu.etaxonomy.cdm.io.stream.IReader;
23
import eu.etaxonomy.cdm.io.stream.ListReader;
24
import eu.etaxonomy.cdm.io.stream.MappedCdmBase;
25
import eu.etaxonomy.cdm.io.stream.PartitionableConverterBase;
26
import eu.etaxonomy.cdm.io.stream.StreamItem;
27
import eu.etaxonomy.cdm.io.stream.terms.TermUri;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.common.Language;
30
import eu.etaxonomy.cdm.model.description.Feature;
31
import eu.etaxonomy.cdm.model.description.TaxonDescription;
32
import eu.etaxonomy.cdm.model.description.TextData;
33
import eu.etaxonomy.cdm.model.media.Rights;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36

    
37
/**
38
 * @author a.mueller
39
 * @since 22.11.2011
40
 */
41
public class GbifDescriptionCsv2CdmConverter extends PartitionableConverterBase<DwcaDataImportConfiguratorBase, DwcaDataImportStateBase<DwcaDataImportConfiguratorBase>>
42
						implements IPartitionableConverter<StreamItem, IReader<CdmBase>, String>{
43

    
44
	@SuppressWarnings("unused")
45
	private static final Logger logger = Logger.getLogger(GbifDescriptionCsv2CdmConverter.class);
46

    
47
	private static final String CORE_ID = "coreId";
48

    
49
	public GbifDescriptionCsv2CdmConverter(DwcaDataImportStateBase state) {
50
		super(state);
51
	}
52

    
53
	@Override
54
    public IReader<MappedCdmBase<? extends CdmBase>> map(StreamItem item ){
55
		List<MappedCdmBase<? extends CdmBase>> resultList = new ArrayList<>();
56

    
57
		Map<String, String> csv = item.map;
58
		Reference sourceReference = state.getTransactionalSourceReference();
59
		String sourceReferecenDetail = null;
60

    
61
		String id = getSourceId(item);
62
		Taxon taxon = getTaxonBase(id, item, Taxon.class, state);
63
		if (taxon != null){
64

    
65
			String description = item.get(TermUri.DC_DESCRIPTION);
66
			//String license = item.get(TermUri.DC_LICENSE);//lorna check - often empty in SP dwca
67

    
68
			Language language = getDcLanguage(item, resultList);
69
			if(language == null){
70
			    language = Language.UNKNOWN_LANGUAGE();
71
			}
72

    
73
			if (isNotBlank(description)){
74
				Feature feature = getFeatureByDcType(item, resultList);
75

    
76
				TaxonDescription taxonDescription = getTaxonDescription(taxon, false);
77

    
78
				//deduplicate - if this taxonDescription already got these Rights attached
79
				boolean addRights = true;
80
				String rights = item.get(TermUri.DC_RIGHTS);
81
				Set<Rights> allRights = taxonDescription.getRights();
82
				for (Rights r : allRights) {
83
					if (r.getText() == rights) {
84
						addRights = false;
85
					}
86
				}
87

    
88
				if (addRights && (isNotBlank(rights))) {
89
					Rights copyright = Rights.NewInstance(rights, language);
90
					taxonDescription.addRights(copyright);
91
				}
92

    
93
				TextData descElement = TextData.NewInstance(feature);
94

    
95
				//Language language = getLanguage(item);  //TODO
96
				descElement.putText(language,description);
97
				taxonDescription.addElement(descElement);
98
			}else{
99
				String message = "Description is empty. Description item will not be imported.";
100
				fireWarningEvent(message, item, 4);
101
			}
102

    
103
			MappedCdmBase<? extends CdmBase>  mcb = new MappedCdmBase<>(item.term, csv.get(CORE_ID), taxon);
104
			resultList.add(mcb);
105
		}else{
106
			String message = "Taxon is not available for id '%s'";
107
			message = String.format(message, id);
108
			fireWarningEvent(message, item, 12);
109
		}
110

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

    
114
	/**
115
	 * Determines the feature by the dc:type attribute. Tries to reuse existing
116
	 * features.
117
	 *
118
	 * @param item
119
	 * @param resultList
120
	 * @return
121
	 */
122
	private Feature getFeatureByDcType(StreamItem item, List<MappedCdmBase<? extends CdmBase>> resultList) {
123
		String descriptionType = item.get(TermUri.DC_TYPE);
124
		item.remove(TermUri.DC_TYPE);
125

    
126
		try {
127
			Feature feature = state.getTransformer().getFeatureByKey(descriptionType);
128
			if (feature != null){
129
				return feature;
130
			}
131
			String namespace = Feature.class.getCanonicalName();
132
			List<Feature> features = state.get(namespace, descriptionType, Feature.class);
133
			if (features.size() > 1){
134
				String message = "There is more than 1 cdm entity matching given locationId '%s'. I take an arbitrary one.";
135
				fireWarningEvent(String.format(message, item), item, 4);
136
				return features.iterator().next();
137
			}
138
			UUID featureUuid = state.getTransformer().getFeatureUuid(descriptionType);
139
			feature = state.getCurrentIO().getFeature(state, featureUuid, descriptionType, descriptionType, null, null);
140
			if (feature == null){
141
				feature = Feature.NewInstance(descriptionType, descriptionType, null);
142
				feature.setSupportsTextData(true);
143
//				state.putMapping(namespace, type, feature);
144
				state.getCurrentIO().saveNewTerm(feature);
145
				MappedCdmBase<? extends CdmBase>  mcb = new MappedCdmBase<>(namespace, descriptionType, feature);
146
				resultList.add(mcb);
147
			}
148
			return feature;
149
		} catch (UndefinedTransformerMethodException e) {
150
			String message = "GetFeature not yet supported by DwcA-Transformer. This should not have happend. Please contact your application developer.";
151
			fireWarningEvent(message, item, 8);
152
			return null;
153
		}
154
	}
155

    
156
	@Override
157
	public String getSourceId(StreamItem item) {
158
		String id = item.get(CORE_ID);
159
		return id;
160
	}
161

    
162
//********************** PARTITIONABLE **************************************/
163

    
164
	@Override
165
	protected void makeForeignKeysForItem(StreamItem item, Map<String, Set<String>> fkMap) {
166
		String value;
167
		String key;
168
		if ( hasValue(value = item.get(CORE_ID))){
169
			key = TermUri.DWC_TAXON.toString();
170
			Set<String> keySet = getKeySet(key, fkMap);
171
			keySet.add(value);
172
		}
173
	}
174

    
175
	@Override
176
	public Set<String> requiredSourceNamespaces() {
177
		Set<String> result = new HashSet<>();
178
 		result.add(TermUri.DWC_TAXON.toString());
179
 		result.add(TermUri.DC_LANGUAGE.toString());
180
        return result;
181
	}
182

    
183
//******************* TO STRING ******************************************/
184

    
185
	@Override
186
	public String toString(){
187
		return this.getClass().getName();
188
	}
189
}
(12-12/17)