/// <summary>
        /// Remove an entity relationshions that are an empty list or null values
        /// </summary>
        /// <param name="dataEntity"></param>
        private void CleanDataEntityRelations(DataEntity dataEntity)
        {
            //create a tempory list of entity children to place the cleaned data into
            EntityChildren entityChildren = new EntityChildren();

            //loop through each child relationship
            foreach (var entityRelation in dataEntity.Children)
            {
                if (entityRelation.Value != null)
                {
                    //retrieve a list of data entities that are not a null value
                    //(note: there should only be one for child-to-parent relations)
                    List <DataEntity> relations = entityRelation.Value.Where(entityChild => entityChild != null).ToList();
                    if (relations.Count > 0)
                    {
                        //add the list of relations to the entity children
                        entityChildren.Add(entityRelation.Key, relations);
                    }
                }
            }

            //add the child relations to the data entity
            dataEntity.Children = entityChildren;
        }
        /// <summary>
        /// When building a data entity, pass in the dictionary of values and add the EntityChildren to your DataEntity.Children.
        /// If it already had Children, you should merge them.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="customProps"></param>
        /// <returns></returns>
        public EntityChildren CreateCustomPropertyiesForDataEntity <T>(IReadOnlyDictionary <string, object> customProps)
        {
            var children = new EntityChildren();

            var ints      = new Dictionary <string, long?>();
            var doubles   = new Dictionary <string, double?>();
            var dateTimes = new Dictionary <string, DateTime?>();
            var guids     = new Dictionary <string, Guid?>();
            var bools     = new Dictionary <string, bool?>();
            var strings   = new Dictionary <string, string>();

            var reversed = this.propertyNamesAndTypes.ToDictionary(x => x.Value, x => x.Key);

            foreach (var prop in customProps)
            {
                var ty = prop.Value.GetType();

                if (TryGetSupportedType(ty, out var supported))
                {
                    switch (supported)
                    {
                    case SupportedCustomType.Int:
                        ints.Add(prop.Key, CustomProperties.ConvertToInt(prop.Value));
                        break;

                    case SupportedCustomType.Double:
                        doubles.Add(prop.Key, CustomProperties.ConvertToDouble(prop.Value));
                        break;

                    case SupportedCustomType.DateTime:
                        dateTimes.Add(prop.Key, CustomProperties.ConvertToDateTime(prop.Value));
                        break;

                    case SupportedCustomType.Guid:
                        guids.Add(prop.Key, CustomProperties.ConvertToGuid(prop.Value));
                        break;

                    case SupportedCustomType.String:
                        strings.Add(prop.Key, CustomProperties.ConvertToString(prop.Value));
                        break;

                    case SupportedCustomType.Bool:
                        bools.Add(prop.Key, CustomProperties.ConvertToBool(prop.Value));
                        break;
                    }
                }
            }

            if (ints.Count > 0)
            {
                var objDefName = CustomType.Int.DictionaryItemName;
                var des        = ints.Select(kv => MakeKeyValDataEntity(kv.Key, kv.Value, objDefName)).ToList();
                var propName   = reversed[SupportedCustomType.Int];
                children.Add(propName, des);
            }

            if (doubles.Count > 0)
            {
                var objDefName = CustomType.Double.DictionaryItemName;
                var des        = ints.Select(kv => MakeKeyValDataEntity(kv.Key, kv.Value, objDefName)).ToList();
                var propName   = reversed[SupportedCustomType.Double];
                children.Add(propName, des);
            }

            if (dateTimes.Count > 0)
            {
                var objDefName = CustomType.DateTime.DictionaryItemName;
                var des        = ints.Select(kv => MakeKeyValDataEntity(kv.Key, kv.Value, objDefName)).ToList();
                var propName   = reversed[SupportedCustomType.DateTime];
                children.Add(propName, des);
            }

            if (guids.Count > 0)
            {
                var objDefName = CustomType.Guid.DictionaryItemName;
                var des        = ints.Select(kv => MakeKeyValDataEntity(kv.Key, kv.Value, objDefName)).ToList();
                var propName   = reversed[SupportedCustomType.Guid];
                children.Add(propName, des);
            }

            if (strings.Count > 0)
            {
                var objDefName = CustomType.String.DictionaryItemName;
                var des        = ints.Select(kv => MakeKeyValDataEntity(kv.Key, kv.Value, objDefName)).ToList();
                var propName   = reversed[SupportedCustomType.String];
                children.Add(propName, des);
            }

            if (bools.Count > 0)
            {
                var objDefName = CustomType.Bool.DictionaryItemName;
                var des        = ints.Select(kv => MakeKeyValDataEntity(kv.Key, kv.Value, objDefName)).ToList();
                var propName   = reversed[SupportedCustomType.Bool];
                children.Add(propName, des);
            }

            return(children);
        }
