示例#1
0
        public override async System.Threading.Tasks.Task<DataAccessLayer.SOAResult> DownloadLocalAsync(User user, string deviceLanguage)
        {
            Console.WriteLine("__________________________________ START ___________________________________________________");
            DataAccessLayer.SOAResult result = DataAccessLayer.SOAResult.FALSE;
            try
                {
                // In this progress we increase the progress status, too
                // Main step: Kunden Data
                ResetProgressStatus();

                ProgressStatusWish = 1;
                DataAccessLayer.DAL_Kunde dal_Kunde = new DataAccessLayer.DAL_Kunde();

                // first check permission
                if (!user.HasPermission(DAC_Object.Kunde,BusinessLayer.DAC_Permission.DOWN_UPLOAD))
                    return DataAccessLayer.SOAResult.TRUE;
                
                using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result = await dal_Kunde.InsertKundeAsync(this, user,true); 
                }
                ProgressStatus = 1;

                // Then the related data
                // 1. Ansprechpartner
                // We don't check permission for Ansprechpartner, cause Tasks and other modules will use it 
                if (result == DataAccessLayer.SOAResult.TRUE )
                {

                    ProgressStatusWish += 2;
                    List<Ansprechpartner> ansprechpartnern = await Ansprechpartner.GetAnsprechpartnernAsync(this.ID, deviceLanguage, user);
                    ProgressStatus += 2;
                    ProgressStatusWish += 1;
                    DataAccessLayer.DAL_Ansprechpartner dal_ansprechpartner = new DataAccessLayer.DAL_Ansprechpartner();
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() =>dal_ansprechpartner.InsertAnsprechpartnernAsync(ansprechpartnern,user,true));
                    }
                    ProgressStatus += 1;
                    ansprechpartnern = null;
                }

                // 2. PersonUmsatz for Charts
                if (result == DataAccessLayer.SOAResult.TRUE && user.HasPermission(DAC_Object.Kunde_Chart,DAC_Permission.DOWN_UPLOAD))
                {

                    ProgressStatusWish += 3;
                    List<BusinessLayer.PersonUmsatz> PersonUmsatzList =  await GetPersonTimeUmsatzAsync ("36",   user);
                    ProgressStatus += 3;
                    ProgressStatusWish += 2;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() =>dal_Kunde.InsertPersonUmsatzAsync(PersonUmsatzList ,this, user, true));
                    }
                    ProgressStatus += 2;
                    PersonUmsatzList = null;

                }

                // 3. Tasks and related data
                // I. Tasks
                DataAccessLayer.DAL_Task dal_task = new DataAccessLayer.DAL_Task();
                if (result == DataAccessLayer.SOAResult.TRUE && user.HasPermission(DAC_Object.Kunde_Task,DAC_Permission.DOWN_UPLOAD))
                {
                    ProgressStatusWish += 11;
                    List<Task> tasks = await dal_task.GetTasksAsync(this.ID,  user);
                    ProgressStatus += 11;
                    ProgressStatusWish += 32;

                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                                           result =  await System.Threading.Tasks.Task.Run(() =>dal_task.InsertTasksAsync(tasks, user, true));
                    }

                    //result = await dal_task.InsertTasksAsync(tasks, user, true);
                    ProgressStatus += 32;
                    tasks = null;

                }

                // II. TaskArt
                // These are help data, so permission checking is not needed
                if (result == DataAccessLayer.SOAResult.TRUE )
                {

                    ProgressStatusWish += 2;
                    List<TaskArt> taskArts = await DataAccessLayer.DAL_Task.GetTaskArtsAsync(user);
                    ProgressStatus += 2;
                    ProgressStatusWish += 1;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() =>dal_task.InsertTaskArtsAsync(taskArts, user, true));
                    }
                    ProgressStatus += 1;
                    taskArts = null;
                }

                // III. TaskTyp
                // These are help data, so permission checking is not needed
                if (result == DataAccessLayer.SOAResult.TRUE )
                {
                    ProgressStatusWish += 3;
                    List<TaskTyp> taskTyps = await DataAccessLayer.DAL_Task.GetTaskTypsAsync(user);
                    ProgressStatus += 3;
                    ProgressStatusWish += 1;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                                           result =  await System.Threading.Tasks.Task.Run(() =>dal_task.InsertTaskTypsAsync(taskTyps, user, true));
                    }
                    ProgressStatus += 1;
                }

                // IV. Mitarbeiter
                // These are help data, so permission checking is not needed
                if (result == DataAccessLayer.SOAResult.TRUE )
                {
                    ProgressStatusWish += 2;
                    List<Mitarbeiter> mitarbeitern = await DataAccessLayer.DAL_Task.GetTaskMitarbeiternAsync(user);
                    ProgressStatus += 2;
                    ProgressStatusWish += 1;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() =>dal_task.InsertMitarbeiternAsync(mitarbeitern, user, true));
                    }
                    ProgressStatus += 1;
                }

                // V. Abteilung
                // These are help data, so permission checking is not needed
                if (result == DataAccessLayer.SOAResult.TRUE )
                {
                    ProgressStatusWish += 2;
                    List<Abteilung> abteilungen = await DataAccessLayer.DAL_Task.GetTaskAbteilungenAsync(user);
                    ProgressStatus += 2;
                    ProgressStatusWish += 1;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() => dal_task.InsertAbteilungenAsync(abteilungen, user, true));
                    }
                    ProgressStatus += 1;
                }

                // VI. Angebote
                DataAccessLayer.DAL_Angebot dal_angebot = new DataAccessLayer.DAL_Angebot();
                DataAccessLayer.DAL_Position dal_position = new DataAccessLayer.DAL_Position();
                if(result == DataAccessLayer.SOAResult.TRUE && user.HasPermission(DAC_Object.Kunde_Angebot,DAC_Permission.DOWN_UPLOAD))
                {
                    ProgressStatusWish += 7;
                    List<Angebot> angebote = await dal_angebot.GetAngeboteAsync(ID, deviceLanguage, user);
                    ProgressStatus += 7;
                    foreach (var angebot in angebote)
                    {
                        List<Position> positionen = await dal_position.GetPositionenAsync(angebot.ID, angebot.Waehrung, deviceLanguage, user);
                        result = await System.Threading.Tasks.Task.Run(() => dal_position.InsertPositionenAsync(positionen, user, true));
                    }
                    ProgressStatusWish += 11;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() => dal_angebot.InsertAngeboteAsync(angebote, user, true));
                    }
                    ProgressStatus += 11;
                }

                // VI. Auftraege
                DataAccessLayer.DAL_Auftrag dal_auftrag = new DataAccessLayer.DAL_Auftrag();

                if(result == DataAccessLayer.SOAResult.TRUE && user.HasPermission(DAC_Object.Kunde_Auftrag,DAC_Permission.DOWN_UPLOAD))
                {
                    ProgressStatusWish += 7;
                    List<Auftrag> auftraege = await dal_auftrag.GetAuftraegeAsync(ID, deviceLanguage, user);
                    ProgressStatus += 7;
                    foreach (var auftrag in auftraege)
                    {

                        List<Position> positionen = await dal_position.GetPositionenAsync(auftrag.ID, auftrag.Waehrung, deviceLanguage, user);
                        result = await System.Threading.Tasks.Task.Run(() => dal_position.InsertPositionenAsync(positionen, user, true));
                    }
                    ProgressStatusWish += 6;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                                           result =  await System.Threading.Tasks.Task.Run(() => dal_auftrag.InsertAuftraegeAsync(auftraege, user, true));
                    }
                    ProgressStatus += 6;
                    Console.WriteLine("__________________________________ END ___________________________________________________");

                }
            }
            catch (Exception ex)
            {
                DataAccessLayer.ExceptionWriter.WriteLogFile(ex);
            }

            return result;

        }
        public override async System.Threading.Tasks.Task<DataAccessLayer.SOAResult> DownloadLocalAsync(User user, string deviceLanguage)
        {
            Console.WriteLine("__________________________________ START ___________________________________________________");
            DataAccessLayer.SOAResult result = DataAccessLayer.SOAResult.FALSE;
            try
                {
                // In this progress we increase the progress status, too
                // Main step: Kunden Data
                DataAccessLayer.PerformanceWatch stopWatch = new DataAccessLayer.PerformanceWatch();
                stopWatch.Start();
                ResetProgressStatus();

                ProgressStatusWish = 1;
                DataAccessLayer.DAL_Kunde dal_Kunde = new DataAccessLayer.DAL_Kunde();

                using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result = await dal_Kunde.InsertKundeAsync(this, user,true); 
                }
//                result = await dal_Kunde.InsertKundeAsync(this,  user,true);
                ProgressStatus = 1;
                stopWatch.Stop();
                stopWatch.PrintConsole ("InsertKundeAsync");



                // Then the related data
                // 1. Ansprechpartner

                if (result == DataAccessLayer.SOAResult.TRUE)
                {
                    stopWatch.Start();
                    ProgressStatusWish += 2;
                    List<Ansprechpartner> ansprechpartnern = await Ansprechpartner.GetAnsprechpartnernAsync(this.ID, deviceLanguage, user);
                    ProgressStatus += 2;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("GetAnsprechpartnernAsync");
                    stopWatch.Start();
                    ProgressStatusWish += 1;
                    DataAccessLayer.DAL_Ansprechpartner dal_ansprechpartner = new DataAccessLayer.DAL_Ansprechpartner();
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() =>dal_ansprechpartner.InsertAnsprechpartnernAsync(ansprechpartnern,user,true));
                    }
                    ProgressStatus += 1;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("InsertAnsprechpartnernAsync");
                    ansprechpartnern = null;
                }

                // 2. PersonUmsatz for Charts
                if (result == DataAccessLayer.SOAResult.TRUE)
                {
                    stopWatch.Start();
                    ProgressStatusWish += 3;
                    List<BusinessLayer.PersonUmsatz> PersonUmsatzList =  await GetPersonTimeUmsatzAsync ("36",   user);
                    ProgressStatus += 3;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("GetPersonTimeUmsatzAsync");
                    stopWatch.Start();
                    ProgressStatusWish += 2;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() =>dal_Kunde.InsertPersonUmsatzAsync(PersonUmsatzList ,this, user, true));
                    }
                    ProgressStatus += 2;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("InsertPersonUmsatzAsync");
                    PersonUmsatzList = null;
                    stopWatch.PrintConsole ("Finished InsertPersonUmsatzAsync");
                }

                // 3. Tasks and related data
                // I. Tasks
                DataAccessLayer.DAL_Task dal_task = new DataAccessLayer.DAL_Task();
                if (result == DataAccessLayer.SOAResult.TRUE)
                {
                    stopWatch.Start();
                    ProgressStatusWish += 11;
                    Console.WriteLine("Start dal_task.GetTasksAsync");
                    List<Task> tasks = await dal_task.GetTasksAsync(this.ID,  user);
                    ProgressStatus += 11;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("GetTasksAsync");
                    stopWatch.Start();
                    stopWatch.PrintConsole ("GetTasksAsync2");
                    ProgressStatusWish += 32;
                    stopWatch.PrintConsole ("GetTasksAsync3");

                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                                           result =  await System.Threading.Tasks.Task.Run(() =>dal_task.InsertTasksAsync(tasks, user, true));
                    }
                    stopWatch.PrintConsole ("GetTasksAsync4");

                    //result = await dal_task.InsertTasksAsync(tasks, user, true);
                    ProgressStatus += 32;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("InsertTasksAsync");
                    tasks = null;

                }

                // II. TaskArt
                if (result == DataAccessLayer.SOAResult.TRUE)
                {
                    stopWatch.Start();
                    ProgressStatusWish += 2;
                    List<TaskArt> taskArts = await DataAccessLayer.DAL_Task.GetTaskArtsAsync(user);
                    ProgressStatus += 2;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("GetTaskArtsAsync");
                    stopWatch.Start();
                    ProgressStatusWish += 1;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() =>dal_task.InsertTaskArtsAsync(taskArts, user, true));
                    }
                    ProgressStatus += 1;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("InsertTaskArtsAsync");
                    taskArts = null;
                }

                // III. TaskTyp
                if (result == DataAccessLayer.SOAResult.TRUE)
                {
                    stopWatch.Start();
                    ProgressStatusWish += 3;
                    List<TaskTyp> taskTyps = await DataAccessLayer.DAL_Task.GetTaskTypsAsync(user);
                    ProgressStatus += 3;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("GetTaskTypsAsync");
                    stopWatch.Start();
                    ProgressStatusWish += 1;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                                           result =  await System.Threading.Tasks.Task.Run(() =>dal_task.InsertTaskTypsAsync(taskTyps, user, true));
                    }
                    ProgressStatus += 1;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("InsertTaskTypsAsync");
                }

                // IV. Mitarbeiter
                if (result == DataAccessLayer.SOAResult.TRUE)
                {
                    stopWatch.Start();
                    ProgressStatusWish += 2;
                    List<Mitarbeiter> mitarbeitern = await DataAccessLayer.DAL_Task.GetTaskMitarbeiternAsync(user);
                    ProgressStatus += 2;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("GetTaskMitarbeiternAsync");
                    stopWatch.Start();
                    ProgressStatusWish += 1;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() =>dal_task.InsertMitarbeiternAsync(mitarbeitern, user, true));
                    }
                    ProgressStatus += 1;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("InsertMitarbeiternAsync");
                }

                // V. Abteilung
                if (result == DataAccessLayer.SOAResult.TRUE)
                {
                    stopWatch.Start();
                    ProgressStatusWish += 2;
                    List<Abteilung> abteilungen = await DataAccessLayer.DAL_Task.GetTaskAbteilungenAsync(user);
                    ProgressStatus += 2;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("GetTaskAbteilungenAsync");
                    stopWatch.Start();
                    ProgressStatusWish += 1;
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() => dal_task.InsertAbteilungenAsync(abteilungen, user, true));
                    }
                    ProgressStatus += 1;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("InsertAbteilungenAsync");
                }

                // VI. Angebote
                DataAccessLayer.DAL_Angebot dal_angebot = new DataAccessLayer.DAL_Angebot();
                DataAccessLayer.DAL_Position dal_position = new DataAccessLayer.DAL_Position();
                if(result == DataAccessLayer.SOAResult.TRUE)
                {
                    stopWatch.Start();
                    ProgressStatusWish += 7;
                    List<Angebot> angebote = await dal_angebot.GetAngeboteAsync(ID, deviceLanguage, user);
                    ProgressStatus += 7;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("GetAngeboteAsync");
                    stopWatch.Start();
                    foreach (var angebot in angebote)
                    {
                        List<Position> positionen = await dal_position.GetPositionenAsync(angebot.ID, angebot.Waehrung, deviceLanguage, user);
                        result = await System.Threading.Tasks.Task.Run(() => dal_position.InsertPositionenAsync(positionen, user, true));
                    }
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("AngebotGetPositionenAsyncInsertPositionenAsync");
                    ProgressStatusWish += 11;
                    stopWatch.Start();
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                    result =  await System.Threading.Tasks.Task.Run(() => dal_angebot.InsertAngeboteAsync(angebote, user, true));
                    }
                    ProgressStatus += 11;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("InsertAngeboteAsync");

                }

                // VI. Auftraege
                DataAccessLayer.DAL_Auftrag dal_auftrag = new DataAccessLayer.DAL_Auftrag();

                if(result == DataAccessLayer.SOAResult.TRUE)
                {
                    stopWatch.Start();
                    ProgressStatusWish += 7;
                    List<Auftrag> auftraege = await dal_auftrag.GetAuftraegeAsync(ID, deviceLanguage, user);
                    ProgressStatus += 7;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("GetAuftraegeAsync");
                    stopWatch.Start();
                    foreach (var auftrag in auftraege)
                    {

                        List<Position> positionen = await dal_position.GetPositionenAsync(auftrag.ID, auftrag.Waehrung, deviceLanguage, user);
                        result = await System.Threading.Tasks.Task.Run(() => dal_position.InsertPositionenAsync(positionen, user, true));
                    }
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("AuftragGetPositionenAsyncInsertPositionenAsync");
                    ProgressStatusWish += 6;
                    stopWatch.Start();
                    using (await DataAccessLayer.SQLiteUtilities.Mutex.LockAsync ().ConfigureAwait (false)) { 
                                           result =  await System.Threading.Tasks.Task.Run(() => dal_auftrag.InsertAuftraegeAsync(auftraege, user, true));
                    }
                    ProgressStatus += 6;
                    stopWatch.Stop();
                    stopWatch.PrintConsole ("InsertAuftraegeAsync");
                    Console.WriteLine("__________________________________ END ___________________________________________________");

                }
            }
            catch (Exception ex)
            {
                DataAccessLayer.ExceptionWriter.WriteLogFile(ex);
            }

            return result;

        }