public bool Equals(IBindingModel x, IBindingModel y)
        {
            var equals = true;

            foreach (var property in Properties)
            {
                var valueX = property.GetValue(x);
                var valueY = property.GetValue(y);

                if (property.PropertyType == typeof(string))
                {
                    equals &= string.Equals((string)valueX, (string)valueY, StringComparison.InvariantCultureIgnoreCase);
                }
                else
                {
                    equals &= (valueX != null && valueX.Equals(valueY)) || (valueX == null && valueY == null);
                }
                if (!equals)
                {
                    break;
                }
            }

            return(equals);
        }
예제 #2
0
        public static TEntity ToEntity <TEntity>(IBindingModel requestModel, IComponentContext container, bool lazyLoading, int level) where TEntity : IEntity, new()
        {
            TEntity entity = new TEntity();

            Type t = typeof(TEntity);

            if (requestModel != null)
            {
                Type c = requestModel.GetType();
                foreach (var propInfo in c.GetProperties())
                {
                    var attr = propInfo.GetCustomAttributes(true).OfType <ColumnDefinitionAttribute>().FirstOrDefault();
                    if (t.GetProperty(propInfo.Name) != null)
                    {
                        if (!t.GetProperty(propInfo.Name).PropertyType.IsAssignableTo <IBindingModel>())
                        {
                            t.GetProperty(propInfo.Name).SetValue(entity, propInfo.GetValue(requestModel, null), null);
                        }
                    }
                    else if (t.GetProperty(propInfo.Name) == null && propInfo.PropertyType != typeof(DropDownOptions) && propInfo.PropertyType != typeof(DropDownStringOptions) && attr != null && t.GetProperty(attr.EntityProperty) != null)
                    {
                        if (propInfo.PropertyType.IsArray)
                        {
                            Type nestedEntityType = Type.GetType(string.Format(System.Web.Configuration.WebConfigurationManager.AppSettings["DataAssemblyFormat"], attr.EntityProperty));
                            var  list             = Activator.CreateInstance(typeof(HashSet <>).MakeGenericType(nestedEntityType));
                            var  nestedEntity     = Activator.CreateInstance(nestedEntityType);

                            foreach (var obj in (IEnumerable)propInfo.GetValue(requestModel, null))
                            {
                                Type[] signature      = new[] { typeof(IBindingModel) };
                                var    methodInfo     = typeof(BaseEntity).GetMethod("ToEntity", signature);
                                var    toEntityMethod = methodInfo.MakeGenericMethod(new[] { nestedEntityType });

                                var nestedEntityObj = toEntityMethod.Invoke(nestedEntity, new object[] { obj });

                                var miAdd = list.GetType().GetMethod("Add");
                                miAdd.Invoke(list, new object[] { nestedEntityObj });
                            }

                            t.GetProperty(attr.EntityProperty).SetValue(entity, list, null);
                        }
                        else //!propInfo.PropertyType.IsArray
                        {
                            Type[] signature      = new[] { typeof(IBindingModel) };
                            var    methodInfo     = typeof(BaseEntity).GetMethod("ToEntity", signature);
                            var    toEntityMethod = methodInfo.MakeGenericMethod(new[] { Type.GetType(string.Format(System.Web.Configuration.WebConfigurationManager.AppSettings["DataAssemblyFormat"], attr.EntityProperty)) });

                            t.GetProperty(attr.EntityProperty).SetValue(entity, toEntityMethod.Invoke(entity, new object[] { propInfo.GetValue(requestModel, null) }), null);
                        }
                    }
                }

                return(entity);
            }

            return(default(TEntity));
        }
예제 #3
0
        public static IEnumerable <string> GetImageUrls(IBindingModel searchQuery)
        {
            var search   = (GoogleImageSearch)searchQuery;
            var response = ParseJsonResponse(GetJson(GenerateUrl(search.SearchText, search.ImageSize, search.MaxImages)));

            if (response == null)
            {
                return(new List <string>());
            }

            return(response.Select(image => image.url).ToList());
        }
예제 #4
0
        public static IEnumerable<string> GetImageUrls(IBindingModel searchQuery)
        {
            var search = (GoogleImageSearch)searchQuery;
            var response = ParseJsonResponse(GetJson(GenerateUrl(search.SearchText, search.ImageSize, search.MaxImages)));

            if (response == null)
            {
                return new List<string>();
            }

            return response.Select(image => image.url).ToList();
        }
예제 #5
0
        public static void CopyProperties(this IEntity entity, IBindingModel requestModel)
        {
            Type t = entity.GetType();
            Type c = requestModel.GetType();

            foreach (var propInfo in t.GetProperties())
            {
                if (c.GetProperty(propInfo.Name) != null && !propInfo.Name.Equals("Id") && !propInfo.PropertyType.IsAssignableTo <IBindingModel>())
                {
                    propInfo.SetValue(entity, c.GetProperty(propInfo.Name).GetValue(requestModel, null), null);
                }
            }
        }
