This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiNsSMMConceptProvider.java
149 lines (118 loc) · 4.9 KB
/
MultiNsSMMConceptProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Copyright (C) 2018-2020 toop.eu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.toop.simulator.mock;
import com.helger.commons.string.ToStringGenerator;
import com.typesafe.config.Config;
import eu.toop.commander.util.Util;
import eu.toop.connector.api.smm.ISMMConceptProvider;
import eu.toop.connector.api.smm.MappedValueList;
import eu.toop.simulator.ToopSimulatorResources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This class represents a semantic mapping service that contains multiple source and
* destination namespaces and is able to perform bidirectional mapping (a source can act
* as a destination and vice versa)
*
* @author muhammet yildiz
* @since 0.10.6
*/
public class MultiNsSMMConceptProvider implements ISMMConceptProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(MultiNsSMMConceptProvider.class);
private final Map<String, Map<String, Map<String, String>>> m_nsMap;
/**
* Create the concept provider by initializin the semantic map
*/
public MultiNsSMMConceptProvider() {
this.m_nsMap = initializeSemanticMap();
}
/**
* Parse and resolve the sms.conf file/classpath resource and create
* a mapping from and to TOOP concepts.
* @return
*/
private static Map<String, Map<String, Map<String, String>>> initializeSemanticMap() {
LOGGER.debug("Preparing SMS Simulator");
Config config = Util.resolveConfiguration(ToopSimulatorResources.getSMSResourceURL(), false);
List<Map<String, Object>> mappings = (List<Map<String, Object>>) config.getAnyRef("Mappings");
//iterate over every mapping, create an inverse of it as well, and add both to the MultiNSSmm...
//this can be simplified by helper objects
Map<String, Map<String, Map<String, String>>> mapOpMapsOfMaps = new HashMap<>();
for (Map<String, Object> mapping : mappings) {
Map<String, String> conceptMap = new HashMap<>();
Map<String, String> inverseConceptMap = new HashMap<>();
String sourceNS = (String) mapping.get("sourceNS");
String targetNS = (String) mapping.get("targetNS");
Map<String, String> concepts = (Map<String, String>) mapping.get("concepts");
for (String key : concepts.keySet()) {
String value = concepts.get(key);
conceptMap.put(key, value);
inverseConceptMap.put(value, key);
}
//forward mapping
populateMapping(mapOpMapsOfMaps, conceptMap, sourceNS, targetNS);
//backwards mappind
populateMapping(mapOpMapsOfMaps, inverseConceptMap, targetNS, sourceNS);
}
return mapOpMapsOfMaps;
}
private static void populateMapping(Map<String, Map<String, Map<String, String>>> conversionMap,
Map<String, String> conceptMap, String sourceNS, String targetNS) {
Map<String, Map<String, String>> nsMap;
if (conversionMap.containsKey(sourceNS)) {
nsMap = conversionMap.get(sourceNS);
} else {
nsMap = new HashMap<>();
conversionMap.put(sourceNS, nsMap);
}
if (nsMap.containsKey(targetNS)) {
throw new IllegalStateException(sourceNS + "->" + targetNS + " mapping already defined");
}
nsMap.put(targetNS, conceptMap);
}
@Nonnull
@Override
public MappedValueList getAllMappedValues(@Nonnull String sLogPrefix, @Nonnull String sSourceNamespace, @Nonnull String sDestNamespace) {
LOGGER.info(sLogPrefix +
"Using static Map for SMM mappings from '" +
sSourceNamespace +
"' to '" +
sDestNamespace +
"'");
final MappedValueList ret = new MappedValueList();
if (m_nsMap.containsKey(sSourceNamespace)) {
Map<String, Map<String, String>> targetMap = m_nsMap.get(sSourceNamespace);
if (targetMap.containsKey(sDestNamespace)) {
Map<String, String> concepts = targetMap.get(sDestNamespace);
for (final Map.Entry<String, String> aEntry : concepts.entrySet())
ret.addMappedValue(sSourceNamespace, aEntry.getKey(), sDestNamespace, aEntry.getValue());
}
}
if (ret.isEmpty() && LOGGER.isDebugEnabled()){
LOGGER.debug("Namespace query mismatch. Queried '" + sSourceNamespace + "' to '" + sDestNamespace);
}
return ret;
}
@Override
public String toString() {
return new ToStringGenerator(this).getToString();
}
}