Stored Functions

Content-Type: application/json
URN: urn:openhie.org:openinfoman-rapidpro:select_hfr_facility

Description



 
    Creates selection menu of all the CSD organizations with a specified parent.  Designed as part of heierarchical organization selection menus in RapidPro webhooks.
  

Definition

Source:
import module namespace functx = "http://www.functx.com";
declare namespace csd = "urn:ihe:iti:csd:2013";
declare variable $careServicesRequest as item() external;

let $t_top_orgid  :=  $careServicesRequest/query[@name = 'orgid']/text()
let $top_org :=  (/csd:CSD/csd:organizationDirectory/csd:organization[@entityID = $t_top_orgid])[1]

let $selected_text := $careServicesRequest/query[@name = 'input']/text()

let $selected_index := if ($selected_text castable as xs:integer) then xs:integer($selected_text) else -1

let $selected :=
  if ($selected_index = 0)
  then
    /csd:CSD/csd:organizationDirectory/csd:organization[
      @entityID = /csd:CSD/csd:organizationDirectory/csd:organization[@entityID = $top_org/@entityID]/csd:parent/@entityID
    ]
  else if ($selected_index > 0)
  then
    let $select_orgs := 
      if (exists($top_org))
      then /csd:CSD/csd:organizationDirectory/csd:organization[./csd:parent/@entityID = $top_org/@entityID]
      else /csd:CSD/csd:organizationDirectory/csd:organization[count(./csd:parent[@entityID]) = 0]
    return ($select_orgs)[$selected_index]
  else ()

    
let $orgs := 
  if (exists($selected) and  local-name($selected ) = 'organization')
  then /csd:CSD/csd:organizationDirectory/csd:organization[./csd:parent/@entityID = $selected/@entityID]
  else /csd:CSD/csd:organizationDirectory/csd:organization[count(./csd:parent[@entityID]) = 0]

let $output := 
  if ( $selected[./csd:codedType/@code=7])
  then
    <json type='object'>
      <menutext>Facility Selected</menutext>
      <facility>{string($selected/@entityID)}</facility>
      <facilityname>{$selected/csd:primaryName/text()}</facilityname>
      <orgcode>{string($selected/csd:otherID/@code)}</orgcode>
    </json>
  else  if ( local-name($selected ) = 'organization'  or not(exists($selected)))
  then
    <json type='object'>
      <menutext >
 	{ if (exists($selected)) then concat("In ", $selected/csd:primaryName/text(), ". ")  else () }
	{
	  let $parent := (/csd:CSD/csd:organizationDirectory/csd:organization[@entityID = $selected/csd:parent/@entityID])[1]/csd:primaryName/text()
	  return
	    if ($parent)
	    then concat("O) Up to ", $parent, " ") 
	    else ()
        }
	{ for $ent at $pos in ($orgs)  return concat( $pos, ") ", $ent/csd:primaryName/text())    }
      </menutext>
      <orgid>{if (exists($selected)) then string($selected/@entityID) else ()}</orgid>
      <orgcode>{string($selected/csd:otherID/@code)}</orgcode>
      <facility/>
      <facilityname/>      
    </json>
   else <json type='object'/>

(:return $output :)
return json:serialize($output,map{'format':'direct'})
Content-Type: application/json
URN: urn:openhie.org:openinfoman-rapidpro:select_village

Description



 
    Creates selection menu of all the CSD organizations and faciltiies with a specified parent.  Designed as part of heierarchical facility selection menus in RapidPro webhooks.
  

Definition

Source:
import module namespace functx = "http://www.functx.com";
declare namespace csd = "urn:ihe:iti:csd:2013";
declare variable $careServicesRequest as item() external;

let $t_top_orgid  :=  $careServicesRequest/query[@name = 'orgid']/text()
let $top_org :=  (/csd:CSD/csd:organizationDirectory/csd:organization[@entityID = $t_top_orgid])[1]



