Project

General

Profile

Revision 441eb4b5

ID441eb4b50abaa234de15f9ad635d0d8bb4b5bddb
Parent 44fc1b63
Child cf60f92a

Added by Andreas Müller about 3 years ago

ref #6887 handle languages in DwCA CoL import

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/in/DwcaImportTransformer.java
16 16
import eu.etaxonomy.cdm.io.common.mapping.InputTransformerBase;
17 17
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
18 18
import eu.etaxonomy.cdm.model.description.Feature;
19
import eu.etaxonomy.cdm.model.location.NamedArea;
19 20

  
20 21
/**
21 22
 * @author a.mueller
......
94 95
		return null;
95 96
	}
96 97

  
98
    @Override
99
    public NamedArea getNamedAreaByKey(String key) throws UndefinedTransformerMethodException {
100
        if (key == null){
101
            return null;
102
        }else if (key.equalsIgnoreCase("xyz")){
103
            return null;
104
        }
105
        return null;
106
    }
107

  
108
    @Override
109
    public UUID getNamedAreaUuid(String key) throws UndefinedTransformerMethodException {
110
        if (key == null){
111
            return null;
112
        }else if (key.equalsIgnoreCase("xyz")){
113
            return null;
114
        }
115
        return null;
116
    }
117

  
97 118

  
98 119
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/in/GbifDescriptionCsv2CdmConverter.java
15 15
import java.util.Set;
16 16
import java.util.UUID;
17 17

  
18
import org.apache.commons.lang.StringUtils;
19 18
import org.apache.log4j.Logger;
20 19

  
21 20
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
......
65 64
			String description = item.get(TermUri.DC_DESCRIPTION);
66 65
			//String license = item.get(TermUri.DC_LICENSE);//lorna check - often empty in SP dwca
67 66

  
68
			//TODO: Create the Language from the TermUri.DC_LANGUAGE in the dwca
69
			Language language = getLanguage(item);
70

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

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

  
75 75
				TaxonDescription taxonDescription = getTaxonDescription(taxon, false);
......
111 111
	}
112 112

  
113 113

  
114
	private Language getLanguage(StreamItem item) {
115
		//TODO
116

  
117
		Language language = Language.DEFAULT();
118
		String langString = item.get(TermUri.DC_LANGUAGE);
119

  
120
		/*if (langString != null) {
121
			if (!langString.equals("")) {
122
				language = getTermService().getLanguageByIso(langString.substring(0, 2));
123
				//can getTermService from StreamImportBase which calls GbifDescriptionCsv2CdmConverter.map
124
			}
125
		}*/
126
		return language;
127
	}
128

  
129 114
	/**
130 115
	 * Determines the feature by the dc:type attribute. Tries to reuse existing
131 116
	 * features.
......
193 178
	public Set<String> requiredSourceNamespaces() {
194 179
		Set<String> result = new HashSet<String>();
195 180
 		result.add(TermUri.DWC_TAXON.toString());
196
 		return result;
181
 		result.add(TermUri.DC_LANGUAGE.toString());
182
        return result;
197 183
	}
198 184

  
199 185
//******************* TO STRING ******************************************/
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/in/GbifVernacularNameCsv2CdmConverter.java
59 59
		if (taxon != null){
60 60
			MappedCdmBase<? extends CdmBase>  mcb = new MappedCdmBase<>(item.term, csv.get(CORE_ID), taxon);
61 61
			String vernacular = item.get(TermUri.DWC_VERNACULAR_NAME);
62
			//TODO language, area,
63 62
			TaxonDescription desc = getTaxonDescription(taxon, false);
64 63

  
65
			//TODO
66
			Language language = null;
64
			//TODO area,
65
			Language language = getDcLanguage(item, resultList);
66

  
67 67
			CommonTaxonName commonName = CommonTaxonName.NewInstance(vernacular, language);
68 68
			desc.addElement(commonName);
69 69
			resultList.add(mcb);
......
101 101

  
102 102
	@Override
103 103
	public final Set<String> requiredSourceNamespaces() {
104
		Set<String> result = new HashSet<String>();
104
		Set<String> result = new HashSet<>();
105 105
 		result.add(TermUri.DWC_TAXON.toString());
106
 		return result;
106
 		result.add(TermUri.DC_LANGUAGE.toString());
107
        return result;
107 108
	}
108 109

  
109 110
//************************ STRING ************************************************/
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/in/PartitionableConverterBase.java
25 25
import eu.etaxonomy.cdm.io.stream.StreamImportStateBase;
26 26
import eu.etaxonomy.cdm.io.stream.StreamItem;
27 27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.Language;
28 29
import eu.etaxonomy.cdm.model.description.TaxonDescription;
29 30
import eu.etaxonomy.cdm.model.taxon.Taxon;
30 31
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
......
204 205
		return result;
205 206
	}
