public string MapHtmlColorToCategoryOrNull(string htmlColor, IEntitySynchronizationLogger logger)
        {
            var categoryColor = ColorMapper.MapHtmlColorToCategoryColor(htmlColor);

            if (_categoryByHtmlColor.TryGetValue(htmlColor, out var mapping))
            {
                var mappedCategory = mapping.PreferredCategory;

                if (_outlookColorByCategory.TryGetValue(mappedCategory, out var colorOfPreferredCategory))
                {
                    if (colorOfPreferredCategory == categoryColor)
                    {
                        return(mappedCategory);
                    }
                }
            }

            string category = null;

            foreach (var entry in _outlookColorByCategory)
            {
                if (entry.Value == categoryColor)
                {
                    category = entry.Key;
                    break;
                }
            }

            if (category == null)
            {
                // No category with the required color exists. Create one with the corresponding html color name.
                category = ColorMapper.MapCategoryColorToHtmlColor(categoryColor);
                var(addCategoryResult, existingColorNameOrNull) = _outlookSession.AddCategoryNoThrow(category, categoryColor);
                switch (addCategoryResult)
                {
                case CreateCategoryResult.DidAlreadyExist:
                    logger.LogWarning($"Did not map html color '{htmlColor}' to category '{category}', since category already exists with the wrong color ('{existingColorNameOrNull}').");
                    return(null);

                case CreateCategoryResult.Error:
                    logger.LogError($"Error while trying to create category '{category}'.");
                    return(null);

                case CreateCategoryResult.Ok:
                    _outlookColorByCategory.Add(category, categoryColor);
                    break;
                }
            }

            ColorCategoryMapping newMapping = new ColorCategoryMapping
            {
                HtmlColor         = htmlColor,
                PreferredCategory = category
            };

            _categoryByHtmlColor[htmlColor] = newMapping;
            _colorMappingsDataAccess.Save(_categoryByHtmlColor.Values);

            return(category);
        }
コード例 #2
0
 public string MapCategoryToHtmlColorOrNull(string categoryName)
 {
     if (_outlookColorByCategory.TryGetValue(categoryName, out var color))
     {
         return(ColorMapper.MapCategoryColorToHtmlColor(color));
     }
     else
     {
         return(null);
     }
 }