461 lines
15 KiB
C#
461 lines
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.ServiceModel;
|
|
using System.Threading.Tasks;
|
|
using HtmlAgilityPack;
|
|
using Kreta.BusinessLogic.Classes;
|
|
using Kreta.BusinessLogic.HelperClasses;
|
|
using Kreta.BusinessLogic.KirService;
|
|
using Kreta.BusinessLogic.Utils;
|
|
using Kreta.Core;
|
|
using Kreta.Core.ConnectionType;
|
|
using Kreta.Core.Exceptions;
|
|
|
|
namespace Kreta.BusinessLogic.Helpers
|
|
{
|
|
public class KirHelper : LogicBase
|
|
{
|
|
private static HttpClient HttpClient = new HttpClient();
|
|
|
|
public KirHelper(IConnectionType connectionType, string omAzonosito, string felhasznaloNev, string jelszo = null, string kirToken = null) : base(connectionType)
|
|
{
|
|
LoginUrl = ApplicationData.KirLoginUrl;
|
|
|
|
if (string.IsNullOrWhiteSpace(LoginUrl))
|
|
{
|
|
throw new ArgumentException("KirLoginUrl nincs megadva az AppSettings-ben!");
|
|
}
|
|
|
|
OmAzonosito = omAzonosito;
|
|
FelhasznaloNev = felhasznaloNev;
|
|
|
|
if (jelszo != null)
|
|
{
|
|
Jelszo = jelszo;
|
|
GetKirToken();
|
|
}
|
|
else if (kirToken != null)
|
|
{
|
|
KirToken = kirToken;
|
|
}
|
|
else
|
|
{
|
|
throw new ArgumentException("Jelszó vagy KIR Token megadása kötelező!");
|
|
}
|
|
}
|
|
|
|
public KirHelper(IConnectionType connectionType, string omAzonosito, string felhasznaloNev, string kirToken) : base(connectionType)
|
|
{
|
|
OmAzonosito = omAzonosito;
|
|
FelhasznaloNev = felhasznaloNev;
|
|
KirToken = kirToken;
|
|
}
|
|
|
|
private string OmAzonosito { get; }
|
|
|
|
private string FelhasznaloNev { get; }
|
|
|
|
private string Jelszo { get; }
|
|
|
|
public string KirToken { get; private set; }
|
|
|
|
public string KretaKod { get; private set; }
|
|
|
|
private static string ExternalId => "1";
|
|
|
|
private static string Version => "1";
|
|
|
|
private static string ClientApp => "KRETA";
|
|
|
|
private static int PageSize => 100;
|
|
|
|
private string LoginUrl { get; }
|
|
|
|
public static string GetServiceUrl()
|
|
{
|
|
using (var client = new Service_KretaClient())
|
|
{
|
|
return client.Endpoint.Address.Uri.ToString();
|
|
}
|
|
}
|
|
|
|
public List<KretaPedagogusAdatType> GetPedagogusList(int telephelyKod, DateTime? modosulasDatumKesobbiMint = null)
|
|
{
|
|
SetKretaKod(telephelyKod);
|
|
|
|
var request = new PedagogusListaRequestType
|
|
{
|
|
externalId = ExternalId,
|
|
version = Version,
|
|
clientApp = ClientApp,
|
|
pageSize = PageSize,
|
|
pageSizeSpecified = true,
|
|
pageNumber = 1,
|
|
pageNumberSpecified = true,
|
|
KretaKod = KretaKod,
|
|
|
|
Item = modosulasDatumKesobbiMint.HasValue
|
|
? (object)modosulasDatumKesobbiMint.Value //ModosulasDatumKesobbiMint
|
|
: (object)true //TeljesAdatLekeres
|
|
};
|
|
|
|
long totalCount;
|
|
var result = GetPedagogusList(request, out totalCount);
|
|
|
|
if (PageSize < totalCount)
|
|
{
|
|
List<KretaPedagogusAdatType> pageItems;
|
|
int pageCount = (int)(totalCount / PageSize) + 1;
|
|
for (int pageNumber = 2; pageNumber <= pageCount; pageNumber++)
|
|
{
|
|
request.pageNumber = pageNumber;
|
|
pageItems = GetPedagogusList(request, out totalCount);
|
|
|
|
if (pageItems.Count == 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
result.AddRange(pageItems);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public List<KretaTanuloAdatType> GetTanuloList(int telephelyKod, DateTime? modosulasDatumKesobbiMint = null)
|
|
{
|
|
SetKretaKod(telephelyKod);
|
|
|
|
var request = new TanuloListaRequestType
|
|
{
|
|
externalId = ExternalId,
|
|
version = Version,
|
|
clientApp = ClientApp,
|
|
|
|
pageSize = PageSize,
|
|
pageSizeSpecified = true,
|
|
pageNumber = 1,
|
|
pageNumberSpecified = true,
|
|
|
|
KretaKod = KretaKod,
|
|
|
|
Item = modosulasDatumKesobbiMint.HasValue
|
|
? (object)modosulasDatumKesobbiMint.Value //ModosulasDatumKesobbiMint
|
|
: (object)true //TeljesAdatLekeres
|
|
};
|
|
|
|
long totalCount;
|
|
var result = GetTanuloList(request, out totalCount);
|
|
|
|
if (PageSize < totalCount)
|
|
{
|
|
List<KretaTanuloAdatType> pageItems;
|
|
int pageCount = (int)(totalCount / PageSize) + 1;
|
|
for (int pageNumber = 2; pageNumber <= pageCount; pageNumber++)
|
|
{
|
|
request.pageNumber = pageNumber;
|
|
pageItems = GetTanuloList(request, out totalCount);
|
|
|
|
if (pageItems.Count == 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
result.AddRange(pageItems);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public Dictionary<string, string> GetTelephelyList()
|
|
{
|
|
var request = new TelephelyListaRequestType
|
|
{
|
|
externalId = ExternalId,
|
|
version = Version,
|
|
clientApp = ClientApp,
|
|
};
|
|
|
|
var response = CallService(x => x.TelephelyLista(GetAuthHeader(), request));
|
|
|
|
if (response.isSuccess)
|
|
{
|
|
return response.TelephelyLista.ToDictionary(x => x.TelephelyKod.ToString(), x => x.TelephelyNev);
|
|
}
|
|
|
|
return new Dictionary<string, string>();
|
|
}
|
|
|
|
private List<KretaPedagogusAdatType> GetPedagogusList(PedagogusListaRequestType request, out long totalCount)
|
|
{
|
|
totalCount = 0;
|
|
var response = CallService(x => x.PedagogusLista(GetAuthHeader(), request));
|
|
if (response.isSuccess)
|
|
{
|
|
totalCount = response.total;
|
|
return response.PedagogusLista.ToList();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private List<KretaTanuloAdatType> GetTanuloList(TanuloListaRequestType request, out long totalCount)
|
|
{
|
|
totalCount = 0;
|
|
var response = CallService(x => x.TanuloLista(GetAuthHeader(), request));
|
|
if (response.isSuccess)
|
|
{
|
|
totalCount = response.total;
|
|
return response.TanuloLista.ToList();
|
|
}
|
|
|
|
return new List<KretaTanuloAdatType>();
|
|
}
|
|
|
|
private Dictionary<int, string> GetSzolgaltatasList(int telephelyKod)
|
|
{
|
|
var request = new TelephelyListaRequestType
|
|
{
|
|
externalId = ExternalId,
|
|
version = Version,
|
|
clientApp = ClientApp,
|
|
};
|
|
|
|
var response = CallService(x => x.TelephelyLista(GetAuthHeader(), request));
|
|
|
|
if (response.isSuccess)
|
|
{
|
|
var telephely = response.TelephelyLista.SingleOrDefault(x => x.TelephelyKod == telephelyKod);
|
|
|
|
if (telephely != null)
|
|
{
|
|
return telephely.Szolgaltatasok.ToDictionary(x => x.Kod, x => x.Nev);
|
|
}
|
|
}
|
|
|
|
return new Dictionary<int, string>();
|
|
}
|
|
|
|
private void GetKretaKod(int telephelyKod)
|
|
{
|
|
var szolgalatatasList = GetSzolgaltatasList(telephelyKod);
|
|
|
|
if (string.IsNullOrWhiteSpace(KretaKod))
|
|
{
|
|
var request = new KretaTelephelyRegisztracioRequestType
|
|
{
|
|
externalId = ExternalId,
|
|
version = Version,
|
|
clientApp = ClientApp,
|
|
|
|
Item = true, //UjKreta
|
|
//Item = KretaKod //KretaKod -> új KretaToken generáláshoz
|
|
//TokenGeneralas = true,
|
|
//TokenGeneralasSpecified = true
|
|
|
|
TelephelyLista = new[]
|
|
{
|
|
new TelephelyType
|
|
{
|
|
TelephelyKod = telephelyKod,
|
|
Szolgaltatasok = szolgalatatasList.Select(x => new OktatasiSzolgaltatasType { Kod = x.Key }).ToArray()
|
|
}
|
|
}
|
|
};
|
|
var response = CallService(x => x.KretaTelephelyRegisztracio(GetAuthHeader(), request));
|
|
|
|
if (response.isSuccess)
|
|
{
|
|
KretaKod = response.KretaKod;
|
|
//KretaToken = response.KretaToken;
|
|
}
|
|
}
|
|
}
|
|
|
|
private KIR2AuthHeaderType GetAuthHeader()
|
|
{
|
|
return new KIR2AuthHeaderType
|
|
{
|
|
Intezmeny = OmAzonosito,
|
|
Felhasznalo = new FelhasznaloType
|
|
{
|
|
Item = new IntezmenyiFelhasznaloType
|
|
{
|
|
FelhasznaloNev = FelhasznaloNev,
|
|
KIRToken = KirToken
|
|
}
|
|
//Item = KretaToken
|
|
}
|
|
};
|
|
}
|
|
|
|
private string GetKirToken()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(KirToken))
|
|
{
|
|
try
|
|
{
|
|
string requestVerificationToken = GetRequestVerificationToken();
|
|
|
|
string loginResponse = AsyncUtil.RunSync(() => GetLoginResponse(requestVerificationToken));
|
|
|
|
KirToken = GetToken(loginResponse);
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
if (ex.Status == WebExceptionStatus.ProtocolError)
|
|
{
|
|
var response = ex.Response as HttpWebResponse;
|
|
if (response != null)
|
|
{
|
|
var statusCode = (int)response.StatusCode;
|
|
if (statusCode != (int)HttpStatusCode.OK)
|
|
{
|
|
System.Diagnostics.Trace.WriteLine(ex.Message);
|
|
throw new BlException(StringResourcesUtils.GetString(5430), ex);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
System.Diagnostics.Trace.WriteLine(ex.Message);
|
|
throw new BlException(StringResourcesUtils.GetString(5432), ex);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Trace.WriteLine(ex.Message);
|
|
throw new BlException(StringResourcesUtils.GetString(5432), ex);
|
|
}
|
|
}
|
|
|
|
if (KirToken != null)
|
|
{
|
|
return KirToken;
|
|
}
|
|
|
|
throw new BlException(Kreta.Framework.StringResourcesUtil.GetString(5303)/*Sikertelen belépés kérem ellenőrizze a megadott adatokat!*/);
|
|
}
|
|
|
|
private string GetRequestVerificationToken()
|
|
{
|
|
string loginPage = AsyncUtil.RunSync(() => HttpClient.GetStringAsync(LoginUrl));
|
|
|
|
var htmlDocument = new HtmlDocument();
|
|
htmlDocument.LoadHtml(loginPage);
|
|
|
|
HtmlNode requestVerificationTokenNode = htmlDocument.DocumentNode.SelectSingleNode("//input[@name='__RequestVerificationToken']");
|
|
|
|
return requestVerificationTokenNode?.Attributes["value"].Value;
|
|
}
|
|
|
|
private async Task<string> GetLoginResponse(string requestVerificationToken)
|
|
{
|
|
HttpResponseMessage result = await HttpClient.PostAsync(LoginUrl,
|
|
new FormUrlEncodedContent(
|
|
new Dictionary<string, string>
|
|
{
|
|
{ "__RequestVerificationToken", requestVerificationToken },
|
|
{ "UserDto.UserType", "3" },
|
|
{ "UserDto.FenntId", string.Empty },
|
|
{ "UserDto.TKAzonosito", string.Empty },
|
|
{ "UserDto.OMAzonosito", OmAzonosito },
|
|
{ "UserDto.UserName", FelhasznaloNev },
|
|
{ "UserDto.Password", Jelszo },
|
|
{ "ContinueUrl", string.Empty },
|
|
{ "RedirectPartnerId", string.Empty }
|
|
}
|
|
)
|
|
);
|
|
|
|
return await result.Content.ReadAsStringAsync();
|
|
}
|
|
|
|
private string GetToken(string loginResponse)
|
|
{
|
|
var htmlDocument = new HtmlDocument();
|
|
htmlDocument.LoadHtml(loginResponse);
|
|
|
|
HtmlNode tokenNode = htmlDocument.GetElementbyId("token");
|
|
|
|
return tokenNode?.InnerText.Trim();
|
|
}
|
|
|
|
//TODO:Exceptionokat szétszedni
|
|
private T CallService<T>(Func<Service_KretaClient, T> action)
|
|
{
|
|
T res;
|
|
|
|
var client = new Service_KretaClient();
|
|
|
|
try
|
|
{
|
|
res = action(client);
|
|
client.Close();
|
|
}
|
|
catch (BlException ex)
|
|
{
|
|
throw new BlException(ex.Message);
|
|
}
|
|
catch (TimeoutException)
|
|
{
|
|
throw new TimeoutException("Az adatok lekérdezésére túllépte a megadott időkeretet!");
|
|
}
|
|
catch (FaultException)
|
|
{
|
|
throw new BlException(StringResourcesUtils.GetString(5431));
|
|
}
|
|
catch
|
|
{
|
|
if (client.State == CommunicationState.Faulted)
|
|
{
|
|
client.Abort();
|
|
|
|
}
|
|
else if (client.State != CommunicationState.Closed)
|
|
{
|
|
client.Close();
|
|
}
|
|
|
|
throw;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
private void SetKretaKod(int telephelyKod)
|
|
{
|
|
var intezmenyHelper = new IntezmenyHelper(ConnectionType);
|
|
|
|
var intezmenyAdatokId = intezmenyHelper.GetIntezmenyiAdatok().ID;
|
|
|
|
var kirTelephelyHelper = new KirTelephelyHelper(ConnectionType);
|
|
|
|
var kreta = kirTelephelyHelper.GetKretaKodByTelephelyKod(telephelyKod);
|
|
|
|
if (string.IsNullOrWhiteSpace(kreta))
|
|
{
|
|
GetKretaKod(telephelyKod);
|
|
|
|
var kirTelephelyCo = new KirTelephelyCo()
|
|
{
|
|
Telephelykod = telephelyKod,
|
|
IntezmenyAdatokId = intezmenyAdatokId,
|
|
TanevId = TanevId,
|
|
KretaKod = KretaKod,
|
|
Nev = GetTelephelyList().FirstOrDefault(x => x.Key == telephelyKod.ToString()).Value
|
|
};
|
|
|
|
kirTelephelyHelper.SaveKirTelephely(kirTelephelyCo);
|
|
}
|
|
else
|
|
{
|
|
KretaKod = kreta;
|
|
}
|
|
}
|
|
}
|
|
}
|