예제 #6
0
        public static TEntity ToEntityUpdate <TEntity>(IBindingModel requestModel) where TEntity : IEntity, new()
        {
            TEntity entity = new TEntity();

            Type t = typeof(TEntity);
            Type c = requestModel.GetType();

            foreach (var propInfo in c.GetProperties())
            {
                var attr = propInfo.GetCustomAttributes(true).OfType <ColumnDefinitionAttribute>().FirstOrDefault();
                if (t.GetProperty(propInfo.Name) != null)
                {
                    if (!t.GetProperty(propInfo.Name).PropertyType.IsAssignableTo <IBindingModel>())
                    {
                        t.GetProperty(propInfo.Name).SetValue(entity, propInfo.GetValue(requestModel, null), null);
                    }
                }
            }

            return(entity);
        }
        public int GetHashCode(IBindingModel obj)
        {
            var hash = 0;

            foreach (var property in Properties)
            {
                var value = property.GetValue(obj);
                if (value != null)
                {
                    var s = value as string;
                    if (s != null)
                    {
                        hash ^= s.ToLowerInvariant().GetHashCode();
                    }
                    else
                    {
                        hash ^= value.GetHashCode();
                    }
                }
            }

            return(hash);
        }
        public static IBindingModel GetDiff(this IBindingModel source, IBindingModel target)
        {
            if (target == null)
            {
                return(source);
            }

            var metadata = EntityMetadata.GetMetadata(source.GetType());

            var result = (IBindingModel)Activator.CreateInstance(source.GetType());

            if (target.Id != Guid.Empty)
            {
                result.Id = target.Id;
            }
            else if (source.Id != Guid.Empty)
            {
                result.Id = source.Id;
            }

            foreach (var attribute in metadata.CrmAttributes.Where(a => a.CrmMapping.IsValidForUpdate))
            {
                if (source is BindingModelBase)
                {
                    if (!((BindingModelBase)source).InitializedProperties.Contains(attribute.PropertyName))
                    {
                        continue;
                    }
                }

                var valueSource = attribute.Property.GetValue(source);
                var valueTarget = attribute.Property.GetValue(target);


                switch (attribute.AttributeType)
                {
                case AttributeTypeCode.DateTime:
                    if (attribute.DateTimeBehavior == DateTimeBehavior.UserLocal)
                    {
                        valueSource = ((DateTime?)valueSource)?.ToUniversalTime();
                        valueTarget = ((DateTime?)valueTarget)?.ToUniversalTime();
                    }
                    else
                    {
                        valueSource = valueSource != null ? (DateTime?)new DateTime(((DateTime)valueSource).Ticks, DateTimeKind.Utc) : null;
                    }
                    break;

                case AttributeTypeCode.String:
                case AttributeTypeCode.Memo:
                    if (string.Empty == (valueSource as string))
                    {
                        valueSource = null;
                    }
                    break;
                }

                if ((!attribute.IsKey || result.Id != Guid.Empty) &&
                    (
                        (valueSource == null && valueTarget == null) ||
                        (valueSource != null && valueSource.Equals(valueTarget)) ||
                        (valueTarget != null && valueTarget.Equals(valueSource))
                    )
                    )
                {
                    continue;
                }

                attribute.Property.SetValue(result, valueSource);
            }

            return(result);
        }
예제 #9
0
 public BaseViewModel(IBindingModel bindingModel)
 {
     this.BindingModel = bindingModel;
 }
예제 #10
0
 public virtual void Updated(IBindingModel request, TEntity entity)
 {
 }
예제 #11
0
 public virtual void Inserted(IBindingModel request, TEntity entity)
 {
 }
예제 #12
0
 public static TEntity ToEntity <TEntity>(IBindingModel requestModel) where TEntity : IEntity, new()
 {
     return(ToEntity <TEntity>(requestModel, null, false, 0));
 }
예제 #13
0
        public void AddModel(IBindingModel model, IOrganizationService service, IBindingModel extendedModel = null)
        {
            var entity = model.ToEntity(service);

            Entity existingEntity = null;

            if (extendedModel != null)
            {
                foreach (var tempEntity in ExistingRequests.Keys)
                {
                    if (ExistingRequests[tempEntity].Contains(extendedModel))
                    {
                        existingEntity = tempEntity;
                        break;
                    }
                }
            }

            if (existingEntity == null)
            {
                foreach (var tempEntity in ExistingRequests.Keys)
                {
                    if (entity.LogicalName != tempEntity.LogicalName)
                    {
                        continue;
                    }

                    if (entity.Id != Guid.Empty)
                    {
                        if (entity.Id == tempEntity.Id)
                        {
                            existingEntity = tempEntity;
                            break;
                        }
                    }
                    else if (entity.KeyAttributes.Any())
                    {
                        var isOk = true;
                        foreach (var key in entity.KeyAttributes.Keys)
                        {
                            var value = entity.KeyAttributes[key];

                            if (!tempEntity.KeyAttributes.ContainsKey(key) || tempEntity.KeyAttributes[key] != value)
                            {
                                isOk = false;
                                break;
                            }
                        }

                        if (!isOk)
                        {
                            continue;
                        }
                        existingEntity = tempEntity;
                        break;
                    }
                }
            }

            List <IBindingModel> list;

            if (existingEntity == null)
            {
                existingEntity = entity;
                list           = new List <IBindingModel>();
                ExistingRequests[existingEntity] = list;
            }
            else
            {
                list = ExistingRequests[existingEntity];
                existingEntity.MergeWith(entity);
            }

            list.Add(model);
        }
예제 #14
0
 public BaseViewModel(IBindingModel bindingModel)
 {
     this.BindingModel = bindingModel;
 }