public List <EnterpriseAdapterTableColumnModel> GetRecursive(EnterpriseAdapterTableColumn tree, List <EnterpriseAdapterTableColumnModel> list)
        {
            var newAdapter = new EnterpriseAdapterTableColumnModel
            {
                Id               = tree.Id,
                ColumnName       = tree.ColumnName,
                DataType         = (DataTypes)tree.DataTypeId,
                IsForeignKey     = tree.IsForeignKey,
                IsPrimaryKey     = tree.IsPrimaryKey,
                RelatedTableName = tree.RelatedTableName ?? null,
                ParentId         = tree.ParentId ?? null,
                PropertyNameId   = (Common.Enums.PropertyName?)tree.PropertyNameId ?? null,
                Children         = new List <EnterpriseAdapterTableColumnModel>()
            };

            if (tree.EnterpriseAdapterTableColumn1.Count != 0)
            {
                foreach (var child in tree.EnterpriseAdapterTableColumn1)
                {
                    GetRecursive(child, newAdapter.Children);
                    if (newAdapter.Id != child.ParentId)
                    {
                        newAdapter.Children.Add(newAdapter);
                    }
                }
            }
            list.Add(newAdapter);
            return(list);
        }
 public void DeleteRecursive(EnterpriseAdapterTableColumn tableColumn)
 {
     foreach (var child in tableColumn.EnterpriseAdapterTableColumn1.ToList())
     {
         DeleteRecursive(child);
     }
     _entity.EnterpriseAdapterTableColumn.Remove(tableColumn);
 }
        public IResponse <NoValue> CreateXML(string fileAsString, long userProfileId)
        {
            //refacor DataType after delete from DB

            var result = new Response <NoValue>();

            var xml   = XDocument.Parse(fileAsString);
            var files = xml.Root.Elements().ToList();

            try
            {
                var enumConvertSuccess = Enum.TryParse(files[0].Value, true, out AdapterTypeItemEnum adapterType);

                if (!enumConvertSuccess)
                {
                    result.Status  = StatusEnum.Error;
                    result.Message = "Adapter type can be: MSSQL, MySQL or Oracle.";
                    return(result);
                }


                #region connectivity
                var enterpriseAdapterModel = new EnterpriseAdapterModel
                {
                    EnterpriseAdapter = adapterType,
                    ServerIP          = files[1].Value,
                    Port         = Int32.Parse(files[2].Value),
                    Username     = files[4].Value,
                    Password     = files[5].Value,
                    DatabaseName = files[3].Value,
                    Direction    = (DirectionEnum)Enum.Parse(typeof(DirectionEnum), files[6].Value, true)
                };

                var tableElements = new List <XElement>();

                if (enterpriseAdapterModel.Direction == DirectionEnum.Source)
                {
                    tableElements = files[7].Elements().ToList();
                    enterpriseAdapterModel.ParentTable = tableElements[0].Value;
                }

                var connection = TestConnection(enterpriseAdapterModel);
                if (connection.Status != StatusEnum.Success)
                {
                    result.Status  = StatusEnum.Error;
                    result.Message = connection.Message;
                    return(result);
                }
                #endregion

                var newAdapter = new Adapter
                {
                    Name              = files[0].Value + " Adapter",
                    UserProfileId     = userProfileId,
                    StatusId          = (int)Statuses.Active,
                    AdapterTypeItemId = (int)adapterType,
                    DirectionId       = (int)enterpriseAdapterModel.Direction
                };
                _entity.Adapter.Add(newAdapter);

                EnterpriseAdapter enterpriseAdapter = new EnterpriseAdapter
                {
                    ServerIP     = enterpriseAdapterModel.ServerIP,
                    Port         = enterpriseAdapterModel.Port.GetValueOrDefault(),
                    Username     = enterpriseAdapterModel.Username,
                    Password     = enterpriseAdapterModel.Password,
                    DatabaseName = enterpriseAdapterModel.DatabaseName,
                    AdapterId    = newAdapter.Id
                };

                _entity.EnterpriseAdapter.Add(enterpriseAdapter);


                if (enterpriseAdapterModel.Direction == DirectionEnum.Source)
                {
                    EnterpriseAdapterTable adapterTable = new EnterpriseAdapterTable
                    {
                        TableName           = tableElements[0].Value,
                        EnterpriseAdapterId = enterpriseAdapter.Id
                    };
                    _entity.EnterpriseAdapterTable.Add(adapterTable);


                    var columns = tableElements[1].Elements().ToList();

                    foreach (var column in columns)
                    {
                        var adapterPropertyId = adapterTable.Id;
                        var isForeignKey      = Convert.ToBoolean(column.Attribute("FK").Value);
                        var isPrimaryKey      = Convert.ToBoolean(column.Attribute("PK").Value);

                        Common.Enums.PropertyName property = new Common.Enums.PropertyName();
                        var propertyName = column.Attribute("property").Value;


                        if (!isForeignKey)
                        {
                            var foreignKeyElementsExist = column.HasElements;

                            if (foreignKeyElementsExist)
                            {
                                result.Status  = StatusEnum.Error;
                                result.Message = "There was an error in defining the foreign key field. Check the XML Example file.";
                                return(result);
                            }


                            EnterpriseAdapterTableColumn adapterTableColumn = new EnterpriseAdapterTableColumn
                            {
                                EnterpriseAdapterTableId = adapterPropertyId,
                                ColumnName   = column.Value,
                                IsForeignKey = isForeignKey,
                                IsPrimaryKey = isPrimaryKey,
                                DataTypeId   = 1
                            };

                            switch (propertyName.ToLower())
                            {
                            case "transactionid":
                            case "transactionaccount":
                            case "transactionamount":
                            case "transactiontype":
                                property = (Common.Enums.PropertyName)Enum.Parse(typeof(Common.Enums.PropertyName), propertyName, true);
                                adapterTableColumn.PropertyNameId = (int)property;
                                break;
                            }

                            _entity.EnterpriseAdapterTableColumn.Add(adapterTableColumn);
                        }
                        else
                        {
                            var columnsChild = column.Elements().ToList();

                            if (columnsChild.Count < 4)
                            {
                                result.Status  = StatusEnum.Error;
                                result.Message = "You have to provide aditional data if the foreign key attribute is set to \"true\". Check the XML Example file.";
                                return(result);
                            }

                            EnterpriseAdapterTableColumn adapterTableColumnParent = new EnterpriseAdapterTableColumn
                            {
                                EnterpriseAdapterTableId = adapterPropertyId,
                                ColumnName       = columnsChild[0].Value,
                                IsForeignKey     = isForeignKey,
                                IsPrimaryKey     = isPrimaryKey,
                                DataTypeId       = 1,
                                RelatedTableName = columnsChild[1].Value
                            };


                            for (var i = 2; i < columnsChild.Count; i++)
                            {
                                EnterpriseAdapterTableColumn adapterTableColumnChild = new EnterpriseAdapterTableColumn
                                {
                                    EnterpriseAdapterTableId = adapterTable.Id,
                                    ColumnName   = columnsChild[i].Value,
                                    IsForeignKey = false,
                                    IsPrimaryKey = false,
                                    DataTypeId   = 1,
                                    ParentId     = adapterTableColumnParent.Id,
                                    EnterpriseAdapterTableColumn2 = adapterTableColumnParent
                                };


                                Common.Enums.PropertyName propertyChild = new Common.Enums.PropertyName();
                                var propertyNameFK = columnsChild[i].Attribute("property").Value;

                                switch (propertyNameFK.ToLower())
                                {
                                case "transactionaccount":
                                case "transactionamount":
                                case "transactiontype":
                                    propertyChild = (Common.Enums.PropertyName)Enum.Parse(typeof(Common.Enums.PropertyName), propertyNameFK, true);
                                    adapterTableColumnChild.PropertyNameId = (int)propertyChild;
                                    break;
                                }


                                if (columnsChild[i].Name == "primaryKey")
                                {
                                    adapterTableColumnChild.IsPrimaryKey = true;
                                }

                                adapterTableColumnParent.EnterpriseAdapterTableColumn1.Add(adapterTableColumnChild);
                            }
                            _entity.EnterpriseAdapterTableColumn.Add(adapterTableColumnParent);
                        }
                    }
                }
                else if (enterpriseAdapterModel.Direction == DirectionEnum.Destination)
                {
                    _entity.EnterpriseAdapterProperty.Add(new EnterpriseAdapterProperty
                    {
                        PropertyId          = (long)PropertyEnum.TableNamePrefix,
                        EnterpriseAdapterId = newAdapter.Id,
                        Value = files[7].Value
                    });
                }

                _entity.SaveChanges();

                result.Status  = StatusEnum.Success;
                result.Message = "Connection test succeeded. Enterprise adapter added successfully.";
            }
            catch (Exception ex)
            {
                result.Status  = StatusEnum.Error;
                result.Message = ex.Message;
                _logger.Information($"EnterpriseAdapterService.CreateXML(fileAsString: {fileAsString}, userProfileId: {userProfileId})");
                _logger.Error(ex.Message);
            }

            return(result);
        }
        public IResponse <EnterpriseAdapterModel> UpdateEnterpriseAdapter(EnterpriseAdapterModel model)
        {
            var result = new Response <EnterpriseAdapterModel>();

            try
            {
                #region connectivity
                var connection = TestConnection(model);
                if (connection.Status != StatusEnum.Success)
                {
                    result.Status  = StatusEnum.Error;
                    result.Message = connection.Message;
                    return(result);
                }
                #endregion


                var enterpriseAdapter = _entity.EnterpriseAdapter.Find(model.Id);

                enterpriseAdapter.Adapter.Name = model.Name;
                enterpriseAdapter.ServerIP     = model.ServerIP;
                enterpriseAdapter.Port         = (int)model.Port;
                enterpriseAdapter.Username     = model.Username;
                enterpriseAdapter.Password     = model.Password;
                enterpriseAdapter.Adapter.AdapterTypeItemId = (int)model.EnterpriseAdapter;
                enterpriseAdapter.DatabaseName = model.DatabaseName;
                switch (model.Direction)
                {
                case DirectionEnum.Source:
                    if ((int)model.EnterpriseAdapter < 4)
                    {
                        var adapterTable = enterpriseAdapter.EnterpriseAdapterTable.FirstOrDefault();
                        var tableColumn  = adapterTable.EnterpriseAdapterTableColumn.Where(x => x.ParentId == null).ToList();
                        foreach (var child in tableColumn)
                        {
                            DeleteRecursive(child);
                        }
                        var y = _entity.EnterpriseAdapterTableColumn.Select(x => x.Id).ToList().LastOrDefault();
                        if (model.Columns != null)
                        {
                            for (int i = 0; i < model.Columns.Count; i++)
                            {
                                var child = new EnterpriseAdapterTableColumn
                                {
                                    Id                       = (int)model.Columns[i].Id,
                                    ParentId                 = model.Columns[i].ParentId,
                                    IsForeignKey             = model.Columns[i].IsForeignKey,
                                    IsPrimaryKey             = model.Columns[i].IsPrimaryKey,
                                    PropertyNameId           = (int?)model.Columns[i].PropertyNameId,
                                    RelatedTableName         = model.Columns[i].RelatedTableName,
                                    DataTypeId               = 1,
                                    ColumnName               = model.Columns[i].ColumnName,
                                    EnterpriseAdapterTableId = adapterTable.Id
                                };
                                if (model.Columns[i].PropertyNameId == 0)
                                {
                                    child.PropertyNameId = null;
                                }

                                if (model.Columns[i].ParentId == 0)
                                {
                                    child.ParentId = null;
                                }
                                else
                                {
                                    child.ParentId = model.Columns[i].ParentId;
                                }
                                _entity.EnterpriseAdapterTableColumn.Add(child);
                            }
                        }
                    }
                    foreach (var prop in model.Properties.First().SourceProperties ?? new List <PropertyModel>())
                    {
                        var cryptoAdapterProperty = _entity.EnterpriseAdapterProperty.Where(eap => eap.EnterpriseAdapterId == enterpriseAdapter.Id && eap.PropertyId == prop.Id).SingleOrDefault();

                        cryptoAdapterProperty.Value = prop.Value ?? String.Empty;
                    }
                    break;

                case DirectionEnum.Destination:
                    foreach (var prop in model.Properties.First().DestinationProperties ?? new List <PropertyModel>())
                    {
                        var cryptoAdapterProperty = _entity.EnterpriseAdapterProperty.Where(eap => eap.EnterpriseAdapterId == enterpriseAdapter.Id && eap.PropertyId == prop.Id).SingleOrDefault();

                        cryptoAdapterProperty.Value = prop.Value ?? String.Empty;
                    }
                    break;

                default:
                    break;
                }

                _entity.SaveChanges();
                result.Status  = StatusEnum.Success;
                result.Message = Message.ChangesSaved;
            }
            catch (Exception ex)
            {
                result.Status  = StatusEnum.Error;
                result.Message = Message.SomethingWentWrong;
                _logger.Information($"EnterpriseAdapterService.UpdateEnterpriseAdapter(model: {model})");
                _logger.Error(ex.Message);
            }
            return(result);
        }
        public IResponse <NoValue> Create(EnterpriseAdapterModel model, long userProfileID)
        {
            var result = new Response <NoValue>();

            try
            {
                #region connectivity
                var connection = TestConnection(model);
                if (connection.Status != StatusEnum.Success)
                {
                    result.Status  = StatusEnum.Error;
                    result.Message = connection.Message;
                    return(result);
                }
                #endregion

                var enterpriseAdapter = new EnterpriseAdapter();
                var newAdapter        = new Adapter
                {
                    Name = model.Name,
                    AdapterTypeItemId = (int)model.EnterpriseAdapter,
                    UserProfileId     = userProfileID,
                    StatusId          = (int)Statuses.Active,
                    DirectionId       = (int)model.Direction
                };
                _entity.Adapter.Add(newAdapter);

                enterpriseAdapter.ServerIP     = model.ServerIP;
                enterpriseAdapter.Port         = (int)model.Port;
                enterpriseAdapter.Username     = model.Username;
                enterpriseAdapter.Password     = model.Password;
                enterpriseAdapter.DatabaseName = model.DatabaseName;
                enterpriseAdapter.AdapterId    = newAdapter.Id;
                _entity.EnterpriseAdapter.Add(enterpriseAdapter);

                switch (model.Direction)
                {
                case DirectionEnum.Destination:
                    foreach (var prop in model.Properties.First().DestinationProperties ?? new List <PropertyModel>())
                    {
                        var enterpriseAdapterProperty = new EnterpriseAdapterProperty
                        {
                            EnterpriseAdapter = enterpriseAdapter,
                            PropertyId        = prop.Id,
                            Value             = prop.Value ?? String.Empty
                        };
                        _entity.EnterpriseAdapterProperty.Add(enterpriseAdapterProperty);
                    }
                    break;


                case DirectionEnum.Source:
                    if ((int)model.EnterpriseAdapter < 4)
                    {
                        var adapterTable = new EnterpriseAdapterTable
                        {
                            TableName           = model.ParentTable,
                            EnterpriseAdapterId = enterpriseAdapter.Id
                        };
                        _entity.EnterpriseAdapterTable.Add(adapterTable);

                        var y = _entity.EnterpriseAdapterTableColumn.Select(x => x.Id).ToList().LastOrDefault();
                        if (model.Columns != null)
                        {
                            for (int i = 0; i < model.Columns.Count; i++)
                            {
                                var child = new EnterpriseAdapterTableColumn
                                {
                                    Id                       = (int)model.Columns[i].Id,
                                    ParentId                 = model.Columns[i].ParentId,
                                    IsForeignKey             = model.Columns[i].IsForeignKey,
                                    IsPrimaryKey             = model.Columns[i].IsPrimaryKey,
                                    PropertyNameId           = (int?)model.Columns[i].PropertyNameId,
                                    RelatedTableName         = model.Columns[i].RelatedTableName,
                                    DataTypeId               = 1,
                                    ColumnName               = model.Columns[i].ColumnName,
                                    EnterpriseAdapterTableId = adapterTable.Id
                                };
                                if (model.Columns[i].PropertyNameId == 0)
                                {
                                    child.PropertyNameId = null;
                                }

                                if (model.Columns[i].ParentId == 0)
                                {
                                    child.ParentId = null;
                                }
                                else
                                {
                                    child.ParentId = model.Columns[i].ParentId;
                                }
                                _entity.EnterpriseAdapterTableColumn.Add(child);
                            }
                        }
                    }
                    foreach (var prop in model.Properties.First().SourceProperties ?? new List <PropertyModel>())
                    {
                        var enterpriseAdapterProperty = new EnterpriseAdapterProperty
                        {
                            EnterpriseAdapter = enterpriseAdapter,
                            PropertyId        = prop.Id,
                            Value             = prop.Value ?? String.Empty
                        };
                        _entity.EnterpriseAdapterProperty.Add(enterpriseAdapterProperty);
                    }
                    break;

                default:
                    break;
                }

                _entity.SaveChanges();
                result.Status  = StatusEnum.Success;
                result.Message = "Connection test succeeded. Enterprise adapter added successfully.";
            }

            catch (Exception ex)
            {
                result.Status  = StatusEnum.Error;
                result.Message = Message.SomethingWentWrong;
                _logger.Information($"EnterpriseAdapterService.Create(model: {model}, userProfileID: {userProfileID})");
                _logger.Error(ex.Message);
            }
            return(result);
        }