﻿/*
Javascript data object holding entity data
*/

var EntityObject = Class.create();

EntityObject.prototype =
{
    // Constructor
    initialize : function(piEntityXml)
    {
        // Declare variable holding the id of the content entity
        this.Id;
        // Declare variable holding the type information about the entity
        this.Type = ''
        
        // Declare variable holding the entity's xml data
        this._xmlData = piEntityXml;
        
        // Declare variable indicating if the last interaction was successful
        this.Success = false;
        // Declare variable holding the success message in case the last interaction was not successful
        this.SuccessMessage = '';
        // Declare Array variable containing the notifications from the xml message
        this.Notifications = new Array();
        
        // Call ProcessXml function to parse xml entity data
        this.ProcessXml();
    },
    
    // Parses the xml entity data
    ProcessXml : function()
    {        
        try
        {
            // Check for errors and fill this.Success and this.SuccessMessage
            var successInfoNode = this._xmlData.SelectSingleNode('IS_XML_MESSAGE/IS_XML_SUCCESS_INFO/SUCCESS');
            if(!Helpers.IsEmptyXmlNode(successInfoNode))
            {
                // Fill the success variable indicating the last intercxtion's success status
                if(successInfoNode.text.toLowerCase() == 'false')
                {
                    this.Success = false;
                }
                else
                {
                    this.Success = true;
                }
                
                if((this.Success == false) && (!Helpers.IsEmptyXmlNode(this._xmlData.SelectSingleNode('IS_XML_MESSAGE/IS_XML_SUCCESS_INFO/MESSAGE'))))
                {
                    // Select the message node of the success info section
                    this.SuccessMessage = this._xmlData.SelectSingleNode('IS_XML_MESSAGE/IS_XML_SUCCESS_INFO/MESSAGE').text;
                    return;
                }
            }
            else
            {
                // If no success information is provided, assume that the last operation 
                // could not the processed and set success information accordingly
                this.Success = false;
                this.SuccessMessage = 'The entity data could not be loaded.';
                return;
            }
            
            // Get Notifications
            var notificationNodes = this._xmlData.SelectNodes('IS_XML_MESSAGE/IS_XML_NOTIFICATIONS/NOTIFICATION');
            // Iterate through notification xml node list and create Notification object for each of the nodes
            for(var h = 0; h < notificationNodes.length; h++)
            {
                var notificationObject = new NotificationObject(notificationNodes[h]);
                // Push Notification object into the Notifications Array
                this.Notifications.push(notificationObject);
            }
            
            // Fill the type information
            this.Type = this._xmlData.SelectSingleNode('IS_XML_MESSAGE/IS_XML_DATA/IS_ENTITY').attributes.getNamedItem('TYPE').value;
            // Fill the id
            this.Id = this._xmlData.SelectSingleNode('IS_XML_MESSAGE/IS_XML_DATA/IS_ENTITY').attributes.getNamedItem('ID').value;
            
            // Get all group nodes
            var groupNodes = this._xmlData.SelectNodes('IS_XML_MESSAGE/IS_XML_DATA/IS_ENTITY/GROUP');
            // Iterate through groupNodes
            for(var i = 0; i < groupNodes.length; i++)
            {
                // Get the group name
                var groupName = groupNodes[i].attributes.getNamedItem("NAME").value.capitalize();
                // Create new Hash object representing the current group
                this[groupName] = new Hash();
                
                // Get all field nodes
                var fieldNodes = groupNodes[i].SelectNodes('FIELD');
                // Iterate through fieldNodes
                for(var x = 0; x < fieldNodes.length; x++)
                {
                    // Create new FieldObject
                    var fieldObject = new FieldObject(fieldNodes[x], this.Type);
                    // Push fieldObject into group Hash
                    var moin = this[groupName][fieldObject.SystemName.capitalize()] = fieldObject;
                }
                
                // Get all reference nodes
                var referenceNodes = groupNodes[i].SelectNodes('REFERENCE');
                // Check if referenceNodes contains any items and create reference Array if so
                if(referenceNodes.length > 0)
                {
                    this[groupName]['References'] = new Array();
                }
                // Iterate through referenceNodes
                for(var z = 0; z < referenceNodes.length; z++)
                {
                    // Declare reference property  variables
                    var refId = '';
                    var refType = '';
                    var refTarget = '';
                    var refSort = '';
                    
                    // Try to read and fill reference id
                    if(referenceNodes[z].text != null)
                    {
                        refId = referenceNodes[z].text;
                    }
                    
                    // Try to read and fill reference type
                    if(referenceNodes[z].attributes.getNamedItem('TYPE') != null)
                    {
                        refType = referenceNodes[z].attributes.getNamedItem('TYPE').value;
                    }
                    
                    // Try to read and fill reference target html node
                    if(referenceNodes[z].attributes.getNamedItem('PARENT_ELEM_ID') != null)
                    {
                        refTarget = referenceNodes[z].attributes.getNamedItem('PARENT_ELEM_ID').value;
                    }
                    
                    // Try to read and fill reference sort
                    if(referenceNodes[z].attributes.getNamedItem('SORT') != null)
                    {
                        refSort = referenceNodes[z].attributes.getNamedItem('SORT').value;
                    }
                    
                    // Create a Hash for the entity reference
                    var referenceHash = $H({ Id: refId, Type: refType, Target: refTarget, Sort: refSort });
                    // Push the reference Hash into the reference Array
                    this[groupName]['References'].push(referenceHash);
                }
                
                // Get all sub group nodes
                var subGroupNodes = groupNodes[i].SelectNodes('GROUP');
                // Iterate through subGroupNodes
                for(var y = 0; y < subGroupNodes.length; y++)
                {
                    // Get the sub group name
                    var subGroupName = subGroupNodes[y].attributes.getNamedItem('NAME').value.capitalize();
                    // Create new Hash object representing the current sub group
                    this[groupName][subGroupName] = new Hash();
                    
                    // Get all field nodes of the sub group
                    var subFieldNodes = subGroupNodes[y].SelectNodes('FIELD');
                    // Iterate through subFieldNodes
                    for(var v = 0; v < subFieldNodes.length; v++)
                    {
                        // Create new FieldObject
                        var subFieldObject = new FieldObject(subFieldNodes[v], this.Type);
                        // Push subFieldObject into sub group Hash
                        this[groupName][subGroupName][subFieldObject.SystemName.capitalize()] = subFieldObject;
                    }
                }
            }
        }
        catch(e)
        {
            var entityType = "ENTITYOBJECT";
            if((this.Type != null) && (!this.Type.blank()))
            {
                entityType = this.Type;
            }
            
            var entityId = null;
            if((this.Id != null) && (!this.Id.blank()))
            {
                entityId = this.Id;
            }
            
            Logs.WriteException('EXCEPTION_GENERAL', 'ProcessXml', arguments, entityType.toUpperCase(), entityId, e, false)
        }
    }
}