206 207

  
208

  
209
    protected Language getDcLanguage(StreamItem item, List<MappedCdmBase<? extends CdmBase>> resultList ) {
210
        String namespace = TermUri.DC_LANGUAGE.toString();
211
        String languageStr = item.get(TermUri.DC_LANGUAGE);
212
        if (isBlank(languageStr)){
213
            return null;
214
        }
215

  
216
        List<Language> list = state.get(namespace, languageStr, Language.class);
217
        if (list.isEmpty()){
218
            //try to find in cdm
219
            Language lang = Language.getLanguageByIsoCode(languageStr);
220
            if (lang == null){
221
                 lang = Language.getLanguageByLabel(languageStr);
222
            }
223
            if (lang == null){
224
                lang = state.getCurrentIO().getLanguage(state, null, languageStr, languageStr, languageStr, null);
225
            }
226
            state.putMapping(namespace, languageStr, lang);
227
            list.add(lang);
228
//            MappedCdmBase<? extends CdmBase> languageMcb = new MappedCdmBase<>(TermUri.DC_LANGUAGE, languageStr, lang);
229
//            resultList.add(languageMcb);
230
        }
231
        if (list.size() > 1){
232
            String message = "There is more than 1 cdm entity matching given language '%s'. I take an arbitrary one.";
233
            fireWarningEvent(String.format(message, languageStr), item, 4);
234
        }
235
        Language result = list.iterator().next();
236
        return result;
237
    }
238

  
207 239
	protected boolean isNotBlank(String str){
208 240
		return StringUtils.isNotBlank(str);
209 241
	}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/in/StreamPartitioner.java
20 20
import eu.etaxonomy.cdm.io.stream.StreamImportConfiguratorBase;
21 21
import eu.etaxonomy.cdm.io.stream.StreamImportStateBase;
22 22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
23 24
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
24 25
import eu.etaxonomy.cdm.model.reference.Reference;
25 26

  
......
111 112
	 */
