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.stream.mapping;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.HashMap;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Map.Entry;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
20
|
|
21
|
/**
|
22
|
* @author a.mueller
|
23
|
* @since 10.03.2012
|
24
|
*
|
25
|
*/
|
26
|
public class InMemoryMapping implements IImportMapping {
|
27
|
|
28
|
/**
|
29
|
* Locally stored foreign key mapping, first object defines a namespace in the source, 2nd Object is
|
30
|
* a key in the source that is unique within the given namespace, the
|
31
|
*/
|
32
|
private Map<String, Map<String, Set<CdmKey>>> mapping = new HashMap<String, Map<String,Set<CdmKey>>>();
|
33
|
|
34
|
@Override
|
35
|
public void putMapping(String namespace, Integer sourceKey, IdentifiableEntity destinationObject){
|
36
|
putMapping(namespace, String.valueOf(sourceKey), destinationObject);
|
37
|
}
|
38
|
|
39
|
|
40
|
@Override
|
41
|
public void putMapping(String namespace, String sourceKey, IdentifiableEntity destinationObject){
|
42
|
CdmKey<IdentifiableEntity<?>> cdmKey = new CdmKey(destinationObject);
|
43
|
putMapping(namespace, sourceKey, cdmKey);
|
44
|
}
|
45
|
|
46
|
public void putMapping(String namespace, String sourceKey, CdmKey<IdentifiableEntity<?>> cdmKey){
|
47
|
Map<String, Set<CdmKey>> namespaceMap = mapping.get(namespace);
|
48
|
if (namespaceMap == null){
|
49
|
namespaceMap = new HashMap<String, Set<CdmKey>>();
|
50
|
mapping.put(namespace, namespaceMap);
|
51
|
}
|
52
|
Set<CdmKey> keySet = namespaceMap.get(sourceKey);
|
53
|
if (keySet == null){
|
54
|
keySet = new HashSet<InMemoryMapping.CdmKey>();
|
55
|
namespaceMap.put(sourceKey, keySet);
|
56
|
}
|
57
|
|
58
|
keySet.add(cdmKey);
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public Set<CdmKey> get(String namespace, String sourceKey){
|
63
|
Set<CdmKey> result = new HashSet<InMemoryMapping.CdmKey>();
|
64
|
Map<String, Set<CdmKey>> namespaceMap = mapping.get(namespace);
|
65
|
if (namespaceMap != null){
|
66
|
Set<CdmKey> keySet = namespaceMap.get(sourceKey);
|
67
|
if (keySet != null){
|
68
|
result = keySet;
|
69
|
}
|
70
|
}
|
71
|
return result;
|
72
|
}
|
73
|
|
74
|
|
75
|
|
76
|
@Override
|
77
|
public boolean exists(String namespace, String sourceKey,Class<?> destinationClass){
|
78
|
Set<CdmKey> keySet = this.get(namespace, sourceKey);
|
79
|
for (CdmKey<?> key: keySet){
|
80
|
if (destinationClass == null || destinationClass.isAssignableFrom(key.clazz)){
|
81
|
return true;
|
82
|
}
|
83
|
}
|
84
|
return false;
|
85
|
}
|
86
|
|
87
|
@Override
|
88
|
public InMemoryMapping getPartialMapping( Map<String, Set<String>> namespacedSourceKeys) {
|
89
|
InMemoryMapping partialMapping = new InMemoryMapping();
|
90
|
for (Entry<String,Set<String>> entry : namespacedSourceKeys.entrySet()){
|
91
|
String namespace = entry.getKey();
|
92
|
for (String sourceKey : entry.getValue() ){
|
93
|
Set<CdmKey> destObjects = this.get(namespace, sourceKey);
|
94
|
for (CdmKey cdmKey : destObjects){
|
95
|
partialMapping.putMapping(namespace, sourceKey, cdmKey);
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
return partialMapping;
|
100
|
}
|
101
|
|
102
|
|
103
|
/**
|
104
|
* Returns a list for all mapping entries.
|
105
|
* @return
|
106
|
*/
|
107
|
public List<MappingEntry<String, String, Class, Integer>> getEntryList() {
|
108
|
List<MappingEntry<String, String, Class, Integer>> result = new ArrayList<MappingEntry<String,String,Class,Integer>>();
|
109
|
for (Entry<String, Map<String, Set<CdmKey>>> namespaceEntry : mapping.entrySet() ){
|
110
|
String sourceNamespace = namespaceEntry.getKey();
|
111
|
for (Entry<String, Set<CdmKey>> idEntry : namespaceEntry.getValue().entrySet() ){
|
112
|
String sourceId = idEntry.getKey();
|
113
|
for (CdmKey cdmKey : idEntry.getValue()){
|
114
|
result.add(new MappingEntry<String, String, Class, Integer>(sourceNamespace, sourceId, cdmKey.clazz, cdmKey.id));
|
115
|
}
|
116
|
}
|
117
|
}
|
118
|
return result;
|
119
|
}
|
120
|
|
121
|
public boolean writeToDbMapping(DatabaseMapping dbMapping){
|
122
|
// logger.info("Start writing mapping to dbMapping");
|
123
|
for (String nameSpace : mapping.keySet()){
|
124
|
Map<String, Set<CdmKey>> internalMapping = mapping.get(nameSpace);
|
125
|
for (String sourceId : internalMapping.keySet()){
|
126
|
Set<CdmKey> cdmKeys = internalMapping.get(sourceId);
|
127
|
for (CdmKey cdmKey: cdmKeys) {
|
128
|
dbMapping.putMapping(nameSpace, sourceId, cdmKey);
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
// logger.info("Finished writing mapping to dbMapping");
|
133
|
return true;
|
134
|
}
|
135
|
|
136
|
|
137
|
@Override
|
138
|
public void finish() {
|
139
|
mapping = null;
|
140
|
}
|
141
|
|
142
|
}
|