<commit_before>/**
 * Author: <NAME>
 * Date: 2/20/14
 *
 * This is where services are generated
 */
var Q =     require('q');
var _ =     require('lodash');
var fs =    require('fs');

var commonFilters =     require('../common.filters');
var utils =             require('../utensils');
var eventBus =          require('../event.bus');
var debugHandler =      require('../debug.handler');
var annotationHelper =  require('../annotation.helper');

/**
 * Simple function to clear the cache
 * @param injector
 * @constructor
 */
var ServiceFactory = function (injector) {
    this.cache = {};
    this.filterCache = {};
    this.injector = injector;

    // if we are in debug mode, then add the debug handler
    var pattern = this.injector.debugPattern || '*.*.*.*.*';
    var handler = this.injector.debugHandler || debugHandler;
    if (this.injector.debug) {
        eventBus.on(pattern, handler);
    }
};

 /**
 * Candidate if no slash and the modulePath ends in 'Service'
 * @param modulePath
 * @returns {boolean}
 */
ServiceFactory.prototype.isCandidate = function (modulePath) {
    modulePath = modulePath || '';
    var len = modulePath.length;
    return modulePath.indexOf('/') < 0 && len > 7 &&
        modulePath.substring(modulePath.length - 7) === 'Service';
};

/**
 * This small function is needed in order to create a closure around calls
 * which is dynamically generated for each loop in putItAllTogether()
 *
 * @param calls
 * @returns {Function}
 */
ServiceFactory.prototype.getServiceMethod = function (calls) {
    return function (req) {
        return calls.reduce(Q.when, new Q(req));
    };
};

/**
 * This is used to get an mock filter which will be sending out events
 * @param calls
 * @param eventNameBase
 * @param debugParams
 */
ServiceFactory.prototype.addEventEmitter = function (calls, eventNameBase, debugParams) {

    // don't do anything if we are not in debug mode
    if (!this.injector.debug && debugParams) {
        return;
    }

    // this is essentially a filter which will send the current request to the event bus
    var name = _.extend({}, eventNameBase, debugParams);
    calls.push(function (req) {
        var payload = JSON.parse(JSON.stringify(req));  // copy of data for event
        payload.resource = name.resource + 'Resource';  // passing around the resource data is a waste so don't

        var eventData = {
            name: name,
            payload: payload
        };
        var eventName = name.resource + '.' + name.adapter + '.' + name.method;
        eventName += debugParams ? '.' + name.type + '.' + name.timing : '';
        eventBus.emit(eventName, eventData);
        return new Q(req);
    });
};

/**
 * Get the actual filters for a given set of filter names. The filters are cached locally since
 * they are pretty generic.
 *
 * @param filterNames
 * @param moduleStack
 * @returns {{}}
 */
ServiceFactory.prototype.createFilters = function (filterNames, moduleStack) {
    var filters = {};
    var me = this;

    _.each(filterNames, function (name) {
        if (!me.filterCache[name]) {
            var filterPath = me.injector.servicesDir + '/filters/' + name + '.filter';
            me.filterCache[name] = me.injector.loadModule(filterPath, moduleStack);
        }


        filters[name] = me.filterCache[name];
    });

    return filters;
};

/**
 * Get all the before and after filters for a given adapter and method. This is done by looking at both
 * the filter annotations on an adapter method plus the property values. Property values override the
 * annotations because a subclass can then do the filter overrides without implementing the function. So,
 * the precedence is:
 *<commit_msg>Fixes while removing caller input req<commit_after>/**
 * Author: <NAME>
 * Date: 2/20/14
 *
 * This is where services are generated
 */
var Q =     require('q');
var _ =     require('lodash');
var fs =    require('fs');

var commonFilters =     require('../common.filters');
var utils =             require('../utensils');
var eventBus =          require('../event.bus');
var debugHandler =      require('../debug.handler');
var annotationHelper =  require('../annotation.helper');

/**
 * Simple function to clear the cache
 * @param injector
 * @constructor
 */
var ServiceFactory = function (injector) {
    this.cache = {};
    this.filterCache = {};
    this.injector = injector;

    // if we are in debug mode, then add the debug handler
    var pattern = this.injector.debugPattern || '*.*.*.*.*';
    var handler = this.injector.debugHandler || debugHandler;
    if (this.injector.debug) {
        eventBus.on(pattern, handler);
    }
};

 /**
 * Candidate if no slash and the modulePath ends in 'Service'
 * @param modulePath
 * @returns {boolean}
 */
ServiceFactory.prototype.isCandidate = function (modulePath) {
    modulePath = modulePath || '';
    var len = modulePath.length;
    return modulePath.indexOf('/') < 0 && len > 7 &&
        modulePath.substring(modulePath.length - 7) === 'Service';
};

/**
 * This small function is needed in order to create a closure around calls
 * which is dynamically generated for each loop in putItAllTogether()
 *
 * @param calls
 * @returns {Function}
 */
ServiceFactory.prototype.getServiceMethod = function (calls) {
    return function (req) {
        return calls.reduce(Q.when, new Q(req));
    };
};

/**
 * This is used to get an mock filter which will be sending out events
 * @param calls
 * @param eventNameBase
 * @param debugParams
 */
ServiceFactory.prototype.addEventEmitter = function (calls, eventNameBase, debugParams) {

    // don't do anything if we are not in debug mode
    if (!this.injector.debug && debugParams) {
        return;
    }

    // this is essentially a filter which will send the current request to the event bus
    var name = _.extend({}, eventNameBase, debugParams);
    calls.push(function (req) {
        var payload;

        if (req) {
            payload = JSON.parse(JSON.stringify(req));  // copy of data for event
            payload.resource = name.resource + 'Resource';  // passing around the resource data is a waste so don't
        }

        var eventData = {
            name: name,
            payload: payload
        };
        var eventName = name.resource + '.' + name.adapter + '.' + name.method;
        eventName += debugParams ? '.' + name.type + '.' + name.timing : '';
        eventBus.emit(eventName, eventData);
        return new Q(req);
    });
};

/**
 * Get the actual filters for a given set of filter names. The filters are cached locally since
 * they are pretty generic.
 *
 * @param filterNames
 * @param moduleStack
 * @returns {{}}
 */
ServiceFactory.prototype.createFilters = function (filterNames, moduleStack) {
    var filters = {};
    var me = this;

    _.each(filterNames, function (name) {
        if (!me.filterCache[name]) {
            var filterPath = me.injector.servicesDir + '/filters/' + name + '.filter';
            me.filterCache[name] = me.injector.loadModule(filterPath, moduleStack);
        }

        filters[name] = me.filterCache[name];
    });

    return filters;
};

/**
 * Get all the before and after filters for a given adapter and method. This is done by looking at both
 * the filter annotations on an adapter method plus the property values. Property values override the
 * annotations because a subclass can then do the filter overrides without implementing the function. So,
 * the precedence is:
 *
{'fraction_non_alphanumeric': 0.12406316916488223, 'fraction_numerical': 0.0021413276231263384, 'mean_word_length': 2.82642089093702, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '646634', 'n_tokens_mistral': 2079, 'n_tokens_neox': 2042, 'n_words': 702}