Пример #1
0
        private IErrorsInfo RunCopyEntityScript(ref SyncEntity sc, IDataSource sourceds, IDataSource destds, string srcentity, string destentity, IProgress <PassedArgs> progress, CancellationToken token, bool CreateMissingEntity = true)
        {
            try
            {
                int errorcount = 0;


                EntityStructure item = sourceds.GetEntityStructure(srcentity, true);

                if (item != null)
                {
                    if (destds.Category == DatasourceCategory.RDBMS)
                    {
                        IRDBSource rDB = (IRDBSource)destds;
                        rDB.DisableFKConstraints(item);
                    }
                    if (destds.CheckEntityExist(item.EntityName))
                    {
                        object srcTb;
                        string entname;
                        var    src = Task.Run(() => { return(sourceds.GetEntity(item.EntityName, null)); });
                        src.Wait();
                        srcTb = src.Result;
                        List <object> srcList = new List <object>();
                        if (src.Result != null)
                        {
                            DMTypeBuilder.CreateNewObject(item.EntityName, item.EntityName, item.Fields);
                            if (srcTb.GetType().FullName.Contains("DataTable"))
                            {
                                srcList = DMEEditor.Utilfunction.GetListByDataTable((DataTable)srcTb, DMTypeBuilder.myType, item);
                            }
                            if (srcTb.GetType().FullName.Contains("List"))
                            {
                                srcList = (List <object>)srcTb;
                            }

                            if (srcTb.GetType().FullName.Contains("IEnumerable"))
                            {
                                srcList = (List <object>)srcTb;
                            }
                            ScriptCount += srcList.Count();
                            //if (progress != null)
                            //{
                            //    PassedArgs ps = new PassedArgs { ParameterInt1 = CurrentScriptRecord, ParameterInt2 = ScriptCount };
                            //    progress.Report(ps);

                            //}
                            foreach (var r in srcList)
                            {
                                CurrentScriptRecord  += 1;
                                DMEEditor.ErrorObject = destds.InsertEntity(item.EntityName, r);
                                token.ThrowIfCancellationRequested();
                                if (DMEEditor.ErrorObject.Flag == Errors.Failed)
                                {
                                    SyncErrorsandTracking tr = new SyncErrorsandTracking();
                                    errorcount++;
                                    tr.errormessage         = DMEEditor.ErrorObject.Message;
                                    tr.errorsInfo           = DMEEditor.ErrorObject;
                                    tr.rundate              = DateTime.Now;
                                    tr.sourceEntityName     = item.EntityName;
                                    tr.currenrecordindex    = CurrentScriptRecord;
                                    tr.sourceDataSourceName = item.DataSourceID;
                                    tr.parentscriptid       = sc.id;
                                    sc.Tracking.Add(tr);
                                    if (progress != null)
                                    {
                                        PassedArgs ps = new PassedArgs {
                                            EventType = "Update", ParameterInt1 = CurrentScriptRecord, ParameterInt2 = ScriptCount, ParameterString3 = DMEEditor.ErrorObject.Message
                                        };
                                        progress.Report(ps);
                                    }
                                    if (errorcount >= StopErrorCount)
                                    {
                                        stoprun = true;
                                        PassedArgs ps = new PassedArgs {
                                            EventType = "Stop", ParameterInt1 = CurrentScriptRecord, ParameterInt2 = ScriptCount, ParameterString3 = DMEEditor.ErrorObject.Message
                                        };
                                        progress.Report(ps);
                                    }
                                }
                                else
                                {
                                    if (progress != null)
                                    {
                                        PassedArgs ps = new PassedArgs {
                                            EventType = "NA", ParameterInt1 = CurrentScriptRecord, ParameterInt2 = ScriptCount, ParameterString3 = DMEEditor.ErrorObject.Message
                                        };
                                        progress.Report(ps);
                                    }
                                }
                            }
                        }
                        if (progress != null)
                        {
                            PassedArgs ps = new PassedArgs {
                                ParameterString1 = $"Ended Copying Data from {srcentity} on {sourceds.DatasourceName} to {srcentity} on {destds.DatasourceName} ", ParameterInt1 = CurrentScriptRecord, ParameterInt2 = ScriptCount
                            };
                            progress.Report(ps);
                        }
                    }
                    else
                    {
                        DMEEditor.AddLogMessage("Copy Data", $"Error Could not Copy Entity Date {srcentity} on {sourceds.DatasourceName} to {srcentity} on {destds.DatasourceName} ", DateTime.Now, 0, null, Errors.Failed);
                    }
                    if (destds.Category == DatasourceCategory.RDBMS)
                    {
                        IRDBSource rDB = (IRDBSource)destds;
                        rDB.EnableFKConstraints(item);
                    }
                }
                else
                {
                    DMEEditor.AddLogMessage("Copy Data", $"Error Could not Find Entity  {srcentity} on {sourceds.DatasourceName} to {srcentity} on {destds.DatasourceName} ", DateTime.Now, 0, null, Errors.Failed);
                }
            }
            catch (Exception ex)
            {
                DMEEditor.AddLogMessage("Fail", $"Error copying Data {srcentity} on {sourceds.DatasourceName} to {srcentity} on {destds.DatasourceName} ({ex.Message})", DateTime.Now, -1, "CopyDatabase", Errors.Failed);
            }
            return(DMEEditor.ErrorObject);
        }