Project

General

Profile

Download (6.14 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.dwca.in;
10

    
11
import java.util.HashMap;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16

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

    
20
import eu.etaxonomy.cdm.io.common.events.IIoEvent;
21
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
22
import eu.etaxonomy.cdm.io.common.events.IoProblemEvent;
23
import eu.etaxonomy.cdm.io.dwca.TermUri;
24
import eu.etaxonomy.cdm.io.stream.StreamImportBase;
25
import eu.etaxonomy.cdm.io.stream.StreamImportStateBase;
26
import eu.etaxonomy.cdm.io.stream.StreamItem;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.description.TaxonDescription;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31

    
32
/**
33
 * @author a.mueller
34
 * @date 23.11.2011
35
 *
36
 * FIXME URI
37
 */
38
public abstract class PartitionableConverterBase<CONFIG extends DwcaDataImportConfiguratorBase, STATE extends StreamImportStateBase<CONFIG,StreamImportBase>>
39
      implements IPartitionableConverter<StreamItem, IReader<CdmBase>, String> {
40

    
41
	private static final Logger logger = Logger.getLogger(PartitionableConverterBase.class);
42

    
43
	protected STATE state;
44
	protected CONFIG config;  //for convenience only (must always be same as state.getConfig())
45

    
46

    
47

    
48
	public PartitionableConverterBase(STATE state) {
49
		super();
50
		this.state = state;
51
		this.config = state.getConfig();  //for fast access
52
	}
53

    
54
	protected void fireWarningEvent(String message, StreamItem item, Integer severity) {
55
		fireWarningEvent(message, getDataLocation(item), severity, 1);
56
	}
57

    
58
	private String getDataLocation(StreamItem item) {
59
		String location = item.getLocation();
60
		return location;
61
	}
62

    
63
	protected void fireWarningEvent(String message, String dataLocation, Integer severity) {
64
		fireWarningEvent(message, dataLocation, severity, 1);
65
	}
66

    
67
	protected void fireWarningEvent(String message, String dataLocation, Integer severity, int stackDepth) {
68
		stackDepth++;
69
		StackTraceElement[] stackTrace = new Exception().getStackTrace();
70
		int lineNumber = stackTrace[stackDepth].getLineNumber();
71
		String methodName = stackTrace[stackDepth].getMethodName();
72
		String className = stackTrace[stackDepth].getClassName();
73
		Class<?> declaringClass;
74
		try {
75
			declaringClass = Class.forName(className);
76
		} catch (ClassNotFoundException e) {
77
			declaringClass = this.getClass();
78
		}
79

    
80
		IoProblemEvent event = IoProblemEvent.NewInstance(declaringClass, message, dataLocation,
81
				lineNumber, severity, methodName);
82

    
83
		//for performance improvement one may read:
84
		//http://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflection
85
//		Object o = new SecurityManager().getSecurityContext();
86

    
87
		fire(event);
88
	}
89

    
90
	protected void fire(IIoEvent event){
91
		Set<IIoObserver> observers = state.getConfig().getObservers();
92
		for (IIoObserver observer: observers){
93
			observer.handleEvent(event);
94
		}
95
		if (observers.size() == 0){
96
			logger.warn(event.getMessage() +  " (no observer for message!).");
97
		}
98
	}
99

    
100

    
101

    
102
	/**
103
	 * Returns the value for the given term in the item.
104
	 */
105
	protected String getValue(StreamItem item, TermUri term) {
106
		return item.get(term.getUriString());
107
	}
108

    
109
	/**
110
	 * Checks if the given term has a value in item that is not blank (null, empty or whitespace only).
111
	 * @param term
112
	 * @param item
113
	 * @return true if value is not blank
114
	 */
115
	protected boolean exists(TermUri term, StreamItem item) {
116
		return ! StringUtils.isBlank(getValue(item, term));
117
	}
118

    
119
    /*
120
     * To be implemented by subclasses if needed.
121
     */
122
	@Override
123
    public ItemFilter<StreamItem> getItemFilter(){
124
	    return null;
125
	}
126

    
127
	@Override
128
    public Map<String, Set<String>> getPartitionForeignKeys(IReader<StreamItem> instream) {
129
		Map<String, Set<String>> result = new HashMap<String, Set<String>>();
130

    
131
		while (instream.hasNext()){
132
			StreamItem next = instream.read();
133
			makeForeignKeysForItem(next, result);
134
		}
135
		return result;
136
	}
137

    
138

    
139
	/**
140
	 * Fills the the foreign key map with foreign keys required for this item.
141
	 * @param next
142
	 * @param foreignKeyMap
143
	 */
144
	protected abstract void makeForeignKeysForItem(StreamItem next, Map<String, Set<String>> foreignKeyMap);
145

    
146

    
147
	/**
148
	 * False if string is null, empty or whitespace only. True otherwise.
149
	 * @param string String to test.
150
	 */
151
	protected boolean hasValue(String string) {
152
		return StringUtils.isNotBlank(string);
153
	}
154

    
155

    
156
	/**
157
	 * Returns the key set for a given key or creates a new one.
158
	 * @param key
159
	 * @param fkMap
160
	 * @return
161
	 */
162
	protected Set<String> getKeySet(String key, Map<String, Set<String>> fkMap) {
163
		Set<String> keySet = fkMap.get(key);
164
		if (keySet == null){
165
			keySet = new HashSet<String>();
166
			fkMap.put(key, keySet);
167
		}
168
		return keySet;
169
	}
170

    
171

    
172
	protected <T extends TaxonBase> T getTaxonBase(String id, StreamItem item, Class<T> clazz, STATE state) {
173
		if (clazz == null){
174
			clazz = (Class)TaxonBase.class;
175
		}
176
		List<T> taxonList = state.get(TermUri.DWC_TAXON.toString(), id, clazz);
177
		if (taxonList.size() > 1){
178
			String message = "Ambigous taxon mapping for id %s. There is more than 1 matching taxon.";
179
			message = String.format(message, id);
180
			fireWarningEvent(message, item, 8);
181
			logger.warn(message);  //TODO remove when events are handled correctly
182
			return null;
183
		}else if (taxonList.isEmpty()){
184
			return null;
185
		}else{
186
			return taxonList.get(0);
187
		}
188
	}
189

    
190

    
191
	protected TaxonDescription getTaxonDescription(Taxon taxon, boolean isImageGallery) {
192
		TaxonDescription result = null;
193
		Set<TaxonDescription> descs = taxon.getDescriptions();
194
		for (TaxonDescription desc : descs){
195
			if (desc.isImageGallery() == isImageGallery){
196
				result = desc;
197
				break;
198
			}
199
		}
200
		if (result == null){
201
			result = TaxonDescription.NewInstance(taxon);
202
			result.setImageGallery(isImageGallery);
203
		}
204
		return result;
205
	}
206

    
207
	protected boolean isNotBlank(String str){
208
		return StringUtils.isNotBlank(str);
209
	}
210

    
211
	protected boolean isBlank(String str){
212
		return StringUtils.isBlank(str);
213
	}
214

    
215
}
(36-36/37)