|
24 | 24 | import javax.xml.namespace.QName;
|
25 | 25 |
|
26 | 26 | import org.kie.dmn.feel.util.Either;
|
| 27 | +import org.kie.dmn.model.api.Definitions; |
27 | 28 | import org.kie.dmn.model.api.Import;
|
28 | 29 | import org.kie.dmn.model.api.NamespaceConsts;
|
29 | 30 | import org.kie.dmn.model.v1_1.TImport;
|
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
30 | 33 |
|
31 | 34 | public class ImportDMNResolverUtil {
|
32 | 35 |
|
| 36 | + private static final Logger LOGGER = LoggerFactory.getLogger(ImportDMNResolverUtil.class); |
| 37 | + |
33 | 38 | private ImportDMNResolverUtil() {
|
34 | 39 | // No constructor for util class.
|
35 | 40 | }
|
36 | 41 |
|
37 |
| - public static <T> Either<String, T> resolveImportDMN(Import _import, Collection<T> all, Function<T, QName> idExtractor) { |
38 |
| - final String iNamespace = _import.getNamespace(); |
39 |
| - final String iName = _import.getName(); |
40 |
| - final String iModelName = _import.getAdditionalAttributes().get(TImport.MODELNAME_QNAME); |
41 |
| - List<T> allInNS = all.stream() |
42 |
| - .filter(m -> idExtractor.apply(m).getNamespaceURI().equals(iNamespace)) |
43 |
| - .collect(Collectors.toList()); |
44 |
| - if (allInNS.size() == 1) { |
45 |
| - T located = allInNS.get(0); |
| 42 | + public static <T> Either<String, T> resolveImportDMN(Import importElement, Collection<T> dmns, Function<T, QName> idExtractor) { |
| 43 | + final String importerDMNNamespace = ((Definitions) importElement.getParent()).getNamespace(); |
| 44 | + final String importerDMNName = ((Definitions) importElement.getParent()).getName(); |
| 45 | + final String importNamespace = importElement.getNamespace(); |
| 46 | + final String importName = importElement.getName(); |
| 47 | + final String importLocationURI = importElement.getLocationURI(); // This is optional |
| 48 | + final String importModelName = importElement.getAdditionalAttributes().get(TImport.MODELNAME_QNAME); |
| 49 | + |
| 50 | + LOGGER.debug("Resolving an Import in DMN Model with name={} and namespace={}. " + |
| 51 | + "Importing a DMN model with namespace={} name={} locationURI={}, modelName={}", |
| 52 | + importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName); |
| 53 | + |
| 54 | + List<T> matchingDMNList = dmns.stream() |
| 55 | + .filter(m -> idExtractor.apply(m).getNamespaceURI().equals(importNamespace)) |
| 56 | + .collect(Collectors.toList()); |
| 57 | + if (matchingDMNList.size() == 1) { |
| 58 | + T located = matchingDMNList.get(0); |
46 | 59 | // Check if the located DMN Model in the NS, correspond for the import `drools:modelName`.
|
47 |
| - if (iModelName == null || idExtractor.apply(located).getLocalPart().equals(iModelName)) { |
| 60 | + if (importModelName == null || idExtractor.apply(located).getLocalPart().equals(importModelName)) { |
| 61 | + LOGGER.debug("DMN Model with name={} and namespace={} successfully imported a DMN " + |
| 62 | + "with namespace={} name={} locationURI={}, modelName={}", |
| 63 | + importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName); |
48 | 64 | return Either.ofRight(located);
|
49 | 65 | } else {
|
50 |
| - return Either.ofLeft(String.format("While importing DMN for namespace: %s, name: %s, modelName: %s, located within namespace only %s but does not match for the actual name", |
51 |
| - iNamespace, iName, iModelName, |
52 |
| - idExtractor.apply(located))); |
| 66 | + LOGGER.error("DMN Model with name={} and namespace={} can't import a DMN with namespace={}, name={}, modelName={}, " + |
| 67 | + "located within namespace only {} but does not match for the actual modelName", |
| 68 | + importerDMNNamespace, importerDMNName, importNamespace, importName, importModelName, idExtractor.apply(located)); |
| 69 | + return Either.ofLeft(String.format( |
| 70 | + "DMN Model with name=%s and namespace=%s can't import a DMN with namespace=%s, name=%s, modelName=%s, " + |
| 71 | + "located within namespace only %s but does not match for the actual modelName", |
| 72 | + importerDMNNamespace, importerDMNName, importNamespace, importName, importModelName, idExtractor.apply(located))); |
53 | 73 | }
|
54 | 74 | } else {
|
55 |
| - List<T> usingNSandName = allInNS.stream() |
56 |
| - .filter(m -> idExtractor.apply(m).getLocalPart().equals(iModelName)) |
57 |
| - .collect(Collectors.toList()); |
| 75 | + List<T> usingNSandName = matchingDMNList.stream() |
| 76 | + .filter(dmn -> idExtractor.apply(dmn).getLocalPart().equals(importModelName)) |
| 77 | + .collect(Collectors.toList()); |
58 | 78 | if (usingNSandName.size() == 1) {
|
| 79 | + LOGGER.debug("DMN Model with name={} and namespace={} successfully imported a DMN " + |
| 80 | + "with namespace={} name={} locationURI={}, modelName={}", |
| 81 | + importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName); |
59 | 82 | return Either.ofRight(usingNSandName.get(0));
|
60 |
| - } else if (usingNSandName.size() == 0) { |
61 |
| - return Either.ofLeft(String.format("Could not locate required dependency while importing DMN for namespace: %s, name: %s, modelName: %s.", |
62 |
| - iNamespace, iName, iModelName)); |
| 83 | + } else if (usingNSandName.isEmpty()) { |
| 84 | + LOGGER.error("DMN Model with name={} and namespace={} failed to import a DMN with namespace={} name={} locationURI={}, modelName={}.", |
| 85 | + importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName); |
| 86 | + return Either.ofLeft(String.format( |
| 87 | + "DMN Model with name=%s and namespace=%s failed to import a DMN with namespace=%s name=%s locationURI=%s, modelName=%s. ", |
| 88 | + importerDMNNamespace, importerDMNName, importNamespace, importName, importLocationURI, importModelName)); |
63 | 89 | } else {
|
64 |
| - return Either.ofLeft(String.format("While importing DMN for namespace: %s, name: %s, modelName: %s, could not locate required dependency within: %s.", |
65 |
| - iNamespace, iName, iModelName, |
66 |
| - allInNS.stream().map(idExtractor).collect(Collectors.toList()))); |
| 90 | + LOGGER.error("DMN Model with name={} and namespace={} detected a collision ({} elements) trying to import a DMN with namespace={} name={} locationURI={}, modelName={}", |
| 91 | + importerDMNNamespace, importerDMNName, usingNSandName.size(), importNamespace, importName, importLocationURI, importModelName); |
| 92 | + return Either.ofLeft(String.format( |
| 93 | + "DMN Model with name=%s and namespace=%s detected a collision trying to import a DMN with %s namespace, " + |
| 94 | + "%s name and modelName %s. There are %s DMN files with the same namespace in your project. " + |
| 95 | + "Please change the DMN namespaces and make them unique to fix this issue.", |
| 96 | + importerDMNNamespace, importerDMNName, importNamespace, importName, importModelName, usingNSandName.size())); |
67 | 97 | }
|
68 | 98 | }
|
69 | 99 | }
|
70 | 100 |
|
71 |
| - public static enum ImportType { |
| 101 | + public enum ImportType { |
72 | 102 | UNKNOWN,
|
73 | 103 | DMN,
|
74 | 104 | PMML;
|
75 | 105 | }
|
76 | 106 |
|
77 |
| - public static ImportType whichImportType(Import _import) { |
78 |
| - switch (_import.getImportType()) { |
| 107 | + public static ImportType whichImportType(Import importElement) { |
| 108 | + switch (importElement.getImportType()) { |
79 | 109 | case org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase.URI_DMN:
|
80 | 110 | case "http://www.omg.org/spec/DMN1-2Alpha/20160929/MODEL":
|
81 | 111 | case org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase.URI_DMN:
|
|
0 commit comments