private static void ImportICalendarFiles() { BcServiceApp.Instance.Initialize(false); BcEntityContext context = BcEntityContext.Create(); foreach (CountryCalendarImportInfo c in BcServiceApp.Instance.Settings.CountryCalendarImportDirectories) { if (string.IsNullOrEmpty(c.CountryCode) || string.IsNullOrEmpty(c.CountryName)) { throw new NullReferenceException(string.Format("{0} or {1} not specified on {2} in {3}.", EntityReader <CountryCalendarImportInfo> .GetPropertyName(p => p.CountryCode, false), EntityReader <CountryCalendarImportInfo> .GetPropertyName(p => p.CountryName, false), typeof(CountryCalendarImportInfo).Name, BcServiceApp.Instance.Settings.FilePath)); } if (!Directory.Exists(c.ICalendarImportDirectory)) { throw new DirectoryNotFoundException(string.Format("Could not find directory {0} for country {1} ({2}).", c.ICalendarImportDirectory, c.CountryCode, c.CountryName)); } string[] filePaths = Directory.GetFiles(c.ICalendarImportDirectory, "*.ics"); foreach (string f in filePaths) { GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Parsing iCalendar file: {0} ...", f), LogMessageType.Information, LoggingLevel.Normal)); ICalCalendar calendar = ICalPublicHolidayParser.ParseICalendarFile(f, c.CountryCode, c.CountryName); GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Saving iCalendar to DB: {0} ...", f), LogMessageType.Information, LoggingLevel.Normal)); context.SaveICalCalendar(calendar); if (BcServiceApp.Instance.Settings.DeleteICalendarFilesAfterImport) { GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Deleting iCalendar file: {0} ...", f), LogMessageType.Information, LoggingLevel.Normal)); File.Delete(f); } GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Completed iCalendar file import: {0} ...", f), LogMessageType.SuccessAudit, LoggingLevel.Minimum)); } } }
public void SaveICalCalendar(ICalCalendar calendar) { using (TransactionScope t = new TransactionScope()) { foreach (ICalPublicHoliday p in calendar.PublicHolidays) { PublicHoliday original = GetPublicHoliday(calendar.CountryCode, p.DateIdentifier, false); if (original == null) { DB.GetTable <PublicHoliday>().InsertOnSubmit(new PublicHoliday() { PublicHolidayId = Guid.NewGuid(), CountryCode = calendar.CountryCode, CountryName = calendar.CountryName, EventName = p.EventName, DateIdentifier = p.DateIdentifier, Year = p.Year, Month = p.Month, Day = p.Day, HolidayDate = p.GetDate(), DateCreated = DateTime.Now }); } else { original.CountryCode = calendar.CountryCode; original.CountryName = calendar.CountryName; original.EventName = p.EventName; original.DateIdentifier = p.DateIdentifier; original.Year = p.Year; original.Month = p.Month; original.Day = p.Day; original.HolidayDate = p.GetDate(); } DB.SubmitChanges(); } t.Complete(); } }
private static void DownloadICalCalendar(string countryCode, string countryName, string startDate, string endDate) { BcServiceApp.Instance.Initialize(false); string downloadUrl = string.Format(BcServiceApp.Instance.Settings.ICalendarUrl, countryCode, startDate, endDate); using (WebClient webClient = new WebClient()) { string fileName = string.Format("{0}-{1}-{2}.{3}", countryCode, startDate, endDate, ICalPublicHolidayParser.ICALENDAR_FILE_EXTENSION); string filePath = Path.Combine(Path.GetTempPath(), fileName); GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Downloading iCalendar to file: {0} ...", filePath), LogMessageType.Information, LoggingLevel.Normal)); webClient.DownloadFile(downloadUrl, filePath); GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Parsing iCalendar file: {0} ...", filePath), LogMessageType.Information, LoggingLevel.Normal)); ICalCalendar calender = ICalPublicHolidayParser.ParseICalendarFile(filePath, countryCode, countryName); BcEntityContext context = BcEntityContext.Create(); GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Saving iCalendar to DB: {0} ...", filePath), LogMessageType.Information, LoggingLevel.Normal)); context.SaveICalCalendar(calender); GOC.Instance.Logger.LogMessage(new LogMessage(string.Format("Deleting iCalendar file: {0} ...", filePath), LogMessageType.Information, LoggingLevel.Normal)); File.Delete(filePath); } }