2021-04-25 20:00:42 +02:00
|
|
|
"use strict";
|
|
|
|
|
2021-04-25 21:19:18 +02:00
|
|
|
const utils = require('../../utils');
|
|
|
|
|
2021-04-30 22:13:13 +02:00
|
|
|
let becca = null;
|
2021-04-25 21:19:18 +02:00
|
|
|
let repo = null;
|
|
|
|
|
2021-04-25 20:00:42 +02:00
|
|
|
class AbstractEntity {
|
|
|
|
beforeSaving() {
|
|
|
|
this.generateIdIfNecessary();
|
|
|
|
}
|
|
|
|
|
|
|
|
generateIdIfNecessary() {
|
|
|
|
if (!this[this.constructor.primaryKeyName]) {
|
|
|
|
this[this.constructor.primaryKeyName] = utils.newEntityId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generateHash() {
|
|
|
|
let contentToHash = "";
|
|
|
|
|
|
|
|
for (const propertyName of this.constructor.hashedProperties) {
|
|
|
|
contentToHash += "|" + this[propertyName];
|
|
|
|
}
|
|
|
|
|
|
|
|
return utils.hash(contentToHash).substr(0, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUtcDateChanged() {
|
2021-04-25 22:02:32 +02:00
|
|
|
// FIXME
|
|
|
|
return this.utcDateModified || this.utcDateCreated || "FAKE";
|
2021-04-25 20:00:42 +02:00
|
|
|
}
|
|
|
|
|
2021-04-30 22:13:13 +02:00
|
|
|
get becca() {
|
|
|
|
if (!becca) {
|
|
|
|
becca = require('../becca');
|
|
|
|
}
|
|
|
|
|
|
|
|
return becca;
|
|
|
|
}
|
|
|
|
|
|
|
|
// temporarily needed for saving entities
|
2021-04-25 20:00:42 +02:00
|
|
|
get repository() {
|
|
|
|
if (!repo) {
|
2021-04-25 21:19:18 +02:00
|
|
|
repo = require('../../repository');
|
2021-04-25 20:00:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return repo;
|
|
|
|
}
|
|
|
|
|
|
|
|
save() {
|
|
|
|
this.repository.updateEntity(this);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AbstractEntity;
|