580 lines
10 KiB
JavaScript
580 lines
10 KiB
JavaScript
/*
|
|
|
|
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 tools from './tools.js';
|
|
|
|
import unify from './unify.js';
|
|
|
|
import defaultObject from './defaultObject.js';
|
|
|
|
import datatype from '../unify/datatype.js';
|
|
|
|
import placeholder from '../unify/placeholder.js';
|
|
|
|
import serialized from '../unify/serialized.js';
|
|
|
|
import permissionObject from '../unify/permissionObject.js';
|
|
|
|
import consoleColors from './consoleColors.js';
|
|
|
|
class objectSerializer{
|
|
|
|
|
|
serialize( sourceObject, targetObject, client, secure = false ) {
|
|
|
|
console.log("\n");
|
|
|
|
console.log( consoleColors.yellow("Serialize Object" ), consoleColors.yellow( targetObject.getClassName() ) );
|
|
|
|
var serializedObject = this.serializeObject( sourceObject, targetObject, client, secure );
|
|
|
|
this.findColumns( targetObject );
|
|
|
|
var defaultObjectInstance = new defaultObject();
|
|
|
|
console.log("\n");
|
|
|
|
return defaultObjectInstance.agregateDefaultObject( serializedObject );
|
|
|
|
}
|
|
|
|
serializeObject( sourceObject, targetObject, client, depth = 0, secure ) {
|
|
|
|
if( !sourceObject ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unify.extend( targetObject );
|
|
|
|
if( tools.getEnvironment() == "Browser" ) {
|
|
|
|
this.serializeID( sourceObject, targetObject ); // save
|
|
|
|
}
|
|
|
|
this.serializeProperties( sourceObject, targetObject, secure ); // save
|
|
|
|
this.serializeChildren( sourceObject, targetObject, depth, secure ); // save
|
|
|
|
this.serializePermissionObjects( sourceObject, targetObject ); // save
|
|
|
|
return targetObject;
|
|
|
|
}
|
|
|
|
serializeChildren( clientObject, targetObject, depth, secure ) {
|
|
|
|
if( !targetObject.getChildren ){
|
|
|
|
console.error( "This object cannot be serialized", targetObject );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var children = targetObject.getChildren();
|
|
|
|
for( var c = 0; c < children.length; c++ ) {
|
|
|
|
var child = children[c];
|
|
|
|
if( tools.getEnvironment() == "Node" ) {
|
|
|
|
if( child.type == "collection" || child.type == "table" || child.type == "renderCollection" ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if( child.propertyName == "groups" ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.serializeChild( child, clientObject, targetObject, depth, secure );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serializeChild( child, clientObject, targetObject, depth, secure ) {
|
|
|
|
var propertyName = tools.validateString( child.propertyName ); // target class is save
|
|
|
|
var clientObjectChild = clientObject[ propertyName ];
|
|
|
|
console.log(" ".repeat( depth ), consoleColors.yellow(" - set value of" ), consoleColors.yellow( propertyName ) );
|
|
|
|
|
|
// bug fixen needs todo path
|
|
if( clientObjectChild ) {
|
|
|
|
targetObject[ propertyName ] = this.serializeObject( clientObjectChild, child, depth++, secure );
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
serializePermission( sourceObject, targetObject ) {
|
|
|
|
var targetObject = new permissionObject();
|
|
|
|
if( !sourceObject ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if( typeof sourceObject.WRITE == "boolean" ) {
|
|
|
|
targetObject.WRITE = tools.validateBoolean( sourceObject.WRITE );
|
|
|
|
}
|
|
|
|
if( typeof sourceObject.DELETE == "boolean" ) {
|
|
|
|
targetObject.DELETE = tools.validateBoolean( sourceObject.DELETE );
|
|
|
|
}
|
|
|
|
if( typeof sourceObject.READ == "boolean" ) {
|
|
|
|
targetObject.READ = tools.validateBoolean( sourceObject.READ );
|
|
|
|
}
|
|
|
|
return targetObject;
|
|
|
|
}
|
|
|
|
createPermissionObject( currentPermissionObject ) {
|
|
|
|
var permissionObject = new serialized();
|
|
|
|
permissionObject.path = tools.validateString( currentPermissionObject.path );
|
|
|
|
permissionObject.permission = this.validatePermission( currentPermissionObject.permission );
|
|
|
|
}
|
|
|
|
serializePermissionObject( currentPermissionObject, targetObject, c ) {
|
|
|
|
var permissionObject = this.createPermissionObject( currentPermissionObject );
|
|
|
|
targetObject.permissionObjects[ c ] = permissionObject;
|
|
|
|
}
|
|
|
|
serializePermissionObjects( sourceObject, targetObject ) {
|
|
|
|
if( !sourceObject.permissionObjects ) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if( !tools.isArray( sourceObject.permissionObjects ) ) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var permissionObjects = sourceObject.permissionObjects;
|
|
|
|
targetObject.permissionObjects = new Array();
|
|
|
|
for( var c = 0; c < permissionObjects.length;c++) {
|
|
|
|
var currentPermissionObject = permissionObjects[c];
|
|
|
|
this.serializePermissionObject( currentPermissionObject, targetObject, c );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serializeFilterObject( sourceObject, targetObject ) {
|
|
|
|
if( sourceObject.filterObject ) {
|
|
|
|
targetObject.filterObject = new serialized();
|
|
|
|
this.serializeObject( sourceObject.filterObject, targetObject.filterObject );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serializeIDNumber( sourceObject, targetObject ) {
|
|
|
|
var id = tools.validateNumber( sourceObject.id );
|
|
|
|
targetObject.id = id;
|
|
|
|
targetObject.setID( id );
|
|
|
|
}
|
|
|
|
serializeUndefinedID( targetObject ) {
|
|
|
|
if( tools.getEnvironment() == "Node" ) {
|
|
|
|
targetObject.id = false;
|
|
|
|
targetObject.setID( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serializeJoinID( sourceObject, targetObject ) {
|
|
|
|
var join_id = tools.validateNumber( sourceObject.join_id );
|
|
|
|
targetObject.join_id = join_id;
|
|
|
|
targetObject.setJoinID( id );
|
|
|
|
}
|
|
|
|
serializeID( sourceObject, targetObject ) {
|
|
|
|
if( sourceObject && sourceObject.id ) {
|
|
|
|
this.serializeIDNumber( sourceObject, targetObject );
|
|
|
|
} else {
|
|
|
|
this.serializeUndefinedID( targetObject );
|
|
|
|
}
|
|
|
|
if( sourceObject.join_id ) {
|
|
|
|
this.serializeJoinID( sourceObject, targetObject );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serializeNumberValue( sourceObject, targetObject ) {
|
|
|
|
targetObject.value = sourceObject.value;
|
|
|
|
}
|
|
|
|
serializeBooleanValue( sourceObject, targetObject ) {
|
|
|
|
if( !sourceObject.value ) {
|
|
|
|
targetObject.value = false;
|
|
|
|
} else {
|
|
|
|
targetObject.value = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serializeEmptyValue( targetObject ) {
|
|
|
|
targetObject.value = "";
|
|
|
|
|
|
}
|
|
|
|
serializeStringValue( sourceObject, targetObject ) {
|
|
|
|
targetObject.value = tools.validateValue( sourceObject.value );
|
|
|
|
}
|
|
|
|
serializeFalse( targetObject ) {
|
|
|
|
if( typeof targetObject.value ) {
|
|
|
|
targetObject.value = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serializeValue( sourceObject, targetObject ) {
|
|
|
|
if( typeof sourceObject.value == "number" ) {
|
|
|
|
this.serializeNumberValue( sourceObject, targetObject );
|
|
|
|
} else if( sourceObject.datatype == "BOOLEAN" ) {
|
|
|
|
this.serializeBooleanValue( sourceObject, targetObject );
|
|
|
|
} else if( sourceObject.value == "" ) {
|
|
|
|
this.serializeEmptyValue( targetObject );
|
|
|
|
} else if( typeof sourceObject.value && sourceObject.value != "undefined" ) {
|
|
|
|
this.serializeStringValue( sourceObject, targetObject );
|
|
|
|
} else {
|
|
|
|
this.serializeFalse( targetObject );
|
|
|
|
}
|
|
|
|
return targetObject;
|
|
|
|
}
|
|
|
|
serializeProperties( sourceObject, targetObject, method, type, secure ) {
|
|
|
|
|
|
this.serializeValue( sourceObject, targetObject );
|
|
|
|
if( !secure ) {
|
|
|
|
if( sourceObject.permissions ) {
|
|
|
|
targetObject.permissions = this.validatePermission( sourceObject.permissions );
|
|
|
|
}
|
|
/*
|
|
if( !method || method == "callNodeMethod" ) {
|
|
|
|
if( sourceObject.nodeMethodName ) {
|
|
|
|
targetObject.nodeMethodName = tools.validateValue( sourceObject.nodeMethodName );
|
|
|
|
}
|
|
|
|
if( sourceObject.nodeMethodArguments ) {
|
|
|
|
targetObject.nodeMethodArguments = tools.validateValue( sourceObject.nodeMethodArguments );
|
|
|
|
}
|
|
|
|
}
|
|
*/
|
|
}
|
|
|
|
|
|
//if( sourceObject.rowID ) {
|
|
|
|
// targetObject.rowID = tools.validateNumber( sourceObject.rowID );
|
|
|
|
//}
|
|
/*
|
|
if( typeof sourceObject.count ) {
|
|
|
|
targetObject.count = tools.validateNumber( sourceObject.count );
|
|
|
|
}
|
|
|
|
|
|
*/
|
|
/*
|
|
if( typeof sourceObject.WRITE == "boolean" ) {
|
|
|
|
targetObject.WRITE = tools.validateBoolean( sourceObject.WRITE );
|
|
|
|
}
|
|
|
|
if( typeof sourceObject.DELETE == "boolean" ) {
|
|
|
|
targetObject.DELETE = tools.validateBoolean( sourceObject.DELETE );
|
|
|
|
}
|
|
|
|
if( typeof sourceObject.READ == "boolean" ) {
|
|
|
|
targetObject.READ = tools.validateBoolean( sourceObject.READ );
|
|
|
|
}
|
|
*/
|
|
// bug. if enabled permissions dont work.
|
|
|
|
//if( tools.getEnvironment() == "Node" ) {
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//}
|
|
|
|
return targetObject;
|
|
|
|
}
|
|
|
|
validatePermission( sourcePermission ) {
|
|
|
|
var targetPermission = new permissionObject();
|
|
|
|
if( !sourcePermission ) {
|
|
|
|
return targetPermission;
|
|
|
|
}
|
|
|
|
if( typeof sourcePermission.READ == "boolean" ) {
|
|
|
|
targetPermission.READ = tools.validateBoolean( sourcePermission.READ );
|
|
|
|
}
|
|
|
|
targetPermission.WRITE = sourcePermission.WRITE;
|
|
|
|
if( typeof sourcePermission.WRITE == "boolean" ) {
|
|
|
|
targetPermission.WRITE = tools.validateBoolean( sourcePermission.WRITE );
|
|
|
|
}
|
|
|
|
if( typeof sourcePermission.DELETE == "boolean" ) {
|
|
|
|
targetPermission.DELETE = tools.validateBoolean( sourcePermission.DELETE );
|
|
|
|
}
|
|
|
|
return targetPermission;
|
|
|
|
}
|
|
|
|
// Everything above needs to be save.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
copyValueToChild( child, extendedClass, column, table ) {
|
|
|
|
var columnName = tools.getClassName( extendedClass );
|
|
|
|
if( !extendedClass.datatype ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if( !column ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if( !table[ columnName ] ) {
|
|
|
|
return false;
|
|
}
|
|
|
|
child.value = table[ columnName ].value;
|
|
|
|
if( tools.getEnvironment() == "Browser" && child.updateElementContent ) {
|
|
|
|
child.updateElementContent()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
findExtendedClasses( child, parent, table ) {
|
|
|
|
if( child.type == "collection" ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if( child.type == "renderCollection" ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if( !child.datatype ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var extendedClasses = tools.getExtendedClassesMulti( child );
|
|
|
|
for ( var c = 0; c < extendedClasses.length; c++) {
|
|
|
|
var extendedClass = extendedClasses[ c ];
|
|
|
|
var column = tools.isUnifyColumn( extendedClass ) ;
|
|
|
|
this.copyValueToChild( child, extendedClass, column, table );
|
|
|
|
}
|
|
|
|
//console.log("This is an floating column", child, extendedClasses );
|
|
|
|
}
|
|
|
|
findColumns( parent, table = false ) {
|
|
|
|
if( !parent.getChildren ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unify.extend( parent );
|
|
|
|
var children = parent.getChildren();
|
|
|
|
for ( var i = 0; i < children.length; i++ ) {
|
|
|
|
var child = children[i];
|
|
|
|
|
|
if( parent.type == "table" ) {
|
|
|
|
table = parent;
|
|
|
|
}
|
|
|
|
this.findExtendedClasses( child, parent, table );
|
|
|
|
|
|
//if( child.type != "collection" && child.type != "renderCollection" ) {
|
|
|
|
this.findColumns( child, table );
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
export default new objectSerializer(); |