//This call will examine the termAliasOrder list of columns to report, look at multiplexing to determine which term to
        //report in these cases, and return back the final listing of DSF term names, in order reported.
        //public int GetRowCountMultiplex(Guid templateID, List<string /*DSF Alias Name*/> orderedAliasNames, Dictionary<string /*DSF Alias Name*/, List<string /*DSF Name*/>> termAliasMapping, List<string> orderedDSFNames)
        //{
        //    if (_terms == null)
        //        throw new Exception(string.Format("TermStore does not exist for template '{0}'", templateID.ToString()));
        //    orderedDSFNames = new List<string>();     //This listing is defined during this call.
        //    int maxRowCount = 1;

        //    foreach (string termAlias in orderedAliasNames)
        //    {
        //        if (termAliasMapping.ContainsKey(termAlias))
        //        {
        //            if (termAliasMapping[termAlias].Count == 0)
        //            {
        //                throw new Exception(string.Format("The term alias '{0}' does not have any terms assigned for template '{1}'", termAlias, templateID.ToString()));
        //            }

        //            if (termAliasMapping[termAlias].Count == 1)
        //            {
        //                orderedDSFNames.Add(termAliasMapping[termAlias][0]);
        //                string termName = DataStoreField.GetTermName(termAliasMapping[termAlias][0]);
        //                TermStore termStore = _terms.Find(term => term.Name.Equals(termName));
        //                if (termStore != null)
        //                {
        //                    int rowCount = termStore.GetRowCount();
        //                    if (rowCount > maxRowCount)
        //                        maxRowCount = rowCount;
        //                }
        //            }
        //            else
        //            {
        //                //Need to determine which term to use
        //                int termAliasMaxRowCount = 0;
        //                string orderedDSFName = string.Empty;
        //                //Note: Need to ensure that the first valid term found is selected by default.
        //                string validTermName = string.Empty;
        //                //Note: Need to ensure that the first term encountered with a valid value is selected by default.
        //                bool validValue = false;
        //                foreach (string dsfName in termAliasMapping[termAlias])
        //                {
        //                    TermStore termStore = _terms.Find(term => term.Name.Equals(DataStoreField.GetTermName(dsfName)));
        //                    if (termStore != null)
        //                    {
        //                        if (string.IsNullOrEmpty(validTermName))
        //                            validTermName = termStore.Name;
        //                        int rowCount = termStore.GetRowCount();
        //                        bool validTerm = rowCount > 1 || (rowCount == 1 && termStore.GetIndexedValue(0, DataStoreField.GetTermField(dsfName)).Length > 0);
        //                        if (validTerm)
        //                        {
        //                            if (!validValue)
        //                            {
        //                                orderedDSFName = dsfName;
        //                                validValue = true;
        //                            }
        //                            if (rowCount > termAliasMaxRowCount)
        //                            {
        //                                termAliasMaxRowCount = rowCount;
        //                                orderedDSFName = dsfName;
        //                            }
        //                        }
        //                    }
        //                }
        //                if (string.IsNullOrEmpty(orderedDSFName))
        //                    orderedDSFName = validTermName;

        //                if (termAliasMaxRowCount > maxRowCount)
        //                    maxRowCount = termAliasMaxRowCount;
        //                orderedDSFNames.Add(orderedDSFName);
        //            }
        //        }
        //        else
        //        {
        //            throw new Exception(string.Format("The term alias mapping object did not contain the ordered Alias Name '{0}' for template '{1}'", termAlias, templateID.ToString()));
        //        }
        //    }
        //    return maxRowCount;
        //}

        public dataRow GetDataRow(bool isHeader, TemplateStore templateStore, int index, List<string /*DSF Name*/> orderedDSFNames)
        {
            GetTermsList();
            dataRow row = new dataRow();
            row.rowErrors = new List<rowError>();
            StringBuilder sb = new StringBuilder();

            for (int columnIndex = 0; columnIndex < orderedDSFNames.Count; columnIndex++)
            {
                string orderedDSFName = orderedDSFNames[columnIndex];
                string value = string.Empty;
                int? maxLength = null;

                if (isHeader)
                {
                    if (SystemStore.IsStandardColumn(orderedDSFName))
                        value = orderedDSFName;
                    else
                        value = DataStoreField.GetHeaderName(orderedDSFName);
                }
                else
                {
                    if (SystemStore.IsStandardColumn(orderedDSFName))
                    {
                        if (index == 0)
                        {
                            switch (SystemStore.GetStandardColumn(orderedDSFName))
                            {
                                case SystemStore.StandardColumn.ManagedItemNumber:
                                    value = _columnData.managedItemNumber;
                                    break;

                                case SystemStore.StandardColumn.TemplateName:
                                    value = _columnData.templateName;
                                    break;

                                case SystemStore.StandardColumn.WorkflowName:
                                    value = _columnData.activeWorkflow;
                                    break;

                                case SystemStore.StandardColumn.WorkflowStatus:
                                    value = _columnData.status;
                                    break;

                                case SystemStore.StandardColumn.WorkflowState:
                                    value = _columnData.state;
                                    break;
                            }
                        }
                        else
                        {
                            switch (SystemStore.GetStandardColumn(orderedDSFName))
                            {
                                //NOTE - Are all of these values repeated for multi-value terms?
                                case SystemStore.StandardColumn.ManagedItemNumber:
                                    value = _columnData.managedItemNumber;
                                    break;

                                case SystemStore.StandardColumn.TemplateName:
                                    value = _columnData.templateName;
                                    break;

                                case SystemStore.StandardColumn.WorkflowName:
                                    value = _columnData.activeWorkflow;
                                    break;

                                case SystemStore.StandardColumn.WorkflowStatus:
                                    value = _columnData.status;
                                    break;

                                case SystemStore.StandardColumn.WorkflowState:
                                    value = _columnData.state;
                                    break;
                            }
                        }
                    }
                    else
                    {
                        string termName = DataStoreField.GetTermName(orderedDSFName);
                        string fieldName = DataStoreField.GetTermField(orderedDSFName);
                        //A term could be missing from the _termsList if it is from an older manageditem (not the same as the current template).
                        if (_termsList.ContainsKey(termName))
                        {
                            string error = string.Empty;
                            value = _termsList[termName].GetIndexedValue(index, fieldName);

                            //Note - the DataStoreDefinition term (if found) is used here to determine how the manageditem term will be validated.
                            DataStoreField dsf = templateStore.DataStoreFields.First(kvp => kvp.Key.Equals(orderedDSFName)).Value;
                            if (dsf != null)
                            {
                                maxLength = dsf.Length;
                                //If a term does not pass validation, wipe the value and report the error.
                                //If this is an 'assigned' term type, then process it as such.
                                if (TermStore.RequiresValidation(dsf.TextTermFormat))
                                {
                                    error = TermStore.ValidateTermType(value, dsf.TermType, dsf.TextTermFormat);
                                    if (!string.IsNullOrEmpty(error))
                                        value = string.Empty;
                                    else
                                        value = TermStore.GetFormattedValue(value, dsf.TermType, dsf.TextTermFormat, dsf.DateFormat, templateStore.DefaultDateFormat);
                                }
                                else
                                {
                                    error = TermStore.ValidateTermType(value, dsf.TermType, TextTermFormat.Plain);
                                    if (!string.IsNullOrEmpty(error))
                                        value = string.Empty;
                                    else
                                        value = TermStore.GetFormattedValue(value, dsf.TermType, TextTermFormat.Plain, dsf.DateFormat, templateStore.DefaultDateFormat);
                                }
                            }
                            else
                            {
                                //This should never happen since the initial dsf listing is built based on the DataStoreConfiguration from the database.
                                error = string.Format("DSF term '{0}' not found in DataStoreFields for templateStore {1}", orderedDSFName, templateStore.Template.Name);
                            }
                            if (!string.IsNullOrEmpty(error))
                                AddError(row, _columnData.templateName, _columnData.managedItemNumber, termName, fieldName, error);
                        }
                        //20110113 - Per discussion, do not report the case of terms missing from the ManagedItem.
                        //else
                        //{
                        //    //Only need to record this error once per ManagedItem.  Also, do not report this for the special case of 'Attachments'.
                        //    if (index == 0 && !termName.Equals(XMLNames._A_Attachments))
                        //    {
                        //        string error = "Term not found in ManagedItem";
                        //        AddError(row, columnData.templateName, columnData.managedItemNumber, termName, fieldName, error);
                        //    }
                        //}
                    }
                }
                sb = sb.Append(TextHelper.CSVFormat(maxLength, TextHelper.ReplaceWhiteSpace(value), columnIndex == orderedDSFNames.Count - 1));
            }
            row.rowData = sb.ToString();
            return row;
        }
        //This call builds up a collection of TemplateStores based on the supplied DataStoreField collection for a given DataStoreDefinition.
        //Note - the supplied 'templates' dictionary may contain more templates than are actually referenced within the dsfTerms.
        public static Dictionary<Guid /*TemplateID*/, TemplateStore> GetTemplateStoreCollection(Dictionary<string /*Template Name*/, Guid> templates, List<DataStoreField> dsfTerms, string defaultDateFormat)
        {
            Dictionary<Guid /*TemplateID*/, TemplateStore> templateStores = new Dictionary<Guid /*TemplateID*/, TemplateStore>();
            foreach (DataStoreField dsfTerm in dsfTerms)
            {
                //The 'Special Terms' (e.g. ManagedItemNumber, WorkflowState, etc.) will not have an associated Template
                if (!string.IsNullOrEmpty(dsfTerm.TemplateName))
                {
                    if (!templates.ContainsKey(dsfTerm.TemplateName))
                    {
                        throw new Exception(string.Format("Template named '{0}' not found in templates collection", dsfTerm.TemplateName));
                    }

                    TemplateStore templateStore = null;
                    if (!templateStores.ContainsKey(templates[dsfTerm.TemplateName]))
                    {
                        List<DataStoreField> templateDSFTerms = dsfTerms.FindAll(dsf => dsf.TemplateName == null || dsf.TemplateName.Equals(dsfTerm.TemplateName));
                        templateStore = new TemplateStore(templates[dsfTerm.TemplateName], templateDSFTerms, defaultDateFormat);
                    }
                    else
                    {
                        templateStore = templateStores[templates[dsfTerm.TemplateName]];
                    }

                    //Add the unique alias names in the order they are encountered.
                    if (!templateStore.orderedAliasNames.Contains(dsfTerm.Alias))
                        templateStore.orderedAliasNames.Add(dsfTerm.Alias);

                    if (!templateStores.ContainsKey(templates[dsfTerm.TemplateName]))
                        templateStores.Add(templates[dsfTerm.TemplateName], templateStore);
                }
            }

            //Here handle the special case of the definition only containing 'standard columns'.
            if (templateStores.Count == 0 && templates.Count > 0)
            {
                foreach (KeyValuePair<string /*Template Name*/, Guid> template in templates)
                {
                    TemplateStore templateStore = new TemplateStore(template.Value, dsfTerms, defaultDateFormat);
                    foreach (DataStoreField dsfTerm in dsfTerms)
                    {
                        //Add the unique alias names in the order they are encountered.
                        if (!templateStore.orderedAliasNames.Contains(dsfTerm.Alias))
                            templateStore.orderedAliasNames.Add(dsfTerm.Alias);
                    }
                    templateStores.Add(template.Value, templateStore);
                }
            }

            //Once the collections for all of the TemplateStore's have been completed, perform the 'Alias mapping'.
            foreach (KeyValuePair<Guid /*TemplateID*/, TemplateStore> kvp in templateStores)
            {
                kvp.Value.GetTermAliasMapping();
            }

            return templateStores;
        }