protected override List <string> GetDescriptionValues(Entity entityInput, string behavior, bool withManaged, bool withSolutionInfo, bool withUrls, Action <List <string>, Entity, bool, bool, bool> action)
        {
            var entity = entityInput.ToEntity <DisplayString>();

            List <string> values = new List <string>();

            values.AddRange(new[]
            {
                entity.DisplayStringKey
                , LanguageLocale.GetLocaleName(entity.LanguageCode.Value)
                , entity.PublishedDisplayString
                , entity.CustomDisplayString
                , entity.CustomComment
                , entity.FormatParameters.ToString()
                , behavior
            });

            action(values, entity, withUrls, withManaged, withSolutionInfo);

            return(values);
        }
Ejemplo n.º 2
0
        private async Task <string> CheckMailMergeTemplates()
        {
            StringBuilder content = new StringBuilder();

            await _comparerSource.InitializeConnection(_iWriteToOutput, content);

            string operation = string.Format(Properties.OperationNames.CheckingMailMergeTemplatesFormat2, Connection1.Name, Connection2.Name);

            content.AppendLine(_iWriteToOutput.WriteToOutputStartOperation(null, operation));

            Task <List <MailMergeTemplate> > task1 = _comparerSource.GetMailMergeTemplate1Async();
            Task <List <MailMergeTemplate> > task2 = _comparerSource.GetMailMergeTemplate2Async();

            List <MailMergeTemplate> list1 = await task1;

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, "Mail Merge Templates in {0}: {1}", Connection1.Name, list1.Count()));

            List <MailMergeTemplate> list2 = await task2;

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, "Mail Merge Templates in {0}: {1}", Connection2.Name, list2.Count()));

            if (list1.Count == 0 && list2.Count == 0)
            {
                _iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.ThereIsNothingToCompare);
                _iWriteToOutput.WriteToOutputEndOperation(null, operation);
                return(null);
            }

            FormatTextTableHandler tableOnlyExistsIn1 = new FormatTextTableHandler();

            tableOnlyExistsIn1.SetHeader("AssociatedEntity", "Name", "Language", "File Name", "Mail Merge Type", "Viewable By", "Owner", "Id");

            FormatTextTableHandler tableOnlyExistsIn2 = new FormatTextTableHandler();

            tableOnlyExistsIn2.SetHeader("AssociatedEntity", "Name", "Language", "File Name", "Mail Merge Type", "Viewable By", "Owner", "Id");

            Dictionary <Tuple <string, string, string, string>, List <string> > dictDifference = new Dictionary <Tuple <string, string, string, string>, List <string> >();

            foreach (MailMergeTemplate template1 in list1)
            {
                {
                    MailMergeTemplate template2 = list2.FirstOrDefault(template => template.Id == template1.Id);

                    if (template2 != null)
                    {
                        continue;
                    }
                }

                string name1       = template1.Name;
                string entityName1 = template1.TemplateTypeCode;
                int    language1   = template1.LanguageCode.Value;

                string filename   = template1.FileName;
                string reportType = template1.FormattedValues.ContainsKey(MailMergeTemplate.Schema.Attributes.mailmergetype) ? template1.FormattedValues[MailMergeTemplate.Schema.Attributes.mailmergetype] : string.Empty;

                Microsoft.Xrm.Sdk.EntityReference ownerRef = template1.OwnerId;
                string owner = string.Empty;

                if (ownerRef != null)
                {
                    owner = ownerRef.Name;
                }

                string ispersonal = template1.FormattedValues.ContainsKey(MailMergeTemplate.Schema.Attributes.ispersonal) ? template1.FormattedValues[MailMergeTemplate.Schema.Attributes.ispersonal] : string.Empty;

                tableOnlyExistsIn1.AddLine(entityName1, name1, LanguageLocale.GetLocaleName(language1), filename, reportType, ispersonal, owner, template1.Id.ToString());

                this.ImageBuilder.AddComponentSolution1((int)ComponentType.MailMergeTemplate, template1.Id);
            }

            foreach (MailMergeTemplate template2 in list2)
            {
                {
                    MailMergeTemplate template1 = list1.FirstOrDefault(template => template.Id == template2.Id);

                    if (template1 != null)
                    {
                        continue;
                    }
                }

                string name2       = template2.Name;
                string entityName2 = template2.TemplateTypeCode;
                int    language2   = template2.LanguageCode.Value;

                string filename     = template2.FileName;
                string templateType = template2.FormattedValues.ContainsKey(MailMergeTemplate.Schema.Attributes.mailmergetype) ? template2.FormattedValues[MailMergeTemplate.Schema.Attributes.mailmergetype] : string.Empty;

                Microsoft.Xrm.Sdk.EntityReference ownerRef = template2.OwnerId;
                string owner = string.Empty;

                if (ownerRef != null)
                {
                    owner = ownerRef.Name;
                }

                string ispersonal = template2.FormattedValues.ContainsKey(MailMergeTemplate.Schema.Attributes.ispersonal) ? template2.FormattedValues[MailMergeTemplate.Schema.Attributes.ispersonal] : string.Empty;

                tableOnlyExistsIn2.AddLine(entityName2, name2, LanguageLocale.GetLocaleName(language2), filename, templateType, ispersonal, owner, template2.Id.ToString());

                this.ImageBuilder.AddComponentSolution2((int)ComponentType.MailMergeTemplate, template2.Id);
            }

            foreach (MailMergeTemplate template1 in list1)
            {
                MailMergeTemplate template2 = list2.FirstOrDefault(template => template.Id == template1.Id);

                if (template2 == null)
                {
                    continue;
                }

                FormatTextTableHandler tabDiff = new FormatTextTableHandler();
                tabDiff.SetHeader("Attribute", "Organization", "Value");

                foreach (string fieldName in _fieldsToCompareMailMergeTemplateOrdinal)
                {
                    if (ContentCoparerHelper.IsEntityDifferentInField(template1, template2, fieldName))
                    {
                        string str1 = EntityDescriptionHandler.GetAttributeString(template1, fieldName, Connection1);
                        string str2 = EntityDescriptionHandler.GetAttributeString(template2, fieldName, Connection2);

                        tabDiff.AddLine(fieldName, Connection1.Name, str1);
                        tabDiff.AddLine(fieldName, Connection2.Name, str2);
                    }
                }

                foreach (string fieldName in _fieldsToCompareMailMergeTemplateXml)
                {
                    string xml1 = template1.GetAttributeValue <string>(fieldName) ?? string.Empty;
                    string xml2 = template2.GetAttributeValue <string>(fieldName) ?? string.Empty;

                    if (!ContentCoparerHelper.CompareXML(xml1, xml2).IsEqual)
                    {
                        string reason = string.Empty;

                        ContentCopareResult compare = ContentCoparerHelper.CompareXML(xml1.ToLower(), xml2.ToLower(), true);

                        if (!compare.IsEqual)
                        {
                            reason = "InCase";
                        }
                        else
                        {
                            reason = compare.GetCompareDescription();
                        }

                        tabDiff.AddLine(fieldName, string.Empty, string.Format(Properties.OrganizationComparerStrings.FieldDifferenceReasonFormat3, Connection1.Name, Connection2.Name, reason));
                    }
                }

                if (tabDiff.Count > 0)
                {
                    string name1       = template1.Name;
                    string entityName1 = template1.TemplateTypeCode;
                    int    language1   = template1.LanguageCode.Value;

                    var diff = tabDiff.GetFormatedLines(false);
                    this.ImageBuilder.AddComponentDifferent((int)ComponentType.MailMergeTemplate, template1.Id, template2.Id, string.Join(Environment.NewLine, diff));

                    dictDifference.Add(Tuple.Create(entityName1, name1, LanguageLocale.GetLocaleName(language1), template1.Id.ToString()), diff);
                }
            }

            if (tableOnlyExistsIn1.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Mail Merge Templates ONLY EXISTS in {0}: {1}", Connection1.Name, tableOnlyExistsIn1.Count);

                tableOnlyExistsIn1.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()));
            }

            if (tableOnlyExistsIn2.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Mail Merge Templates ONLY EXISTS in {0}: {1}", Connection2.Name, tableOnlyExistsIn2.Count);

                tableOnlyExistsIn2.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()));
            }

            if (dictDifference.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Mail Merge Templates DIFFERENT in {0} and {1}: {2}", Connection1.Name, Connection2.Name, dictDifference.Count);

                FormatTextTableHandler tableDifference = new FormatTextTableHandler();
                tableDifference.SetHeader("AssociatedEntity", "Name", "Language", "Id");

                foreach (KeyValuePair <Tuple <string, string, string, string>, List <string> > template in dictDifference)
                {
                    tableDifference.CalculateLineLengths(template.Key.Item1, template.Key.Item2, template.Key.Item3, template.Key.Item4);
                }

                foreach (KeyValuePair <Tuple <string, string, string, string>, List <string> > template in dictDifference
                         .OrderBy(w => w.Key.Item1)
                         .ThenBy(w => w.Key.Item2)
                         .ThenBy(w => w.Key.Item3)
                         .ThenBy(w => w.Key.Item4)
                         )
                {
                    content.AppendLine().Append(tabSpacer + tableDifference.FormatLine(template.Key.Item1, template.Key.Item2, template.Key.Item3, template.Key.Item4));

                    foreach (string str in template.Value)
                    {
                        content.AppendLine().Append(tabSpacer + tabSpacer + str);
                    }
                }
            }

            if (tableOnlyExistsIn2.Count == 0 &&
                tableOnlyExistsIn1.Count == 0 &&
                dictDifference.Count == 0
                )
            {
                content.AppendLine("No difference in Mail Merge Templates.");
            }

            content.AppendLine().AppendLine().AppendLine(_iWriteToOutput.WriteToOutputEndOperation(null, operation));

            string fileName = EntityFileNameFormatter.GetDifferenceConnectionsForFieldFileName(_OrgOrgName, "Mail Merge Templates");

            string filePath = Path.Combine(_folder, FileOperations.RemoveWrongSymbols(fileName));

            File.WriteAllText(filePath, content.ToString(), new UTF8Encoding(false));

            await SaveOrganizationDifferenceImage();

            return(filePath);
        }
