<commit_before>var api = {};
(function(api, core) {
    var log = new Log('lifecycle');
    var CHECKITEM_TOKEN = 'checkItems';
    var TRANSITION_EXECUTION = 'transitionExecution';

    function Lifecycle(definiton) {
        this.definition = definiton;
    }
    Lifecycle.prototype.getName = function() {
        if (!this.definition.name) {
            throw 'Unable to locate name attribute in the lifecycle definition ';
        }
        return this.definition.name;
    };
    /**
     * The function will obtain the next state of states from the current state
     * @return An array containing the name of the next set of states
     */
    Lifecycle.prototype.nextStates = function(currentStateName) {
        var currentStateName = currentStateName ? currentStateName.toLowerCase() : currentStateName;
        var states = this.definition.configuration.lifecycle.scxml.state;
        var nextStates = [];
        if (!states) {
            throw 'The lifecycle : ' + this.getName() + ' does not have any state information.Make sure that the states are defined in the scxml definition.';
        }
        if (!states[currentStateName]) {
            log.warn('The state: ' + currentStateName + ' is not present in the lifecycle: ' + this.getName());
            return nextStates;
        }
        if (!states[currentStateName].transition) {
            log.warn('The state: ' + currentStateName + ' has not defined any transitions in the lifecycle: ' + this.getName());
            return nextStates;
        }
        var transitions = states[currentStateName].transition;
        for (var index = 0; index < transitions.length; index++) {
            var transition = {};
            transition.state = transitions[index].target;
            transition.action = transitions[index].event;
            nextStates.push(transition);
        }
        return nextStates;
    };
    /**
     * The function builds a state object given raw state information
     * @param  {[type]} rawState [description]
     * @return {[type]}          [description]
     */
    var buildStateObject = function(rawState) {
        var state = {};
        state.id = rawState.id;
        if (!rawState.datamodel) {
            log.warn('Unable to read data model of the state ');
            return state;
        }
        var checkItems = getCheckitems(rawState.data);
        state.checkItems = checkItems;
        return state;
    };
    /**
     * The function returns the check list items for the current state
     * @param  {[type]} data [description]
     * @return {[type]}      [description]
     */
    var getCheckitems = function(data) {
        var items = [];
        for (var index in data) {
            item = data[index];
            if (item.name == CHECKITEM_TOKEN) {
                return item.item || [];
            }
        }
        return items;
    };
    /**
     * The function returns details about the current state
     * @param  {[type]} name The name of the state
     * @return A json object representing the state
     */
    Lifecycle.prototype.state = function(stateName) {
        //Convert the state to lowercase
        var stateName = stateName ? stateName.toLowerCase() : stateName;
        var states = this.definition.configuration.lifecycle.scxml.state;
        var state = {};
        if (!states) {
            throw 'The lifecycle : ' + this.getName() + ' does not have any state information.Make sure that the states are defined in the scxml definition.';
        }
        if (!states[stateName]) {
            log.warn('The state: ' + stateName + ' is not present in the lifecycle: ' + this.getName());
            return state;
        }
        var rawState = states[stateName];
        //Process the raw state 
        state= buildStateObject(rawState);
        //Add the next states
        state.nextStates = this.nextStates(stateName);
        return state;
    };
    /**
     * The function returns the action that can cause transitions from the fromState to the toState
     * @param  {[type]} fromState The from state
     * @param  {[type]} toState   [description]
     * @return {[type]}           [description]
     */
    Lifecycle.prototype.transitionAction = function(fromState, toState) {
        var fromState = fromState ? fromState.toLowerCase() : fromState;
        var toState = toState ? toState.toLowerCase() : toState;
        //Get the list of states that can be reached from the fromState
        var states = this.nextStates(fromState);
        if (states.length == 0) {
            log.warn('There is no way to move from ' + fromState + ' to ' + toState + ' in lifecycle: ' + this.getName());
            return null;
        }
        for (var index = 0; index < states.length; index++) {
            if (states[index].state.toLowerCase() == toState.toLowerCase()) {
                return states[index].action;
            }
        }
        log.warn('There is no transition action to move from ' + fromState + ' to ' + toState + ' in lifecycle: ' + this.getName());
        return null;
    };
    /**
     * The function will return an array of execution event parameters.These parameters
     * are consumed by the executor for the event
     * @param  {[type]} state The state for which the execution event must be returned
     * @param  action The transition action for this state
     * @return {[type]}       An array of transition execution event parameters
     */
    Lifecycle.prototype.transitionExecution = function(state, action) {
        var state = state ? state.toLowerCase() : state;
        var action = action ? action.toLowerCase() : action;
        var states = this.definition.configuration.lifecycle.scxml.state;
        var parameters = [];
        if (!states) {
            throw 'The lifecycle : ' + this.getName() + ' does not have any state information.Make sure that the states are defined in the scxml definition.';
        }
        if (!states[state]) {
            log.warn('The state: ' + state + ' is not present in the lifecycle: ' + this.getName());
            return parameters;
        }
        var data = states[state].datamodel.data;
        if (!data) {
            log.warn('The lifecycle: ' + this.getName() + ' does not have a data property declared.Unable to obtain execution events.');
            return parameters;
        }
        var item;
        for (var index in data) {
            item = data[index];
            if (item.name == 'transitionExecution') {
                var executions = item.execution;
                var execution;
                if (executions) {
                    //Look for the event triggered by the provided action
                    for (var exIndex in executions) {
                        execution = executions[exIndex];
                        //Check if the event matches the action
                        if (execution.forEvent.toLowerCase() == action) {
                            parameters = execution.parameter || [];
                            return parameters;
                        }
                    }
                }
            }
        }
        return parameters;
    };
    // var getTenantId = function(session) {
    //     var server = require('store').server;
    //     var user = server.current(session);
    //     return user.tenantId;
    // };
    api.getLifecycle = function(lifecycleName, tenantId) {
        if (!tenantId) {
            throw 'Unable to locate lifecycle ' + lifecycleName + ' without a tenantId';
        }
        var lcJSON = core.getJSONDef(lifecycleName, tenantId);
        if (!lcJSON) {
            log.warn('Unable to locate lifecycle ' + lifecycleName + ' for the tenant: ' + tenantId);
            return null;
        }
        return new Lifecycle(lcJSON);
    };
}(api, core));<commit_msg>Added support for multiple check list items<commit_after>var api = {};
(function(api, core) {
    var log = new Log('lifecycle');
    var CHECKITEM_TOKEN = 'checkItems';
    var TRANSITION_EXECUTION = 'transitionExecution';

    function Lifecycle(definiton) {
        this.definition = definiton;
    }
    Lifecycle.prototype.getName = function() {
        if (!this.definition.name) {
            throw 'Unable to locate name attribute in the lifecycle definition ';
        }
        return this.definition.name;
    };
    /**
     * The function will obtain the next state of states from the current state
     * @return An array containing the name of the next set of states
     */
    Lifecycle.prototype.nextStates = function(currentStateName) {
        var currentStateName = currentStateName ? currentStateName.toLowerCase() : currentStateName;
        var states = this.definition.configuration.lifecycle.scxml.state;
        var nextStates = [];
        if (!states) {
            throw 'The lifecycle : ' + this.getName() + ' does not have any state information.Make sure that the states are defined in the scxml definition.';
        }
        if (!states[currentStateName]) {
            log.warn('The state: ' + currentStateName + ' is not present in the lifecycle: ' + this.getName());
            return nextStates;
        }
        if (!states[currentStateName].transition) {
            log.warn('The state: ' + currentStateName + ' has not defined any transitions in the lifecycle: ' + this.getName());
            return nextStates;
        }
        var transitions = states[currentStateName].transition;
        for (var index = 0; index < transitions.length; index++) {
            var transition = {};
            transition.state = transitions[index].target;
            transition.action = transitions[index].event;
            nextStates.push(transition);
        }
        return nextStates;
    };
    /**
     * The function builds a state object given raw state information
     * @param  {[type]} rawState [description]
     * @return {[type]}          [description]
     */
    var buildStateObject = function(rawState) {
        var state = {};
        state.id = rawState.id;
        if (!rawState.datamodel) {
            log.warn('Unable to read data model of the state ');
            return state;
        }
        var checkItems = getCheckitems(rawState.data);
        state.checkItems = checkItems;
        return state;
    };
    /**
     * The function returns the check list items for the current state
     * @param  {[type]} data [description]
     * @return {[type]}      [description]
     */
    var getCheckitems = function(data) {
        var items = [];
        for (var index in data) {
            item = data[index];
            //There can be multiple checklist items define din a data model
            if (item.name == CHECKITEM_TOKEN) {
                items.push(item.item || []);
            }
        }
        return items;
    };
    /**
     * The function returns details about the current state
     * @param  {[type]} name The name of the state
     * @return A json object representing the state
     */
    Lifecycle.prototype.state = function(stateName) {
        //Convert the state to lowercase
        var stateName = stateName ? stateName.toLowerCase() : stateName;
        var states = this.definition.configuration.lifecycle.scxml.state;
        var state = {};
        if (!states) {
            throw 'The lifecycle : ' + this.getName() + ' does not have any state information.Make sure that the states are defined in the scxml definition.';
        }
        if (!states[stateName]) {
            log.warn('The state: ' + stateName + ' is not present in the lifecycle: ' + this.getName());
            return state;
        }
        var rawState = states[stateName];
        //Process the raw state 
        state= buildStateObject(rawState);
        //Add the next states
        state.nextStates = this.nextStates(stateName);
        return state;
    };
    /**
     * The function returns the action that can cause transitions from the fromState to the toState
     * @param  {[type]} fromState The from state
     * @param  {[type]} toState   [description]
     * @return {[type]}           [description]
     */
    Lifecycle.prototype.transitionAction = function(fromState, toState) {
        var fromState = fromState ? fromState.toLowerCase() : fromState;
        var toState = toState ? toState.toLowerCase() : toState;
        //Get the list of states that can be reached from the fromState
        var states = this.nextStates(fromState);
        if (states.length == 0) {
            log.warn('There is no way to move from ' + fromState + ' to ' + toState + ' in lifecycle: ' + this.getName());
            return null;
        }
        for (var index = 0; index < states.length; index++) {
            if (states[index].state.toLowerCase() == toState.toLowerCase()) {
                return states[index].action;
            }
        }
        log.warn('There is no transition action to move from ' + fromState + ' to ' + toState + ' in lifecycle: ' + this.getName());
        return null;
    };
    /**
     * The function will return an array of execution event parameters.These parameters
     * are consumed by the executor for the event
     * @param  {[type]} state The state for which the execution event must be returned
     * @param  action The transition action for this state
     * @return {[type]}       An array of transition execution event parameters
     */
    Lifecycle.prototype.transitionExecution = function(state, action) {
        var state = state ? state.toLowerCase() : state;
        var action = action ? action.toLowerCase() : action;
        var states = this.definition.configuration.lifecycle.scxml.state;
        var parameters = [];
        if (!states) {
            throw 'The lifecycle : ' + this.getName() + ' does not have any state information.Make sure that the states are defined in the scxml definition.';
        }
        if (!states[state]) {
            log.warn('The state: ' + state + ' is not present in the lifecycle: ' + this.getName());
            return parameters;
        }
        var data = states[state].datamodel.data;
        if (!data) {
            log.warn('The lifecycle: ' + this.getName() + ' does not have a data property declared.Unable to obtain execution events.');
            return parameters;
        }
        var item;
        for (var index in data) {
            item = data[index];
            if (item.name == 'transitionExecution') {
                var executions = item.execution;
                var execution;
                if (executions) {
                    //Look for the event triggered by the provided action
                    for (var exIndex in executions) {
                        execution = executions[exIndex];
                        //Check if the event matches the action
                        if (execution.forEvent.toLowerCase() == action) {
                            parameters = execution.parameter || [];
                            return parameters;
                        }
                    }
                }
            }
        }
        return parameters;
    };
    // var getTenantId = function(session) {
    //     var server = require('store').server;
    //     var user = server.current(session);
    //     return user.tenantId;
    // };
    api.getLifecycle = function(lifecycleName, tenantId) {
        if (!tenantId) {
            throw 'Unable to locate lifecycle ' + lifecycleName + ' without a tenantId';
        }
        var lcJSON = core.getJSONDef(lifecycleName, tenantId);
        if (!lcJSON) {
            log.warn('Unable to locate lifecycle ' + lifecycleName + ' for the tenant: ' + tenantId);
            return null;
        }
        return new Lifecycle(lcJSON);
    };
}(api, core));
{'fraction_non_alphanumeric': 0.10267941977576486, 'fraction_numerical': 0.0003800595426616837, 'mean_word_length': 1.9844990548204158, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '2489700', 'n_tokens_mistral': 4004, 'n_tokens_neox': 3974, 'n_words': 1293}