Project

General

Profile

Download (2.87 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2007 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;
10

    
11
import eu.etaxonomy.cdm.common.URI;
12
import java.net.URISyntaxException;
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.log4j.Logger;
19
import org.springframework.stereotype.Component;
20

    
21
import eu.etaxonomy.cdm.app.images.AbstractImageImporter;
22
import eu.etaxonomy.cdm.app.images.ImageImportState;
23
import eu.etaxonomy.cdm.common.ExcelUtils;
24
import eu.etaxonomy.cdm.model.media.ExternalLinkType;
25
import eu.etaxonomy.cdm.model.name.TaxonName;
26

    
27
/**
28
 * @author n.hoffmann
29
 * @since 19.11.2008
30
 */
31
@Component
32
public class PalmaeProtologueImport extends AbstractImageImporter {
33
    private static final long serialVersionUID = -7178567387220714414L;
34

    
35
    private static final Logger logger = Logger.getLogger(PalmaeProtologueImport.class);
36

    
37
	public static final String SPECIES = "Species";
38
	public static final String TAXONID = "Taxon ID";
39
	public static final String LINK_PROTO = "Link proto";
40

    
41
	@Override
42
	protected void invokeImageImport(ImageImportState state) {
43

    
44
		List<Map<String, String>> contents;
45
		try {
46
			contents = ExcelUtils.parseXLS(state.getConfig().getSource());
47
		} catch (/*FileNotFound*/Exception e) {
48
			logger.error("FileNotFound: " + state.getConfig().getSource().toString());
49
			state.setUnsuccessfull();
50
			return;
51
		}
52

    
53
		Set<TaxonName> taxonNameStore = new HashSet<>();
54

    
55
		int count = 0;
56

    
57
		for (Map<String, String> row : contents){
58
			count++;
59

    
60
			TaxonName taxonName = null;
61
			String species = null;
62
			String taxonId = null;
63
			String linkProto = null;
64
			try{
65
				species = row.get(PalmaeProtologueImport.SPECIES).trim();
66
				taxonId = row.get(PalmaeProtologueImport.TAXONID);
67
				linkProto= row.get(PalmaeProtologueImport.LINK_PROTO).trim();
68
				taxonName = getCommonService().getSourcedObjectByIdInSource(TaxonName.class, "palm_tn_" + taxonId.replace(".0", ""), "TaxonName");
69
			}catch (Exception e){
70
				logger.error("The row has errors: rowNumber: " +count + ", content: "  + row, e);
71
			}
72

    
73
			if(taxonName == null){
74
				logger.warn("no taxon with this name found: " + species + ", idInSource: " + taxonId);
75
			}else{
76
				try {
77
					URI uri = new URI(linkProto);
78
					taxonName.addProtologue(uri, null, ExternalLinkType.WebSite);
79
				} catch (URISyntaxException e) {
80
					String message= "URISyntaxException when trying to convert: " + linkProto;
81
					logger.error(message);
82
					e.printStackTrace();
83
				}
84

    
85
				taxonNameStore.add(taxonName);
86
				if(count % 50 == 0){
87
					logger.info(count + " protologues processed.");
88
				}
89
			}
90
		}
91

    
92
		getNameService().save(taxonNameStore);
93
		logger.info(count + " protologues imported to CDM store.");
94

    
95
		return;
96
	}
97
}
(2-2/3)