Project

General

Profile

Download (5.03 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.HashMap;
13
import java.util.Set;
14
import java.util.UUID;
15

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

    
20
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
21
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.io.common.ICdmIO;
24
import eu.etaxonomy.cdm.io.excel.common.ExcelImporterBase;
25
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
26
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
27
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
28

    
29
/**
30
 * @author p.kelbert
31
 * @created 29.10.2008
32
 * @version 1.0
33
 */
34
@Component
35
public class NamedAreaLevelExcelImport  extends ExcelImporterBase<SpecimenCdmExcelImportState>  implements ICdmIO<SpecimenCdmExcelImportState> {
36
	private static final Logger logger = Logger.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(HashMap<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 = (String) 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
	protected String getWorksheetName() {
155
		return WORKSHEET_NAME;
156
	}
157
	
158
	@Override
159
	protected boolean needsNomenclaturalCode() {
160
		return false;
161
	}
162

    
163

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

    
169

    
170
}
(2-2/12)