let $selected_text := $careServicesRequest/query[@name = 'input']/text()
(:
let $selections := $careServicesRequest/values/item[./pair[@name = 'label' and ./text() = 'facilityselection']]
let $selected_text := 
  if (count($selections) > 0)
  then
    let $max_time := max(for $time in $selections/pair[@name='time'] return xs:dateTime($time))
    return $selections[./pair[@name = 'time'] = $max_time]/pair[@name='text']/text()
   else ()
:)

let $selected_index := if ($selected_text castable as xs:integer) then xs:integer($selected_text) else -1


let $selected :=
  if ($selected_index = 0)
  then
    /csd:CSD/csd:organizationDirectory/csd:organization[
      @entityID = /csd:CSD/csd:organizationDirectory/csd:organization[@entityID = $top_org/@entityID]/csd:parent/@entityID
    ]
  else if ($selected_index > 0)
  then
    let $select_orgs := 
      if (exists($top_org))
      then /csd:CSD/csd:organizationDirectory/csd:organization[./csd:parent/@entityID = $top_org/@entityID]
      else /csd:CSD/csd:organizationDirectory/csd:organization[count(./csd:parent[@entityID]) = 0]
	
    let $select_facs := 
      if (exists($top_org))
      then /csd:CSD/csd:facilityDirectory/csd:facility[./csd:organizations/csd:organization[@entityID = $top_org/@entityID]]
      else ()
    return ($select_orgs,$select_facs)[ $selected_index]
  else ()

    
let $orgs := 
  if (exists($selected) and  local-name($selected ) = 'organization')
  then /csd:CSD/csd:organizationDirectory/csd:organization[./csd:parent/@entityID = $selected/@entityID]
  else /csd:CSD/csd:organizationDirectory/csd:organization[count(./csd:parent[@entityID]) = 0]
	
let $facs := 
  if (exists($selected) and  local-name($selected ) = 'organization')
  then /csd:CSD/csd:facilityDirectory/csd:facility[./csd:organizations/csd:organization[@entityID = $selected/@entityID]]
  else ()



let $output := 
  if ( local-name($selected ) = 'facility')
  then
    <json type='object'>
      <menutext>You already selected a facility.  You shouldn't see this message</menutext>
      <facility>{string($selected/@entityID)}</facility>
      <facilityname>{$selected/csd:primaryName/text()}</facilityname>
    </json>
  else  if ( local-name($selected ) = 'organization'  or not(exists($selected)))
  then
    <json type='object'>
      <menutext >
 	{ if (exists($selected)) then concat("In ", $selected/csd:primaryName/text(), ". ")  else () }
	{
	  let $parent := (/csd:CSD/csd:organizationDirectory/csd:organization[@entityID = $selected/csd:parent/@entityID])[1]/csd:primaryName/text()
	  return
	    if ($parent)
	    then concat("O) Up to ", $parent, " ") 
	    else ()
        }
	{ for $ent at $pos in ($orgs,$facs)  return concat( $pos, ") ", $ent/csd:primaryName/text())    }
      </menutext>
      <orgid>{if (exists($selected)) then string($selected/@entityID) else ()}</orgid>
      <facility/>
      <facilityname/>      
    </json>
   else <json type='object'/>

(:return $output :)
return json:serialize($output,map{'format':'direct'})
Content-Type: text/xml
URN: urn:openhie.org:openinfoman-tz:hfr_facilities_to_csd

Description



 
    Updates the CSD document by an import from Health Facility Registry export of the Organizational hierarchy
  

Definition

Source:
import module namespace dxf2csd = "http://dhis2.org/csd/dxf/2.0";
import module namespace csd_webconf =  "https://github.com/openhie/openinfoman/csd_webconf";
import module namespace csd_dm = "https://github.com/openhie/openinfoman/csd_dm";
import module namespace functx = "http://www.functx.com";

declare namespace csd = "urn:ihe:iti:csd:2013";

declare variable $careServicesRequest as item() external;

let $doc_name := string($careServicesRequest/@resource)
let $doc := csd_dm:open_document($csd_webconf:db,$doc_name)
let $org_dir := $doc/csd:CSD/csd:organizationDirectory