112 113
	private void addItemToRelatedObjects(MappedCdmBase<? extends CdmBase> item) {
113 114
		CdmBase cdmBase = item.getCdmBase();
114
		if (cdmBase.getId() == 0){
115
		if (cdmBase.getId() == 0 || cdmBase.isInstanceOf(DefinedTermBase.class)){
115 116
			if (cdmBase.isInstanceOf(IdentifiableEntity.class)){
116 117
			    IdentifiableEntity<?> identifiableEntity = CdmBase.deproxy(cdmBase, IdentifiableEntity.class);
117 118
			    Set<String> requiredSourceNamespaces = converter.requiredSourceNamespaces();
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/stream/StreamImportBase.java
27 27
import eu.etaxonomy.cdm.model.common.CdmBase;
28 28
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
29 29
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
30
import eu.etaxonomy.cdm.model.common.Language;
30 31
import eu.etaxonomy.cdm.model.common.TermVocabulary;
31 32
import eu.etaxonomy.cdm.model.description.DescriptionBase;
32 33
import eu.etaxonomy.cdm.model.description.Feature;
34
import eu.etaxonomy.cdm.model.location.NamedArea;
33 35
import eu.etaxonomy.cdm.model.name.TaxonName;
34 36
import eu.etaxonomy.cdm.model.occurrence.Collection;
35 37
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
......
38 40
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39 41

  
40 42
/**
41
 *
42 43
 * @author a.mueller
43 44
 *
45
 * @param <CONFIG>
46
 * @param <STATE>
44 47
 */
45 48
public abstract class StreamImportBase<CONFIG extends StreamImportConfiguratorBase, STATE extends StreamImportStateBase<CONFIG,StreamImportBase>>
46 49
        extends CdmImportBase<CONFIG, STATE>{
......
92 95
					handleResults(state, partStream, location);
93 96
					commitTransaction(tx);
94 97
				} catch (Exception e) {
98
				    e.printStackTrace();
95 99
					String message = "An exception occurred while handling partition: " + e;
96 100
					String codeLocation;
97 101
					if (e.getStackTrace().length > 0){
......
164 168
				IdentifiableEntity<?> entity = CdmBase.deproxy(cdmBase, IdentifiableEntity.class);
165 169

  
166 170
				String namespace = mappedCdmBase.getNamespace();
167
				state.putMapping(namespace,mappedCdmBase.getSourceId(), entity);
171
				state.putMapping(namespace, mappedCdmBase.getSourceId(), entity);
168 172
			}
169 173
		}
170 174
	}
......
280 284
	}
281 285

  
282 286

  
283
	//Make public to allow to use by converters
284
	@Override
285
	public Feature getFeature(STATE state, UUID uuid, String label, String description, String labelAbbrev, TermVocabulary<Feature> voc) {
286
		return super.getFeature(state, uuid, label, description, labelAbbrev, voc);
287
	}
288

  
289 287
	/**
290 288
	 * Saves a new term. Immediate saving is required to avoid by Transient-Object-Exceptions.
291 289
	 * @param newTerm
......
295 293
	}
296 294

  
297 295

  
296
    //Make public to allow to use by converters
297
    @Override
298
    public Feature getFeature(STATE state, UUID uuid, String label, String description, String labelAbbrev, TermVocabulary<Feature> voc) {
299
        return super.getFeature(state, uuid, label, description, labelAbbrev, voc);
300
    }
301

  
302
    /**
303
     * {@inheritDoc}
304
     *
305
     * If uuid is null a random one is created.
306
     */
307
    @Override
308
    public Language getLanguage(STATE state,
309
            UUID uuid, String label, String text, String labelAbbrev, TermVocabulary voc) {
310
        if (uuid == null){
311
            uuid = UUID.randomUUID();
312
        }
313
        return super.getLanguage(state, uuid, label, text, labelAbbrev, voc);
314
    }
315

  
316
    public NamedArea getNamedArea(STATE state, UUID namedAreaUuid,
317
            String label, String description, String abbrevLabel, TermVocabulary voc) {
318
        return super.getNamedArea(state, namedAreaUuid, label, description, abbrevLabel,
319
                null, null, voc, null);
320
    }
321

  
322

  
298 323

  
299 324
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/stream/StreamImportStateBase.java
28 28
import eu.etaxonomy.cdm.io.dwca.in.MappedCdmBase;
29 29
import eu.etaxonomy.cdm.io.dwca.in.MappingEntry;
30 30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
31 32
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
32 33
import eu.etaxonomy.cdm.model.reference.Reference;
33 34

  
......
85 86
		if (! mappedCdmBase.getCdmBase().isInstanceOf(IdentifiableEntity.class)){
86 87
			throw new IllegalArgumentException("Mapped cdmBase does not map an identifiable entity");
87 88
		}
88
		mapping.putMapping(mappedCdmBase.getNamespace(), mappedCdmBase.getSourceId(), CdmBase.deproxy(mappedCdmBase.getCdmBase(), IdentifiableEntity.class));
89
		putMapping(mappedCdmBase.getNamespace(), mappedCdmBase.getSourceId(), CdmBase.deproxy(mappedCdmBase.getCdmBase(), IdentifiableEntity.class));
89 90
	}
90 91

  
91 92

  
92 93
	public void putMapping(String namespace, Integer sourceKey, IdentifiableEntity<?> destinationObject){
93
		mapping.putMapping(namespace, sourceKey, destinationObject);
94
	    putMapping(namespace, String.valueOf(sourceKey), destinationObject);
94 95
	}
95 96

  
96 97
	public void putMapping(String namespace, String sourceKey, IdentifiableEntity<?> destinationObject){
97
		mapping.putMapping(namespace, sourceKey, destinationObject);
98
		if (destinationObject.isInstanceOf(DefinedTermBase.class)){
99
		    addRelatedObject(namespace, sourceKey, destinationObject);
100
		}
101
	    mapping.putMapping(namespace, sourceKey, destinationObject);
98 102
	}
99 103

  
100 104

  
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/Language.java
1263 1263
        }
1264 1264
        return null;
1265 1265
    }
1266

  
1267
    /* (non-Javadoc)
1268
     * @see eu.etaxonomy.cdm.model.common.TermBase#toString()
1269
     */
1270
    @Override
1271
    public String toString() {
1272
        if (this.getLabel() != null){
1273
            return this.getLabel();
1274
        }else{
1275
            return super.toString();
1266
    public static Language getLanguageByIsoCode(String code){
1267
        if (StringUtils.isBlank(code)){
1268
            return null;
1269
        }
1270
        for (Language language : termMap.values()){
1271
            if (code.equalsIgnoreCase(language.getIso639_1())){
1272
                return language;
1273
            } else if (code.equalsIgnoreCase(language.getIso639_2())){
1274
                return language;
1275
            }
1276 1276
        }
1277
        return null;
1277 1278
    }
1278 1279

  
1280

  
1279 1281
    @Override
1280 1282
    protected void setDefaultTerms(TermVocabulary<Language> termVocabulary) {
1281 1283
        if (termMap == null){  //there are 2 language vocabularies now
......
1331 1333
        }
1332 1334
    }
1333 1335

  
1336

  
1337
 // ******************************** toString *********************/
1338
     @Override
1339
     public String toString() {
1340
         if (this.getLabel() != null){
1341
             return this.getLabel();
1342
         }else{
1343
             return super.toString();
1344
         }
1345
     }
1346

  
1334 1347
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)