parseHierarchy

by riaanc

HTML

<script src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
<button onClick="RunDemo();">Parse Input</button>
<br/>
<textarea id="txtInput" style="float:left; width: 400px; height: 400px;">  
[
  {
    "terr_territoryid": -1069146751,
    "terr_caption": "Bellville",
    "terr_parentid": -2147483638,
    "terr_rangeend": -971116126
  },
  {
    "terr_territoryid": -2147483637,
    "terr_caption": "Cape Town Medi-clinic",
    "terr_parentid": -2147483638,
    "terr_rangeend": -2049453012
  },
  {
    "terr_territoryid": 991947098,
    "terr_caption": "Cape Town MRI",
    "terr_parentid": -2147483639,
    "terr_rangeend": 1074047745
  },
  {
    "terr_territoryid": 160831245,
    "terr_caption": "Century City",
    "terr_parentid": -2147483638,
    "terr_rangeend": 285667112
  },
  {
    "terr_territoryid": -1657330507,
    "terr_caption": "Chris Barnard Memorial Hospital",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1559299882
  },
  {
    "terr_territoryid": -873085499,
    "terr_caption": "Claremont Hospital",
    "terr_parentid": -2147483638,
    "terr_rangeend": -775054874
  },
  {
    "terr_territoryid": -2147483638,
    "terr_caption": "Dr Morton and Partners",
    "terr_parentid": -2147483639,
    "terr_rangeend": 909846449
  },
  {
    "terr_territoryid": -1461269255,
    "terr_caption": "Fountain Medical Centre",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1363238630
  },
  {
    "terr_territoryid": -1951422385,
    "terr_caption": "Gatesville Medical Centre",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1853391760
  },
  {
    "terr_territoryid": -1559299881,
    "terr_caption": "Goodwood",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1461269256
  },
  {
    "terr_territoryid": -1363238629,
    "terr_caption": "Kenilworth Medicross",
    "terr_parentid": -2147483638,
    "terr_rangeend": -1265208004
  },
  {
    "terr_territoryid": -186871117,
    "terr_caption": "Khayelitsha Medicross",
   ...

JavaScript

function RunDemo()
{
	var inputJson = JSON.parse( $("#txtInput").val() );  
  var outputJson = parseHierarchy(inputJson);
  $("#txtOutput").val(JSON.stringify(outputJson, null, 2));
}

function transformRecord(aOriginal)
{
	return {
  	territoryID: aOriginal.terr_territoryid,
    name: aOriginal.terr_caption,
    children: [],
    depth: 0
  };
}

function addTerritory(aParent, aRecords)
{
	var childRecords = $.grep(aRecords, function( rec ) { return rec.terr_parentid == aParent.territoryID; });
  
  $.each(childRecords, function(idx, childRec){
  	var childTerritory = transformRecord(childRec);
    childTerritory.depth = aParent.depth+1;
    aParent.children.push(childTerritory);
    addTerritory(childTerritory, aRecords);
  });
}

function parseHierarchy(aRecords)
{
	var recordsSorted = aRecords.sort(function(a,b){ return a.terr_rangeend < b.terr_rangeend ? -1 : (a.terr_rangeend == b.terr_rangeend ? 0 : 1)});  

	//Find the parent element. Parent element has terr_parentid == 0
  var rootRecordOriginal = $.grep(recordsSorted, function( rec ) { return rec.terr_parentid == 0; })[0];
  var rootRecord = transformRecord(rootRecordOriginal);
  addTerritory(rootRecord, recordsSorted);
  
  
  return rootRecord;  
}