Ejemplo n.º 3
0
        private static void FillLabelEntityOnlyFirst(List <string> listStrings, Label displayName, Label displayCollectionName, Label description, string tabSpacer)
        {
            List <int> listLocale = new List <int>();

            if (displayName != null && displayName.LocalizedLabels != null)
            {
                foreach (var item in displayName.LocalizedLabels)
                {
                    if (!string.IsNullOrEmpty(item.Label))
                    {
                        if (!listLocale.Contains(item.LanguageCode))
                        {
                            listLocale.Add(item.LanguageCode);
                        }
                    }
                }
            }

            listLocale.Sort(LocaleComparer.Comparer);

            var localeId = listLocale.FirstOrDefault();

            if (localeId != default(int))
            {
                if (listStrings.Count > 0)
                {
                    listStrings.Add(string.Empty);
                }

                listStrings.Add(string.Format("({0}):", LanguageLocale.GetLocaleName(localeId)));

                HashSet <string> hashShowed = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

                if (displayName != null && displayName.LocalizedLabels != null)
                {
                    var locLabel = displayName.LocalizedLabels.FirstOrDefault(lbl => lbl.LanguageCode == localeId && !hashShowed.Contains(lbl.Label));

                    if (locLabel != null && !string.IsNullOrEmpty(locLabel.Label) && !hashShowed.Contains(locLabel.Label))
                    {
                        string[] split = string.Format("DisplayName: {0}", locLabel.Label).Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

                        foreach (var item in split)
                        {
                            listStrings.Add(tabSpacer + item);
                        }

                        hashShowed.Add(locLabel.Label);
                    }
                }

                if (displayCollectionName != null && displayCollectionName.LocalizedLabels != null)
                {
                    var locLabel = displayCollectionName.LocalizedLabels.FirstOrDefault(lbl => lbl.LanguageCode == localeId && !hashShowed.Contains(lbl.Label));

                    if (locLabel != null && !string.IsNullOrEmpty(locLabel.Label) && !hashShowed.Contains(locLabel.Label))
                    {
                        string[] split = string.Format("DisplayCollectionName: {0}", locLabel.Label).Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

                        foreach (var item in split)
                        {
                            listStrings.Add(tabSpacer + item);
                        }

                        hashShowed.Add(locLabel.Label);
                    }
                }

                if (description != null && description.LocalizedLabels != null)
                {
                    var locLabel = description.LocalizedLabels.FirstOrDefault(lbl => lbl.LanguageCode == localeId && !hashShowed.Contains(lbl.Label));

                    if (locLabel != null && !string.IsNullOrEmpty(locLabel.Label) && !hashShowed.Contains(locLabel.Label))
                    {
                        string[] split = string.Format("Description: {0}", locLabel.Label).Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

                        foreach (var item in split)
                        {
                            listStrings.Add(tabSpacer + item);
                        }

                        hashShowed.Add(locLabel.Label);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private static void FillLabelDisplayNameAndDescriptionAll(List <string> listStrings, Label displayName, Label description, string tabSpacer)
        {
            List <int> listLocale = new List <int>();

            if (displayName != null && displayName.LocalizedLabels != null)
            {
                foreach (var item in displayName.LocalizedLabels)
                {
                    if (!string.IsNullOrEmpty(item.Label))
                    {
                        if (!listLocale.Contains(item.LanguageCode))
                        {
                            listLocale.Add(item.LanguageCode);
                        }
                    }
                }
            }

            if (description != null && description.LocalizedLabels != null)
            {
                foreach (var item in description.LocalizedLabels)
                {
                    if (!string.IsNullOrEmpty(item.Label))
                    {
                        if (!listLocale.Contains(item.LanguageCode))
                        {
                            listLocale.Add(item.LanguageCode);
                        }
                    }
                }
            }

            listLocale.Sort(LocaleComparer.Comparer);

            if (listLocale.Any())
            {
                HashSet <string> hashShowed = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

                if (displayName != null && displayName.LocalizedLabels != null)
                {
                    var sortedDisplay = displayName.LocalizedLabels.Where(l => listLocale.Contains(l.LanguageCode) && !string.IsNullOrEmpty(l.Label) && !hashShowed.Contains(l.Label));

                    if (sortedDisplay.Any())
                    {
                        if (listStrings.Count > 0)
                        {
                            listStrings.Add(string.Empty);
                        }

                        listStrings.Add("DisplayName:");

                        foreach (var localeId in listLocale)
                        {
                            var locLabel = sortedDisplay.FirstOrDefault(l => l.LanguageCode == localeId);

                            if (locLabel != null && !string.IsNullOrEmpty(locLabel.Label) && !hashShowed.Contains(locLabel.Label))
                            {
                                var str = string.Format("({0}): {1}", LanguageLocale.GetLocaleName(localeId), locLabel.Label);

                                listStrings.Add(tabSpacer + str);

                                hashShowed.Add(locLabel.Label);
                            }
                        }
                    }
                }

                if (description != null && description.LocalizedLabels != null)
                {
                    var sortedDescription = description.LocalizedLabels.Where(l => listLocale.Contains(l.LanguageCode) && !string.IsNullOrEmpty(l.Label) && !hashShowed.Contains(l.Label));

                    if (sortedDescription.Any())
                    {
                        if (listStrings.Count > 0)
                        {
                            listStrings.Add(string.Empty);
                        }

                        listStrings.Add("Description:");

                        foreach (var localeId in listLocale)
                        {
                            var locLabel = sortedDescription.FirstOrDefault(l => l.LanguageCode == localeId);

                            if (locLabel != null && !string.IsNullOrEmpty(locLabel.Label) && !hashShowed.Contains(locLabel.Label))
                            {
                                string[] split = string.Format("({0}): {1}", LanguageLocale.GetLocaleName(localeId), locLabel.Label).Split(new string[] { "\r", "\n" }, StringSplitOptions.None);

                                foreach (var item in split)
                                {
                                    listStrings.Add(tabSpacer + item);
                                }

                                hashShowed.Add(locLabel.Label);
                            }
                        }
                    }
                }
            }
        }
        private void CompareTranslations(StringBuilder content, Translation translation1, Translation translation2)
        {
            {
                FormatTextTableHandler displayStringsOnlyExistsIn1 = new FormatTextTableHandler();
                displayStringsOnlyExistsIn1.SetHeader("EntityName", "StringKey", "LanguageCode", "Label");

                FormatTextTableHandler displayStringsOnlyExistsIn2 = new FormatTextTableHandler();
                displayStringsOnlyExistsIn2.SetHeader("EntityName", "StringKey", "LanguageCode", "Label");

                FormatTextTableHandler displayStringsDifference = new FormatTextTableHandler();
                displayStringsDifference.SetHeader("EntityName", "StringKey", "LanguageCode", "Connection", "Label");

                foreach (var displayString1 in translation1.DisplayStrings)
                {
                    {
                        var displayString2 = translation2.DisplayStrings.FirstOrDefault(d =>
                                                                                        string.Equals(displayString1.EntityName, d.EntityName, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                        string.Equals(displayString1.StringKey, d.StringKey, StringComparison.InvariantCultureIgnoreCase)
                                                                                        );

                        if (displayString2 != null)
                        {
                            continue;
                        }
                    }

                    foreach (var label in displayString1.Labels)
                    {
                        displayStringsOnlyExistsIn1.AddLine(displayString1.EntityName, displayString1.StringKey, LanguageLocale.GetLocaleName(label.LanguageCode), label.Value);
                    }
                }

                foreach (var displayString2 in translation2.DisplayStrings)
                {
                    {
                        var displayString1 = translation1.DisplayStrings.FirstOrDefault(d =>
                                                                                        string.Equals(displayString2.EntityName, d.EntityName, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                        string.Equals(displayString2.StringKey, d.StringKey, StringComparison.InvariantCultureIgnoreCase)
                                                                                        );

                        if (displayString1 != null)
                        {
                            continue;
                        }
                    }

                    foreach (var label in displayString2.Labels)
                    {
                        displayStringsOnlyExistsIn2.AddLine(displayString2.EntityName, displayString2.StringKey, LanguageLocale.GetLocaleName(label.LanguageCode), label.Value);
                    }
                }

                foreach (var displayString1 in translation1.DisplayStrings)
                {
                    var displayString2 = translation2.DisplayStrings.FirstOrDefault(d =>
                                                                                    string.Equals(displayString1.EntityName, d.EntityName, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                    string.Equals(displayString1.StringKey, d.StringKey, StringComparison.InvariantCultureIgnoreCase)
                                                                                    );

                    if (displayString2 == null)
                    {
                        continue;
                    }

                    var diff = LabelComparer.GetDifference(displayString1, displayString2);

                    if (!diff.IsEmpty)
                    {
                        foreach (var item in diff.LabelsOnlyIn1)
                        {
                            displayStringsDifference.AddLine(displayString1.EntityName, displayString1.StringKey, LanguageLocale.GetLocaleName(item.LanguageCode), Connection1.Name, item.Value);
                        }

                        foreach (var item in diff.LabelsOnlyIn2)
                        {
                            displayStringsDifference.AddLine(displayString1.EntityName, displayString1.StringKey, LanguageLocale.GetLocaleName(item.LanguageCode), Connection2.Name, item.Value);
                        }

                        foreach (var item in diff.LabelDifference)
                        {
                            displayStringsDifference.AddLine(displayString1.EntityName, displayString1.StringKey, LanguageLocale.GetLocaleName(item.LanguageCode), Connection1.Name, item.Value1);
                            displayStringsDifference.AddLine(displayString1.EntityName, displayString1.StringKey, LanguageLocale.GetLocaleName(item.LanguageCode), Connection2.Name, item.Value2);
                        }
                    }
                }

                if (displayStringsOnlyExistsIn1.Count > 0)
                {
                    content
                    .AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .AppendLine(new string('-', 150))
                    .AppendLine()
                    .AppendLine();

                    content.AppendLine().AppendLine().AppendFormat("Display Strings ONLY EXISTS in {0}: {1}", Connection1.Name, displayStringsOnlyExistsIn1.Count);

                    displayStringsOnlyExistsIn1.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()));
                }

                if (displayStringsOnlyExistsIn2.Count > 0)
                {
                    content
                    .AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .AppendLine(new string('-', 150))
                    .AppendLine()
                    .AppendLine();

                    content.AppendLine().AppendLine().AppendFormat("Display Strings ONLY EXISTS in {0}: {1}", Connection2.Name, displayStringsOnlyExistsIn2.Count);

                    displayStringsOnlyExistsIn2.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()));
                }

                if (displayStringsDifference.Count > 0)
                {
                    content
                    .AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .AppendLine(new string('-', 150))
                    .AppendLine()
                    .AppendLine();

                    content.AppendLine().AppendLine().AppendFormat("Display Strings DIFFERENT in {0} and {1}: {2}", Connection1.Name, Connection2.Name, displayStringsDifference.Count);

                    displayStringsDifference.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()).AppendLine());
                }

                if (displayStringsOnlyExistsIn2.Count == 0 &&
                    displayStringsOnlyExistsIn1.Count == 0 &&
                    displayStringsDifference.Count == 0
                    )
                {
                    content.AppendLine("No difference in Display Strings.");
                }
            }

            {
                FormatTextTableHandler localizedLabelsOnlyExistsIn1 = new FormatTextTableHandler();
                localizedLabelsOnlyExistsIn1.SetHeader("EntityName", "ObjectId", "ColumnName", "LanguageCode", "Label");

                FormatTextTableHandler localizedLabelsOnlyExistsIn2 = new FormatTextTableHandler();
                localizedLabelsOnlyExistsIn2.SetHeader("EntityName", "ObjectId", "ColumnName", "LanguageCode", "Label");

                FormatTextTableHandler localizedLabelsDifference = new FormatTextTableHandler();
                localizedLabelsDifference.SetHeader("EntityName", "ObjectId", "ColumnName", "LanguageCode", "Connection", "Label");

                foreach (var locLabel1 in translation1.LocalizedLabels)
                {
                    {
                        var locLabel2 = translation2.LocalizedLabels.FirstOrDefault(d =>
                                                                                    string.Equals(locLabel1.EntityName, d.EntityName, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                    string.Equals(locLabel1.ColumnName, d.ColumnName, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                    locLabel1.ObjectId == d.ObjectId
                                                                                    );

                        if (locLabel2 != null)
                        {
                            continue;
                        }
                    }

                    foreach (var label in locLabel1.Labels)
                    {
                        localizedLabelsOnlyExistsIn1.AddLine(locLabel1.EntityName, locLabel1.ObjectId.ToString(), locLabel1.ColumnName, LanguageLocale.GetLocaleName(label.LanguageCode), label.Value);
                    }
                }

                foreach (var locLabel2 in translation2.LocalizedLabels)
                {
                    {
                        var locLabel1 = translation1.LocalizedLabels.FirstOrDefault(d =>
                                                                                    string.Equals(locLabel2.EntityName, d.EntityName, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                    string.Equals(locLabel2.ColumnName, d.ColumnName, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                    locLabel2.ObjectId == d.ObjectId
                                                                                    );

                        if (locLabel1 != null)
                        {
                            continue;
                        }
                    }

                    foreach (var label in locLabel2.Labels)
                    {
                        localizedLabelsOnlyExistsIn2.AddLine(locLabel2.EntityName, locLabel2.ObjectId.ToString(), locLabel2.ColumnName, LanguageLocale.GetLocaleName(label.LanguageCode), label.Value);
                    }
                }

                foreach (var locLabel1 in translation1.LocalizedLabels)
                {
                    var locLabel2 = translation2.LocalizedLabels.FirstOrDefault(d =>
                                                                                string.Equals(locLabel1.EntityName, d.EntityName, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                string.Equals(locLabel1.ColumnName, d.ColumnName, StringComparison.InvariantCultureIgnoreCase) &&
                                                                                locLabel1.ObjectId == d.ObjectId
                                                                                );

                    if (locLabel2 == null)
                    {
                        continue;
                    }

                    var diff = LabelComparer.GetDifference(locLabel1, locLabel2);

                    if (!diff.IsEmpty)
                    {
                        foreach (var item in diff.LabelsOnlyIn1)
                        {
                            localizedLabelsDifference.AddLine(locLabel1.EntityName, locLabel1.ObjectId.ToString(), locLabel1.ColumnName, LanguageLocale.GetLocaleName(item.LanguageCode), Connection1.Name, item.Value);
                        }

                        foreach (var item in diff.LabelsOnlyIn2)
                        {
                            localizedLabelsDifference.AddLine(locLabel1.EntityName, locLabel1.ObjectId.ToString(), locLabel1.ColumnName, LanguageLocale.GetLocaleName(item.LanguageCode), Connection2.Name, item.Value);
                        }

                        foreach (var item in diff.LabelDifference)
                        {
                            localizedLabelsDifference.AddLine(locLabel1.EntityName, locLabel1.ObjectId.ToString(), locLabel1.ColumnName, LanguageLocale.GetLocaleName(item.LanguageCode), Connection1.Name, item.Value1);
                            localizedLabelsDifference.AddLine(locLabel1.EntityName, locLabel1.ObjectId.ToString(), locLabel1.ColumnName, LanguageLocale.GetLocaleName(item.LanguageCode), Connection2.Name, item.Value2);
                        }
                    }
                }

                if (localizedLabelsOnlyExistsIn1.Count > 0)
                {
                    content
                    .AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .AppendLine(new string('-', 150))
                    .AppendLine()
                    .AppendLine();

                    content.AppendLine().AppendLine().AppendFormat("Localized Labels ONLY EXISTS in {0}: {1}", Connection1.Name, localizedLabelsOnlyExistsIn1.Count);

                    localizedLabelsOnlyExistsIn1.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()));
                }

                if (localizedLabelsOnlyExistsIn2.Count > 0)
                {
                    content
                    .AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .AppendLine(new string('-', 150))
                    .AppendLine()
                    .AppendLine();

                    content.AppendLine().AppendLine().AppendFormat("Localized Labels ONLY EXISTS in {0}: {1}", Connection2.Name, localizedLabelsOnlyExistsIn2.Count);

                    localizedLabelsOnlyExistsIn2.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()));
                }

                if (localizedLabelsDifference.Count > 0)
                {
                    content
                    .AppendLine()
                    .AppendLine()
                    .AppendLine()
                    .AppendLine(new string('-', 150))
                    .AppendLine()
                    .AppendLine();

                    content.AppendLine().AppendLine().AppendFormat("Localized Labels DIFFERENT in {0} and {1}: {2}", Connection1.Name, Connection2.Name, localizedLabelsDifference.Count);

                    localizedLabelsDifference.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()).AppendLine());
                }

                if (localizedLabelsOnlyExistsIn2.Count == 0 &&
                    localizedLabelsOnlyExistsIn1.Count == 0 &&
                    localizedLabelsDifference.Count == 0
                    )
                {
                    content.AppendLine("No difference in Localized Labels.");
                }
            }
        }
        private async Task <string> CheckDisplayStrings()
        {
            StringBuilder content = new StringBuilder();

            await _comparerSource.InitializeConnection(_iWriteToOutput, content);

            string operation = string.Format(Properties.OperationNames.CheckingDisplayStringsFormat2, Connection1.Name, Connection2.Name);

            content.AppendLine(_iWriteToOutput.WriteToOutputStartOperation(null, operation));

            var task1 = _comparerSource.GetDisplayString1Async();
            var task2 = _comparerSource.GetDisplayString2Async();

            var list1 = await task1;

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, "Display Strings in {0}: {1}", Connection1.Name, list1.Count()));

            var list2 = await task2;

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, "Display Strings in {0}: {1}", Connection2.Name, list2.Count()));

            if (list1.Count == 0 && list2.Count == 0)
            {
                _iWriteToOutput.WriteToOutput(null, Properties.OrganizationComparerStrings.ThereIsNothingToCompare);
                _iWriteToOutput.WriteToOutputEndOperation(null, operation);
                return(null);
            }

            var taskMap1 = _comparerSource.GetDisplayStringMap1Async();
            var taskMap2 = _comparerSource.GetDisplayStringMap2Async();

            var listMap1 = await taskMap1;

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, "Display Strings Maps in {0}: {1}", Connection1.Name, listMap1.Count()));

            var listMap2 = await taskMap2;

            content.AppendLine(_iWriteToOutput.WriteToOutput(null, "Display Strings Maps in {0}: {1}", Connection1.Name, listMap2.Count()));

            FormatTextTableHandler tableOnlyExistsIn1 = new FormatTextTableHandler();

            tableOnlyExistsIn1.SetHeader("Key", "LanguageCode", "Published", "Custom", "CustomComment", "FormatParameters");

            FormatTextTableHandler tableOnlyExistsIn2 = new FormatTextTableHandler();

            tableOnlyExistsIn2.SetHeader("Key", "LanguageCode", "Published", "Custom", "CustomComment", "FormatParameters");

            var dictDifference = new Dictionary <Tuple <string, string>, List <string> >();

            foreach (var displayString1 in list1)
            {
                var displaystringkey1 = displayString1.DisplayStringKey;
                var languagecode1     = displayString1.LanguageCode.Value;

                {
                    var displayString2 = list2.FirstOrDefault(displayString =>
                    {
                        var displaystringkey2 = displayString.DisplayStringKey;
                        var languagecode2     = displayString.LanguageCode.Value;

                        return(displaystringkey1 == displaystringkey2 && languagecode1 == languagecode2);
                    });

                    if (displayString2 != null)
                    {
                        continue;
                    }
                }

                var customComment          = displayString1.CustomComment;
                var customDisplayString    = displayString1.CustomComment;
                var formatParameters       = displayString1.FormatParameters.Value;
                var publishedDisplayString = displayString1.PublishedDisplayString;

                tableOnlyExistsIn1.AddLine(displaystringkey1, LanguageLocale.GetLocaleName(languagecode1), publishedDisplayString, customDisplayString, customComment, formatParameters.ToString());

                this.ImageBuilder.AddComponentSolution1((int)ComponentType.DisplayString, displayString1.Id);
            }

            foreach (var displayString2 in list2)
            {
                var displaystringkey2 = displayString2.DisplayStringKey;
                var languagecode2     = displayString2.LanguageCode.Value;

                {
                    var displayString1 = list1.FirstOrDefault(displayString =>
                    {
                        var displaystringkey1 = displayString.DisplayStringKey;
                        var languagecode1     = displayString.LanguageCode.Value;

                        return(displaystringkey1 == displaystringkey2 && languagecode1 == languagecode2);
                    });

                    if (displayString1 != null)
                    {
                        continue;
                    }
                }

                var customComment          = displayString2.CustomComment;
                var customDisplayString    = displayString2.CustomDisplayString;
                var formatParameters       = displayString2.FormatParameters.Value;
                var publishedDisplayString = displayString2.PublishedDisplayString;

                tableOnlyExistsIn2.AddLine(displaystringkey2, LanguageLocale.GetLocaleName(languagecode2), publishedDisplayString, customDisplayString, customComment, formatParameters.ToString());

                this.ImageBuilder.AddComponentSolution2((int)ComponentType.DisplayString, displayString2.Id);
            }

            foreach (var displayString1 in list1)
            {
                var displaystringkey1 = displayString1.DisplayStringKey;
                var languagecode1     = displayString1.LanguageCode.Value;

                var displayString2 = list2.FirstOrDefault(displayString =>
                {
                    var displaystringkey2 = displayString.DisplayStringKey;
                    var languagecode2     = displayString.LanguageCode.Value;

                    return(displaystringkey1 == displaystringkey2 && languagecode1 == languagecode2);
                });

                if (displayString2 == null)
                {
                    continue;
                }

                FormatTextTableHandler tabDiff = new FormatTextTableHandler();
                tabDiff.SetHeader("Attribute", "Organization", "Value");

                var temp1 = listMap1.Where(s => s.DisplayStringId == displayString1.Id);
                var temp2 = listMap2.Where(s => s.DisplayStringId == displayString2.Id);

                if (temp1.SequenceEqual(temp2))
                {
                    tabDiff.AddLine("DisplayStringMap", Connection1.Name, "Differs");
                    tabDiff.AddLine("DisplayStringMap", Connection2.Name, "Differs");
                }

                List <string> fieldsToCompare = new List <string>()
                {
                    DisplayString.Schema.Attributes.customcomment
                    , DisplayString.Schema.Attributes.customdisplaystring
                    , DisplayString.Schema.Attributes.formatparameters
                    , DisplayString.Schema.Attributes.publisheddisplaystring
                };

                foreach (var fieldName in fieldsToCompare)
                {
                    if (ContentComparerHelper.IsEntityDifferentInField(displayString1, displayString2, fieldName))
                    {
                        var str1 = EntityDescriptionHandler.GetAttributeString(displayString1, fieldName, Connection1);
                        var str2 = EntityDescriptionHandler.GetAttributeString(displayString2, fieldName, Connection2);

                        tabDiff.AddLine(fieldName, Connection1.Name, str1);
                        tabDiff.AddLine(fieldName, Connection2.Name, str2);
                    }
                }

                if (tabDiff.Count > 0)
                {
                    var diff = tabDiff.GetFormatedLines(false);

                    dictDifference.Add(Tuple.Create(displaystringkey1, LanguageLocale.GetLocaleName(languagecode1)), diff);

                    this.ImageBuilder.AddComponentDifferent((int)ComponentType.DisplayString, displayString1.Id, displayString2.Id, string.Join(Environment.NewLine, diff));
                }
            }

            if (tableOnlyExistsIn1.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Display Strings ONLY EXISTS in {0}: {1}", Connection1.Name, tableOnlyExistsIn1.Count);

                tableOnlyExistsIn1.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()));
            }

            if (tableOnlyExistsIn2.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Display Strings ONLY EXISTS in {0}: {1}", Connection2.Name, tableOnlyExistsIn2.Count);

                tableOnlyExistsIn2.GetFormatedLines(true).ForEach(e => content.AppendLine().Append((tabSpacer + e).TrimEnd()));
            }

            if (dictDifference.Count > 0)
            {
                content
                .AppendLine()
                .AppendLine()
                .AppendLine()
                .AppendLine(new string('-', 150))
                .AppendLine()
                .AppendLine();

                content.AppendLine().AppendLine().AppendFormat("Display Strings DIFFERENT in {0} and {1}: {2}", Connection1.Name, Connection2.Name, dictDifference.Count);

                FormatTextTableHandler tableDifference = new FormatTextTableHandler();
                tableDifference.SetHeader("Key", "LanguageCode");

                foreach (var displayString in dictDifference)
                {
                    tableDifference.CalculateLineLengths(displayString.Key.Item1, displayString.Key.Item2);
                }

                foreach (var displayString in dictDifference
                         .OrderBy(w => w.Key.Item1)
                         .ThenBy(w => w.Key.Item2)
                         )
                {
                    content.AppendLine().Append(tabSpacer + tableDifference.FormatLine(displayString.Key.Item1, displayString.Key.Item2));

                    foreach (var str in displayString.Value)
                    {
                        content.AppendLine().Append(tabSpacer + tabSpacer + str);
                    }
                }
            }

            if (tableOnlyExistsIn2.Count == 0 &&
                tableOnlyExistsIn1.Count == 0 &&
                dictDifference.Count == 0
                )
            {
                content.AppendLine("No difference in Display Strings.");
            }

            content.AppendLine().AppendLine().AppendLine(_iWriteToOutput.WriteToOutputEndOperation(null, operation));

            string fileName = EntityFileNameFormatter.GetDifferenceConnectionsForFieldFileName(_OrgOrgName, "Display Strings");

            string filePath = Path.Combine(_folder, FileOperations.RemoveWrongSymbols(fileName));

            File.WriteAllText(filePath, content.ToString(), new UTF8Encoding(false));

            await SaveOrganizationDifferenceImage();

            return(filePath);
        }
        internal static LabelDifferenceResult GetDifference(TranslationDisplayString displayString1, TranslationDisplayString displayString2)
        {
            List <int> listLocale = new List <int>();

            foreach (var item in displayString1.Labels)
            {
                if (!string.IsNullOrEmpty(item.Value))
                {
                    if (!listLocale.Contains(item.LanguageCode))
                    {
                        listLocale.Add(item.LanguageCode);
                    }
                }
            }

            foreach (var item in displayString2.Labels)
            {
                if (!string.IsNullOrEmpty(item.Value))
                {
                    if (!listLocale.Contains(item.LanguageCode))
                    {
                        listLocale.Add(item.LanguageCode);
                    }
                }
            }

            listLocale.Sort(LocaleComparer.Comparer);

            LabelDifferenceResult result = new LabelDifferenceResult();

            if (listLocale.Any())
            {
                var sorted1 = displayString1.Labels.Where(l => listLocale.Contains(l.LanguageCode) && !string.IsNullOrEmpty(l.Value));
                var sorted2 = displayString2.Labels.Where(l => listLocale.Contains(l.LanguageCode) && !string.IsNullOrEmpty(l.Value));

                foreach (var localeId in listLocale)
                {
                    var str = string.Format("({0}):", LanguageLocale.GetLocaleName(localeId));

                    var locLabel1 = sorted1.FirstOrDefault(l => l.LanguageCode == localeId);
                    var locLabel2 = sorted2.FirstOrDefault(l => l.LanguageCode == localeId);

                    if (locLabel1 != null && locLabel2 == null)
                    {
                        result.LabelsOnlyIn1.Add(new LabelString()
                        {
                            LanguageCode = localeId, Locale = str, Value = locLabel1.Value
                        });
                    }
                    else if (locLabel1 == null && locLabel2 != null)
                    {
                        result.LabelsOnlyIn2.Add(new LabelString()
                        {
                            LanguageCode = localeId, Locale = str, Value = locLabel2.Value
                        });
                    }
                    else if (locLabel1 != null && locLabel2 != null)
                    {
                        if (locLabel1.Value != locLabel2.Value)
                        {
                            result.LabelDifference.Add(new LabelStringDifference()
                            {
                                LanguageCode = localeId, Locale = str, Value1 = locLabel1.Value, Value2 = locLabel2.Value
                            });
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 8
0
        public static LabelDifferenceResult GetDifference(Label label1, Label label2)
        {
            List <int> listLocale = new List <int>();

            foreach (var item in label1.LocalizedLabels)
            {
                if (!string.IsNullOrEmpty(item.Label))
                {
                    if (!listLocale.Contains(item.LanguageCode))
                    {
                        listLocale.Add(item.LanguageCode);
                    }
                }
            }

            foreach (var item in label2.LocalizedLabels)
            {
                if (!string.IsNullOrEmpty(item.Label))
                {
                    if (!listLocale.Contains(item.LanguageCode))
                    {
                        listLocale.Add(item.LanguageCode);
                    }
                }
            }

            listLocale.Sort(new LocaleComparer());

            LabelDifferenceResult result = new LabelDifferenceResult();

            if (listLocale.Any())
            {
                var sorted1 = label1.LocalizedLabels.Where(l => listLocale.Contains(l.LanguageCode) && !string.IsNullOrEmpty(l.Label));
                var sorted2 = label2.LocalizedLabels.Where(l => listLocale.Contains(l.LanguageCode) && !string.IsNullOrEmpty(l.Label));

                foreach (var localeId in listLocale)
                {
                    var str = string.Format("({0}):", LanguageLocale.GetLocaleName(localeId));

                    var locLabel1 = sorted1.FirstOrDefault(l => l.LanguageCode == localeId);
                    var locLabel2 = sorted2.FirstOrDefault(l => l.LanguageCode == localeId);

                    if (locLabel1 != null && locLabel2 == null)
                    {
                        result.LabelsOnlyIn1.Add(new LabelString()
                        {
                            LanguageCode = localeId, Locale = str, Value = locLabel1.Label
                        });
                    }
                    else if (locLabel1 == null && locLabel2 != null)
                    {
                        result.LabelsOnlyIn2.Add(new LabelString()
                        {
                            LanguageCode = localeId, Locale = str, Value = locLabel2.Label
                        });
                    }
                    else if (locLabel1 != null && locLabel2 != null)
                    {
                        if (locLabel1.Label != locLabel2.Label)
                        {
                            result.LabelDifference.Add(new LabelStringDifference()
                            {
                                LanguageCode = localeId, Locale = str, Value1 = locLabel1.Label, Value2 = locLabel2.Label
                            });
                        }
                    }
                }
            }

            return(result);
        }
        private static async Task LoadOrganizationDataAsync(OrganizationServiceExtentedProxy service, OrganizationDetail organizationDetail, WhoAmIResponse whoAmIResponse = null)
        {
            try
            {
                Guid?idOrganization = null;

                if (organizationDetail != null)
                {
                    idOrganization = organizationDetail.OrganizationId;

                    service.ConnectionData.OrganizationInformationExpirationDate = DateTime.Now.AddHours(_hoursOrganizationInformation);

                    service.ConnectionData.FriendlyName        = organizationDetail.FriendlyName;
                    service.ConnectionData.OrganizationId      = organizationDetail.OrganizationId;
                    service.ConnectionData.OrganizationVersion = organizationDetail.OrganizationVersion;
                    service.ConnectionData.OrganizationState   = organizationDetail.State.ToString();
                    service.ConnectionData.UniqueOrgName       = organizationDetail.UniqueName;
                    service.ConnectionData.UrlName             = organizationDetail.UrlName;

                    if (organizationDetail.Endpoints.ContainsKey(EndpointType.OrganizationService))
                    {
                        var organizationUrlEndpoint = organizationDetail.Endpoints[EndpointType.OrganizationService];

                        if (string.IsNullOrEmpty(service.ConnectionData.OrganizationUrl) &&
                            !string.IsNullOrEmpty(organizationUrlEndpoint)
                            )
                        {
                            service.ConnectionData.OrganizationUrl = organizationUrlEndpoint;
                        }
                    }

                    if (organizationDetail.Endpoints.ContainsKey(EndpointType.WebApplication))
                    {
                        var publicUrl = organizationDetail.Endpoints[EndpointType.WebApplication];

                        if (string.IsNullOrEmpty(service.ConnectionData.PublicUrl) &&
                            !string.IsNullOrEmpty(publicUrl)
                            )
                        {
                            service.ConnectionData.PublicUrl = publicUrl;
                        }
                    }
                }

                if (!idOrganization.HasValue)
                {
                    if (whoAmIResponse == null)
                    {
                        whoAmIResponse = await service.ExecuteAsync <WhoAmIResponse>(new WhoAmIRequest());
                    }

                    idOrganization = whoAmIResponse.OrganizationId;
                }

                service.ConnectionData.DefaultLanguage        = string.Empty;
                service.ConnectionData.BaseCurrency           = string.Empty;
                service.ConnectionData.DefaultLanguage        = string.Empty;
                service.ConnectionData.InstalledLanguagePacks = string.Empty;

                if (idOrganization.HasValue)
                {
                    var organization = await service
                                       .RetrieveAsync <Organization>(Organization.EntityLogicalName, idOrganization.Value, new ColumnSet(Organization.Schema.Attributes.languagecode, Organization.Schema.Attributes.basecurrencyid))
                    ;

                    if (organization.BaseCurrencyId != null)
                    {
                        service.ConnectionData.BaseCurrency = organization.BaseCurrencyId.Name;
                    }

                    var request  = new RetrieveInstalledLanguagePacksRequest();
                    var response = await service.ExecuteAsync <RetrieveInstalledLanguagePacksResponse>(request);

                    var rep = new EntityMetadataRepository(service);

                    var isEntityExists = rep.IsEntityExists(LanguageLocale.EntityLogicalName);

                    if (isEntityExists)
                    {
                        var repository = new LanguageLocaleRepository(service);

                        if (organization.LanguageCode.HasValue)
                        {
                            var lang = (await repository.GetListAsync(organization.LanguageCode.Value)).FirstOrDefault();

                            if (lang != null)
                            {
                                service.ConnectionData.DefaultLanguage = lang.ToString();
                            }
                            else
                            {
                                service.ConnectionData.DefaultLanguage = LanguageLocale.GetLocaleName(organization.LanguageCode.Value);
                            }
                        }

                        if (response.RetrieveInstalledLanguagePacks != null && response.RetrieveInstalledLanguagePacks.Any())
                        {
                            var list = await repository.GetListAsync(response.RetrieveInstalledLanguagePacks);

                            service.ConnectionData.InstalledLanguagePacks = string.Join(",", list.OrderBy(s => s.LocaleId.Value, LocaleComparer.Comparer).Select(l => l.ToString()));
                        }
                    }
                    else
                    {
                        if (organization.LanguageCode.HasValue)
                        {
                            service.ConnectionData.DefaultLanguage = LanguageLocale.GetLocaleName(organization.LanguageCode.Value);
                        }

                        if (response.RetrieveInstalledLanguagePacks != null && response.RetrieveInstalledLanguagePacks.Any())
                        {
                            service.ConnectionData.InstalledLanguagePacks = string.Join(",", response.RetrieveInstalledLanguagePacks.OrderBy(s => s, LocaleComparer.Comparer).Select(l => LanguageLocale.GetLocaleName(l)));
                        }
                    }
                }

                if (string.IsNullOrEmpty(service.ConnectionData.PublicUrl) &&
                    !string.IsNullOrEmpty(service.ConnectionData.OrganizationUrl)
                    )
                {
                    var orgUrl = service.ConnectionData.OrganizationUrl.TrimEnd('/');

                    if (orgUrl.EndsWith("/XRMServices/2011/Organization.svc", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var lastIndex = orgUrl.LastIndexOf("/XRMServices/2011/Organization.svc", StringComparison.InvariantCultureIgnoreCase);

                        var publicUrl = orgUrl.Substring(0, lastIndex + 1).TrimEnd('/');

                        service.ConnectionData.PublicUrl = publicUrl;
                    }
                }
            }
            catch (Exception ex)
            {
                DTEHelper.WriteExceptionToOutput(service.ConnectionData, ex);
            }
        }