Exemple #1
0
        protected override string Schedule => "1 0 * * *"; //Expression represents 1 minute past every night

        public override Task ProcessInScope(IServiceProvider serviceProvider)
        {
            string sWebRootFolder   = hostingEnvironment.WebRootPath;
            string connectionString = configuration.GetValue <string>("ConnectionStrings:DefaultConnection");
            string url = configuration.GetValue <string>("IATI:Url");
            string countryCode = configuration.GetValue <string>("IATI:Country");
            string currencyUrl = configuration.GetValue <string>("IATI:CurrencyUrl");
            string transactionTypesUrl = configuration.GetValue <string>("IATI:TransactionTypesUrl");
            string financeTypesUrl = configuration.GetValue <string>("IATI:FinanceTypesUrl");
            string sectorVocabularyUrl = configuration.GetValue <string>("IATI:SectorsVocabulary");
            string organizationTypesUrl = configuration.GetValue <string>("IATI:OrganizationTypesUrl");
            string countriesUrl = configuration.GetValue <string>("IATI:CountriesUrl");
            string sectorsUrl = configuration.GetValue <string>("IATI:SectorsUrl");
            string filePath = Path.Combine(sWebRootFolder, "IATISomali.xml");
            string sectorsFilePath = Path.Combine(sWebRootFolder, "Sectors");
            string organizationTypesPath = Path.Combine(sWebRootFolder, "OrganizationTypes.json");
            string currenciesFilePath = Path.Combine(sWebRootFolder, "Currency.json");
            string countriesFilePath = Path.Combine(sWebRootFolder, "Country.json");
            string transactionTypesPath = Path.Combine(sWebRootFolder, "IATITransactionTypes.json");
            string financeTypesPath = Path.Combine(sWebRootFolder, "IATIFinanceTypes.json");
            string sectorsVocabPath = Path.Combine(sWebRootFolder, "IATISectorVocabulary.json");
            string xml = "", sectorsXml = "", json = "", transactionTypesJson = "", financeTypesJson = "", sectorsVocabJson = "",
                   organizationTypesJson = "", errorsFile = "errors.txt", errorsFilePath = "";

            //, countriesJson = ""

            using (var scope = scopeFactory.CreateScope())
            {
                bool                          isIATIDownloading      = false;
                HttpClient                    httpClient             = new HttpClient();
                IWebHostEnvironment           hostingEnvironment     = scope.ServiceProvider.GetRequiredService <IWebHostEnvironment>();
                ExchangeRateHttpService       httpService            = new ExchangeRateHttpService(httpClient);
                AIMSDbContext                 dbContext              = scope.ServiceProvider.GetRequiredService <AIMSDbContext>();
                IMapper                       imapper                = scope.ServiceProvider.GetRequiredService <IMapper>();
                IUserService                  userService            = new UserService(dbContext, imapper);
                INotificationService          notificationService    = new NotificationService(dbContext, imapper);
                IATIService                   service                = new IATIService(dbContext);
                IFinancialYearSettingsService fySettingsService      = new FinancialYearSettingsService(dbContext);
                ISectorTypesService           sectorTypeService      = new SectorTypesService(dbContext, imapper);
                IProjectService               projectService         = new ProjectService(dbContext, imapper);
                IOrganizationMergeService     orgMergeService        = new OrganizationMergeService(dbContext);
                IDataBackupService            backupService          = new DataBackupService(dbContext);
                IContactMessageService        contactService         = new ContactMessageService(dbContext, imapper);
                IEmailService                 emailService           = new EmailService(dbContext);
                IProjectDeletionService       projectDeletionService = new ProjectDeletionService(dbContext, imapper);
                backupService.SetDirectoryPath(hostingEnvironment.WebRootPath);
                IFinancialYearTransitionService financialYearTransitionService = new FinancialYearTransitionService(dbContext);

                try
                {
                    using (var client = new WebClient())
                    {
                        json = client.DownloadString(currencyUrl);
                    }

                    using (var client = new WebClient())
                    {
                        organizationTypesJson = client.DownloadString(organizationTypesUrl);
                    }

                    using (var client = new WebClient())
                    {
                        transactionTypesJson = client.DownloadString(transactionTypesUrl);
                    }

                    using (var client = new WebClient())
                    {
                        financeTypesJson = client.DownloadString(financeTypesUrl);
                    }

                    using (var client = new WebClient())
                    {
                        sectorsVocabJson = client.DownloadString(sectorVocabularyUrl);
                    }

                    //Save sectors to db
                    var iatiSettings = service.GetIATISettings();
                    if (iatiSettings != null)
                    {
                        if (!string.IsNullOrEmpty(iatiSettings.BaseUrl))
                        {
                            url = iatiSettings.BaseUrl;
                            isIATIDownloading = iatiSettings.IsDownloading;
                        }
                    }

                    if (!isIATIDownloading)
                    {
                        //Download latest iati
                        using (var client = new WebClient())
                        {
                            service.SetIATIDownloading();
                            xml = client.DownloadString(url);
                        }
                        File.WriteAllText(filePath, xml);
                        service.SetIATIDownloaded();
                    }

                    var sectorTypesSources = sectorTypeService.GetSectorSources();
                    if (sectorTypesSources.Count() > 0)
                    {
                        using (var client = new WebClient())
                        {
                            int fileCounter = 1;
                            foreach (var stype in sectorTypesSources)
                            {
                                if (!string.IsNullOrEmpty(stype.SourceUrl))
                                {
                                    sectorsXml = client.DownloadString(stype.SourceUrl);
                                    int counter     = (stype.IATICode == null) ? fileCounter : Convert.ToInt32(stype.IATICode);
                                    var newFilePath = sectorsFilePath + counter + ".xml";
                                    File.WriteAllText(newFilePath, sectorsXml);
                                    stype.FilePath = newFilePath;
                                }
                                ++fileCounter;
                            }
                        }
                    }

                    var cleanedTTypeJson         = service.ExtractTransactionTypesJson(transactionTypesJson);
                    var cleanedFTypeJson         = service.ExtractFinanceTypesJson(financeTypesJson);
                    var cleanedSectorVocabJson   = service.ExtractSectorsVocabJson(sectorsVocabJson);
                    var cleanedOrgTypesVocabJson = service.ExtractOrganizationsVocabJson(organizationTypesJson);
                    File.WriteAllText(transactionTypesPath, cleanedTTypeJson);
                    File.WriteAllText(financeTypesPath, cleanedFTypeJson);
                    File.WriteAllText(sectorsVocabPath, cleanedSectorVocabJson);
                    File.WriteAllText(organizationTypesPath, cleanedOrgTypesVocabJson);

                    //userService.SetNotificationsForUsers();
                    var sectorResponse = service.ExtractAndSaveIATISectors(filePath, sectorsVocabPath);
                    if (sectorTypesSources.Count() > 0)
                    {
                        foreach (var stype in sectorTypesSources)
                        {
                            if (!string.IsNullOrEmpty(stype.FilePath))
                            {
                                service.NameSectorsCorrectly(stype.FilePath, stype.Id);
                            }
                        }
                    }

                    service.ExtractAndSaveLocations(filePath);
                    var orgResponse = service.ExtractAndSaveOrganizations(filePath, cleanedOrgTypesVocabJson);
                    notificationService.SendNotificationsForNewSectors(sectorResponse.ReturnedId);

                    var currencyList = httpService.ParseAndExtractCurrencyList(json);
                    if (currencyList.Count > 0)
                    {
                        ICurrencyService currencyService = new CurrencyService(dbContext, imapper);
                        currencyService.AddMultiple(currencyList);
                    }

                    var fySettings = fySettingsService.Get();
                    int fyMonth = 1, fyDay = 1;
                    int currentMonth = DateTime.Now.Month, currentDay = DateTime.Now.Day;
                    if (fySettings != null)
                    {
                        fyMonth = fySettings.Month;
                        fyDay   = fySettings.Day;
                    }

                    var financialTransitionForYear = financialYearTransitionService.IsFinancialTransitionApplied();
                    if ((fyMonth == currentMonth && fyDay == currentDay) || financialTransitionForYear.Exists == false)
                    {
                        int year = 0;
                        if (!financialTransitionForYear.Exists)
                        {
                            year = financialTransitionForYear.Year;
                            var backup   = backupService.BackupData(connectionString);
                            var response = projectService.AdjustDisbursementsForProjectsAsync(null, year, true).GetAwaiter().GetResult();
                            if (response.Success)
                            {
                            }
                        }
                    }

                    var pendingOrgMergeRequests    = orgMergeService.GetTwoWeeksOlderRequests();
                    if (pendingOrgMergeRequests.Any())
                    {
                        var requests = (from r in pendingOrgMergeRequests
                                        select r.RequestId).ToList();
                        orgMergeService.MergeOrganizationsAuto(requests).GetAwaiter().GetResult();
                    }
                    var pendingContactMessages = contactService.GetUnRepliedMessages();
                    foreach (var message in pendingContactMessages)
                    {
                        EmailModel emailModel = new EmailModel()
                        {
                            EmailsList = new List <EmailAddress>()
                            {
                                new EmailAddress()
                                {
                                    Email = message.SenderEmail
                                }
                            },
                            Subject       = message.Subject,
                            Message       = message.Message,
                            FooterMessage = null,
                        };
                        emailService.SendEmailForPendingMessages(emailModel, message.SenderName, message.SenderEmail, message.ProjectTitle);
                    }
                    contactService.SetMessagesNotifiedAsync().GetAwaiter().GetResult();
                    projectDeletionService.SendPendingDeletionRequestsToManagement();

                    //File cleanup
                    string excelFiles = Path.Combine(sWebRootFolder, "ExcelFiles");
                    var    directory  = Directory.CreateDirectory(excelFiles);

                    if (directory.GetFiles().Any())
                    {
                        string[] files = Directory.GetFiles(excelFiles);

                        if (files.Length > 0)
                        {
                            foreach (string file in files)
                            {
                                FileInfo fi = new FileInfo(file);
                                if (fi.LastAccessTime < DateTime.Now.AddMinutes(-120))
                                {
                                    fi.Delete();
                                }
                            }
                        }
                    }
                }

                catch (Exception ex)
                {
                    service.SetIATIDownloaded();
                    errorsFilePath = Path.Combine(sWebRootFolder, errorsFile);
                    if (!File.Exists(errorsFilePath))
                    {
                        File.Create(errorsFilePath);
                        FileIOPermission fp = new FileIOPermission(FileIOPermissionAccess.AllAccess, errorsFilePath);
                    }
                    string errorMessage = ex.Message;
                    if (ex.InnerException != null)
                    {
                        errorMessage = ex.InnerException.Message;
                    }
                    File.AppendAllText(errorsFilePath, errorMessage);
                }
            }

            return(Task.CompletedTask);
        }
