<commit_before>/*
 * Copyright 2017 Crown Copyright
 *
 * Licensed 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.
 */

'use strict';

angular.module('app').factory('types', ['config', 'common', function(config, common) {

    var service = {};
    var types = {};
    var simpleClassNames = {};

    config.get().then(function(myConfig) {
        if(myConfig) {
            types = myConfig.types;
            var simpleClassNamesLowerCase = [];
            for(var className in types) {
                var parts = className.split('.');
                if(parts.length > 1) {
                    var simpleClassName = className.split('.').pop().replace(/<.*>/, "");
                    if(simpleClassNamesLowerCase.indexOf(simpleClassName) === -1) {
                        simpleClassNamesLowerCase.push(simpleClassName);
                        simpleClassNames[simpleClassName] = className;
                    }
                }
            }
            if('JSON' in types) {
                simpleClassNames['JSON'] = 'JSON';
            }
        }
    });

    var defaultShortValue = function(value) {
        return angular.toJson(value);
    }

    var mapShortValue = function(value) {
        return Object.keys(value).map(function(key) {
            return key + ": " + service.getShortValue(value[key]);
        }).join(", ");
    }

    var listShortValue = function(values) {
        return values.map(function(value) {
            return service.getShortValue(value);
        }).join(', ');
    }

    var customShortValue = function(fields, parts) {
        var showWithLabel = (fields.length !== 1)

        return fields.map(function(field) {
            var layers = field.key.split('.');
            var customValue = parts;
            for (var i in layers) {
                customValue = customValue[layers[i]];
            }

            customValue = service.getShortValue(customValue);

            if (showWithLabel) {
                return field.label + ': ' + customValue;
            }
            return customValue;
        }).join(', ');
    }

    service.getFields = function(className) {
        var knownType = types[className];

        if(knownType) {
            return knownType.fields;
        }

        return unknownType.fields;
    }

    service.isKnown = function(className) {
        var knownType = types[className];

        if(knownType) {
            return true;
        }

        return false;
    }

    service.getSimpleClassNames = function() {
        return simpleClassNames;
    }

    var unknownType =
    {
        fields: [
            {
                label: "Value",
                type: "text",
                class: "java.lang.String"
            }
        ]
    }
    
    var getType = function(typeClass) {
        if (typeClass !== undefined && types[typeClass]) {
            return types[typeClass];
        }
        return unknownType;
    }

    service.getClassName = function(simpleClassName) {
        var className;
        if (simpleClassName !== undefined) {
            var simpleClassNameLowerCase = simpleClassName.toLowerCase()
            if(simpleClassName in simpleClassNames) {
                className = simpleClassNames[simpleClassName];
            } else if(simpleClassName in simpleClassNames) {
               className = simpleClassNames[simpleClassNameLowerCase];
            } else {
                for(var simpleType in simpleClassNames) {
                    if(simpleType.toLowerCase() === simpleClassNameLowerCase) {
                        className = simpleClassNames[simpleType];
                        break;
                    }
                }
            }

            if(className === undefined) {
                if(simpleClassName in types) {
                    className = simpleClassName;
                } else {
                    for(var type in types) {
                        if(type.toLowerCase() === simpleClassNameLowerCase) {
                            className = type;
                            break;
                        }
                    }
                }
            }
        }

        return className;
    }

    service.createValue = function(typeClass, parts) {
        if ((getType(typeClass).wrapInJson && Object.keys(parts)[0] !== 'undefined') || Object.keys(parts).length > 1) {
            return parts;
        }
        return parts[Object.keys(parts)[0]];
    }

    service.createJsonValue = function(typeClass, parts, stringify) {
        var value = {};
        var type = getType(typeClass);

        if(type.wrapInJson || Object.keys(parts).length > 1) {
            if (Object.keys(parts).length === 1 && undefined in parts) {
                value[typeClass] = parts['undefined'];
            } else {
                value[typeClass] = parts;
            }
            if(stringify) {
                value = JSON.stringify(value);
            }
            return value;
        }

        if(typeClass === 'JSON') {
            try {
                return JSON.parse(parts[Object.keys(parts)[0]]);
            } catch(err) {
               console.log(err);
               alert('Failed to parse JSON: ' + err.message);
            }
        }

        return parts[Object.keys(parts)[0]];

    }

    service.createParts = function(typeClass, value) {
        if(typeClass !== undefined && value[typeClass]) {
            return value[typeClass];
        }

        var type = getType(typeClass);
        if(type.key === undefined && type.fields.length > 1) {
            return value;
        }

        var parts = {};
        parts[getType(typeClass).key] = value;
        return parts;
    }

    service.getShortValue = function(value) {

        if (value === undefined || value === null || typeof value === 'string' || value instanceof String || typeof value === 'number') {
            return value;
        }

        if(Object.keys(value).length != 1) {
            return defaultShortValue(value);
        }

        var typeClass = Object.keys(value)[0];
        var parts = value[typeClass]; // the value without the class prepended


        var type = getType(typeClass);

        if (type.custom) {
            return customShortValue(type.fields, parts)
        }

        if (common.endsWith(typeClass, 'Map')) {
            return mapShortValue(parts);
        } else if (common.endsWith(typeClass, 'List') || common.endsWith(typeClass, 'Set')) {
            return listShortValue(parts);
        }

        if (Object.keys(parts).length > 0) {
            return Object.keys(parts).map(function(key){
                var val = parts[key];
                return service.getShortValue(val);
            }).join("|");
        }

        return value[typeClass];
    }

    service.getCsvHeader = function(typeClass) {
        var type = getType(typeClass);

        var partKeys = [];
        for(var i in type.fields) {
            if(type.fields[i].key === undefined) {
                partKeys.push("");
            } else {
                partKeys.push(type.fields[i].key);<commit_msg>gh-382 - fixed issue with converting value to short value<commit_after>/*
 * Copyright 2017 Crown Copyright
 *
 * Licensed 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.
 */

'use strict';

angular.module('app').factory('types', ['config', 'common', function(config, common) {

    var service = {};
    var types = {};
    var simpleClassNames = {};

    config.get().then(function(myConfig) {
        if(myConfig) {
            types = myConfig.types;
            var simpleClassNamesLowerCase = [];
            for(var className in types) {
                var parts = className.split('.');
                if(parts.length > 1) {
                    var simpleClassName = className.split('.').pop().replace(/<.*>/, "");
                    if(simpleClassNamesLowerCase.indexOf(simpleClassName) === -1) {
                        simpleClassNamesLowerCase.push(simpleClassName);
                        simpleClassNames[simpleClassName] = className;
                    }
                }
            }
            if('JSON' in types) {
                simpleClassNames['JSON'] = 'JSON';
            }
        }
    });

    var defaultShortValue = function(value) {
        return angular.toJson(value);
    }

    var mapShortValue = function(value) {
        return Object.keys(value).map(function(key) {
            return key + ": " + service.getShortValue(value[key]);
        }).join(", ");
    }

    var listShortValue = function(values) {
        return values.map(function(value) {
            return service.getShortValue(value);
        }).join(', ');
    }

    var customShortValue = function(fields, parts) {
        var showWithLabel = (fields.length !== 1)

        return fields.map(function(field) {
            var layers = field.key.split('.');
            var customValue = parts;
            for (var i in layers) {
                customValue = customValue[layers[i]];
            }

            customValue = service.getShortValue(customValue);

            if (showWithLabel) {
                return field.label + ': ' + customValue;
            }
            return customValue;
        }).join(', ');
    }

    service.getFields = function(className) {
        var knownType = types[className];

        if(knownType) {
            return knownType.fields;
        }

        return unknownType.fields;
    }

    service.isKnown = function(className) {
        var knownType = types[className];

        if(knownType) {
            return true;
        }

        return false;
    }

    service.getSimpleClassNames = function() {
        return simpleClassNames;
    }

    var unknownType =
    {
        fields: [
            {
                label: "Value",
                type: "text",
                class: "java.lang.String"
            }
        ]
    }
    
    var getType = function(typeClass) {
        if (typeClass !== undefined && types[typeClass]) {
            return types[typeClass];
        }
        return unknownType;
    }

    service.getClassName = function(simpleClassName) {
        var className;
        if (simpleClassName !== undefined) {
            var simpleClassNameLowerCase = simpleClassName.toLowerCase()
            if(simpleClassName in simpleClassNames) {
                className = simpleClassNames[simpleClassName];
            } else if(simpleClassName in simpleClassNames) {
               className = simpleClassNames[simpleClassNameLowerCase];
            } else {
                for(var simpleType in simpleClassNames) {
                    if(simpleType.toLowerCase() === simpleClassNameLowerCase) {
                        className = simpleClassNames[simpleType];
                        break;
                    }
                }
            }

            if(className === undefined) {
                if(simpleClassName in types) {
                    className = simpleClassName;
                } else {
                    for(var type in types) {
                        if(type.toLowerCase() === simpleClassNameLowerCase) {
                            className = type;
                            break;
                        }
                    }
                }
            }
        }

        return className;
    }

    service.createValue = function(typeClass, parts) {
        if ((getType(typeClass).wrapInJson && Object.keys(parts)[0] !== 'undefined') || Object.keys(parts).length > 1) {
            return parts;
        }
        return parts[Object.keys(parts)[0]];
    }

    service.createJsonValue = function(typeClass, parts, stringify) {
        var value = {};
        var type = getType(typeClass);

        if(type.wrapInJson || Object.keys(parts).length > 1) {
            if (Object.keys(parts).length === 1 && undefined in parts) {
                value[typeClass] = parts['undefined'];
            } else {
                value[typeClass] = parts;
            }
            if(stringify) {
                value = JSON.stringify(value);
            }
            return value;
        }

        if(typeClass === 'JSON') {
            try {
                return JSON.parse(parts[Object.keys(parts)[0]]);
            } catch(err) {
               console.log(err);
               alert('Failed to parse JSON: ' + err.message);
            }
        }

        return parts[Object.keys(parts)[0]];

    }

    service.createParts = function(typeClass, value) {
        if(typeClass !== undefined && value[typeClass]) {
            return value[typeClass];
        }

        var type = getType(typeClass);
        if(type.key === undefined && type.fields.length > 1) {
            return value;
        }

        var parts = {};
        parts[getType(typeClass).key] = value;
        return parts;
    }

    service.getShortValue = function(value) {

        if (value === undefined || value === null || typeof value === 'string' || value instanceof String || typeof value === 'number') {
            return value;
        }

        if(Object.keys(value).length != 1) {
            return defaultShortValue(value);
        }

        var typeClass = Object.keys(value)[0];
        var parts = value[typeClass]; // the value without the class prepended
        if(parts === undefined) {
            return "";
        }

        var type = getType(typeClass);

        if (type.custom) {
            return customShortValue(type.fields, parts)
        }

        if (common.endsWith(typeClass, 'Map')) {
            return mapShortValue(parts);
        } else if (common.endsWith(typeClass, 'List') || common.endsWith(typeClass, 'Set')) {
            return listShortValue(parts);
        }

        if (typeof parts === "object"){
            if(Object.keys(parts).length > 0) {
                return Object.keys(parts).map(function(key){
                    var val = parts[key];
                    return service.getShortValue(val);
                }).join("|");
            } else {
                return "";
            }
        }

        return parts;
    }

    service.getCsvHeader = function(typeClass) {
        var type = getType(typeClass);

        var partKeys = [];
        for(var i in type.fields) {
            if(type.fields[i].key === undefined) {
                partKeys.push("");
            } else {
                partKeys.push(type.fields[i].key);
{'fraction_non_alphanumeric': 0.11448598130841121, 'fraction_numerical': 0.0030503634475597093, 'mean_word_length': 1.6376241013351591, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4323913', 'n_tokens_mistral': 3987, 'n_tokens_neox': 3828, 'n_words': 885}