<commit_before>package widgets.supportClasses
{

import com.esri.ags.FeatureSet;
import com.esri.ags.Graphic;
import com.esri.ags.layers.FeatureLayer;
import com.esri.ags.layers.supportClasses.CodedValue;
import com.esri.ags.layers.supportClasses.CodedValueDomain;
import com.esri.ags.layers.supportClasses.FeatureType;
import com.esri.ags.layers.supportClasses.Field;
import com.esri.ags.layers.supportClasses.LayerDetails;

import mx.core.FlexGlobals;
import mx.core.LayoutDirection;
import mx.formatters.DateFormatter;

[Bindable]
public class ResultAttributes
{
    public var attributes:Object;
    public var title:String;
    public var content:String;
    public var link:String;
    public var linkAlias:String;

    public static function toResultAttributes(fields:XMLList,
                                              graphic:Graphic = null,
                                              featureSet:FeatureSet = null,
                                              layer:FeatureLayer = null,
                                              layerDetails:LayerDetails = null,
                                              fallbackTitle:String = null,
                                              titleField:String = null,
                                              linkField:String = null,
                                              linkAlias:String = null):ResultAttributes
    {
        var resultAttributes:ResultAttributes = new ResultAttributes;

        var value:String = "";
        var title:String = "";
        var content:String = "";
        var link:String = "";
        var linkAlias:String;

        var fieldsXMLList:XMLList = fields ? fields.field : null;
        if (fields && fields[0].@all[0] == "true")
        {
            if (layerDetails.fields)
            {
                for each (var field:Field in layerDetails.fields)
                {
                    if (field.name in graphic.attributes)
                    {
                        displayFields(field.name, getFieldXML(field.name, fieldsXMLList), field);
                    }
                }
            }
            else
            {
                for (var fieldName:String in graphic.attributes)
                {
                    displayFields(fieldName, getFieldXML(fieldName, fieldsXMLList), null);
                }
            }
        }
        else
        {
            for each (var fieldXML:XML in fieldsXMLList) // display the fields in the same order as specified
            {
                if (fieldXML.@name[0] in graphic.attributes)
                {
                    displayFields(fieldXML.@name[0], fieldXML, getField(layer, fieldXML.@name[0]));
                }
            }
        }

        resultAttributes.attributes = graphic.attributes;
        resultAttributes.title = title ? title : fallbackTitle;
        resultAttributes.content = content.replace(/\n$/, '');
        resultAttributes.link = link ? link : null;
        resultAttributes.linkAlias = linkAlias;

        function displayFields(fieldName:String, fieldXML:XML, field:Field):void
        {
            var fieldNameTextValue:String = graphic.attributes[fieldName];
            value = fieldNameTextValue ? fieldNameTextValue : "";

            if (value)
            {
                var isDateField:Boolean;
                var useUTC:Boolean;
                var dateFormat:String;
                if (fieldXML)
                {
                    useUTC = fieldXML.format.@useutc[0] == "true" || fieldXML.@useutc[0] == "true";
                    dateFormat = fieldXML.format.@dateformat[0] || fieldXML.@dateformat[0];
                    if (dateFormat)
                    {
                        isDateField = true;
                    }
                }
                if (!isDateField && field)
                {
                    isDateField = field.type == Field.TYPE_DATE;
                }
                if (isDateField)
                {
                    var dateMS:Number = Number(value);
                    if (!isNaN(dateMS))
                    {
                        value = msToDate(dateMS, dateFormat, useUTC);
                    }
                }
                else
                {
                    var typeID:String = layerDetails.typeIdField ? graphic.attributes[layerDetails.typeIdField] : null;
                    if (fieldName == layerDetails.typeIdField)
                    {
                        var featureType:FeatureType = getFeatureType(layer, typeID);
                        if (featureType && featureType.name)
                        {
                            value = featureType.name;
                        }
                    }
                    else
                    {
                        var codedValue:CodedValue = getCodedValue(layer, fieldName, value, typeID);
                        if (codedValue)
                        {
                            value = codedValue.name;
                        }
                    }
                }
            }

            var upperCaseFieldName:String = fieldName.toUpperCase();
            if (titleField && upperCaseFieldName == titleField.toUpperCase())
            {
                title = value;
            }
            else if (linkField && upperCaseFieldName == linkField.toUpperCase())
            {
                link = value;
                linkAlias = linkAlias;
            }
            else if (upperCaseFieldName != "SHAPE_LENGTH" && upperCaseFieldName != "SHAPE_AREA")
            {
                var fieldLabel:String;

                if (fieldXML && fieldXML.@alias[0])
                {
                    fieldLabel = fieldXML.@alias[0];
                }
                else
                {
                    fieldLabel = featureSet.fieldAliases[fieldName];
                }

                if (FlexGlobals.topLevelApplication.layoutDirection == LayoutDirection.RTL)
                {
                    content += value + " :" + fieldLabel + "\n";
                }
                else
                {
                    content += fieldLabel + ": " + value + "\n";
                }
            }
        }

        return resultAttributes;
    }

    private static function getFieldXML(fieldName:String, fields:XMLList):XML
    {<commit_msg>Issue #173: Allow Shape_Length/Shape_Area field if defined by user.<commit_after>package widgets.supportClasses
{

import com.esri.ags.FeatureSet;
import com.esri.ags.Graphic;
import com.esri.ags.layers.FeatureLayer;
import com.esri.ags.layers.supportClasses.CodedValue;
import com.esri.ags.layers.supportClasses.CodedValueDomain;
import com.esri.ags.layers.supportClasses.FeatureType;
import com.esri.ags.layers.supportClasses.Field;
import com.esri.ags.layers.supportClasses.LayerDetails;

import mx.core.FlexGlobals;
import mx.core.LayoutDirection;
import mx.formatters.DateFormatter;

[Bindable]
public class ResultAttributes
{
    public var attributes:Object;
    public var title:String;
    public var content:String;
    public var link:String;
    public var linkAlias:String;

    public static function toResultAttributes(fields:XMLList,
                                              graphic:Graphic = null,
                                              featureSet:FeatureSet = null,
                                              layer:FeatureLayer = null,
                                              layerDetails:LayerDetails = null,
                                              fallbackTitle:String = null,
                                              titleField:String = null,
                                              linkField:String = null,
                                              linkAlias:String = null):ResultAttributes
    {
        var resultAttributes:ResultAttributes = new ResultAttributes;

        var value:String = "";
        var title:String = "";
        var content:String = "";
        var link:String = "";
        var linkAlias:String;

        var fieldsXMLList:XMLList = fields ? fields.field : null;
        if (fields && fields[0].@all[0] == "true")
        {
            if (layerDetails.fields)
            {
                for each (var field:Field in layerDetails.fields)
                {
                    if (field.name in graphic.attributes)
                    {
                        displayFields(field.name, getFieldXML(field.name, fieldsXMLList), field);
                    }
                }
            }
            else
            {
                for (var fieldName:String in graphic.attributes)
                {
                    displayFields(fieldName, getFieldXML(fieldName, fieldsXMLList), null);
                }
            }
        }
        else
        {
            for each (var fieldXML:XML in fieldsXMLList) // display the fields in the same order as specified
            {
                if (fieldXML.@name[0] in graphic.attributes)
                {
                    displayFields(fieldXML.@name[0], fieldXML, getField(layer, fieldXML.@name[0]));
                }
            }
        }

        resultAttributes.attributes = graphic.attributes;
        resultAttributes.title = title ? title : fallbackTitle;
        resultAttributes.content = content.replace(/\n$/, '');
        resultAttributes.link = link ? link : null;
        resultAttributes.linkAlias = linkAlias;

        function displayFields(fieldName:String, fieldXML:XML, field:Field):void
        {
            var fieldNameTextValue:String = graphic.attributes[fieldName];
            value = fieldNameTextValue ? fieldNameTextValue : "";

            if (value)
            {
                var isDateField:Boolean;
                var useUTC:Boolean;
                var dateFormat:String;
                if (fieldXML)
                {
                    useUTC = fieldXML.format.@useutc[0] == "true" || fieldXML.@useutc[0] == "true";
                    dateFormat = fieldXML.format.@dateformat[0] || fieldXML.@dateformat[0];
                    if (dateFormat)
                    {
                        isDateField = true;
                    }
                }
                if (!isDateField && field)
                {
                    isDateField = field.type == Field.TYPE_DATE;
                }
                if (isDateField)
                {
                    var dateMS:Number = Number(value);
                    if (!isNaN(dateMS))
                    {
                        value = msToDate(dateMS, dateFormat, useUTC);
                    }
                }
                else
                {
                    var typeID:String = layerDetails.typeIdField ? graphic.attributes[layerDetails.typeIdField] : null;
                    if (fieldName == layerDetails.typeIdField)
                    {
                        var featureType:FeatureType = getFeatureType(layer, typeID);
                        if (featureType && featureType.name)
                        {
                            value = featureType.name;
                        }
                    }
                    else
                    {
                        var codedValue:CodedValue = getCodedValue(layer, fieldName, value, typeID);
                        if (codedValue)
                        {
                            value = codedValue.name;
                        }
                    }
                }
            }

            var upperCaseFieldName:String = fieldName.toUpperCase();
            if (titleField && upperCaseFieldName == titleField.toUpperCase())
            {
                title = value;
            }
            else if (linkField && upperCaseFieldName == linkField.toUpperCase())
            {
                link = value;
                linkAlias = linkAlias;
            }
            else
            {
                var isShapeMeasurementField:Boolean = (upperCaseFieldName == "SHAPE_LENGTH" || upperCaseFieldName == "SHAPE_AREA");
                var isUserDefinedField:Boolean = fields && fields.field.(@name == fieldName).length() > 0;
                if (!isShapeMeasurementField || isUserDefinedField)
                {
                    var fieldLabel:String;

                    if (fieldXML && fieldXML.@alias[0])
                    {
                        fieldLabel = fieldXML.@alias[0];
                    }
                    else
                    {
                        fieldLabel = featureSet.fieldAliases[fieldName];
                    }

                    if (FlexGlobals.topLevelApplication.layoutDirection == LayoutDirection.RTL)
                    {
                        content += value + " :" + fieldLabel + "\n";
                    }
                    else
                    {
                        content += fieldLabel + ": " + value + "\n";
                    }
                }
            }
        }

        return resultAttributes;
    }

    private static function getFieldXML(fieldName:String, fields:XMLList):XML
    {
{'fraction_non_alphanumeric': 0.08374723007564759, 'fraction_numerical': 0.001986704363108428, 'mean_word_length': 1.1981860933825998, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '667081', 'n_tokens_mistral': 3129, 'n_tokens_neox': 2952, 'n_words': 580}