<commit_before>/*
 *  Copyright (c) 2005-2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. licenses this file to you under the Apache License,
 *  Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
var api = {};
(function(api) {
    var log = new Log();
    var ACTION_STATE = 'state';
    var ACTION_CHANGE_STATE = 'change-state';
    var ACTION_CHECKLIST = 'checklist';
    var ACTION_UPDATE_CHECKLIST = 'update-checklist';
    var ACTION_LC_HISTORY = 'lifecycle-history';
    var ACTION_ADD_TAGS = 'add-tags';
    var ACTION_REMOVE_TAGS = 'remove-tags';
    var ACTION_RETRIEVE_TAGS = 'tags';
    var ACTION_CREATE_VERSION = 'create-version';
    var HTTP_ERROR_NOT_IMPLEMENTED = 501;
    var CONTENT_TYPE_JSON = 'application/json';
    var MSG_ERROR_NOT_IMPLEMENTED = 'The provided action is not supported by this endpoint';
    var lifecycleAPI = require('/modules/lifecycle/lifecycle-api.js').api;
    var utils = require('utils');
    var rxtModule = require('rxt');
    var es = require("store").server;
    var tagsAPI = require('/modules/tags-api.js').api;
    var assetAPI = require('/modules/asset-api.js').api;        
    var utility = require('/modules/utility.js').rxt_utility();
    var exceptionModule = utils.exception;
    var constants = rxtModule.constants;
    var msg = function(code, message, data) {
        var obj = {};
        obj.code = code;
        obj.message = message;
        obj.data = data;
        return obj;
    };
    var successMsg = function(obj) {
        obj.success = true;
        return obj;
    };
    var errorMsg = function(obj) {
        obj.success = false;
        return obj;
    };
    var validateOptions = function(options) {
        if (!options.type) {
            var error = 'Unable to obtain state information without knowing the type of asset of id: ' + options.id;
            log.error(error);
            throw exceptionModule.buildExceptionObject(error, constants.STATUS_CODES.BAD_REQUEST);
        }
    };
    var parseContentType = function(contentTypString) {
        var comps = contentTypString.split(';');
        return comps[0];
    };
    /**
     * Allows request data to be sent in either the request body or
     * as url encoded query parameters
     * @param  {Object} req  Request object
     * @return {Object}      An object which contains data from request body and url encoded parameters
     */
    var obtainData = function(req) {
        var data = {};
        var contentType = parseContentType(req.getContentType());
        if (contentType === CONTENT_TYPE_JSON) {
            try {
                data = req.getContent();
            } catch (e) {
                log.error('Unable to obtain content from request', e);
            }
        }
        var params = req.getAllParameters('UTF-8');
        //Mix the content with request parameters
        for (var key in params) {
            data[key] = params[key];
        }
        return data;
    };
    api.state = function(req, res, session, options) {
        if (req.getMethod() !== 'GET') {
            return error(msg(405, 'State must be retrieved using a GET'));
        }
        validateOptions(options);
        var result;
        try {
            result = lifecycleAPI.getState(options, req, res, session);
        } catch (e) {
            if (e == "Unauthorized Action - does not have permissions to view lifecycle state") {
                return successMsg(msg(401, 'User does not have permission to view lifecycle state', e));
            } else {
                throw e;
            }
        }
        return successMsg(msg(200, 'Asset state information returned', result));
    };
    api.changeState = function(req, res, session, options) {
        var newState = null;
        var result = {};
        var data;
        var succcess;
        if (req.getMethod() !== 'POST') {
            return errorMsg(msg(405, 'State must be changed by a POST'));
        }
        data = obtainData(req);
        if (!data.nextState) {
            log.error('State change requires a nextState to be provided either as a query string or in the body of the request');
            return errorMsg(msg(400, 'State change requires a nextState to be provided either as a query string or in the body of the request'));
        }
        options.nextState = data.nextState;
        options.comment = data.comment;
        //Call the state change api

        try {
            success = lifecycleAPI.changeState(options, req, res, session);
        } catch (e) {
            throw e;
        }
        if (success) {
            //Obtain the next states<commit_msg>STORE-1138 - enhancing error message creation<commit_after>/*
 *  Copyright (c) 2005-2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. licenses this file to you under the Apache License,
 *  Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
var api = {};
(function(api) {
    var log = new Log();
    var ACTION_STATE = 'state';
    var ACTION_CHANGE_STATE = 'change-state';
    var ACTION_CHECKLIST = 'checklist';
    var ACTION_UPDATE_CHECKLIST = 'update-checklist';
    var ACTION_LC_HISTORY = 'lifecycle-history';
    var ACTION_ADD_TAGS = 'add-tags';
    var ACTION_REMOVE_TAGS = 'remove-tags';
    var ACTION_RETRIEVE_TAGS = 'tags';
    var ACTION_CREATE_VERSION = 'create-version';
    var HTTP_ERROR_NOT_IMPLEMENTED = 501;
    var CONTENT_TYPE_JSON = 'application/json';
    var MSG_ERROR_NOT_IMPLEMENTED = 'The provided action is not supported by this endpoint';
    var lifecycleAPI = require('/modules/lifecycle/lifecycle-api.js').api;
    var utils = require('utils');
    var rxtModule = require('rxt');
    var es = require("store").server;
    var tagsAPI = require('/modules/tags-api.js').api;
    var assetAPI = require('/modules/asset-api.js').api;        
    var utility = require('/modules/utility.js').rxt_utility();
    var exceptionModule = utils.exception;
    var constants = rxtModule.constants;
    var msg = function(code, message, data) {
        var obj = {};
        obj.code = code;
        obj.message = message;
        obj.data = data;
        return obj;
    };
    var successMsg = function(obj) {
        obj.success = true;
        return obj;
    };
    var errorMsg = function(obj) {
        obj.success = false;
        return obj;
    };
    var validateOptions = function(options) {
        if (!options.type) {
            var error = 'Unable to obtain state information without knowing the type of asset of id: ' + options.id;
            log.error(error);
            throw exceptionModule.buildExceptionObject(error, constants.STATUS_CODES.BAD_REQUEST);
        }
    };
    var parseContentType = function(contentTypString) {
        var comps = contentTypString.split(';');
        return comps[0];
    };
    /**
     * Allows request data to be sent in either the request body or
     * as url encoded query parameters
     * @param  {Object} req  Request object
     * @return {Object}      An object which contains data from request body and url encoded parameters
     */
    var obtainData = function(req) {
        var data = {};
        var contentType = parseContentType(req.getContentType());
        if (contentType === CONTENT_TYPE_JSON) {
            try {
                data = req.getContent();
            } catch (e) {
                log.error('Unable to obtain content from request', e);
            }
        }
        var params = req.getAllParameters('UTF-8');
        //Mix the content with request parameters
        for (var key in params) {
            data[key] = params[key];
        }
        return data;
    };
    api.state = function(req, res, session, options) {
        if (req.getMethod() !== 'GET') {
            return error(msg(405, 'State must be retrieved using a GET'));
        }
        validateOptions(options);
        var result;
        try {
            result = lifecycleAPI.getState(options, req, res, session);
        } catch (e) {
            if (e == "Unauthorized Action - does not have permissions to view lifecycle state") {
                var errorResponse = msg(401, 'User does not have permission to view lifecycle state', e);
                errorResponse.errorContent = {};

                errorResponse.errorContent.message = 'User does not have permission to view lifecycle state';
                errorResponse.errorContent.exception = e;

                return errorMsg(errorResponse);
            } else {
                throw e;
            }
        }
        return successMsg(msg(200, 'Asset state information returned', result));
    };
    api.changeState = function(req, res, session, options) {
        var newState = null;
        var result = {};
        var data;
        var succcess;
        if (req.getMethod() !== 'POST') {
            return errorMsg(msg(405, 'State must be changed by a POST'));
        }
        data = obtainData(req);
        if (!data.nextState) {
            log.error('State change requires a nextState to be provided either as a query string or in the body of the request');
            return errorMsg(msg(400, 'State change requires a nextState to be provided either as a query string or in the body of the request'));
        }
        options.nextState = data.nextState;
        options.comment = data.comment;
        //Call the state change api

        try {
            success = lifecycleAPI.changeState(options, req, res, session);
        } catch (e) {
            throw e;
        }
        if (success) {
            //Obtain the next states
{'fraction_non_alphanumeric': 0.10259568018188708, 'fraction_numerical': 0.007010231148162183, 'mean_word_length': 2.2785714285714285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5000316', 'n_tokens_mistral': 2929, 'n_tokens_neox': 2879, 'n_words': 959}