Skip to content

Commit

Permalink
Improved Error Messages + Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
yesamer committed Jul 9, 2024
1 parent c124b58 commit afa8a6d
Showing 1 changed file with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,74 @@
import org.kie.dmn.model.api.Import;
import org.kie.dmn.model.api.NamespaceConsts;
import org.kie.dmn.model.v1_1.TImport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImportDMNResolverUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(ImportDMNResolverUtil.class);

private ImportDMNResolverUtil() {
// No constructor for util class.
}

public static <T> Either<String, T> resolveImportDMN(Import _import, Collection<T> all, Function<T, QName> idExtractor) {
final String iNamespace = _import.getNamespace();
final String iName = _import.getName();
final String iModelName = _import.getAdditionalAttributes().get(TImport.MODELNAME_QNAME);
List<T> allInNS = all.stream()
.filter(m -> idExtractor.apply(m).getNamespaceURI().equals(iNamespace))
public static <T> Either<String, T> resolveImportDMN(Import importElement, Collection<T> dmns, Function<T, QName> idExtractor) {
final String importNamespace = importElement.getNamespace();
final String importName = importElement.getName();
final String importLocationURI = importElement.getLocationURI(); // This is optional
final String importModelName = importElement.getAdditionalAttributes().get(TImport.MODELNAME_QNAME);

LOGGER.debug("Resolving DMN Import with namespace={} name={} locationURI={}, modelName={}",
importNamespace, importName, importLocationURI, importModelName);

List<T> matchingDmns = dmns.stream()
.filter(m -> idExtractor.apply(m).getNamespaceURI().equals(importNamespace))
.collect(Collectors.toList());
if (allInNS.size() == 1) {
T located = allInNS.get(0);
if (matchingDmns.size() == 1) {
T located = matchingDmns.get(0);
// Check if the located DMN Model in the NS, correspond for the import `drools:modelName`.
if (iModelName == null || idExtractor.apply(located).getLocalPart().equals(iModelName)) {
if (importModelName == null || idExtractor.apply(located).getLocalPart().equals(importModelName)) {
LOGGER.debug("DMN Import with namespace={} and importModelName={} resolved!", importNamespace, importModelName);
return Either.ofRight(located);
} else {
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",
iNamespace, iName, iModelName,
LOGGER.error("Impossible to find the Imported DMN with {} namespace, {} name and {} modelName.",
importNamespace, importName, importModelName);
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 modelName",
importNamespace, importName, importModelName,
idExtractor.apply(located)));
}
} else {
List<T> usingNSandName = allInNS.stream()
.filter(m -> idExtractor.apply(m).getLocalPart().equals(iModelName))
.collect(Collectors.toList());
List<T> usingNSandName = matchingDmns.stream()
.filter(m -> idExtractor.apply(m).getLocalPart().equals(importModelName))
.toList();
if (usingNSandName.size() == 1) {
LOGGER.debug("DMN Import with namespace={} and importModelName={} resolved!", importNamespace, importModelName);
return Either.ofRight(usingNSandName.get(0));
} else if (usingNSandName.size() == 0) {
return Either.ofLeft(String.format("Could not locate required dependency while importing DMN for namespace: %s, name: %s, modelName: %s.",
iNamespace, iName, iModelName));
} else if (usingNSandName.isEmpty()) {
LOGGER.error("Impossible to find the Imported DMN with {} namespace, {} name and {} modelName.",
importNamespace, importName, importModelName);
return Either.ofLeft(String.format("Impossible to find the Imported DMN with %s namespace %s name and %s modelName.",
importNamespace, importName, importModelName));
} else {
return Either.ofLeft(String.format("While importing DMN for namespace: %s, name: %s, modelName: %s, could not locate required dependency within: %s.",
iNamespace, iName, iModelName,
allInNS.stream().map(idExtractor).collect(Collectors.toList())));
LOGGER.error("Found {} number of collision resolving an Imported DMN with {} namespace {} name and {} modelName.",
usingNSandName.size(), importNamespace, importName, importModelName);
return Either.ofLeft(String.format("Found a collision resolving an Imported DMN with %s namespace, %s " +
"name and modelName %s. There are %s DMN files with the same namespace in your project. Please " +
"change the DMN namespaces and make them unique to fix this issue.",
importNamespace, importName, importModelName, usingNSandName.size()));
}
}
}

public static enum ImportType {
public enum ImportType {
UNKNOWN,
DMN,
PMML;
}

public static ImportType whichImportType(Import _import) {
switch (_import.getImportType()) {
public static ImportType whichImportType(Import importElement) {
switch (importElement.getImportType()) {
case org.kie.dmn.model.v1_1.KieDMNModelInstrumentedBase.URI_DMN:
case "http://www.omg.org/spec/DMN1-2Alpha/20160929/MODEL":
case org.kie.dmn.model.v1_2.KieDMNModelInstrumentedBase.URI_DMN:
Expand Down

0 comments on commit afa8a6d

Please sign in to comment.