Esempio n. 3
0
        //public static T From<T>(DataEntity de, ObjDefs e) where T : new()
        //{
        //    var data = new T();
        //    // TODO: Consider getting this from the type instead of DataEntity
        //    var od = e.GetOrBuild(de.ObjectDefinitionFullName);
        //    foreach (var property in de.Properties)
        //    {
        //        var propertySetter = od.Properties[property.Key];
        //        propertySetter?.Set(data, property.Value);
        //    }

        //    foreach (var c in de.Children)
        //    {
        //        var dataEntityProp = od.Properties[c.Key] as IDataEntityProperty;
        //        if (dataEntityProp != null)
        //        {
        //            var dataEntity = c.Value.FirstOrDefault();

        //            if (dataEntity != null)
        //            {
        //                dataEntityProp.Set(data, dataEntity);
        //            }
        //        }

        //        var dataEntityListProp = od.Properties[c.Key] as IDataEntityListProperty;
        //        if (dataEntityListProp != null)
        //        {
        //            var dataEntity = c.Value;

        //            if (dataEntity != null)
        //            {
        //                dataEntityListProp.Set(data, dataEntity);
        //            }
        //        }

        //    }

        //    return data;
        //}

        public static DataEntity To <T>(T data, ObjDefs e, QueryEntity qe = null)
        {
            if (data == null)
            {
                return(null);
            }
            var stub     = MetadataReflector.GetObjectDefStubFromType(typeof(T));
            var od       = e.GetOrBuild(stub.Name);
            var de       = new DataEntity(od.Name);
            var props    = new EntityProperties();
            var children = new EntityChildren();

            foreach (var keyValuePair in od.Properties)
            {
                var name   = keyValuePair.Key;
                var getter = keyValuePair.Value;
                if (!getter.IsObjectDefProp)
                {
                    if (qe == null || qe.PropertyList.Contains(name))
                    {
                        props.Add(name, getter.Get(data));
                    }
                }
                else
                {
                    var dataEntityGetter = getter as IDataEntityProperty;
                    if (dataEntityGetter != null)
                    {
                        if (qe == null)
                        {
                            var cde = dataEntityGetter.Get(data);
                            var v   = cde == null ? new List <DataEntity>() : new List <DataEntity> {
                                cde
                            };
                            children.Add(name, v);
                        }
                        else
                        {
                            var propQe = qe.ChildList.FirstOrDefault(q => q.Name == name);
                            if (propQe != null)
                            {
                                var cde = dataEntityGetter.Get(data, propQe);
                                var v   = cde == null ? new List <DataEntity>() : new List <DataEntity> {
                                    cde
                                };
                                children.Add(name, v);
                            }
                        }
                    }

                    var dataEntityListGetter = getter as IDataEntityListProperty;
                    if (dataEntityListGetter != null)
                    {
                        if (qe == null)
                        {
                            var cde = dataEntityListGetter.Get(data);

                            // Workaround fopr CORE Bug
                            if (cde == null)
                            {
                                cde = new List <DataEntity>();
                            }
                            children.Add(name, cde);
                        }
                        else
                        {
                            var propQe = qe.ChildList.FirstOrDefault(q => q.Name == name);
                            if (propQe != null)
                            {
                                var cde = dataEntityListGetter.Get(data, propQe);
                                // Workaround fopr CORE Bug
                                if (cde == null)
                                {
                                    cde = new List <DataEntity>();
                                }
                                children.Add(name, cde);
                            }
                        }
                    }
                }
            }

            de.Properties = props;
            de.Children   = children;
            return(de);
        }
