Exemplo n.º 1
0
        /// <summary>
        /// Generates a string to be used as the skip token value within the next link.
        /// </summary>
        /// <param name="lastMember"> Object based on which SkipToken value will be generated.</param>
        /// <param name="model">The edm model.</param>
        /// <param name="orderByNodes">List of orderByNodes used to generate the skiptoken value.</param>
        /// <returns>Value for the skiptoken to be used in the next link.</returns>
        private static string GenerateSkipTokenValue(Object lastMember, IEdmModel model, IList <OrderByNode> orderByNodes)
        {
            if (lastMember == null)
            {
                return(String.Empty);
            }

            IEnumerable <IEdmProperty> propertiesForSkipToken = GetPropertiesForSkipToken(lastMember, model, orderByNodes);
            StringBuilder skipTokenBuilder = new StringBuilder(String.Empty);

            if (propertiesForSkipToken == null)
            {
                return(skipTokenBuilder.ToString());
            }

            int    count = 0;
            string uriLiteral;
            object value;
            int    lastIndex         = propertiesForSkipToken.Count() - 1;
            IEdmStructuredObject obj = lastMember as IEdmStructuredObject;

            foreach (IEdmProperty edmProperty in propertiesForSkipToken)
            {
                bool   islast          = count == lastIndex;
                string clrPropertyName = EdmLibHelpers.GetClrPropertyName(edmProperty, model);
                if (obj != null)
                {
                    obj.TryGetPropertyValue(clrPropertyName, out value);
                }
                else
                {
                    value = lastMember.GetType().GetProperty(clrPropertyName).GetValue(lastMember);
                }

                if (value == null)
                {
                    uriLiteral = ODataUriUtils.ConvertToUriLiteral(value, ODataVersion.V401);
                }
                else if (edmProperty.Type.IsEnum())
                {
                    ODataEnumValue enumValue = new ODataEnumValue(value.ToString(), value.GetType().FullName);
                    uriLiteral = ODataUriUtils.ConvertToUriLiteral(enumValue, ODataVersion.V401, model);
                }
                else if (edmProperty.Type.IsDateTimeOffset() && value is DateTime)
                {
                    var dateTime            = (DateTime)value;
                    var dateTimeOffsetValue = TimeZoneInfoHelper.ConvertToDateTimeOffset(dateTime);
                    uriLiteral = ODataUriUtils.ConvertToUriLiteral(dateTimeOffsetValue, ODataVersion.V401, model);
                }
                else
                {
                    uriLiteral = ODataUriUtils.ConvertToUriLiteral(value, ODataVersion.V401, model);
                }

                skipTokenBuilder.Append(edmProperty.Name).Append(propertyDelimiter).Append(uriLiteral).Append(islast ? String.Empty : CommaDelimiter.ToString());
                count++;
            }

            return(skipTokenBuilder.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Setup our DTO mapping profile.
        /// </summary>
        internal static void SetupProfile(IMapperConfigurationExpression mapper)
        {
            if (mapper is null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            mapper.CreateMap <FollowRequest, FollowRequestDto>()
            .ForMember(dest => dest.ReceiverId, o => o.MapFrom(src => src.Id.ReceiverId))
            .ForMember(dest => dest.RequesterId, o => o.MapFrom(src => src.Id.RequesterId));
            mapper.CreateMap <Reaction, ReactionDto>().ReverseMap();
            mapper.CreateMap <ReactionWrapper, ReactionWrapperDto>();
            mapper.CreateMap <TokenWrapper, TokenWrapperDto>();
            mapper.CreateMap <UploadWrapper, UploadWrapperDto>();
            mapper.CreateMap <User, UserDto>();
            mapper.CreateMap <User, UserCompleteDto>();
            mapper.CreateMap <UserWithStats, UserWithStatsDto>();
            mapper.CreateMap <UserWithRelationWrapper, UserWithRelationWrapperDto>();
            mapper.CreateMap <Vlog, VlogDto>().ReverseMap();
            mapper.CreateMap <VlogWrapper, VlogWrapperDto>();
            mapper.CreateMap <VlogLike, VlogLikeDto>()
            .ForMember(dest => dest.UserId, o => o.MapFrom(src => src.Id.UserId))
            .ForMember(dest => dest.VlogId, o => o.MapFrom(src => src.Id.VlogId));
            mapper.CreateMap <VlogLikeSummary, VlogLikeSummaryDto>();
            mapper.CreateMap <VlogLikingUserWrapper, VlogLikingUserWrapperDto>();

            // Create custom mapping for time zone types
            mapper.CreateMap <TimeZoneInfo, string>().ConvertUsing(tz => tz == null
                ? null
                : TimeZoneInfoHelper.MapTimeZoneToStringOrNull(tz));
            mapper.CreateMap <string, TimeZoneInfo>().ConvertUsing(s => string.IsNullOrEmpty(s)
                ? null
                : TimeZoneInfoHelper.MapStringToTimeZone(s));
        }
        public void NullTimeZoneMapsToNull()
        {
            // Act.
            var result = TimeZoneInfoHelper.MapTimeZoneToStringOrNull(null);

            // Assert.
            Assert.Null(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 添加周期性作业
        /// </summary>
        /// <param name="jobItem"></param>
        /// <param name="timeZone">job 时区信息</param>
        /// <returns></returns>
        public bool AddHttprecurringjob(HttpJobItem jobItem)
        {
            if (string.IsNullOrEmpty(jobItem.QueueName))
            {
                jobItem.QueueName = EnqueuedState.DefaultQueue;
            }
            else
            {
                //get queues from server
                // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault
                var server = JobStorage.Current.GetMonitoringApi().Servers().Where(p => p.Queues.Count > 0).FirstOrDefault();
                // ReSharper disable once PossibleNullReferenceException
                var queues = server.Queues.ToList();
                if (!queues.Exists(p => p == jobItem.QueueName.ToLower()) || queues.Count == 0)
                {
                    Logger.Warn($"HttpJobDispatcher.AddHttprecurringjob Error => HttpJobItem.QueueName:`{jobItem.QueueName}` not exist, Use DEFAULT extend!");
                }
            }

            var queueName = !string.IsNullOrEmpty(jobItem.AgentClass) ? "JobAgent" : jobItem.QueueName;

            if (string.IsNullOrEmpty(queueName))
            {
                queueName = EnqueuedState.DefaultQueue;
            }


            try
            {
                // 先用每个job配置的 如果没有就用系统配置的 在没有就用Local
                TimeZoneInfo timeZone = null;
                if (!string.IsNullOrEmpty(jobItem.TimeZone))
                {
                    timeZone = TimeZoneInfoHelper.OlsonTimeZoneToTimeZoneInfo(jobItem.TimeZone);
                }

                if (timeZone == null)
                {
                    timeZone = CodingUtil.HangfireHttpJobOptions.RecurringJobTimeZone ?? TimeZoneInfo.Local;
                }
                if (string.IsNullOrEmpty(jobItem.Cron))
                {
                    //支持添加一个 只能手动出发的
                    RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), Cron.Never,
                                             timeZone, jobItem.QueueName.ToLower());
                    return(true);
                }

                RecurringJob.AddOrUpdate(jobItem.JobName, () => HttpJob.Excute(jobItem, jobItem.JobName, queueName, jobItem.EnableRetry, null), jobItem.Cron,
                                         timeZone, jobItem.QueueName.ToLower());
                return(true);
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.AddHttprecurringjob", ex);
                return(false);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Return value as <typeparamref name="TimeZoneInfo"/>.
        /// </summary>
        /// <remarks>
        ///     This uses our custom timezone storage format, being
        ///     <see cref="RegexConstants.TimeZone"/>.
        /// </remarks>
        /// <typeparam name="TFieldType">Type to return value to.</typeparam>
        /// <param name="reader">Input reader to extend.</param>
        /// <param name="ordinal">Column ordinal.</param>
        /// <returns>Value or null.</returns>
        public static TimeZoneInfo GetTimeZoneInfo(this DbDataReader reader, int ordinal)
        {
            if (reader is null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            return(reader.IsDBNull(ordinal) ? null : TimeZoneInfoHelper.MapStringToTimeZone(reader.GetString(ordinal)));
        }
        public void GivenAState_ConvertByState_ReturnsATimeZoneInfo(string state)
        {
            // Arrange.

            // Act.
            var timeZoneInfo = TimeZoneInfoHelper.ConvertByState(state);

            // Asert.
            timeZoneInfo.ShouldNotBeNull();
        }
        public void MappedTimeZoneInfoMatchesRegex()
        {
            // Arrange.
            var timeZoneInfo = TimeZoneInfo.Utc;

            // Act.
            var result = TimeZoneInfoHelper.MapTimeZoneToStringOrNull(timeZoneInfo);

            // Assert.
            Assert.Matches(RegexConstants.TimeZoneRegex, result);
        }
Exemplo n.º 8
0
 /// <summary>
 ///     Maps a swabbr user entity onto a writer.
 /// </summary>
 /// <param name="context">The context to add parameters to.</param>
 /// <param name="user">The user object.</param>
 private static void MapToWriter(DatabaseContext context, User user)
 {
     context.AddParameterWithValue("birth_date", user.BirthDate);
     context.AddParameterWithValue("country", user.Country);
     context.AddParameterWithValue("daily_vlog_request_limit", (int)user.DailyVlogRequestLimit);
     context.AddParameterWithValue("first_name", user.FirstName);
     context.AddParameterWithValue("follow_mode", user.FollowMode);
     context.AddParameterWithValue("gender", user.Gender);
     context.AddParameterWithValue("has_profile_image", user.HasProfileImage);
     context.AddParameterWithValue("is_private", user.IsPrivate);
     context.AddParameterWithValue("last_name", user.LastName);
     context.AddParameterWithValue("latitude", user.Latitude);
     context.AddParameterWithValue("longitude", user.Longitude);
     context.AddParameterWithValue("nickname", user.Nickname);
     context.AddParameterWithValue("timezone", TimeZoneInfoHelper.MapTimeZoneToStringOrNull(user.TimeZone));
 }
Exemplo n.º 9
0
        internal static object ConvertUnsupportedPrimitives(object value)
        {
            if (value != null)
            {
                Type type = value.GetType();

                // Note that type cannot be a nullable type as value is not null and it is boxed.
                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Char:
                    return(new String((char)value, 1));

                case TypeCode.UInt16:
                    return((int)(ushort)value);

                case TypeCode.UInt32:
                    return((long)(uint)value);

                case TypeCode.UInt64:
                    return(checked ((long)(ulong)value));

                case TypeCode.DateTime:
                    DateTime dateTime = (DateTime)value;
                    return(TimeZoneInfoHelper.ConvertToDateTimeOffset(dateTime));

                default:
                    if (type == typeof(char[]))
                    {
                        return(new String(value as char[]));
                    }
                    else if (type == typeof(XElement))
                    {
                        return(((XElement)value).ToString());
                    }
#if NETFX // System.Data.Linq.Binary is only supported in the AspNet version.
                    else if (type == typeof(Binary))
                    {
                        return(((Binary)value).ToArray());
                    }
#endif
                    break;
                }
            }

            return(value);
        }
Exemplo n.º 10
0
        internal static object ConvertUnsupportedPrimitives(object value, TimeZoneInfo timeZoneInfo)
        {
            if (value != null)
            {
                Type type = value.GetType();

                // Note that type cannot be a nullable type as value is not null and it is boxed.
                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Char:
                    return(new string((char)value, 1));

                case TypeCode.UInt16:
                    return((int)(ushort)value);

                case TypeCode.UInt32:
                    return((long)(uint)value);

                case TypeCode.UInt64:
                    return(checked ((long)(ulong)value));

                case TypeCode.DateTime:
                    DateTime dateTime = (DateTime)value;
                    return(TimeZoneInfoHelper.ConvertToDateTimeOffset(dateTime, timeZoneInfo));

                default:
                    if (type == typeof(char[]))
                    {
                        return(new string(value as char[]));
                    }
                    else if (type == typeof(XElement))
                    {
                        return(((XElement)value).ToString());
                    }

                    break;
                }
            }

            return(value);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 添加周期性作业
        /// </summary>
        /// <returns></returns>
        public string AddHttprecurringjob(HttpJobItem jobItem, bool addOnly = false)
        {
            if (string.IsNullOrEmpty(jobItem.QueueName))
            {
                jobItem.QueueName = EnqueuedState.DefaultQueue;
            }
            else
            {
                //get queues from server
                // ReSharper disable once ReplaceWithSingleCallToFirstOrDefault
                var server = JobStorage.Current.GetMonitoringApi().Servers().Where(p => p.Queues.Count > 0).FirstOrDefault();
                // ReSharper disable once PossibleNullReferenceException
                if (server == null)
                {
                    return("active server not exist!");
                }
                var queues = server.Queues.Select(m => m.ToLower()).ToList();
                if (!queues.Exists(p => p == jobItem.QueueName.ToLower()) || queues.Count == 0)
                {
                    Logger.Warn($"HttpJobDispatcher.AddHttprecurringjob Error => HttpJobItem.QueueName:`{jobItem.QueueName}` not exist, Use DEFAULT extend!");
                    jobItem.QueueName = EnqueuedState.DefaultQueue;
                }
            }

            try
            {
                // 先用每个job配置的 如果没有就用系统配置的 在没有就用Local
                TimeZoneInfo timeZone = null;
                if (!string.IsNullOrEmpty(jobItem.TimeZone))
                {
                    timeZone = TimeZoneInfoHelper.OlsonTimeZoneToTimeZoneInfo(jobItem.TimeZone);
                }

                //https://github.com/yuzd/Hangfire.HttpJob/issues/78
                var jobidentifier = string.IsNullOrEmpty(jobItem.RecurringJobIdentifier) ? jobItem.JobName : jobItem.RecurringJobIdentifier;
                if (addOnly)
                {
                    using (var connection = JobStorage.Current.GetConnection())
                    {
                        var existItem = connection.GetAllEntriesFromHash("recurring-job:" + jobidentifier);
                        if (existItem != null && existItem.Count > 0)
                        {
                            return(jobidentifier + "is registerd!");
                        }
                    }
                }

                if (timeZone == null)
                {
                    timeZone = TimeZoneInfo.Local;
                }
                if (string.IsNullOrEmpty(jobItem.Cron))
                {
                    //支持添加一个 只能手动出发的
                    RecurringJob.AddOrUpdate(jobidentifier, () => HttpJob.Excute(jobItem, null, null, false, null), Cron.Never,
                                             timeZone, jobItem.QueueName.ToLower());
                    return(string.Empty);
                }

                RecurringJob.AddOrUpdate(jobidentifier, () => HttpJob.Excute(jobItem, null, null, false, null), jobItem.Cron,
                                         timeZone, jobItem.QueueName.ToLower());
                return(string.Empty);
            }
            catch (Exception ex)
            {
                Logger.ErrorException("HttpJobDispatcher.AddHttprecurringjob", ex);
                return(ex.Message);
            }
        }