Ejemplo n.º 1
0
        public static long?GetMoodleDisciplinaId
        (
            this Disciplina item,
            AppConfiguration configuration,
            Modalidade modalidade,
            GetCourseByNameClient client = null,
            HttpClient httpClient        = null
        )
        {
            string disciplinaNome     = GetNomeDisciplina(item, configuration, modalidade);
            long?  moodleDisciplineId = MoodleFromToCache.GetCachedMoodleCourse(modalidade.IdModalidade, disciplinaNome);

            if (moodleDisciplineId.HasValue)
            {
                return(moodleDisciplineId.Value);
            }

            GetCourseByNameClient getClient = client;

            if (getClient == null)
            {
                getClient = new GetCourseByNameClient();
            }

            if (httpClient != null)
            {
                getClient.AddHttpClient(httpClient);
            }

            getClient.AddBaseUrl(modalidade.MoodleUrl)
            .AddServiceUrl(modalidade.MoodleServiceUrl)
            .AddToken(modalidade.MoodleGetInfoServiceToken);

            GetByNameRequest request = new GetByNameRequest()
            {
                Name = item.DisciplinaNome
            };

            Task <CourseResponse> task = getClient.Post(request);

            task.Wait();

            CourseResponse response = task.Result;

            if (response?.Id > 0)
            {
                MoodleFromToCache.AddCourse(modalidade.IdModalidade, disciplinaNome, response.Id);
            }

            return(response?.Id);
        }
Ejemplo n.º 2
0
        protected override void ProcessItem(Disciplina item, AbstractMoodleServiceClient createClient, AbstractMoodleServiceClient verifyClient)
        {
            try
            {
                item.DisciplinaNome = item.GetNomeDisciplina(Configuration, ModalidadeAtual);
                item.ShortName      = item.GetShortNameDisciplina(Configuration, ModalidadeAtual);

                long?cachedMoodleId = MoodleFromToCache.GetCachedMoodleCourse(ModalidadeAtual.IdModalidade, item.DisciplinaNome);

                if (cachedMoodleId.HasValue)
                {
                    LastUrl = "cached_value";

                    var reason = new NotImportedReason <Disciplina>()
                    {
                        Data   = item,
                        Url    = LastUrl,
                        Reason = $"Disciplina [{item.DisciplinaNome}] já está adicionada ao MOODLE ({LastUrl})."
                    };

                    Result.NotImported.Enqueue(reason);
                    Log(reason.Reason);
                    return;
                }

                CourseResponse exists = VerifyIfExists(verifyClient, item.DisciplinaNome);

                if (exists?.Id > 0)
                {
                    MoodleFromToCache.AddCourse(ModalidadeAtual.IdModalidade, item.DisciplinaNome, exists.Id);

                    var reason = new NotImportedReason <Disciplina>()
                    {
                        Data   = item,
                        Url    = LastUrl,
                        Reason = $"Disciplina [{item.DisciplinaNome}] já está adicionada ao MOODLE ({LastUrl})."
                    };

                    Result.NotImported.Enqueue(reason);
                    Log(reason.Reason);
                    return;
                }

                CourseResponse response = SendItem(createClient, item);

                ImportedResult <Disciplina, CourseResponse> importedResult = new ImportedResult <Disciplina, CourseResponse>()
                {
                    Date   = DateTime.Now,
                    Data   = item,
                    Url    = LastUrl,
                    Result = response,
                    Active = true,
                };

                Result.ImportedSuccessfully.Enqueue(importedResult);
                MoodleFromToCache.AddCourse(ModalidadeAtual.IdModalidade, item.DisciplinaNome, response.Id);

                Log($"Disciplina {item.DisciplinaNome} adicionado.");
            }
            catch (MoodleDataNotExistsException mex)
            {
                var reason = new NotImportedReason <Disciplina>()
                {
                    Data   = item,
                    Url    = LastUrl,
                    Reason = mex.Message
                };

                Result.NotImported.Enqueue(reason);
                Log(reason.Reason);
            }
            catch (AggregateException agex)
            {
                var exception = agex.InnerExceptions[0];

                var reason = new NotImportedReason <Disciplina>()
                {
                    Data      = item,
                    Url       = LastUrl,
                    Reason    = exception.Message,
                    Exception = exception
                };

                Result.NotImported.Enqueue(reason);
                Log(reason.Reason);
            }
            catch (Exception ex)
            {
                var reason = new NotImportedReason <Disciplina>()
                {
                    Data      = item,
                    Url       = LastUrl,
                    Exception = ex,
                    Reason    = ex.Message
                };

                Result.NotImported.Enqueue(reason);
                Log(reason.Reason);
            }
        }