<commit_before>/**
 * Represents the application api.  If the variable is already defined use it,
 * otherwise create an empty object.
 *
 * @type {EScreeningDashboardApp|*|EScreeningDashboardApp|*|{}|{}}
 */
var EScreeningDashboardApp = EScreeningDashboardApp || {};
/**
 * Represents the application static variable. Use existing static variable, if one already exists,
 * otherwise create a new one.
 *
 * @static
 * @type {*|EScreeningDashboardApp.models|*|EScreeningDashboardApp.models|Object|*|Object|*}
 */
EScreeningDashboardApp.models = EScreeningDashboardApp.models || EScreeningDashboardApp.namespace("gov.va.escreening.models");

/**
 * Constructor method for the TemplateBlock class.  The properties of this class can be initialized with
 * the jsonConfig.
 * @class
 * @classdesc   This class is a domain model class; which means it has both behavior and state
 *              information about the user.
 * @param {JSON}  jsonConfig  Represents the JSON representation of an TemplateBlock object.
 * @param {Object} parent optionally the parent block object
 * @constructor
 * @author <NAME>
 */
EScreeningDashboardApp.models.TemplateBlock = function (jsonConfig, parent) {
    var self = this;
    var myparent = parent;
    var cleanSummaryReg = /(<\/*[^>]+>)|(&#[a-zA-Z0-9]+;)/g;
    var TEXT_NAME_LENGTH = 20;
    
    this.guid = EScreeningDashboardApp.getInstance().guid();
    this.section;
    this.name;
    this.type;
    this.summary;
    this.left;
    this.operator;
    this.conditions;
    this.content;
    this.right;
    this.children = [];
    this.contents = [];

    if(jsonConfig){
        //this.guid = (Object.isDefined(jsonConfig.guid))? jsonConfig.guid: this.guid;
        this.section = (Object.isDefined(jsonConfig.section))? jsonConfig.section: null;
        this.name = (Object.isDefined(jsonConfig.name))? jsonConfig.name: null;
        this.type = (Object.isDefined(jsonConfig.type))? jsonConfig.type: null;
        this.summary = (Object.isDefined(jsonConfig.summary))? jsonConfig.summary: null;
        this.left = (Object.isDefined(jsonConfig.left))? new EScreeningDashboardApp.models.TemplateLeftVariable(jsonConfig.left): null;
        this.operator = (Object.isDefined(jsonConfig.operator))? jsonConfig.operator : null;
        this.conditions = (Object.isArray(jsonConfig.conditions))? EScreeningDashboardApp.models.TemplateCondition.createConditionsArray(jsonConfig.conditions): [];
        this.content = (Object.isDefined(jsonConfig.content))? jsonConfig.content: '';
        this.right = (Object.isDefined(jsonConfig.right))? new EScreeningDashboardApp.models.TemplateRightVariable(jsonConfig.right): null;

        if(Object.isArray(jsonConfig.contents)){
            jsonConfig.contents.forEach(function(blockData){
                var contentObj = angular.copy(blockData);
                if(contentObj.type == "var"){
                    contentObj.content = new EScreeningDashboardApp.models.TemplateVariableContent(contentObj.content);
                }
                self.contents.push(contentObj);   
            });
        }
        
        if(Object.isDefined(jsonConfig.children)){
            angular.copy(jsonConfig.children, this.children);
            this.children.forEach(function(childData){
                angular.extend(childData, new EScreeningDashboardApp.models.TemplateBlock(childData, self));
            });
        }
    }

	function transformTextContent(TemplateBlockService, variableHash){

		if(this.type == "text"){
			this.contents = TemplateBlockService.parseIntoContents(this.content, variableHash);
			delete(this.content);
		}
		
		if(Object.isDefined(this.children)){
            this.children.forEach(function(block){ 
                block.transformTextContent(TemplateBlockService, variableHash); });
        }
	}
	
	function setTextContent(TemplateBlockService){
        this.content = TemplateBlockService.joinContents(this.contents);
    }

	function autoGenerateFields(variableHash){

		var block = this;

		if(block.type == "text"){
			block.summary = "";
			block.name = Object.isDefined(block.name) ? block.name :"";
			var setTitle = this.name.trim() == "";

			for(var i=0; ((setTitle && block.name < TEXT_NAME_LENGTH) || block.summary.length < 100)
				&&  i< block.contents.length; i++){
				var blockContent = block.contents[i];
				if(blockContent.type == "text"){
                    var text = blockContent.content.replace(cleanSummaryReg, "");
					block.summary += text;

					if(setTitle && block.name.length < TEXT_NAME_LENGTH){
						var neededChars = TEXT_NAME_LENGTH - block.name.length;
						block.name += text.slice(0, neededChars);
					}
				}
				else if(blockContent.type == "var"){
					var varName = "(" + blockContent.content.getName().replace(/\s+/g, "_") + ")";
					block.summary += varName;

					if(setTitle && block.name.length < TEXT_NAME_LENGTH){
						block.name += varName;
					}
				}
				block.summary += " ";
				block.name += " ";
			}
		}

		if (block.type !== 'text' && block.type !== 'else') {
			if (!block.name) block.name = block.type + "_" + (block.left.content.displayName || block.left.content.name) + "_" + block.operator + "_" + block.right.content;
			block.summary = (block.left.content.displayName || block.left.content.name) + " " + block.operator + " " + block.right.content;
			block.conditions.forEach(function (condition) {
				if (!condition.summary) {
					condition.autoGenerateFields();
					block.summary += " " + condition.summary;
				}
			});
		}<commit_msg>#660 added better logic to have summary and text block name ignor initial spaces in the content<commit_after>/**
 * Represents the application api.  If the variable is already defined use it,
 * otherwise create an empty object.
 *
 * @type {EScreeningDashboardApp|*|EScreeningDashboardApp|*|{}|{}}
 */
var EScreeningDashboardApp = EScreeningDashboardApp || {};
/**
 * Represents the application static variable. Use existing static variable, if one already exists,
 * otherwise create a new one.
 *
 * @static
 * @type {*|EScreeningDashboardApp.models|*|EScreeningDashboardApp.models|Object|*|Object|*}
 */
EScreeningDashboardApp.models = EScreeningDashboardApp.models || EScreeningDashboardApp.namespace("gov.va.escreening.models");

/**
 * Constructor method for the TemplateBlock class.  The properties of this class can be initialized with
 * the jsonConfig.
 * @class
 * @classdesc   This class is a domain model class; which means it has both behavior and state
 *              information about the user.
 * @param {JSON}  jsonConfig  Represents the JSON representation of an TemplateBlock object.
 * @param {Object} parent optionally the parent block object
 * @constructor
 * @author <NAME>
 */
EScreeningDashboardApp.models.TemplateBlock = function (jsonConfig, parent) {
    var self = this;
    var myparent = parent;
    var cleanSummaryReg = /(<\/*[^>]+>)|(&#[a-zA-Z0-9]+;)/g;
    var TEXT_NAME_LENGTH = 20;
    
    this.guid = EScreeningDashboardApp.getInstance().guid();
    this.section;
    this.name;
    this.type;
    this.summary;
    this.left;
    this.operator;
    this.conditions;
    this.content;
    this.right;
    this.children = [];
    this.contents = [];

    if(jsonConfig){
        //this.guid = (Object.isDefined(jsonConfig.guid))? jsonConfig.guid: this.guid;
        this.section = (Object.isDefined(jsonConfig.section))? jsonConfig.section: null;
        this.name = (Object.isDefined(jsonConfig.name))? jsonConfig.name: null;
        this.type = (Object.isDefined(jsonConfig.type))? jsonConfig.type: null;
        this.summary = (Object.isDefined(jsonConfig.summary))? jsonConfig.summary: null;
        this.left = (Object.isDefined(jsonConfig.left))? new EScreeningDashboardApp.models.TemplateLeftVariable(jsonConfig.left): null;
        this.operator = (Object.isDefined(jsonConfig.operator))? jsonConfig.operator : null;
        this.conditions = (Object.isArray(jsonConfig.conditions))? EScreeningDashboardApp.models.TemplateCondition.createConditionsArray(jsonConfig.conditions): [];
        this.content = (Object.isDefined(jsonConfig.content))? jsonConfig.content: '';
        this.right = (Object.isDefined(jsonConfig.right))? new EScreeningDashboardApp.models.TemplateRightVariable(jsonConfig.right): null;

        if(Object.isArray(jsonConfig.contents)){
            jsonConfig.contents.forEach(function(blockData){
                var contentObj = angular.copy(blockData);
                if(contentObj.type == "var"){
                    contentObj.content = new EScreeningDashboardApp.models.TemplateVariableContent(contentObj.content);
                }
                self.contents.push(contentObj);   
            });
        }
        
        if(Object.isDefined(jsonConfig.children)){
            angular.copy(jsonConfig.children, this.children);
            this.children.forEach(function(childData){
                angular.extend(childData, new EScreeningDashboardApp.models.TemplateBlock(childData, self));
            });
        }
    }

	function transformTextContent(TemplateBlockService, variableHash){

		if(this.type == "text"){
			this.contents = TemplateBlockService.parseIntoContents(this.content, variableHash);
			delete(this.content);
		}
		
		if(Object.isDefined(this.children)){
            this.children.forEach(function(block){ 
                block.transformTextContent(TemplateBlockService, variableHash); });
        }
	}
	
	function setTextContent(TemplateBlockService){
        this.content = TemplateBlockService.joinContents(this.contents);
    }

	function autoGenerateFields(variableHash){

		var block = this;

		if(block.type == "text"){
			block.summary = "";
			block.name = Object.isDefined(block.name) ? block.name :"";
			var setTitle = this.name.trim() == "";

			for(var i=0; ((setTitle && block.name < TEXT_NAME_LENGTH) || block.summary.length < 100)
				&&  i< block.contents.length; i++){
				var blockContent = block.contents[i];
				if(blockContent.type == "text"){
                    var text = blockContent.content.replace(cleanSummaryReg, "");
                    if(block.summary == ""){
                        text = text.replace(/^\s+/, "");
                    }
					block.summary += text;

					if(setTitle && block.name.length < TEXT_NAME_LENGTH){
						var neededChars = TEXT_NAME_LENGTH - block.name.length;
						block.name += text.slice(0, neededChars);
					}
				}
				else if(blockContent.type == "var"){
					var varName = "(" + blockContent.content.getName().replace(/\s+/g, "_") + ")";
					block.summary += varName;

					if(setTitle && block.name.length < TEXT_NAME_LENGTH){
						block.name += varName;
					}
				}
				block.summary += " ";
				block.name += " ";
			}
		}

		if (block.type !== 'text' && block.type !== 'else') {
			if (!block.name) block.name = block.type + "_" + (block.left.content.displayName || block.left.content.name) + "_" + block.operator + "_" + block.right.content;
			block.summary = (block.left.content.displayName || block.left.content.name) + " " + block.operator + " " + block.right.content;
			block.conditions.forEach(function (condition) {
				if (!condition.summary) {
					condition.autoGenerateFields();
					block.summary += " " + condition.summary;
				}
			});
		}
{'fraction_non_alphanumeric': 0.13524299396949271, 'fraction_numerical': 0.0018623625399077687, 'mean_word_length': 3.220434131736527, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, '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': '5281636', 'n_tokens_mistral': 3541, 'n_tokens_neox': 3251, 'n_words': 613}