Project

General

Profile

Download (8.46 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.stream;
11

    
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.UUID;
19

    
20
import org.apache.log4j.Logger;
21

    
22
import eu.etaxonomy.cdm.api.service.IIdentifiableEntityService;
23
import eu.etaxonomy.cdm.io.common.ImportStateBase;
24
import eu.etaxonomy.cdm.io.dwca.TermUri;
25
import eu.etaxonomy.cdm.io.dwca.in.IImportMapping;
26
import eu.etaxonomy.cdm.io.dwca.in.IImportMapping.CdmKey;
27
import eu.etaxonomy.cdm.io.dwca.in.InMemoryMapping;
28
import eu.etaxonomy.cdm.io.dwca.in.MappedCdmBase;
29
import eu.etaxonomy.cdm.io.dwca.in.MappingEntry;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33

    
34
/**
35
 * @author a.mueller
36
 * @created 23.11.2011
37
 */
38
public abstract class StreamImportStateBase<CONFIG extends StreamImportConfiguratorBase, IO extends StreamImportBase> extends ImportStateBase<CONFIG, IO>{
39
	private static final Logger logger = Logger.getLogger(StreamImportStateBase.class);
40

    
41
	private UUID uuid = UUID.randomUUID();
42

    
43
	boolean taxaCreated;
44
	private Map<String, Map<String, IdentifiableEntity>> partitionStore;
45

    
46
	private final IImportMapping mapping;
47

    
48
	public StreamImportStateBase(CONFIG config) {
49
		super(config);
50
		if (config.getStateUuid()!= null){
51
		    uuid = config.getStateUuid();
52
		}else{
53
		    String message = "State uuid: " + uuid.toString();
54
		    logger.warn(message);
55
		    System.out.println(message);
56
		}
57
		mapping = getConfig().getMappingType().getMappingInstance(uuid.toString(), getConfig().getDatabaseMappingFile());
58
	}
59

    
60
	/**
61
	 * True, if taxa have been fully created.
62
	 * @return
63
	 */
64
	public boolean isTaxaCreated() {
65
		return taxaCreated;
66

    
67
	}
68

    
69
	/**
70
	 * @param taxaCreated the taxaCreated to set
71
	 */
72
	public void setTaxaCreated(boolean taxaCreated) {
73
		this.taxaCreated = taxaCreated;
74
	}
75

    
76
	public void finish(){
77
		this.mapping.finish();
78
	}
79

    
80
//********************* MAPPING ACCESS *********************************
81
	//TODO this may move to an external class soon
82

    
83
	public void putMapping(MappedCdmBase mappedCdmBase) throws IllegalArgumentException{
84
		if (! mappedCdmBase.getCdmBase().isInstanceOf(IdentifiableEntity.class)){
85
			throw new IllegalArgumentException("Mapped cdmBase does not map an identifiable entity");
86
		}
87
		mapping.putMapping(mappedCdmBase.getNamespace(), mappedCdmBase.getSourceId(), CdmBase.deproxy(mappedCdmBase.getCdmBase(), IdentifiableEntity.class));
88
	}
89

    
90

    
91
	public void putMapping(String namespace, Integer sourceKey, IdentifiableEntity<?> destinationObject){
92
		mapping.putMapping(namespace, sourceKey, destinationObject);
93
	}
94

    
95
	public void putMapping(String namespace, String sourceKey, IdentifiableEntity<?> destinationObject){
96
		mapping.putMapping(namespace, sourceKey, destinationObject);
97
	}
98

    
99

    
100
	public List<IdentifiableEntity> get(String namespace, String sourceKey){
101
		return get(namespace, sourceKey, null);
102
	}
103

    
104
	public <CLASS extends IdentifiableEntity> List<CLASS> get(String namespace, String sourceKey,Class<CLASS> destinationClass){
105
		List<CLASS> result = new ArrayList<CLASS>();
106
		if (this.partitionStore != null){
107
			Map<String, IdentifiableEntity> namespaceMap = this.partitionStore.get(namespace);
108
			if (namespaceMap != null){
109
				IdentifiableEntity<?> cdmBase = namespaceMap.get(sourceKey);
110
				if (cdmBase == null){
111
					logger.info("CdmBase does not exist in mapping: " + sourceKey);
112
				}else if (cdmBase.isInstanceOf(destinationClass)){
113
					CLASS typedCdmBase = CdmBase.deproxy(cdmBase, destinationClass);
114
					result.add(typedCdmBase);
115
				}
116

    
117
			}
118
		}else{
119
			Set<CdmKey> keySet = mapping.get(namespace, sourceKey);
120
			for (CdmKey<CLASS> key: keySet){
121
				if (destinationClass == null || destinationClass.isAssignableFrom(key.getClazz())){
122
					IIdentifiableEntityService<CLASS> service = getCurrentIO().getServiceByClass(key.getClazz());
123
					CLASS entity = CdmBase.deproxy(service.find(key.getId()), key.getClazz());
124
					result.add(entity);
125
				}
126
			}
127
			return result;
128
		}
129
		return result;
130
	}
131

    
132
	public boolean exists(String namespace, String sourceKey,Class<?> destinationClass){
133
		return mapping.exists(namespace, sourceKey, destinationClass);
134
	}
135

    
136

    
137
	public  void loadRelatedObjects (InMemoryMapping mapping){
138
		Map<String, Map<String, IdentifiableEntity>> result = new HashMap<>();
139

    
140
		List<MappingEntry<String, String, Class, Integer>> mappingEntryList = mapping.getEntryList();
141

    
142
		//order ids by destination classes
143
		Map<Class, Set<Integer>> destinationNamespaceMap = new HashMap<>();
144
		for (MappingEntry<String, String, Class, Integer> entry : mappingEntryList){
145
			Set<Integer> idSet = destinationNamespaceMap.get(entry.getDestinationNamespace());
146
			if (idSet == null){
147
				idSet = new HashSet<Integer>();
148
				destinationNamespaceMap.put(entry.getDestinationNamespace(), idSet);
149
			}
150
			idSet.add(entry.getDestinationId());
151
		}
152

    
153
		//retrieve cdm objects per class
154
		Map<Class, Map<Integer, IdentifiableEntity>> classMap = new HashMap<>();
155
		for (Class<?> cdmClass : destinationNamespaceMap.keySet()){
156
			IIdentifiableEntityService<?> classService = getCurrentIO().getServiceByClass(cdmClass);
157
			Set<Integer> idSet = destinationNamespaceMap.get(cdmClass);
158
			List<? extends IdentifiableEntity> relatedObjects = classService.findById(idSet);
159

    
160
			//put into id map
161
			Map<Integer, IdentifiableEntity> idMap = new HashMap<>();
162
			for (IdentifiableEntity<?> identEnt : relatedObjects){
163
				idMap.put(identEnt.getId(), identEnt);
164
			}
165

    
166
			//add to class map
167
			classMap.put(cdmClass, idMap);
168
		}
169

    
170
		//fill related object map
171
		for (MappingEntry<String, String, Class, Integer> entry : mappingEntryList){
172
			IdentifiableEntity cdmBase = getCdmObject(classMap, entry);
173

    
174
			Map<String, IdentifiableEntity> namespaceMap = getOrMakeNamespaceMap(result, entry.getNamespace());
175
			if (cdmBase != null){
176
				namespaceMap.put(entry.getSourceKey(), cdmBase);
177
			}else{
178
				logger.info("CdmBase not found for mapping entry.");
179
			}
180
		}
181

    
182
		//store
183
		this.partitionStore = result;
184

    
185
	}
186

    
187
	public void addRelatedObject(String sourceNamespace, String sourceKey, IdentifiableEntity<?> cdmEntity){
188
		Map<String, IdentifiableEntity> namespaceMap = getOrMakeNamespaceMap(this.partitionStore, sourceNamespace);
189
		if (cdmEntity != null){
190
			namespaceMap.put(sourceKey, cdmEntity);
191
		}else{
192
			logger.info("CdmBase is null and will not be added to related objects.");
193
		}
194
	}
195

    
196

    
197
//	public Map<String, Map<String, IdentifiableEntity>> getPartitionStore() {
198
//		return partitionStore;
199
//	}
200

    
201
	public void unloadPartitionStore(Map<String, Map<String, IdentifiableEntity>> partitionStore) {
202
		this.partitionStore = new HashMap<>();
203
	}
204

    
205
	public IImportMapping getMapping() {
206
		return this.mapping;
207
	}
208

    
209

    
210
	private Map<String, IdentifiableEntity> getOrMakeNamespaceMap(Map<String, Map<String, IdentifiableEntity>> relatedObjectMap2, String namespace) {
211
		Map<String, IdentifiableEntity> namespaceMap = relatedObjectMap2.get(namespace);
212
		if (namespaceMap == null){
213
			namespaceMap = new HashMap<>();
214
			relatedObjectMap2.put(namespace, namespaceMap);
215
		}
216
		return namespaceMap;
217
	}
218

    
219

    
220
	private IdentifiableEntity getCdmObject(Map<Class, Map<Integer, IdentifiableEntity>> classMap,
221
			MappingEntry<String, String, Class, Integer> entry) {
222
		Class<?> cdmClass = entry.getDestinationNamespace();
223
		Integer cdmKey = entry.getDestinationId();
224
		Map<Integer, IdentifiableEntity> idMap = classMap.get(cdmClass);
225
		if (idMap != null){
226
			return idMap.get(cdmKey);
227
		}else{
228
			return null;
229
		}
230
	}
231

    
232
	/**
233
	 * Returns the source reference object that is attached to the current transaction.
234
	 * @return
235
	 */
236
	public Reference getTransactionalSourceReference() {
237
		TermUri namespaceSourceReference = TermUri.CDM_SOURCE_REFERENCE;
238
		UUID sourceReferenceUuid = getConfig().getSourceRefUuid();
239
		List<Reference> references = this.get(namespaceSourceReference.toString(), sourceReferenceUuid.toString(), Reference.class);
240
		if (references.isEmpty()){
241
			//TODO better fire warning, but not yet available for state
242
			throw new RuntimeException("Source reference can not be found. This should not happen.");
243
		}else if (references.size() > 1){
244
			//TODO better fire warning, but not yet available for state
245
			throw new RuntimeException("More than 1 source reference found. This is not yet handled.");
246
		}else{
247
			return references.get(0);
248
		}
249

    
250

    
251

    
252
	}
253

    
254

    
255

    
256

    
257

    
258

    
259
}
(4-4/5)