132 lines
5.0 KiB
C#
132 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.ServiceModel;
|
|
using System.ServiceModel.Web;
|
|
using System.Text;
|
|
using GeneralApi.IntegrationUtility.Common;
|
|
using log4net;
|
|
using Microsoft.Practices.ServiceLocation;
|
|
using Taloyhtio.GeneralApi.Common.Extensions;
|
|
using Taloyhtio.GeneralApi.IntegrationUtility.TaloyhtioIntegration;
|
|
using Condo = Taloyhtio.GeneralApi.Common.DTO.Condo;
|
|
|
|
namespace Taloyhtio.GeneralApi.IntegrationUtility.Services.Impl
|
|
{
|
|
public class FileProcessor<T> : IFileProcessor<T>
|
|
where T : class
|
|
{
|
|
private ILog logger = ServiceLocator.Current.GetInstance<ILog>();
|
|
private IFileParser<T> parser;
|
|
private ISettingsProvider settingsProvider;
|
|
private ISender<T> sender;
|
|
|
|
public FileProcessor(IFileParser<T> parser, ISettingsProvider settingsProvider, ISender<T> sender)
|
|
{
|
|
this.parser = parser;
|
|
this.sender = sender;
|
|
this.settingsProvider = settingsProvider;
|
|
this.sender = sender;
|
|
}
|
|
|
|
public bool ProcessFile(string filePath)
|
|
{
|
|
try
|
|
{
|
|
return this.processFile(filePath);
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.logger.ErrorFormat(string.Format("{0}\n{1}", x.Message, x.StackTrace));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private bool processFile(string filePath)
|
|
{
|
|
if (string.IsNullOrEmpty(filePath))
|
|
{
|
|
this.logger.WarnFormat("File path is null or empty");
|
|
return false;
|
|
}
|
|
var items = this.parser.Parse(filePath);
|
|
if (items.IsNullOrEmpty())
|
|
{
|
|
this.logger.WarnFormat("No items were found in the file '{0}'", filePath);
|
|
return true;
|
|
}
|
|
this.logger.InfoFormat("'{0}' item(s) were found in the file '{1}'", items.Count, filePath);
|
|
|
|
var proxy = new TaloyhtioIntegrationClient();
|
|
using (new OperationContextScope(proxy.InnerChannel))
|
|
{
|
|
string loginName = this.settingsProvider.Get<string>(Constants.Settings.LOGIN_NAME_SETTING_KEY);
|
|
string pwd = this.settingsProvider.Get<string>(Constants.Settings.PASSWORD_SETTING_KEY);
|
|
WebOperationContext.Current.OutgoingRequest.Headers.Add(
|
|
Taloyhtio.GeneralApi.Common.Constants.HttpHeaders.Username, loginName);
|
|
WebOperationContext.Current.OutgoingRequest.Headers.Add(
|
|
Taloyhtio.GeneralApi.Common.Constants.HttpHeaders.Password, pwd);
|
|
|
|
this.logger.Info("Start processing items...");
|
|
// pairs of (original index, result)
|
|
var results = new List<KeyValuePair<int, bool>>();
|
|
int count = items.Count;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
var item = items[i];
|
|
if (item == null)
|
|
{
|
|
this.logger.WarnFormat("Item with index '{0}' is null. Skip it", i);
|
|
continue;
|
|
}
|
|
this.logger.InfoFormat("Send item with index '{0}': {1}...", i, item.ToString());
|
|
bool result = this.send(item, proxy);
|
|
results.Add(new KeyValuePair<int, bool>(i, result));
|
|
this.logger.InfoFormat("Complete sending item with index '{0}'", i);
|
|
}
|
|
this.logger.Info("Finish processing items");
|
|
bool commonResult = results.All(r => r.Value);
|
|
this.logger.InfoFormat("Common result is '{0}'", commonResult);
|
|
this.logger.Info("Detailed result:");
|
|
for (int i = 0; i < results.Count; i++)
|
|
{
|
|
this.logger.InfoFormat("Index {0}: {1}", results[i].Key, results[i].Value);
|
|
}
|
|
this.logger.Info("End of detailed result");
|
|
return commonResult;
|
|
}
|
|
}
|
|
|
|
private bool send(T item, TaloyhtioIntegrationClient proxy)
|
|
{
|
|
try
|
|
{
|
|
if (item == null)
|
|
{
|
|
this.logger.Error("Item is null. It will not be sent");
|
|
return false;
|
|
}
|
|
|
|
if (proxy == null)
|
|
{
|
|
this.logger.Error("Proxy is null. Item will not be sent");
|
|
return false;
|
|
}
|
|
|
|
this.logger.InfoFormat("Sending item information to Taloyhtio...");
|
|
|
|
//proxy.SaveCondoInformation(item);
|
|
this.sender.Send(item, proxy);
|
|
this.logger.Info("Item information was successfully sent");
|
|
|
|
return true;
|
|
}
|
|
catch (Exception x)
|
|
{
|
|
this.logger.ErrorFormat(string.Format("Error occured during sending item information:\n{0}\n{1}", x.Message, x.StackTrace));
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|