let $oid := $careServicesRequest/oid
let $csv_src := $careServicesRequest/csv  
let $csv :=    csv:parse($csv_src, map{ 'header': true()})/csv/record



let $namespace_uuid := "7ee93e32-78da-4913-82f8-49eb0a618cfc"

let $generate_UUID_v3 := function($name) {
  concat('urn:uuid:' , dxf2csd:generate_UUID_v3($name,$namespace_uuid))
}

let $zone_csd :=
for $zone_record in distinct-values($csv/Zone)
 let $now := current-dateTime()
 let $zone_urn := $generate_UUID_v3(concat('zone:',$zone_record))
  return
   <csd:organization entityID="{$zone_urn}">
     <csd:codedType code="1" codingScheme="{$oid}"/>
     <csd:primaryName>{$zone_record}</csd:primaryName>
     <csd:record
       created="{$now}"
       updated="{$now}"
       sourceDirectory="http://hfrportal.ehealth.go.tz"/>
   </csd:organization>

let $region_csd :=
for $region_record in distinct-values($csv/Region)
 let $now := current-dateTime()
 let $region_urn := $generate_UUID_v3(concat('region:',$region_record))
 let $parent_urns :=
 for $record in $csv  
  let $zone := $record/Zone
  let $region := $record/Region
  return if($region_record=$region)
   then $generate_UUID_v3(concat('zone:',$zone))
   else ()
 return
   <csd:organization entityID="{$region_urn}">
     <csd:codedType code="2" codingScheme="{$oid}"/>
     <csd:primaryName>{$region_record}</csd:primaryName>
     <csd:record
       created="{$now}"
       updated="{$now}"
       sourceDirectory="http://hfrportal.ehealth.go.tz"/>
     <csd:parent entityID="{$parent_urns[last()]}"/>
   </csd:organization>

let $district_csd :=
for $district_record in distinct-values($csv/District)
 let $now := current-dateTime()
 let $district_urn := $generate_UUID_v3(concat('district:',$district_record))
 let $parent_urns :=
 for $record in $csv
  let $region := $record/Region
  let $district := $record/District
  return if($district_record=$district)
   then $generate_UUID_v3(concat('region:',$region))
   else ()
 return
   <csd:organization entityID="{$district_urn}">
     <csd:codedType code="3" codingScheme="{$oid}"/>
     <csd:primaryName>{$district_record}</csd:primaryName>
     <csd:record
       created="{$now}"
       updated="{$now}"
       sourceDirectory="http://hfrportal.ehealth.go.tz"/>
     <csd:parent entityID="{$parent_urns[last()]}"/>
   </csd:organization>

let $council_csd :=
for $council_record in distinct-values($csv/Council)
 let $now := current-dateTime()
 let $council_urn := $generate_UUID_v3(concat('council:',$council_record))
 let $parent_urns :=
 for $record in $csv
  let $district := $record/District
  let $council := $record/Council
  return if($council_record=$council)
   then $generate_UUID_v3(concat('district:',$district))
   else ()
 return
   <csd:organization entityID="{$council_urn}">
     <csd:codedType code="4" codingScheme="{$oid}"/>
     <csd:primaryName>{$council_record}</csd:primaryName>
     <csd:record
       created="{$now}"
       updated="{$now}"
       sourceDirectory="http://hfrportal.ehealth.go.tz"/>
     <csd:parent entityID="{$parent_urns[last()]}"/>
   </csd:organization>

let $ward_csd :=
for $ward_record in distinct-values($csv/Ward)
 let $now := current-dateTime()
 let $ward_urn := $generate_UUID_v3(concat('ward:',$ward_record))
 let $parent_urns :=
 for $record in $csv
  let $council := $record/Council
  let $ward := $record/Ward
  return if($ward_record=$ward)
   then $generate_UUID_v3(concat('council:',$council))
   else ()
 return if (not (contains($ward_record,"Not set"))) then
   <csd:organization entityID="{$ward_urn}">
     <csd:codedType code="5" codingScheme="{$oid}"/>
     <csd:primaryName>{$ward_record}</csd:primaryName>
     <csd:record
       created="{$now}"
       updated="{$now}"
       sourceDirectory="http://hfrportal.ehealth.go.tz"/>
     <csd:parent entityID="{$parent_urns[last()]}"/>
   </csd:organization>
   else ()