Esempio n. 4
0
        /// <summary>
        /// Remove an entity relationshions that are an empty list or null values
        /// </summary>
        /// <param name="dataEntity"></param>
        private void CleanDataEntityRelations(DataEntity dataEntity)
        {
            //create a tempory list of entity children to place the cleaned data into
            EntityChildren entityChildren = new EntityChildren();

            //loop through each child relationship
            foreach (var entityRelation in dataEntity.Children)
            {
                if (entityRelation.Value != null)
                {
                    //retrieve a list of data entities that are not a null value
                    //(note: there should only be one for child-to-parent relations)
                    List<DataEntity> relations = entityRelation.Value.Where(entityChild => entityChild != null).ToList();
                    if (relations.Count > 0)
                    {
                        //add the list of relations to the entity children
                        entityChildren.Add(entityRelation.Key, relations);
                    }
                }
            }

            //add the child relations to the data entity
            dataEntity.Children = entityChildren;
        }
Esempio n. 5
0
        public void CreateOrUpdateObjectForReplicationColumnRemovedTest()
        {
            MethodInput methodInput = new MethodInput {
                Name = "CreateOrUpdateObjectForReplication"
            };

            DataEntity tableEntity = new DataEntity {
                ObjectDefinitionFullName = "Parameters"
            };

            EntityProperties properties = new EntityProperties {
                { "Name", "Products" }
            };

            EntityChildren columns = new EntityChildren();

            List <DataEntity> columnList = new List <DataEntity>();

            DataEntity column = new DataEntity("DataPropertyDefinition");

            column.Properties.Add("Name", "RecordId");
            column.Properties.Add("DataType", typeof(Guid));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "ProductNumber");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", true);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "ProductName");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Type");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            //Note Type has been removed

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "UoMSchedule");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Cost");
            column.Properties.Add("DataType", typeof(decimal));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "StandardCost");
            column.Properties.Add("DataType", typeof(decimal));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "QuantityInStock");
            column.Properties.Add("DataType", typeof(Int32));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "QuantityOnOrder");
            column.Properties.Add("DataType", typeof(Int32));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Discontinued");
            column.Properties.Add("DataType", typeof(Int16));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "CreatedOn");
            column.Properties.Add("DataType", typeof(DateTime));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "CreatedBy");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "ModifiedOn");
            column.Properties.Add("DataType", typeof(DateTime));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "ModifiedBy");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            columns.Add("RSPropertyDefinitions", columnList);

            tableEntity.Children   = columns;
            tableEntity.Properties = properties;

            methodInput.Input = tableEntity;

            MethodResult result = _rsTargetConnector.ExecuteMethod(methodInput);

            Assert.IsTrue(result.Success);
            Assert.IsTrue(Convert.ToBoolean(result.Return.Properties["SchemaChanged"]));
        }
Esempio n. 6
0
        public void CreateOrUpdateObjectForReplicationValidTest()
        {
            MethodInput methodInput = new MethodInput {
                Name = "CreateOrUpdateObjectForReplication"
            };

            DataEntity tableEntity = new DataEntity {
                ObjectDefinitionFullName = "Parameters"
            };

            EntityProperties properties = new EntityProperties {
                { "Name", "CherryCola" }
            };

            EntityChildren columns = new EntityChildren();

            List <DataEntity> columnList = new List <DataEntity>();

            DataEntity column = new DataEntity("DataPropertyDefinition");

            column.Properties.Add("Name", "ColaId");
            column.Properties.Add("DataType", typeof(Guid));
            column.Properties.Add("MaximumLength", 50);
            column.Properties.Add("Nullable", true);
            column.Properties.Add("InPrimaryKey", true);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Cost");
            column.Properties.Add("DataType", typeof(decimal));
            column.Properties.Add("MaximumLength", 38);
            column.Properties.Add("Nullable", true);
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Calories");
            column.Properties.Add("DataType", typeof(int));
            column.Properties.Add("MaximumLength", 38);
            column.Properties.Add("Nullable", true);
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Volume");
            column.Properties.Add("DataType", typeof(int));
            column.Properties.Add("MaximumLength", 38);
            column.Properties.Add("Nullable", true);
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            columns.Add("RSPropertyDefinitions", columnList);

            tableEntity.Children   = columns;
            tableEntity.Properties = properties;

            methodInput.Input = tableEntity;

            MethodResult result = _rsTargetConnector.ExecuteMethod(methodInput);

            Assert.IsTrue(result.Success);
            Assert.IsTrue(Convert.ToBoolean(result.Return.Properties["SchemaChanged"]));
        }
