﻿/* ********************************************************** */
/* KeaneCacheManagerPrototype declaration                     */        
/* ********************************************************** */
var KeaneCacheManagerPrototype = function(spec) {
    // Private properties/methods.
    var 
        that = {},
        constants = spec.constants,
        cacheData = null;

    // Deserializes cache data embedded within the page.
    var deserializeCacheData = function() {
        // Use a dev express hidden field and deserialize from json.
        var raw, deserialized;
        raw = _cacheData.Get('cacheData');
        if (raw != null) {
            deserialized = eval('(' + raw + ')');
        }
        if (deserialized != null) {
            cacheData = deserialized;
        }
    };

    // Updates the cached entity ID (after an insert).
    // *** This is necessary so that after an insert the 
    // *** deserialized entity ID on the server side is upto date.
    var updateEntityId = function(entityId) {
        var raw, updatedValue;
        cacheData.entityInfo.id = entityId;
        raw = _cacheData.Get('cacheData');
        if (raw) {
            updatedValue = ':';
            updatedValue += entityId;
            raw = raw.replace(':0', updatedValue);
            _cacheData.Set('cacheData', raw);
        }
    }

    // Validates cache data embedded within the page.
    var validateCacheData = function() {
        // Defensive coding.
        if (cacheData == null) {
            KeaneUtils.throwError("Developer message :: Page cache data has not been injected into the page.", "Page Cache Data Deserialization Error");
        }

        // Exception if expected attributes do not exist.        
        if ((cacheData.hasOwnProperty("commandInfoCollection") !== true) ||
            (cacheData.hasOwnProperty("commandStateCollection") !== true) ||
            (cacheData.hasOwnProperty("defaultMessageInfo") !== true) ||
            (cacheData.defaultMessageInfo.hasOwnProperty("error") !== true) ||
            (cacheData.defaultMessageInfo.hasOwnProperty("errorSuffix") !== true) ||
            (cacheData.defaultMessageInfo.hasOwnProperty("progress") !== true) ||
            (cacheData.defaultMessageInfo.hasOwnProperty("progressSuffix") !== true) ||
            (cacheData.defaultMessageInfo.hasOwnProperty("redirect") !== true) ||
            (cacheData.defaultMessageInfo.hasOwnProperty("redirectSuffix") !== true) ||
            (cacheData.hasOwnProperty("entityInfo") !== true) ||
            (cacheData.entityInfo.hasOwnProperty("id") !== true) ||
            (cacheData.entityInfo.hasOwnProperty("typeKey") !== true) ||
            (cacheData.hasOwnProperty("nameValuePairCollection") !== true) ||
            (cacheData.hasOwnProperty("pageLoadCommand") !== true) ||
            (cacheData.hasOwnProperty("pageLoadCommandData") !== true) ||
            (cacheData.hasOwnProperty("pageLoadMessage") !== true) ||
            (cacheData.hasOwnProperty("standardInfo") !== true) ||
            (cacheData.standardInfo.hasOwnProperty("appName") !== true) ||
            (cacheData.standardInfo.hasOwnProperty("appVersion") !== true) ||
            (cacheData.standardInfo.hasOwnProperty("appReleaseDate") !== true) ||
            (cacheData.standardInfo.hasOwnProperty("clientName") !== true) ||
            (cacheData.standardInfo.hasOwnProperty("userDisplayName") !== true) ||
            (cacheData.hasOwnProperty("editMode") !== true)) {
            KeaneUtils.throwError("Developer message :: Page cache data failed standard validation.", "Page Cache Data Deserialization Error");
        }

        // Validation has passed.
        return true;
    };

    // Standard initialisation routine.
    var initialise = function() {
        deserializeCacheData();
        if (validateCacheData()) {
            that.data = cacheData;
            that.defaultMessageInfo = cacheData.defaultMessageInfo;
            that.standardInfo = cacheData.standardInfo;
            that.entityInfo = cacheData.entityInfo;
            that.pageLoadCommand = cacheData.pageLoadCommand;
            that.pageLoadCommandData = cacheData.pageLoadCommandData;
            that.pageLoadMessage = cacheData.pageLoadMessage;
            that.editMode = cacheData.editMode;
        }
    };

    // Derives a named value item from the cache data.
    var getNamedValue = function(name) {
        var result = null, i;
        if (name != null && cacheData != null) {
            for (i = 0; i < cacheData.nameValuePairCollection.length; i++) {
                var nameValue = cacheData.nameValuePairCollection[i];
                if (nameValue.name === name) {
                    result = nameValue.value;
                    break;
                }
            }
        }
        return result;
    };

    // Returns all cached command information.
    var getCommandInfoCollection = function() {
        var result = null;
        if (cacheData != null) {
            result = cacheData.commandInfoCollection;
        }
        return result;
    };

    // Clears all cached command information.
    var clearCommandInfoCollection = function() {
        var result = null;
        if (cacheData != null) {
            cacheData.commandInfoCollection = [];
        }
        return result;
    };

    // Returns all cached command state information.
    var getCommandStateCollection = function() {
        var result = null;
        if (cacheData != null) {
            result = cacheData.commandStateCollection;
        }
        return result;
    };

    // Clears all cached command state information.
    var clearCommandStateCollection = function() {
        var result = null;
        if (cacheData != null) {
            cacheData.commandStateCollection = [];
        }
        return result;
    };

    // Retrieves cached command information.
    var getCommandInfo = function(name) {
        var result = null, i;
        var commandInfoCollection = getCommandInfoCollection();
        if (name && commandInfoCollection) {
            for (i = 0; i < commandInfoCollection.length; i++) {
                var commandInfo = commandInfoCollection[i];
                if (commandInfo.name === name) {
                    result = commandInfo;
                    break;
                }
            }
        }
        return result;
    };

    // Gets the edit mode.
    // 0 = Unspecified (the default).
    // 1 = View (The user is viewing data)
    // 2 = Insert (The user is inserting data)
    // 3 = Update (The user is updating data)
    var getEditMode = function() {
        var result = 0;
        if (cacheData != null) {
            result = cacheData.editMode;
        }
        return result;
    };

    // Public properties/methods.
    that.initialise = initialise;
    that.getNamedValue = getNamedValue;
    that.getCommandInfo = getCommandInfo;
    that.getCommandInfoCollection = getCommandInfoCollection;
    that.clearCommandInfoCollection = clearCommandInfoCollection;
    that.getCommandStateCollection = getCommandStateCollection;
    that.clearCommandStateCollection = clearCommandStateCollection;
    that.updateEntityId = updateEntityId;
    that.getEditMode = getEditMode;

    // Return constructed object.
    return that;
}