/* Copyright (c) 2020, 2023, The Unified Company. This code is part of Unify. This program is free software; you can redistribute it and/or modify it under the terms of the ESA Software Community License - Strong Copyleft LICENSE, as published by the ESA. See the ESA Software Community License - Strong Copyleft LICENSE, for more details. https://unifyjs.org */ import Console from '../console.js'; import tools from '../../unify/tools.js'; import database from '../database.js'; import shared from '../../unify/shared.js'; import userManager from '../userManager.js'; import defaultObject from '../../unify/defaultObject.js'; import consoleColors from '../../unify/consoleColors.js'; export default class tableController { __className = "tableController"; __sourcePath = "./framework/server/cachedControllers/tableController.js"; __publicMethods = "save,delete,get,create,count"; id = 3; constructor() { Console.setFilename("tableController.js"); } getLastID() { return ++this.id; } getParentObject( object ) { if( object.type == "table" ) { return object; } else { return object.parent; } } async save( object, client, eventName ) { var parentObject = this.getParentObject( object ); if( !parentObject.isAllowed( client.user, "WRITE" ) ) { parentObject = parentObject.deserialize(); return parentObject; } parentObject.signed.value = true; if( parentObject.id == 0 || !parentObject.id ) { var parentObject = this.create( parentObject, client, eventName ); } else { parentObject.save(); } //this.updateBidirectionalCollection( parentObject, collection, client, eventName ); return parentObject; } updateBidirectionalCollection( object, collection, client, eventName ) { if( object.parent.getCollection ) { var collection = object.parent.getCollection(); if( collection ) { this.objectManager.updateClient( collection, client, eventName ); } } } updateBidirectionalObject( object, client, eventName ) { if( eventName ) { this.objectManager.updateClient( object, client, eventName ); } } registerBidirectionalObject( object ) { this.objectManager.registerObject( object, client ); var collection = object.getCollection(); if( collection ) { this.objectManager.registerObject( collection, client ); } } delete( object, client, eventName ) { if( !object.isAllowed( client.user, "DELETE" ) ) { object = object.deserialize(); return object; } var result = object.delete(); //this.updateBidirectionalCollection( object ); return object; } getObject( object, client, eventName ) { if( object.preprocess ) { object.preprocess( object, client, eventName ); } //console.log("object.get"); object.get( client ); //console.log("//object.get"); if( object.process ) { object.process( object, client, eventName ); } } get( object, client, eventName ) { object.updated = false; if( !object.isAllowed( client.user, "READ" ) ) { object.deserialize(); object.updateChildrenPermissions( object, client.user ); return object; } if( object.get ) { if( object.type == "renderCollection" ) { //console.log("clearCollection"); //object.removeChildren( ); //object.getChildrenRenderCollections( client ); } this.getObject( object, client, eventName ); } //this.registerBidirectionalObject( object ); return object; } insertObject( object, client ) { var last_id = database.insertRow( object ); object.permissions = userManager.computePermissions( object, client.user ); object.id = last_id; return object; } insertCollection( object, last_insert_id ) { var collection = object.getCollection(); if( collection ) { collection.addObject( object ); //if( !collection.id ) { console.log(""); console.log(consoleColors.green( "create->insertCollection(" ), collection.id, last_insert_id, consoleColors.green(")") ); var parent_id = collection.parent.id; var insert = collection.queryInsert( parent_id, last_insert_id ); //} } return insert; } create( object, client, eventName ){ if( !object.isAllowed( client.user, "WRITE" ) ) { object = object.deserialize(); return object; } object = this.insertObject( object, client ); //this.objectManager.registerObject( object, client ); this.insertCollection( object, object.id ); //console.log("return object", object); return object; } count( collection, client, eventName ) { collection.count = collection.countRows(); if( collection.count == 0 ) { collection.count = 0; } return collection; } }