Taylohtio/GeneralApi/GeneralApi.IntegrationUtility/Program.cs

190 lines
8.1 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using GeneralApi.IntegrationUtility.Common;
using GeneralApi.IntegrationUtility.Infrastructure;
using log4net;
using Microsoft.Practices.ServiceLocation;
using Taloyhtio.GeneralApi.Common.DTO;
using Taloyhtio.GeneralApi.Common.Extensions;
using Taloyhtio.GeneralApi.IntegrationUtility.Common;
using Taloyhtio.GeneralApi.IntegrationUtility.Repositories;
using Taloyhtio.GeneralApi.IntegrationUtility.Services;
namespace GeneralApi.IntegrationUtility
{
class Program
{
private static ILog logger;
static void Main(string[] args)
{
IoCConfiguration.Configure();
logger = ServiceLocator.Current.GetInstance<ILog>();
try
{
var settingsProvider = ServiceLocator.Current.GetInstance<ISettingsProvider>();
// for self signed certificates wwe need to ignore warning.
// See http://weblogs.asp.net/smehaffie/archive/2009/09/10/calling-web-services-that-use-self-signed-certificates.aspx
logger.Info("Determine should ignore SSL warnings or not...");
var ignoreSSLWarn = settingsProvider.GetVal<bool>(Constants.Settings.IGNORE_SSL_WARNING_SETTING_KEY);
if (ignoreSSLWarn.HasValue && ignoreSSLWarn.Value)
{
logger.Info("SSL warnings are ignored");
ServicePointManager.ServerCertificateValidationCallback = (s, cert, c, p) => true;
}
else
{
logger.Info("SSL warnings are not ignored");
}
string inputFolder = settingsProvider.Get<string>(Constants.Settings.INPUT_FOLDER_SETTING_KEY);
if (string.IsNullOrEmpty(inputFolder))
{
inputFolder = Constants.Common.DEFAULT_INPUT_FOLDER;
logger.WarnFormat("Input folder not found in the config file. Default folder '{0}' will be used", inputFolder);
}
logger.InfoFormat("Input folder is set to '{0}'", inputFolder);
FileUtilities.EnsureFolderExists(inputFolder);
string archiveFolder = settingsProvider.Get<string>(Constants.Settings.ARCHIVE_FOLDER_SETTING_KEY);
if (string.IsNullOrEmpty(archiveFolder))
{
archiveFolder = Constants.Common.DEFAULT_ARCHIVE_FOLDER;
logger.WarnFormat("Archive folder not found in the config file. Default folder '{0}' will be used", archiveFolder);
}
logger.InfoFormat("Archive folder is set to '{0}'", archiveFolder);
FileUtilities.EnsureFolderExists(archiveFolder);
// get all files from input folder
var filesRepository = ServiceLocator.Current.GetInstance<IFileRepository>();
var files = filesRepository.GetAllNew(inputFolder);
if (files.IsNullOrEmpty())
{
logger.InfoFormat("There is no files in '{0}' folder", inputFolder);
return;
}
logger.InfoFormat("Were found '{0}' file(s) in the folder '{1}'", files.Count, inputFolder);
// filter matching files
var filesFilter = ServiceLocator.Current.GetInstance<IFileFilter>();
files = filesFilter.Filter(files);
if (files.IsNullOrEmpty())
{
logger.InfoFormat("There is no matching files in '{0}' folder", inputFolder);
return;
}
// sort files using date in file name
var fileSorter = ServiceLocator.Current.GetInstance<IFileSorter>();
files = fileSorter.Sort(files);
if (files.IsNullOrEmpty())
{
logger.InfoFormat("There is no matching sorted files in '{0}' folder", inputFolder);
return;
}
var condoFileProcessor = ServiceLocator.Current.GetInstance<IFileProcessor<Condo>>();
var boardMemberFileProcessor = ServiceLocator.Current.GetInstance<IFileProcessor<BoardMember>>();
foreach (string file in files)
{
try
{
logger.InfoFormat("Start processing file '{0}'...", file);
var fileNameToLower = getFileNameToLower(file);
if (fileNameToLower.StartsWith(Constants.Common.CONDO_PREFIX.ToLower()))
{
processFile(condoFileProcessor, file);
}
else if (fileNameToLower.StartsWith(Constants.Common.BOARD_MEMBER_PREFIX.ToLower()))
{
processFile(boardMemberFileProcessor, file);
}
else
{
logger.WarnFormat("File '{0}' is unknown. It won't be processed", file);
}
logger.InfoFormat("Finish processing file '{0}'", file);
}
finally
{
bool? move = settingsProvider.GetVal<bool>(Constants.Settings.MOVE_FILES_TO_ARCHIVE_AFTER_PROCESSING);
if (move.HasValue && move.Value)
{
moveOrDelete(archiveFolder, filesRepository, file);
}
}
}
}
catch (Exception x)
{
logger.FatalFormat(string.Format("{0}\n{1}", x.Message, x.StackTrace));
}
}
private static string getFileNameToLower(string file)
{
if (string.IsNullOrEmpty(file))
{
return string.Empty;
}
file = Path.GetFileName(file);
if (string.IsNullOrEmpty(file))
{
return string.Empty;
}
file = file.ToLower();
return file;
}
private static void processFile<T>(IFileProcessor<T> processor, string file)
where T : class
{
if (processor.ProcessFile(file))
{
logger.InfoFormat("Processing of the file '{0}' was sucessfully finished without errors", file);
}
else
{
logger.InfoFormat("Processing of the file '{0}' was finished with errors", file);
}
}
private static void moveOrDelete(string archiveFolder, IFileRepository filesRepository, string file)
{
string newFilePath = Path.Combine(archiveFolder, Path.GetFileName(file));
logger.InfoFormat("Moving file '{0}' to '{1}' location...", file, newFilePath);
if (!filesRepository.Move(file, newFilePath))
{
string uniqueFileName = FileUtilities.AddCurrentTimeToFileName(Path.GetFileName(file));
newFilePath = Path.Combine(archiveFolder, uniqueFileName);
logger.InfoFormat("Second try: moving file '{0}' to '{1}' location...", file, newFilePath);
if (!filesRepository.Move(file, newFilePath))
{
// delete file in order to avoid duplicate processing
logger.ErrorFormat("Error occured during moving of file '{0}' to the new location", file);
logger.InfoFormat("Trying to delete file '{0}'...", file);
File.Delete(file);
logger.InfoFormat("File '{0}' was succcessfully deleted", file);
}
else
{
logger.InfoFormat("File '{0}' was successfully moved to the '{1}'", file, newFilePath);
}
}
else
{
logger.InfoFormat("File '{0}' was successfully moved to the '{1}'", file, newFilePath);
}
}
}
}