Esempio n. 7
0
        public void CreateOrUpdateObjectForReplicationColumnAddedTest()
        {
            MethodInput methodInput = new MethodInput { Name = "CreateOrUpdateObjectForReplication" };

            DataEntity tableEntity = new DataEntity { ObjectDefinitionFullName = "Parameters" };

            EntityProperties properties = new EntityProperties { { "Name", "Products" } };

            EntityChildren columns = new EntityChildren();

            List<DataEntity> columnList = new List<DataEntity>();

            DataEntity column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "RecordId");
            column.Properties.Add("DataType", typeof(Guid));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "ProductNumber");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", true);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "ProductName");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            //this is the new column
            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "NewPrice");
            column.Properties.Add("DataType", typeof(decimal));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Type");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Type");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "UoMSchedule");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Cost");
            column.Properties.Add("DataType", typeof(decimal));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "StandardCost");
            column.Properties.Add("DataType", typeof(decimal));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "QuantityInStock");
            column.Properties.Add("DataType", typeof(Int32));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "QuantityOnOrder");
            column.Properties.Add("DataType", typeof(Int32));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Discontinued");
            column.Properties.Add("DataType", typeof(Int16));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "CreatedOn");
            column.Properties.Add("DataType", typeof(DateTime));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "CreatedBy");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "ModifiedOn");
            column.Properties.Add("DataType", typeof(DateTime));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "ModifiedBy");
            column.Properties.Add("DataType", typeof(string));
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            columns.Add("RSPropertyDefinitions", columnList);

            tableEntity.Children = columns;
            tableEntity.Properties = properties;

            methodInput.Input = tableEntity;

            MethodResult result = _rsTargetConnector.ExecuteMethod(methodInput);

            Assert.IsTrue(result.Success);
        }
Esempio n. 8
0
        public void CreateOrUpdateObjectForReplicationValidTest()
        {
            MethodInput methodInput = new MethodInput { Name = "CreateOrUpdateObjectForReplication" };

            DataEntity tableEntity = new DataEntity { ObjectDefinitionFullName = "Parameters" };

            EntityProperties properties = new EntityProperties { { "Name", "CherryCola" } };

            EntityChildren columns = new EntityChildren();

            List<DataEntity> columnList = new List<DataEntity>();

            DataEntity column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "ColaId");
            column.Properties.Add("DataType", typeof(Guid));
            column.Properties.Add("MaximumLength", 50);
            column.Properties.Add("Nullable", true);
            column.Properties.Add("InPrimaryKey", true);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Cost");
            column.Properties.Add("DataType", typeof(decimal));
            column.Properties.Add("MaximumLength", 38);
            column.Properties.Add("Nullable", true);
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Calories");
            column.Properties.Add("DataType", typeof(int));
            column.Properties.Add("MaximumLength", 38);
            column.Properties.Add("Nullable", true);
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            column = new DataEntity("DataPropertyDefinition");
            column.Properties.Add("Name", "Volume");
            column.Properties.Add("DataType", typeof(int));
            column.Properties.Add("MaximumLength", 38);
            column.Properties.Add("Nullable", true);
            column.Properties.Add("InPrimaryKey", false);
            columnList.Add(column);

            columns.Add("RSPropertyDefinitions", columnList);

            tableEntity.Children = columns;
            tableEntity.Properties = properties;

            methodInput.Input = tableEntity;

            MethodResult result = _rsTargetConnector.ExecuteMethod(methodInput);

            Assert.IsTrue(result.Success);
            Assert.IsTrue(Convert.ToBoolean(result.Return.Properties["SchemaChanged"]));
        }