Exemple #2
0
        public override Task ProcessInScope(IServiceProvider serviceProvider)
        {
            try
            {
                string    message  = "IATI updated successfully at: " + DateTime.Now.ToLongDateString();
                string    country  = configuration.GetValue <string>("IATI:Country");
                string    url      = "http://datastore.iatistandard.org/api/1/access/activity.xml?recipient-country=" + country;
                XmlReader xReader  = XmlReader.Create(url);
                XDocument xDoc     = XDocument.Load(xReader);
                var       activity = (from el in xDoc.Descendants("iati-activity")
                                      select el.FirstAttribute).FirstOrDefault();

                IParser parser;
                ICollection <IATIActivity> activityList  = new List <IATIActivity>();
                ICollection <Organization> organizations = new List <Organization>();
                string version = "";
                version = activity.Value;
                switch (version)
                {
                case "1.03":
                    parser       = new ParserIATIVersion13();
                    activityList = parser.ExtractAcitivities(xDoc);
                    break;

                case "2.01":
                    parser       = new ParserIATIVersion21(configuration);
                    activityList = parser.ExtractAcitivities(xDoc);
                    break;
                }

                //Extract organizations for future use
                if (activityList.Count > 0)
                {
                    if (activityList != null)
                    {
                        var organizationList = from a in activityList
                                               select a.ParticipatingOrganizations;

                        if (organizationList.Count() > 0)
                        {
                            foreach (var orgCollection in organizationList)
                            {
                                var orgList = from list in orgCollection
                                              select list;

                                foreach (var org in orgList)
                                {
                                    var orgExists = (from o in organizations
                                                     where o.Name.ToLower().Equals(org.Name.ToLower())
                                                     select o).FirstOrDefault();

                                    if (orgExists == null)
                                    {
                                        organizations.Add(new Organization()
                                        {
                                            Name = org.Name,
                                            Role = org.Role
                                        });
                                    }
                                }
                            }
                        }
                    }
                }

                Debug.WriteLine("IATI Data updated at: " + DateTime.Now.ToLongDateString());
                using (var scope = scopeFactory.CreateScope())
                {
                    var         dbContext = scope.ServiceProvider.GetRequiredService <AIMSDbContext>();
                    IATIService service   = new IATIService(dbContext);
                    IATIModel   model     = new IATIModel()
                    {
                        Data          = JsonConvert.SerializeObject(activityList),
                        Organizations = JsonConvert.SerializeObject(organizations)
                    };
                    service.Add(model);
                    Debug.WriteLine(message);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return(Task.CompletedTask);
        }