Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
subtituteNodeRestriction(clause) {
const restriction = clause.restriction;
// Handle "node" as a special case, updating restrictions to either foreignSource+foreignId or node.id
if (restriction.attribute === 'node') {
if (restriction.value.indexOf(':') > 0) {
if (restriction.comparator.id !== API.Comparators.EQ.id) {
console.log('WARNING: Using a comparator other than EQ will probably not work as expected with a foreignSource:foreignId node criteria.');
}
const nodeCriteria = restriction.value.split(':');
const replacement = new API.NestedRestriction(
new API.Clause(new API.Restriction('node.foreignSource', restriction.comparator, nodeCriteria[0]), API.Operators.AND),
new API.Clause(new API.Restriction('node.foreignId', restriction.comparator, nodeCriteria[1]), API.Operators.AND),
);
clause.restriction = replacement;
} else if (isNumber(restriction.value)) {
clause.restriction = new API.Restriction('node.id', restriction.comparator, restriction.value);
} else if (restriction.value === '{}') {
return true;
} else {
console.log('WARNING: found a "node" criteria but it does not appear to be a node ID nor a foreignSource:foreignId tuple.',restriction);
}
}
return false;
}
subtituteNodeRestriction(clause) {
const restriction = clause.restriction;
// Handle "node" as a special case, updating restrictions to either foreignSource+foreignId or node.id
if (restriction.attribute === 'node') {
if (restriction.value.indexOf(':') > 0) {
if (restriction.comparator.id !== API.Comparators.EQ.id) {
console.log('WARNING: Using a comparator other than EQ will probably not work as expected with a foreignSource:foreignId node criteria.');
}
const nodeCriteria = restriction.value.split(':');
const replacement = new API.NestedRestriction(
new API.Clause(new API.Restriction('node.foreignSource', restriction.comparator, nodeCriteria[0]), API.Operators.AND),
new API.Clause(new API.Restriction('node.foreignId', restriction.comparator, nodeCriteria[1]), API.Operators.AND),
);
clause.restriction = replacement;
} else if (isNumber(restriction.value)) {
clause.restriction = new API.Restriction('node.id', restriction.comparator, restriction.value);
} else if (restriction.value === '{}') {
return true;
} else {
console.log('WARNING: found a "node" criteria but it does not appear to be a node ID nor a foreignSource:foreignId tuple.',restriction);
}
}
return false;
}
cloneClause(clause) {
const operator = _.find(API.Operators, function(operator) {
return operator.label === clause.operator.label;
});
// Nested restriction
if (clause.restriction.clauses) {
const nestedRestriction = this.cloneNestedRestriction(clause.restriction);
return new API.Clause(nestedRestriction, operator);
} else { // Normal restriction
const restriction = this.cloneRestriction(clause.restriction);
return new API.Clause(restriction, operator);
}
}
expect(uiFilter.getQueryString()).to.eql("select all alarms where location = 'Stuttgart' or (severity >= 'WARNING' and severity <= 'MAJOR')");
// let's try the other way around
uiFilter.clear();
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.LE, 'MAJOR'), API.Operators.AND)), API.Operators.OR));
uiFilter.addClause(new UI.Clause(uiSegmentSrv, UI.Operators.AND, new UI.RestrictionDTO('location', UI.Comparators.EQ, 'Stuttgart')));
expect(uiFilter.getQueryString()).to.eql("select all alarms where (severity >= 'WARNING' and severity <= 'MAJOR') and location = 'Stuttgart'");
// let's try 2 nested restrictions
uiFilter.clear();
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('location', API.Comparators.EQ, 'Stuttgart'), API.Operators.OR))
.withClause(new API.Clause(new API.Restriction('location', API.Comparators.EQ, 'Fulda'), API.Operators.OR)), API.Operators.AND)
);
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.LE, 'MAJOR'), API.Operators.AND)), API.Operators.AND)
);
expect(uiFilter.getQueryString()).to.eql("select all alarms where (location = 'Stuttgart' or location = 'Fulda') and (severity >= 'WARNING' and severity <= 'MAJOR')");
done();
});
it("should map from api to ui operator", function (done) {
expect(mapping.getUiOperator(API.Operators.AND)).to.eql("AND");
expect(mapping.getUiOperator(API.Operators.OR)).to.eql("OR");
done();
});
getUiOperator(apiOperator) {
const theOperator = API.Operators[apiOperator.label];
if (theOperator && theOperator.label) {
return theOperator.label;
}
throw { message: "No operator found with label " + apiOperator.label, operators: API.Operators};
}
findOperators() {
const operators = _.map(API.Operators, function(operator) {
return {
id: operator.id,
label: operator.label
}
});
return this.$q.when(operators);
}
getUiOperator(apiOperator) {
const theOperator = API.Operators[apiOperator.label];
if (theOperator && theOperator.label) {
return theOperator.label;
}
throw { message: "No operator found with label " + apiOperator.label, operators: API.Operators};
}
getApiOperator(uiOperator) {
const apiOperator = _.find(API.Operators, function(eachOperator) {
return eachOperator.matches(uiOperator);
});
if (!apiOperator) {
throw new Error("Could not map uiOperator '" + uiOperator + "' to API operator.");
}
return apiOperator;
}
}