2020-05-23 12:27:44 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Expression = require('./expression');
|
|
|
|
const NoteSet = require('../note_set');
|
2021-05-17 22:09:49 +02:00
|
|
|
const becca = require('../../../becca/becca.js');
|
2020-05-23 12:27:44 +02:00
|
|
|
|
|
|
|
class RelationWhereExp extends Expression {
|
|
|
|
constructor(relationName, subExpression) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.relationName = relationName;
|
|
|
|
this.subExpression = subExpression;
|
|
|
|
}
|
|
|
|
|
2020-12-11 13:54:41 +01:00
|
|
|
execute(inputNoteSet, executionContext) {
|
2020-05-23 12:27:44 +02:00
|
|
|
const candidateNoteSet = new NoteSet();
|
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
for (const attr of becca.findAttributes('relation', this.relationName)) {
|
2020-05-23 12:27:44 +02:00
|
|
|
const note = attr.note;
|
|
|
|
|
2021-03-30 21:39:42 +02:00
|
|
|
if (inputNoteSet.hasNoteId(note.noteId) && attr.targetNote) {
|
2020-05-23 12:27:44 +02:00
|
|
|
const subInputNoteSet = new NoteSet([attr.targetNote]);
|
2020-12-11 13:54:41 +01:00
|
|
|
const subResNoteSet = this.subExpression.execute(subInputNoteSet, executionContext);
|
2020-05-23 12:27:44 +02:00
|
|
|
|
|
|
|
if (subResNoteSet.hasNote(attr.targetNote)) {
|
|
|
|
if (attr.isInheritable) {
|
2021-05-17 22:35:36 +02:00
|
|
|
candidateNoteSet.addAll(note.getSubtreeNotesIncludingTemplated());
|
|
|
|
} else if (note.isTemplate()) {
|
|
|
|
candidateNoteSet.addAll(note.getTemplatedNotes());
|
2020-05-23 12:27:44 +02:00
|
|
|
} else {
|
|
|
|
candidateNoteSet.add(note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return candidateNoteSet.intersection(inputNoteSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RelationWhereExp;
|