/**
 * Base data service that provide dataUtils and other default helper functions
 * @constructor
 * @type {XXXServiceBase}
 * @extends Base
 * @ignore
 */
var XXXServiceBase = Class.create();
XXXServiceBase.prototype = Object.extendsObject(XXXBase, /** @lends BACServiceBase.prototype */ {


    /** @member {string} */
    TABLE: '',

    /** @constant {String} */
    LOG_PROPERTY: 'base_script_include.log.level',

    /**
     * @return {XXXServiceBase}
     */
    initialize: function () {
        XXXBase.prototype.initialize.call(this);
        return this;
    },

    /**
     * Returns an empty initialized GlideRecord for the current table
     * @return {GlideRecord} return initialized GlideRecord
     */
    getEmptyRecord: function () {
        var gr = new GlideRecord(this.TABLE);
        gr.newRecord();
        return gr;
    },

    /**
     * @param {string} id
     * @returns {GlideRecord|null}
     */
    getGlideRecordByID: function (id) {
        var gr = new GlideRecord(this.TABLE);
        if (gr.get(id)) {
            return gr;
        }
        return null;
    },

    /**
     * @param {GlideRecord|GlideRecordSecure} grToValidate
     * @param {string} encodedQuery
     * @returns {string[]|null}
     */
    getSysIDArrayByCondition: function (grToValidate, encodedQuery) {
        var matchedGR = [];
        var grSource = this.getGlideRecordByEncodedQuery(encodedQuery);
        if (grSource) {
            while (grSource.next()) {
                // check if record fulfill conditions on interface record
                var hasMatch = GlideFilter.checkRecord(grToValidate, grSource.condition);
                if (hasMatch) {
                    gs.debug('Found match: ' + grSource.name);
                    matchedGR.push('' + grSource.sys_id);
                }
            }
            if (matchedGR.length > 0) {
                gs.debug('Found results');
                return matchedGR;
            }
            gs.debug('Nothing in matchedGR');
            return null;

        }
        gs.debug('No matches found for this GlideRecord.');
        return null;

    },

    /**
     * @param {string} key
     * @param {*} value
     * @returns {GlideRecord|GlideRecordSecure|null}
     */
    getGlideRecordByKeyValue: function (key, value) {
		this.logger.logDebug('result id ' + this.TABLE);
        var gr = new GlideRecord(this.TABLE);
        if (gr.get(key, value)) {
            return gr;
        }
        return null;

    },

    /**
     * @param {string} encodedQuery
     * @returns {number|null}
     */
    getCountAggregate: function (encodedQuery) {
        var gr = new GlideAggregate(this.TABLE);
        gr.addAggregate('COUNT');
        if (encodedQuery != '') {
            gr.addEncodedQuery(encodedQuery);
        }
        gr.query();
        if (gr.next()) {
            return gr.getAggregate('COUNT');
        }
        return null;


    },

    /**
     * @param {string} encodedQuery
     * @returns {GlideRecord|GlideRecordSecure|null}
     */
    getGlideRecordByEncodedQuery: function (encodedQuery) {
        var gr = new GlideRecord(this.TABLE);
        if (encodedQuery != '') {
            gr.addEncodedQuery(encodedQuery);
        }
        gr.query();
        if (gr.hasNext()) {
            return gr;
        }
        return null;

    },

    /**
     * @param {GlideRecord|GlideRecordSecure} recordGr
     * @param {string} parentField
     * @returns {boolean}
     */
    checkParentRecursion: function (recordGr, parentField) {
        var table = this.TABLE;
        var recursion = false;
        var parentId = recordGr.getValue(parentField);
        if (parentId == recordGr.getValue('sys_id')) {
            recursion = true;
        } else {
            checkRecursion(recordGr);
        }

        function checkRecursion(recordGr) {
            var childGr = new GlideRecord(table);
            childGr.addQuery(parentField, recordGr.getValue('sys_id'));
            childGr.query();
            while (childGr.next()) {
                if (childGr.getValue('sys_id') == parentId) {
                    recursion = true;
                    return;
                }

                checkRecursion(childGr);
            }
        }

        return recursion;
    },

    type: 'XXXServiceBase'
});