Project

General

Profile

Download (4.4 KB) Statistics
| Branch: | Tag: | 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

    
10
package eu.etaxonomy.cdm.io.specimen.excel.in;
11

    
12
import java.util.Map;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import org.apache.commons.lang.StringUtils;
17
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
18
import org.springframework.stereotype.Component;
19

    
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.io.common.ICdmIO;
22
import eu.etaxonomy.cdm.io.excel.common.ExcelImportBase;
23
import eu.etaxonomy.cdm.model.common.ExtensionType;
24

    
25
/**
26
 * @author a.mueller
27
 * @since 18.05.2010
28
 */
29
@Component
30
public class ExtensionTypeExcelImport
31
            extends ExcelImportBase<SpecimenCdmExcelImportState, SpecimenCdmExcelImportConfigurator, SpecimenRow>
32
            implements ICdmIO<SpecimenCdmExcelImportState> {
33

    
34
    private static final long serialVersionUID = -6812740621254308971L;
35

    
36
    private static final Logger logger = LogManager.getLogger(ExtensionTypeExcelImport.class);
37

    
38
	private static final String WORKSHEET_NAME = "ExtensionTypes";
39

    
40
	private static final String UUID_COLUMN = "UUID";
41
	private static final String LABEL_COLUMN = "Label";
42
	private static final String ABBREVIATION_COLUMN = "Abbreviation";
43
	private static final String DESCRIPTION_COLUMN = "Description";
44
	private static final String POSTFIX_COLUMN = "Postfix";
45

    
46

    
47
	public ExtensionTypeExcelImport() {
48
		super();
49
	}
50

    
51
	@Override
52
	protected void analyzeRecord(Map<String, String> record, SpecimenCdmExcelImportState state) {
53
		Set<String> keys = record.keySet();
54

    
55
    	NamedAreaLevellRow row = new NamedAreaLevellRow();
56
    	state.setNamedAreaLevelRow(row);
57

    
58
    	for (String originalKey: keys) {
59
    		Integer index = 0;
60
    		String indexedKey = CdmUtils.removeDuplicateWhitespace(originalKey.trim()).toString();
61
    		String[] split = indexedKey.split("_");
62
    		String key = split[0];
63
    		if (split.length > 1){
64
    			String indexString = split[split.length - 1];
65
    			try {
66
    				index = Integer.valueOf(indexString);
67
				} catch (NumberFormatException e) {
68
					String message = "Index must be integer";
69
					logger.error(message);
70
					continue;
71
				}
72
    		}
73

    
74
    		String value = record.get(indexedKey);
75
    		if (! StringUtils.isBlank(value)) {
76
    			if (logger.isDebugEnabled()) { logger.debug(key + ": " + value); }
77
        		value = CdmUtils.removeDuplicateWhitespace(value.trim()).toString();
78
    		}else{
79
    			continue;
80
    		}
81

    
82
    		if (key.equalsIgnoreCase(UUID_COLUMN)) {
83
    			row.setUuid(UUID.fromString(value)); //VALIDATE UUID
84
 			} else if(key.equalsIgnoreCase(LABEL_COLUMN)) {
85
				row.setLabel(value);
86
			} else if(key.equalsIgnoreCase(ABBREVIATION_COLUMN)) {
87
				row.setAbbreviation(value);
88
			} else if(key.equalsIgnoreCase(DESCRIPTION_COLUMN)) {
89
				row.setDescription(value);
90
			} else if(key.equalsIgnoreCase(POSTFIX_COLUMN)) {
91
				row.setPostfix(value);
92
			}else {
93
				state.setUnsuccessfull();
94
				logger.error("Unexpected column header " + key);
95
			}
96
    	}
97
    	return;
98
	}
99

    
100

    
101
	@Override
102
	protected void firstPass(SpecimenCdmExcelImportState state) {
103
		NamedAreaLevellRow row = state.getNamedAreaLevelRow();
104

    
105
		//level
106
		UUID uuid = row.getUuid();
107
		String label = row.getAbbreviation();
108
		String text = row.getDescription();
109
		String labelAbbrev = row.getAbbreviation();
110

    
111
		ExtensionType term = getExtensionType(state, uuid, label, text, labelAbbrev);
112

    
113
		if (StringUtils.isNotBlank(row.getPostfix())){
114
			state.putPostfixExtensionType(row.getPostfix(), term);
115
		}
116

    
117
		//save
118
		getTermService().save(term);
119
		return;
120
	}
121

    
122

    
123

    
124
	@Override
125
	protected void secondPass(SpecimenCdmExcelImportState state) {
126
		//no second path defined yet
127
		return;
128
	}
129

    
130
	/* (non-Javadoc)
131
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
132
	 */
133
	@Override
134
	protected boolean doCheck(SpecimenCdmExcelImportState state) {
135
		logger.warn("Validation not yet implemented for " + this.getClass().getSimpleName());
136
		return true;
137
	}
138

    
139
	@Override
140
    protected String getWorksheetName(SpecimenCdmExcelImportConfigurator config) {
141
		return WORKSHEET_NAME;
142
	}
143

    
144
	@Override
145
	protected boolean requiresNomenclaturalCode() {
146
		return false;
147
	}
148

    
149

    
150
	@Override
151
	protected boolean isIgnore(SpecimenCdmExcelImportState state) {
152
		return !state.getConfig().isDoExtensionTypes();
153
	}
154

    
155

    
156
}
(1-1/12)