using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using AutoMapper; using gehGassi.Domain.Localization; using gehGassi.Web.Helper; using gehGassi.Web.Services; using Microsoft.AspNetCore.Hosting; using Newtonsoft.Json; namespace gehGassi.Web.Mapper { #region UTC To Local public class UtcToLocalDateTimeResolver : IMemberValueResolver { private readonly ICurrentTimeZoneService _currentTimeZoneService; public UtcToLocalDateTimeResolver(ICurrentTimeZoneService currentTimeZoneService) { _currentTimeZoneService = currentTimeZoneService; } public DateTime Resolve(object source, object destination, DateTime sourceMember, DateTime destMember, ResolutionContext context) { return TimeZoneHelper.ConvertToLocal(sourceMember, _currentTimeZoneService.GetAsync().Result); } } public class UtcToLocalDateTimeStringResolver : IMemberValueResolver { private readonly ICurrentTimeZoneService _currentTimeZoneService; public UtcToLocalDateTimeStringResolver(ICurrentTimeZoneService currentTimeZoneService) { _currentTimeZoneService = currentTimeZoneService; } public string Resolve(object source, object destination, DateTime sourceMember, string destMember, ResolutionContext context) { return TimeZoneHelper.ConvertToLocal(sourceMember, _currentTimeZoneService.GetAsync().Result).ToString(CultureInfo.CurrentCulture); } } public class NullableUtcToLocalDateTimeResolver : IMemberValueResolver { private readonly ICurrentTimeZoneService _currentTimeZoneService; public NullableUtcToLocalDateTimeResolver(ICurrentTimeZoneService currentTimeZoneService) { _currentTimeZoneService = currentTimeZoneService; } public DateTime? Resolve(object source, object destination, DateTime? sourceMember, DateTime? destMember, ResolutionContext context) { if (sourceMember.HasValue) return TimeZoneHelper.ConvertToLocal(sourceMember.Value, _currentTimeZoneService.GetAsync().Result); return null; } } public class NullableUtcToLocalDateTimeStringResolver : IMemberValueResolver { private readonly ICurrentTimeZoneService _currentTimeZoneService; public NullableUtcToLocalDateTimeStringResolver(ICurrentTimeZoneService currentTimeZoneService) { _currentTimeZoneService = currentTimeZoneService; } public string Resolve(object source, object destination, DateTime? sourceMember, string destMember, ResolutionContext context) { if (sourceMember.HasValue) return TimeZoneHelper.ConvertToLocal(sourceMember.Value, _currentTimeZoneService.GetAsync().Result).ToString(CultureInfo.CurrentCulture); return string.Empty; } } #endregion #region Local to UTC public class LocalToUtcDateTimeResolver : IMemberValueResolver { private readonly ICurrentTimeZoneService _currentTimeZoneService; public LocalToUtcDateTimeResolver(ICurrentTimeZoneService currentTimeZoneService) { _currentTimeZoneService = currentTimeZoneService; } public DateTime Resolve(object source, object destination, DateTime sourceMember, DateTime destMember, ResolutionContext context) { return TimeZoneHelper.ConvertToUtc(sourceMember, _currentTimeZoneService.GetAsync().Result); } } public class LocalToUtcDateTimeStringResolver : IMemberValueResolver { private readonly ICurrentTimeZoneService _currentTimeZoneService; public LocalToUtcDateTimeStringResolver(ICurrentTimeZoneService currentTimeZoneService) { _currentTimeZoneService = currentTimeZoneService; } public string Resolve(object source, object destination, DateTime sourceMember, string destMember, ResolutionContext context) { return TimeZoneHelper.ConvertToUtc(sourceMember, _currentTimeZoneService.GetAsync().Result).ToString(CultureInfo.CurrentCulture); } } public class NullableLocalToUtcDateTimeResolver : IMemberValueResolver { private readonly ICurrentTimeZoneService _currentTimeZoneService; public NullableLocalToUtcDateTimeResolver(ICurrentTimeZoneService currentTimeZoneService) { _currentTimeZoneService = currentTimeZoneService; } public DateTime? Resolve(object source, object destination, DateTime? sourceMember, DateTime? destMember, ResolutionContext context) { if (sourceMember.HasValue) return TimeZoneHelper.ConvertToUtc(sourceMember.Value, _currentTimeZoneService.GetAsync().Result); return null; } } public class NullableLocalToUtcDateTimeStringResolver : IMemberValueResolver { private readonly ICurrentTimeZoneService _currentTimeZoneService; public NullableLocalToUtcDateTimeStringResolver(ICurrentTimeZoneService currentTimeZoneService) { _currentTimeZoneService = currentTimeZoneService; } public string Resolve(object source, object destination, DateTime? sourceMember, string destMember, ResolutionContext context) { if (sourceMember.HasValue) return TimeZoneHelper.ConvertToUtc(sourceMember.Value, _currentTimeZoneService.GetAsync().Result).ToString(CultureInfo.CurrentCulture); return string.Empty; } } #endregion #region Documents / Files public class FilenameToIconResolver : IMemberValueResolver { private readonly IWebHostEnvironment _hostingEnvironment; public FilenameToIconResolver(IWebHostEnvironment webHostEnvironment) { _hostingEnvironment = webHostEnvironment; } public string Resolve(object source, object destination, string sourceMember, string destMember, ResolutionContext context) { var fileName = sourceMember; var result = "/images/FileExtensions/_blank.png"; if (!string.IsNullOrWhiteSpace(fileName)) { fileName = fileName.Replace(@"\", "/"); var extension = fileName.Split('/').Last(); extension = extension.Split('.').Last(); var imageUrl = "/images/FileExtensions/" + extension + ".png"; var absolutePath = _hostingEnvironment.WebRootPath + imageUrl; if (!System.IO.File.Exists(absolutePath)) { imageUrl = "/images/FileExtensions/_blank.png"; } result = imageUrl; } return result; } } #endregion #region Localized /// /// Resolver der aus einem ILocalizable die Übersetzung in der aktuellen Sprache zurückgibt /// public class LocalizedResolver : IMemberValueResolver { public string Resolve(object source, object destination, string sourceMember, string destMember, ResolutionContext context) { if (source is ILocalizable helper) { try { var language = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName.ToUpper(); var dictionary = JsonConvert.DeserializeObject>(sourceMember); if (dictionary != null) { if (dictionary.ContainsKey(language)) return dictionary[language]; } } catch { } } return string.Empty; } } #endregion }