Project

General

Profile

Download (6.27 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.dwca.TermUri;
22
import eu.etaxonomy.cdm.io.stream.StreamItem;
23
import eu.etaxonomy.cdm.model.common.CdmBase;
24
import eu.etaxonomy.cdm.model.common.Language;
25
import eu.etaxonomy.cdm.model.description.Feature;
26
import eu.etaxonomy.cdm.model.description.TaxonDescription;
27
import eu.etaxonomy.cdm.model.description.TextData;
28
import eu.etaxonomy.cdm.model.media.Rights;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31

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

    
40
	@SuppressWarnings("unused")
41
	private static final Logger logger = Logger.getLogger(GbifDescriptionCsv2CdmConverter.class);
42

    
43
	private static final String CORE_ID = "coreId";
44

    
45
	/**
46
	 * @param state
47
	 */
48
	public GbifDescriptionCsv2CdmConverter(DwcaDataImportStateBase state) {
49
		super(state);
50
	}
51

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

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

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

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

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

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

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

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

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

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

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

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

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

    
113

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

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

    
154
	}
155

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

    
162

    
163
//********************** PARTITIONABLE **************************************/
164

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

    
176

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

    
185
//******************* TO STRING ******************************************/
186

    
187
	@Override
188
	public String toString(){
189
		return this.getClass().getName();
190
	}
191

    
192

    
193
}
(16-16/37)