protected void GuessAgency(participatingorg org, bool isFilterByType)
{
var IsNotFoundInAims = true;
var exAgencies = Sessions.iOrgs.ExecutingAgencies;
//var exAgencies = isFilterByType ? $filter('filter')($scope.ExecutingAgencies, { ExecutingAgencyTypeId: org.ExecutingAgencyTypeId }) : $scope.ExecutingAgencies;
if (org.@ref != null)
{
var exa = exAgencies.Find(f => f.IATICode == org.@ref);
if (exa != null)
{
org.AllID = exa.AllID;
IsNotFoundInAims = false;
if (!isFilterByType) org.ExecutingAgencyTypeId = exa.ExecutingAgencyTypeId;
}
}
if (IsNotFoundInAims)
{
//try to set executing agency
ExecutingAgencyLookupItem agencyGuessed = null;
var minDistance = 1000;
for (var i = 0; i < exAgencies.Count; i++)
{
var distance = getEditDistance(org.Name.ToLower(), exAgencies[i].Name.ToLower());
if (minDistance > distance)
{
minDistance = distance;
agencyGuessed = exAgencies[i];
}
}
if (agencyGuessed != null)
{
var tolaratedDistance = isFilterByType ? (org.Name.Length + agencyGuessed.Name.Length) / 2 : ((org.Name.Length + agencyGuessed.Name.Length) / 2) * 50 / 100;
if (minDistance < tolaratedDistance)
{
org.AllID = agencyGuessed.AllID;
if (!isFilterByType) org.ExecutingAgencyTypeId = agencyGuessed.ExecutingAgencyTypeId;
}
}
else
{
org.ExecutingAgencyTypeId = 4;
}
}
}