let $village_csd :=
for $village_record in distinct-values($csv/VillageStreet)
 let $now := current-dateTime()
 let $village_urn := $generate_UUID_v3(concat('village:',$village_record))
 let $parent_urns :=
 for $record in $csv
  let $ward := if ((contains($record/Ward,"Not set"))) then $record/Council else($record/Ward)
  let $village := $record/VillageStreet
  return if($village_record=$village)
   then $generate_UUID_v3(concat('ward:',$ward))
   else ()
 return if (not (contains($village_record,"Not set"))) then
   <csd:organization entityID="{$village_urn}">
     <csd:codedType code="6" codingScheme="{$oid}"/>
     <csd:primaryName>{$village_record}</csd:primaryName>
     <csd:record
       created="{$now}"
       updated="{$now}"
       sourceDirectory="http://hfrportal.ehealth.go.tz"/>
     <csd:parent entityID="{$parent_urns[last()]}"/>
   </csd:organization>
   else ()

let $facility_csd :=
for $record in $csv
 let $facNum := $record/FacilityNumber
 let $facName  := $record/FacilityName
 let $now := current-dateTime()
 let $facility_urn := $generate_UUID_v3(concat('facility:',$facNum))
 let $parent := if ((contains($record/VillageStreet,"Not set"))) then 
                     (if ((contains($record/Ward,"Not set"))) then $record/Council else $record/Ward) 
                     else($record/VillageStreet)
 let $parent_string := if ((contains($record/VillageStreet,"Not set"))) then
                     (if ((contains($record/Ward,"Not set"))) then "council:" else "ward:")
                     else("village:")
 let $parent_urn := $generate_UUID_v3(concat($parent_string,$parent))
 return if (not (contains($facName,"Not set"))) then
   <csd:organization entityID="{$facility_urn}">
     <csd:otherID assigningAuthorityName='health_facility_registry' code="{$facNum}"/>
     <csd:codedType code="7" codingScheme="{$oid}"/>
     <csd:primaryName>{$facName/text()}</csd:primaryName>
     <csd:record
       created="{$now}"
       updated="{$now}"
       sourceDirectory="http://hfrportal.ehealth.go.tz"/>
     <csd:parent entityID="{$parent_urn}"/>
   </csd:organization>
   else ()

let $csd :=
  <csd:CSD>
    <csd:organizationDirectory>
      {($zone_csd,$region_csd,$district_csd,$council_csd,$ward_csd,$village_csd,$facility_csd)}
    </csd:organizationDirectory>
    <csd:serviceDirectory/>
    <csd:facilityDirectory/>
    <csd:providerDirectory/>
  </csd:CSD>

return  csd_dm:add($csd_webconf:db,$csd,$doc_name)
Content-Type: text/xml
URN: urn:openhie.org:openinfoman-tz:rm_org_hierarchy_to_csd

Description



 
    Updates the CSD document by an import from Resource Map's export of the Organizational hierarchy
  

Definition

Source:
import module namespace dxf2csd = "http://dhis2.org/csd/dxf/2.0";
import module namespace csd_dm = "https://github.com/openhie/openinfoman/csd_dm";
import module namespace functx = "http://www.functx.com";

declare namespace csd = "urn:ihe:iti:csd:2013";

declare variable $careServicesRequest as item() external;


let $doc_name := string($careServicesRequest/@resource)
let $doc := csd_dm:open_document($doc_name)


let $oid := $careServicesRequest/oid
let $csv_src := $careServicesRequest/csv  
let $csv :=    csv:parse($csv_src, map{ 'header': true()})/csv/record



