176 lines
5.0 KiB
JavaScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
2020-08-27 23:54:02 +02:00
const Note = require('./note.js');
const AbstractEntity = require("./abstract_entity.js");
const sql = require("../../sql.js");
const dateUtils = require("../../date_utils.js");
const promotedAttributeDefinitionParser = require("../../promoted_attribute_definition_parser");
class Attribute extends AbstractEntity {
static get entityName() { return "attributes"; }
static get primaryKeyName() { return "attributeId"; }
static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable"]; }
2021-04-16 23:00:08 +02:00
constructor(becca, row) {
super();
2021-04-16 23:00:08 +02:00
/** @param {Becca} */
this.becca = becca;
2020-05-16 23:12:29 +02:00
/** @param {string} */
this.attributeId = row.attributeId;
/** @param {string} */
this.noteId = row.noteId;
/** @param {string} */
this.type = row.type;
/** @param {string} */
this.name = row.name;
2020-12-11 22:06:12 +01:00
/** @param {int} */
this.position = row.position;
2020-05-16 23:12:29 +02:00
/** @param {string} */
2020-12-13 23:27:42 +01:00
this.value = row.value;
2020-05-16 23:12:29 +02:00
/** @param {boolean} */
this.isInheritable = !!row.isInheritable;
2021-04-16 23:00:08 +02:00
this.becca.attributes[this.attributeId] = this;
2021-04-16 23:00:08 +02:00
if (!(this.noteId in this.becca.notes)) {
// entities can come out of order in sync, create skeleton which will be filled later
2021-04-16 23:00:08 +02:00
this.becca.notes[this.noteId] = new Note(this.becca, {noteId: this.noteId});
}
2021-04-16 23:00:08 +02:00
this.becca.notes[this.noteId].ownedAttributes.push(this);
2020-05-16 23:12:29 +02:00
2020-12-14 22:12:26 +01:00
const key = `${this.type}-${this.name.toLowerCase()}`;
2021-04-16 23:00:08 +02:00
this.becca.attributeIndex[key] = this.becca.attributeIndex[key] || [];
this.becca.attributeIndex[key].push(this);
2020-05-16 23:12:29 +02:00
const targetNote = this.targetNote;
if (targetNote) {
targetNote.targetRelations.push(this);
}
}
get isAffectingSubtree() {
return this.isInheritable
|| (this.type === 'relation' && this.name === 'template');
}
isAutoLink() {
return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name);
}
2020-05-16 23:12:29 +02:00
get note() {
2021-04-16 23:00:08 +02:00
return this.becca.notes[this.noteId];
2020-05-16 23:12:29 +02:00
}
get targetNote() {
if (this.type === 'relation') {
2021-04-16 23:00:08 +02:00
return this.becca.notes[this.value];
2020-05-16 23:12:29 +02:00
}
}
2021-02-10 22:56:23 +01:00
/**
* @returns {Note|null}
*/
getNote() {
return this.repository.getNote(this.noteId);
}
/**
* @returns {Note|null}
*/
getTargetNote() {
if (this.type !== 'relation') {
throw new Error(`Attribute ${this.attributeId} is not relation`);
}
if (!this.value) {
return null;
}
return this.repository.getNote(this.value);
}
/**
* @return {boolean}
*/
isDefinition() {
return this.type === 'label' && (this.name.startsWith('label:') || this.name.startsWith('relation:'));
}
getDefinition() {
return promotedAttributeDefinitionParser.parse(this.value);
}
getDefinedName() {
if (this.type === 'label' && this.name.startsWith('label:')) {
return this.name.substr(6);
} else if (this.type === 'label' && this.name.startsWith('relation:')) {
return this.name.substr(9);
} else {
return this.name;
}
}
2021-04-25 21:19:18 +02:00
getPojo() {
return {
attributeId: this.attributeId,
noteId: this.noteId,
type: this.type,
name: this.name,
position: this.position,
value: this.value,
2021-04-25 22:02:32 +02:00
isInheritable: this.isInheritable,
utcDateModified: dateUtils.utcNowDateTime(),
isDeleted: false
};
}
beforeSaving() {
if (!this.value) {
if (this.type === 'relation') {
throw new Error(`Cannot save relation ${this.name} since it does not target any note.`);
}
// null value isn't allowed
this.value = "";
}
if (this.position === undefined) {
this.position = 1 + sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM attributes WHERE noteId = ?`, [this.noteId]);
}
if (!this.isInheritable) {
this.isInheritable = false;
}
2021-02-10 22:56:23 +01:00
if (!this.isDeleted) {
this.isDeleted = false;
}
super.beforeSaving();
}
2021-02-10 22:56:23 +01:00
createClone(type, name, value, isInheritable) {
return new Attribute({
noteId: this.noteId,
type: type,
name: name,
value: value,
position: this.position,
isInheritable: isInheritable,
isDeleted: false,
utcDateModified: this.utcDateModified
});
2021-02-10 22:56:23 +01:00
}
2021-04-26 22:24:55 +02:00
markAttributeAsDeleted() {
sql.execute("UPDATE attributes SET isDeleted = 1 WHERE attributeId = ?", [this.attributeId]);
// FIXME: this needs to be published into entity_changes (for sync and becca cleanup)
}
2020-05-16 23:12:29 +02:00
}
2020-05-17 09:48:24 +02:00
module.exports = Attribute;