Project

General

Profile

Download (4.96 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.location.NamedAreaLevel;
24

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

    
34
    private static final long serialVersionUID = 6790172880945153291L;
35

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

    
38
	private static final String WORKSHEET_NAME = "AreaLevels";
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
	private static final String GEOSERVER_LABEL_COLUMN = "GeoserverLabel";
46
	private static final String GEOSERVER_ATTRIBUTE_COLUMN = "GeoserverAttribute";
47
	private static final String ORDER_INDEX_COLUMN = "OrderIndex";
48

    
49

    
50
	public NamedAreaLevelExcelImport() {
51
		super();
52
	}
53

    
54
	@Override
55
	protected void analyzeRecord(Map<String, String> record, SpecimenCdmExcelImportState state) {
56
		Set<String> keys = record.keySet();
57

    
58
    	NamedAreaLevellRow row = new NamedAreaLevellRow();
59
    	state.setNamedAreaLevelRow(row);
60

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

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

    
85
    		if (key.equalsIgnoreCase(UUID_COLUMN)) {
86
    			row.setUuid(UUID.fromString(value)); //VALIDATE UUID
87
 			} else if(key.equalsIgnoreCase(LABEL_COLUMN)) {
88
				row.setLabel(value);
89
			} else if(key.equalsIgnoreCase(ABBREVIATION_COLUMN)) {
90
				row.setAbbreviation(value);
91
			} else if(key.equalsIgnoreCase(DESCRIPTION_COLUMN)) {
92
				row.setDescription(value);
93
			} else if(key.equalsIgnoreCase(POSTFIX_COLUMN)) {
94
				row.setPostfix(value);
95
			} else if(key.equalsIgnoreCase(GEOSERVER_LABEL_COLUMN)) {
96
				row.setGeoserverLabel(value);
97
			} else if(key.equalsIgnoreCase(GEOSERVER_ATTRIBUTE_COLUMN)) {
98
				row.setGeoServerAttribute(value);
99
			} else if(key.equalsIgnoreCase(ORDER_INDEX_COLUMN)) {
100
				row.setOrderIndex(value);
101
			}else {
102
				state.setUnsuccessfull();
103
				logger.error("Unexpected column header " + key);
104
			}
105
    	}
106
    	return;
107
	}
108

    
109

    
110
	@Override
111
	protected void firstPass(SpecimenCdmExcelImportState state) {
112
		NamedAreaLevellRow row = state.getNamedAreaLevelRow();
113

    
114
		//level
115
		UUID uuid = row.getUuid();
116
		String label = row.getAbbreviation();
117
		String text = row.getDescription();
118
		String labelAbbrev = row.getAbbreviation();
119

    
120
		NamedAreaLevel level = getNamedAreaLevel(state, uuid, label, text, labelAbbrev, null);
121

    
122
		//TODO orderIndex
123

    
124
		//TODO geoserverLabel
125

    
126
		//TODO geoserverAttribute
127

    
128
		if (StringUtils.isNotBlank(row.getPostfix())){
129
			state.putPostfixLevel(row.getPostfix(), level);
130
		}
131

    
132
		//save
133
		getTermService().save(level);
134
		return;
135
	}
136

    
137

    
138

    
139
	@Override
140
	protected void secondPass(SpecimenCdmExcelImportState state) {
141
		//no second path defined yet
142
		return;
143
	}
144

    
145
	/* (non-Javadoc)
146
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
147
	 */
148
	@Override
149
	protected boolean doCheck(SpecimenCdmExcelImportState state) {
150
		logger.warn("Validation not yet implemented for " + this.getClass().getSimpleName());
151
		return true;
152
	}
153

    
154
	@Override
155
    protected String getWorksheetName(SpecimenCdmExcelImportConfigurator config) {
156
		return WORKSHEET_NAME;
157
	}
158

    
159
	@Override
160
	protected boolean requiresNomenclaturalCode() {
161
		return false;
162
	}
163

    
164

    
165
	@Override
166
	protected boolean isIgnore(SpecimenCdmExcelImportState state) {
167
		return !state.getConfig().isDoAreaLevels();
168
	}
169

    
170

    
171
}
(2-2/12)