let $namespace_uuid := "7ee93e32-78da-4913-82f8-49eb0a618cfc"

let $generate_UUID_v3 := function($name) {
  concat('urn:uuid:' , dxf2csd:generate_UUID_v3($name,$namespace_uuid))
}



let $csd_orgs := 
  for $record in $csv
   let $village := $record/VillMtaa
   let $ward  := $record/Ward
   let $council  := $record/Council
   let $district  := $record/District
   let $region  := $record/Region
   let $zone  := $record/Zone 
   let $country  := $record/Country
   let $name  := $record/Name
   let $id  := $record/NodeID
   let $urn := $generate_UUID_v3(concat('organization:',$id))
   let $level := 
      if (not(functx:all-whitespace($village)))
      then  '6'
      else if (not(functx:all-whitespace($ward)))
      then  '5'
      else if (not(functx:all-whitespace($council)))
      then  '4'
      else if (not(functx:all-whitespace($district)))
      then  '3'
      else if (not(functx:all-whitespace($region)))
      then  '2'
      else if (not(functx:all-whitespace($zone)))
      then  '1'
      else if (not(functx:all-whitespace($country)))
      then  '0'
      else ()
   let $pid := 
      if ( not(functx:all-whitespace($village)) )
      then  $csv[./Country = $country and ./Zone = $zone and ./Region = $region and ./District = $district and ./Council = $council and ./Ward = $ward and functx:all-whitespace(./VillMtaa)]/NodeID/text()
      else if (not(functx:all-whitespace($ward)))
      then  $csv[./Country = $country and ./Zone = $zone and ./Region = $region and ./District = $district and ./Council = $council and functx:all-whitespace(./VillMtaa) and functx:all-whitespace(./Ward)]/NodeID/text()
      else if (not(functx:all-whitespace($council)))
      then  $csv[./Country = $country and ./Zone = $zone and ./Region = $region and ./District = $district and functx:all-whitespace(./Council) and functx:all-whitespace(./VillMtaa) and functx:all-whitespace(./Ward) ]/NodeID/text()
      else if (not(functx:all-whitespace($district)))
      then  $csv[./Country = $country and ./Zone = $zone and ./Region = $region and functx:all-whitespace(./District) and functx:all-whitespace(./Council) and functx:all-whitespace(./VillMtaa) and functx:all-whitespace(./Ward) ]/NodeID/text()
      else if (not(functx:all-whitespace($region)))
      then  $csv[./Country = $country and ./Zone = $zone and functx:all-whitespace(./Region) and functx:all-whitespace(./District) and functx:all-whitespace(./Council) and functx:all-whitespace(./VillMtaa) and functx:all-whitespace(./Ward)]/NodeID/text()
      else if (not(functx:all-whitespace($zone)))
      then  $csv[./Country = $country  and functx:all-whitespace(./Zone) and functx:all-whitespace(./Region) and functx:all-whitespace(./District) and functx:all-whitespace(./Council) and functx:all-whitespace(./VillMtaa) and functx:all-whitespace(./Ward)]/NodeID/text()
      else ()
   let $parent := 
     if ($pid)
     then
       let $parent_urn := $generate_UUID_v3(concat('organization:',$pid))
       return <csd:parent entityID="{$parent_urn}"/>
     else ()
   return 
     if (($name) and ($level)  and ($urn) )
     then 
       <csd:organization entityID="{$urn}">
	 <csd:otherID assigningAuthorityName='resource_map_tanzania' code="{$id}"/>
	 <csd:codedType code="{$level}" codingScheme="{$oid}"/>
	 <csd:primaryName>{$name/text()}</csd:primaryName>
	 {$parent}
       </csd:organization>
     else ($record)  (:no name or id or type :)



let $csd :=
  <csd:CSD>
    <csd:organizationDirectory>
      {$csd_orgs}
    </csd:organizationDirectory>
    <csd:serviceDirectory/>
    <csd:facilityDirectory/>  
    <csd:providerDirectory/>
  </csd:CSD>

return  csd_dm:add($csd,$doc_name)