icenine

extract_nrls.pl 6/1/2014

Jan 6th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 22.16 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use 5.010;
  5.  
  6. # Required for parsing NRLS data
  7. use Excel::Reader::XLSX;
  8. use DateTime::Format::Excel;
  9.  
  10. # Required for inputing data into Neo4j
  11. use REST::Neo4p;
  12.  
  13. use Data::Dumper;
  14. use Getopt::Long;
  15.  
  16. use Try::Tiny;
  17.  
  18. $| = 1;
  19.  
  20. my ($input_file, $debug);
  21. my $neo_uri = 'http://localhost:7474';
  22.  
  23. GetOptions (
  24.     "input_file=s"  => \$input_file,
  25.     "neo_uri=s"     => \$neo_uri,
  26.     "debug:s"       => \$debug,);
  27.  
  28. if (!defined($input_file)) {
  29.     die "Usage: $0 --input_file <xlsx file> [--neo_uri neo4j_uri] [--debug]";
  30. }
  31.  
  32. # Define required column headers
  33. my @required_columns = (
  34.     "IN07",             # Incident description
  35.     "PD09",             # Indident degree of harm
  36.     "RP07",             # NHS Trust
  37.     "IN05_lvl1",        # Incident category
  38.     "PD05_lvl1",        # Speciality
  39.     "PD02",             # Patient sex
  40.     "IN01",             # Incident date
  41.     "IN02_A_01",        # Incident hour
  42.     "Age_at_Incident",  # Patient age
  43.     "PD11",             # Patient ethnicity
  44.     "ST01_LVL1",        # Recorder staff type
  45.     "IN03_lvl1",        # Incident location
  46.     "incidentID",       # Unique incident ID
  47. );
  48.  
  49. my $reader = Excel::Reader::XLSX->new();
  50.  
  51. my $workbook = $reader->read_file($input_file) or
  52.     die ("Could not read $input_file.");
  53.  
  54. my $worksheet = ($workbook->worksheets())[0] or
  55.     die ("Failed to assign worksheet from workbook.");
  56.  
  57. my $column_header_row = $worksheet->next_row() or
  58.     die ("Could not get column header row.");
  59.  
  60. my %header_map;
  61.  
  62. # Find column positions for required columns
  63. while (my $cell = $column_header_row->next_cell()) {
  64.     my $cell_value = $cell->value();
  65.  
  66.     if (grep /$cell_value/, @required_columns) {
  67.         $header_map{$cell_value} = $cell->col();
  68.     }
  69. }
  70.  
  71. # Create connection to Neo4j
  72. say "Connecting to Neo4j : $neo_uri " , scalar localtime if defined($debug);
  73. REST::Neo4p->connect($neo_uri) or
  74.     die ("Could not connect to $neo_uri.");
  75. say "Done connecting to Neo4j : $neo_uri " , scalar localtime if defined($debug);
  76.  
  77. # Create queries
  78. say "Creating REST::Neo4p::Query objects " , scalar localtime if defined($debug);
  79.  
  80. # Create NHS Trust
  81. my $create_nhs_trust_cql =<<'EOCQL';
  82. MATCH (ndt:NRLS_DATA_TYPE)
  83. USING INDEX ndt:NRLS_DATA_TYPE(code)
  84. WHERE
  85. ndt.code = {code}
  86. CREATE UNIQUE
  87. (n:NHS_TRUST { name : {name} })-[:HAS_NRLS_DATA_TYPE]->(ndt)
  88. RETURN n.name
  89. EOCQL
  90.  
  91. my $create_nhs_trust_query = REST::Neo4p::Query->new($create_nhs_trust_cql)
  92.     or die ("Could not create query for : $create_nhs_trust_cql");
  93.  
  94. # Create NHS Trust Location
  95. my $create_nhs_trust_location_cql =<<'EOCQL';
  96. MERGE (ntl:NHS_TRUST_LOCATION { location_level_01 : { location_level_01 } })
  97. RETURN ntl.location_level_01;
  98. EOCQL
  99.  
  100. my $create_nhs_trust_location_query =
  101.     REST::Neo4p::Query->new($create_nhs_trust_location_cql)
  102.     or die ("Could not create query for : $create_nhs_trust_location_cql");
  103.  
  104. # Create NHS Trust/NHS Trust Location relationship
  105. my $create_nhs_trust_location_rel_cql =<<'EOCQL';
  106. MATCH (nt:NHS_TRUST), (ntl:NHS_TRUST_LOCATION)
  107. USING INDEX nt:NHS_TRUST(name)
  108. USING INDEX ntl:NHS_TRUST_LOCATION(location_level_01)
  109. WHERE nt.name = { name }
  110. AND ntl.location_level_01 = { location_level_01 }
  111. CREATE UNIQUE
  112. (nt)-[hntl:HAS_NHS_TRUST_LOCATION]->(ntl) RETURN hntl
  113. EOCQL
  114.    
  115. my $create_nhs_trust_location_rel_query =
  116.     REST::Neo4p::Query->new($create_nhs_trust_location_rel_cql)
  117.     or die ("Could not create query for : $create_nhs_trust_location_rel_cql");
  118.  
  119. # Create Incident
  120. my $create_incident_cql =<<'EOCQL';
  121. MERGE (i:INCIDENT { incident_id : { incident_id } })
  122. ON CREATE SET i.incident_timestamp = { incident_timestamp },
  123. i.incident_description = { incident_description }
  124. RETURN i.incident_id
  125. EOCQL
  126.  
  127. my $create_incident_query =
  128.     REST::Neo4p::Query->new($create_incident_cql)
  129.     or die ("Could not create query for : $create_incident_cql");
  130.  
  131. # Create Incident/NRLS Data Type relationship
  132. my $create_incident_ndt_rel_cql =<<'EOCQL';
  133. MATCH (ndt:NRLS_DATA_TYPE), (i:INCIDENT)
  134. USING INDEX ndt:NRLS_DATA_TYPE(code)
  135. USING INDEX i:INCIDENT(incident_id)
  136. WHERE i.incident_id = { incident_id }
  137. AND ndt.code = { code }
  138. CREATE UNIQUE
  139. (i)-[hndt:HAS_NRLS_DATA_TYPE]->(ndt)
  140. RETURN hndt
  141. EOCQL
  142.  
  143. my $create_incident_ndt_rel_query =
  144.     REST::Neo4p::Query->new($create_incident_ndt_rel_cql)
  145.     or die ("Could not create query for : $create_incident_ndt_rel_cql");
  146.  
  147. # Create Incident/NHS Trust relationship
  148. my $create_incident_nhs_trust_rel_cql =<<'EOCQL';
  149. MATCH (n:NHS_TRUST), (i:INCIDENT)
  150. USING INDEX n:NHS_TRUST(name)
  151. USING INDEX i:INCIDENT(incident_id)
  152. WHERE n.name = { name }
  153. AND i.incident_id = { incident_id }
  154. CREATE UNIQUE
  155. (i)-[r:IS_NHS_TRUST_INCIDENT]->(n)
  156. RETURN r
  157. EOCQL
  158.  
  159. my $create_incident_nhs_trust_rel_query =
  160.     REST::Neo4p::Query->new($create_incident_nhs_trust_rel_cql)
  161.     or die ("Could not create query for : $create_incident_nhs_trust_rel_cql");
  162.  
  163. # Create Incident/NHS Trust Location relationship
  164. my $create_incident_ntl_rel_cql =<<'EOCQL';
  165. MATCH (ntl:NHS_TRUST_LOCATION), (i:INCIDENT)
  166. USING INDEX ntl:NHS_TRUST_LOCATION(location_level_01)
  167. USING INDEX i:INCIDENT(incident_id)
  168. WHERE ntl.location_level_01 = { location_level_01 }
  169. AND i.incident_id = { incident_id }
  170. CREATE UNIQUE
  171. (i)-[r:IS_NHS_TRUST_LOCATION_INCIDENT]->(ntl)
  172. RETURN r
  173. EOCQL
  174.  
  175. my $create_incident_ntl_rel_query =
  176.     REST::Neo4p::Query->new($create_incident_ntl_rel_cql)
  177.     or die ("Could not create query for : $create_incident_ntl_rel_cql");
  178.  
  179. # Create Incident Category
  180. my $create_incident_category_cql =<<'EOCQL';
  181. MATCH (ndt:NRLS_DATA_TYPE)
  182. USING INDEX ndt:NRLS_DATA_TYPE(code)
  183. WHERE ndt.code = { code }
  184. CREATE UNIQUE
  185. (ic:INCIDENT_CATEGORY { category_level_01 : { category_level_01 }
  186. })-[:HAS_NRLS_DATA_TYPE]->(ndt)
  187. RETURN ic.category_level_01
  188. EOCQL
  189.  
  190. my $create_incident_category_query =
  191.     REST::Neo4p::Query->new($create_incident_category_cql)
  192.     or die ("Could not create query for : $create_incident_category_cql");
  193.  
  194. # Create Incident/Incident Category relationship    
  195. my $create_incident_category_rel_cql =<<'EOCQL';
  196. MATCH (i:INCIDENT), (ic:INCIDENT_CATEGORY)
  197. USING INDEX ic:INCIDENT_CATEGORY(category_level_01)
  198. USING INDEX i:INCIDENT(incident_id)
  199. WHERE i.incident_id = { incident_id }
  200. AND ic.category_level_01 = { category_level_01 }
  201. CREATE UNIQUE
  202. (i)-[r:HAS_INCIDENT_CATEGORY]->(ic)
  203. RETURN r
  204. EOCQL
  205.  
  206. my $create_incident_category_rel_query =
  207.     REST::Neo4p::Query->new($create_incident_category_rel_cql)
  208.     or die ("Could not create query for : $create_incident_category_rel_cql");
  209.  
  210. # Create Incident Speciality
  211. my $create_incident_speciality_cql =<<'EOCQL';
  212. MATCH (ndt:NRLS_DATA_TYPE)
  213. USING INDEX ndt:NRLS_DATA_TYPE(code)
  214. WHERE ndt.code = { code }
  215. CREATE UNIQUE
  216. (is:INCIDENT_SPECIALITY { speciality_level_01 : { speciality_level_01 }
  217. })-[:HAS_NRLS_DATA_TYPE]->(ndt)
  218. RETURN is.speciality_level_01
  219. EOCQL
  220.  
  221. my $create_incident_speciality_query =
  222.     REST::Neo4p::Query->new($create_incident_speciality_cql)
  223.     or die ("Could not create query for : $create_incident_speciality_cql");
  224.  
  225. # Create Incident/Incident Speciality relationship  
  226. my $create_incident_speciality_rel_cql =<<'EOCQL';
  227. MATCH (i:INCIDENT), (is:INCIDENT_SPECIALITY)
  228. USING INDEX is:INCIDENT_SPECIALITY(speciality_level_01)
  229. USING INDEX i:INCIDENT(incident_id)
  230. WHERE i.incident_id = { incident_id }
  231. AND is.speciality_level_01 = { speciality_level_01 }
  232. CREATE UNIQUE
  233. (i)-[r:HAS_INCIDENT_SPECIALITY]->(is)
  234. RETURN r
  235. EOCQL
  236.  
  237. my $create_incident_speciality_rel_query =
  238.     REST::Neo4p::Query->new($create_incident_speciality_rel_cql)
  239.     or die ("Could not create query for : $create_incident_speciality_rel_cql");
  240.  
  241. # Create Incident Reporter
  242. my $create_incident_reporter_cql =<<'EOCQL';
  243. MATCH (ndt:NRLS_DATA_TYPE)
  244. USING INDEX ndt:NRLS_DATA_TYPE(code)
  245. WHERE ndt.code = { code }
  246. CREATE UNIQUE
  247. (ir:INCIDENT_REPORTER { reporter_level_01 : { reporter_level_01 }
  248. })-[:HAS_NRLS_DATA_TYPE]->(ndt)
  249. RETURN ir.reporter_level_01
  250. EOCQL
  251.  
  252. my $create_incident_reporter_query =
  253.     REST::Neo4p::Query->new($create_incident_reporter_cql)
  254.     or die ("Could not create query for : $create_incident_reporter_cql");
  255.  
  256. # Create Incident/Incident Reporter relationship  
  257. my $create_incident_reporter_rel_cql =<<'EOCQL';
  258. MATCH (i:INCIDENT), (ir:INCIDENT_REPORTER)
  259. USING INDEX ir:INCIDENT_REPORTER(reporter_level_01)
  260. USING INDEX i:INCIDENT(incident_id)
  261. WHERE i.incident_id = { incident_id }
  262. AND ir.reporter_level_01 = { reporter_level_01 }
  263. CREATE UNIQUE
  264. (i)-[r:HAS_INCIDENT_REPORTER]->(ir)
  265. RETURN r
  266. EOCQL
  267.  
  268. my $create_incident_reporter_rel_query =
  269.     REST::Neo4p::Query->new($create_incident_reporter_rel_cql)
  270.     or die ("Could not create query for : $create_incident_reporter_rel_cql");
  271.  
  272. # Create Incident Patient
  273. my $create_incident_patient_cql =<<'EOCQL';
  274. MATCH (ndt:NRLS_DATA_TYPE)
  275. USING INDEX ndt:NRLS_DATA_TYPE(code)
  276. WHERE ndt.code = { code }
  277. CREATE UNIQUE
  278. (p:PATIENT {
  279. patient_age : { patient_age },
  280. patient_sex : { patient_sex },
  281. patient_ethnicity : { patient_ethnicity }
  282. })-[:HAS_NRLS_DATA_TYPE]->(ndt)
  283. RETURN p.patient_age
  284. EOCQL
  285.  
  286. my $create_incident_patient_query =
  287.     REST::Neo4p::Query->new($create_incident_patient_cql)
  288.     or die ("Could not create query for : $create_incident_patient_cql");
  289.  
  290. # Create Incident/Incident Patient relationship  
  291. my $create_incident_patient_rel_cql =<<'EOCQL';
  292. MATCH (i:INCIDENT), (p:PATIENT)
  293. WHERE i.incident_id = { incident_id }
  294. AND p.patient_age = { patient_age }
  295. AND p.patient_sex = { patient_sex }
  296. AND p.patient_ethnicity = { patient_ethnicity }
  297. CREATE UNIQUE
  298. (i)-[r:HAS_INCIDENT_PATIENT]->(p)
  299. RETURN r
  300. EOCQL
  301.  
  302. my $create_incident_patient_rel_query =
  303.     REST::Neo4p::Query->new($create_incident_patient_rel_cql)
  304.     or die ("Could not create query for : $create_incident_patient_rel_cql");
  305.  
  306. # Create Degree Of Harm
  307. my $create_incident_doh_cql =<<'EOCQL';
  308. MATCH (ndt:NRLS_DATA_TYPE)
  309. USING INDEX ndt:NRLS_DATA_TYPE(code)
  310. WHERE ndt.code = { code }
  311. CREATE UNIQUE
  312. (doh:DEGREE_OF_HARM { degree_of_harm : { degree_of_harm }
  313. })-[:HAS_NRLS_DATA_TYPE]->(ndt) RETURN doh.degree_of_harm
  314. EOCQL
  315.  
  316. my $create_incident_doh_query =
  317.     REST::Neo4p::Query->new($create_incident_doh_cql)
  318.     or die ("Could not create query for : $create_incident_doh_cql");
  319.  
  320. # Create Incident/Degree Of Harm relationship
  321. my $create_incident_doh_rel_cql =<<'EOCQL';
  322. MATCH (i:INCIDENT), (doh:DEGREE_OF_HARM)
  323. USING INDEX doh:DEGREE_OF_HARM(degree_of_harm)
  324. USING INDEX i:INCIDENT(incident_id)
  325. WHERE i.incident_id = { incident_id }
  326. AND doh.degree_of_harm = { degree_of_harm }
  327. CREATE UNIQUE
  328. (i)-[r:HAS_INCIDENT_DEGREE_OF_HARM]->(doh) RETURN r
  329. EOCQL
  330.  
  331. my $create_incident_doh_rel_query =
  332.     REST::Neo4p::Query->new($create_incident_doh_rel_cql)
  333.     or die ("Could not create query for : $create_incident_doh_rel_cql");
  334.  
  335. say "Done creating REST::Neo4p::Query objects " , scalar localtime if defined($debug);
  336.  
  337. # Place holders
  338. my (%nodes, %relationships, %created_nhs_trust_location);
  339.  
  340. my $created_nhs_trust_node;
  341.  
  342. say "Beginning processing loop " , scalar localtime if defined($debug);
  343.  
  344. my $track_count = defined($debug) ? 100 : 500;
  345.  
  346. while (my $row = $worksheet->next_row) {
  347.     my @values = $row->values();
  348.     if (@values == 0) {
  349.         warn ("Row at ", $row->row_number() , " contains no values.");
  350.     }
  351.  
  352.     @values = map {
  353.         if ($_ =~ /^ ?$/ or !defined($_)) {
  354.             $_ = "NOT_DEFINED";
  355.         } else {
  356.             $_;
  357.         }
  358.     } @values;
  359.  
  360.     my ($nhs_trust, $nhs_trust_location, $degree_of_harm,
  361.         $patient_age, $patient_sex, $patient_ethnicity,
  362.         $incident_id, $incident_date,
  363.         $incident_hour, $incident_description,
  364.         $incident_category, $incident_reporter,
  365.         $incident_speciality);
  366.  
  367.     my ($last_known_query, $neo4p_query, $need_rollback);
  368.  
  369.     try {
  370.        
  371.         $nhs_trust =            $values[$header_map{"RP07"}];
  372.         $nhs_trust_location =   $values[$header_map{"IN03_lvl1"}];
  373.         $degree_of_harm =       $values[$header_map{"PD09"}];
  374.         $patient_age =          $values[$header_map{"Age_at_Incident"}];
  375.         $patient_sex =          $values[$header_map{"PD02"}];
  376.         $patient_ethnicity =    $values[$header_map{"PD11"}];
  377.         $incident_id =          $values[$header_map{"incidentID"}];
  378.         $incident_date =        $values[$header_map{"IN01"}];
  379.         $incident_hour =        $values[$header_map{"IN02_A_01"}];
  380.         $incident_description = $values[$header_map{"IN07"}];
  381.         $incident_category =    $values[$header_map{"IN05_lvl1"}];
  382.         $incident_reporter =    $values[$header_map{"ST01_LVL1"}];
  383.         $incident_speciality =  $values[$header_map{"PD05_lvl1"}];
  384.  
  385.         # Normalize double quotes in incident description
  386.         $incident_description =~ s/"/\\"/g;
  387.  
  388.         # Assign unique trust id just to ensure uniqueness on set
  389.         my $trust_incident_id = $incident_id . "_" . $nhs_trust;
  390.  
  391.         # Normalize incident hour
  392.         if ($incident_hour eq 'NOT_DEFINED') {
  393.             $incident_hour = '00';
  394.         } else {
  395.             $incident_hour = substr($incident_hour, 0, 2);
  396.         }
  397.  
  398.         # Normalize date
  399.         my $incident_timestamp = DateTime::Format::Excel
  400.             ->parse_datetime($incident_date)->ymd . "T" .
  401.             $incident_hour . ":00:00";
  402.  
  403.         # Begin transaction
  404.         REST::Neo4p->begin_work;
  405.         $need_rollback = 1;
  406.        
  407.         # Build nodes
  408.  
  409.         # Create nodes
  410.        
  411.         # Create unique NHS Trust node
  412.         if (!defined($created_nhs_trust_node)) {
  413.                      
  414.             $created_nhs_trust_node = 1;
  415.            
  416.             ($neo4p_query, $last_known_query) =
  417.                 ($create_nhs_trust_query, $create_nhs_trust_cql);
  418.  
  419.             &run_query(
  420.                 $last_known_query,
  421.                 $neo4p_query,
  422.                 { "code" => "RP07", "name" => $nhs_trust });
  423.         }
  424.  
  425.         if (!defined($created_nhs_trust_location{$nhs_trust_location})) {
  426.             # Create unique NHS Trust Location node
  427.             ($neo4p_query, $last_known_query) =
  428.                 ($create_nhs_trust_location_query
  429.                 , $create_nhs_trust_location_cql);
  430.                
  431.             &run_query(
  432.                 $last_known_query,
  433.                 $neo4p_query,
  434.                 { "location_level_01" => $nhs_trust_location });
  435.            
  436.             # Create relationship between NHS Trust/Location
  437.             ($neo4p_query, $last_known_query) =
  438.                 ($create_nhs_trust_location_rel_query
  439.                 , $create_nhs_trust_location_rel_cql);
  440.                
  441.             &run_query(
  442.                 $last_known_query,
  443.                 $neo4p_query,
  444.                 {"name" => $nhs_trust, "location_level_01" => $nhs_trust_location });
  445.            
  446.             $created_nhs_trust_location{$nhs_trust_location} = 1;
  447.         }
  448.        
  449.         # Create unique Incident node
  450.         ($neo4p_query, $last_known_query) =
  451.             ($create_incident_query, $create_incident_cql);
  452.            
  453.         &run_query(
  454.             $last_known_query,
  455.             $neo4p_query,
  456.             { "incident_id" => $incident_id
  457.             , "incident_timestamp" => $incident_timestamp
  458.             , "incident_description" => $incident_description });
  459.            
  460.         # Create unique Incident node NRLS_DATA_TYPE relationship ('INO7')
  461.         ($neo4p_query, $last_known_query) =
  462.             ($create_incident_ndt_rel_query, $create_incident_ndt_rel_cql);
  463.            
  464.         &run_query(
  465.             $last_known_query,
  466.             $neo4p_query,
  467.             { "incident_id" => $incident_id, "code" => "IN07" });
  468.        
  469.         # Create relationship to NHS Trust ${nhs_trust} ${incident_id}
  470.         ($neo4p_query, $last_known_query) =
  471.             ($create_incident_nhs_trust_rel_query
  472.             , $create_incident_nhs_trust_rel_cql);
  473.            
  474.         &run_query(
  475.             $last_known_query,
  476.             $neo4p_query,
  477.             { "name" => $nhs_trust, "incident_id" => $incident_id });
  478.                    
  479.         # Create relationship to NHS Trust Location
  480.         ($neo4p_query, $last_known_query) =
  481.             ($create_incident_ntl_rel_query, $create_incident_ntl_rel_cql);
  482.            
  483.         &run_query(
  484.             $last_known_query,
  485.             $neo4p_query,
  486.             { "location_level_01" => $nhs_trust_location
  487.             , "incident_id" => $incident_id });
  488.        
  489.         # Create unique Incident Category node
  490.         ($neo4p_query, $last_known_query) =
  491.             ($create_incident_category_query, $create_incident_category_cql);
  492.            
  493.         &run_query(
  494.             $last_known_query,
  495.             $neo4p_query,
  496.             { "code" => "IN05_lvl1"
  497.             , "category_level_01" => $incident_category });
  498.        
  499.         # Create unique Incident Category relationship
  500.         ($neo4p_query, $last_known_query) =
  501.             ($create_incident_category_rel_query
  502.             , $create_incident_category_rel_cql);
  503.            
  504.         &run_query(
  505.             $last_known_query,
  506.             $neo4p_query,
  507.             { "incident_id" => $incident_id
  508.             , "category_level_01" => $incident_category });
  509.        
  510.         # Create unique Incident Speciality node
  511.         ($neo4p_query, $last_known_query) =
  512.             ($create_incident_speciality_query, $create_incident_speciality_cql);
  513.            
  514.         &run_query(
  515.             $last_known_query,
  516.             $neo4p_query,
  517.             { "code" => "PD05_lvl1"
  518.             , "speciality_level_01" => $incident_speciality });
  519.        
  520.         # Create unique Incident Speciality relationship
  521.         ($neo4p_query, $last_known_query) =
  522.             ($create_incident_speciality_rel_query
  523.             , $create_incident_speciality_rel_cql);
  524.            
  525.         &run_query(
  526.             $last_known_query,
  527.             $neo4p_query,
  528.             { "incident_id" => $incident_id
  529.             , "speciality_level_01" => $incident_speciality });
  530.        
  531.         # Create unique Incident Reporter node
  532.         ($neo4p_query, $last_known_query) =
  533.             ($create_incident_reporter_query, $create_incident_reporter_cql);
  534.            
  535.         &run_query(
  536.             $last_known_query,
  537.             $neo4p_query,
  538.             { "code" => "ST01_LVL1"
  539.             , "reporter_level_01" => $incident_reporter });
  540.        
  541.         # Create unique Incident Reporter relationship
  542.         ($neo4p_query, $last_known_query) =
  543.             ($create_incident_reporter_rel_query
  544.             , $create_incident_reporter_rel_cql);
  545.            
  546.         &run_query(
  547.             $last_known_query,
  548.             $neo4p_query,
  549.             { "incident_id" => $incident_id
  550.             , "reporter_level_01" => $incident_reporter });
  551.        
  552.         # Create unique Patient node
  553.         ($neo4p_query, $last_known_query) =
  554.             ($create_incident_patient_query
  555.             , $create_incident_patient_cql);
  556.            
  557.         &run_query(
  558.             $last_known_query,
  559.             $neo4p_query,
  560.             { "code" => "Age_at_Incident"
  561.             , "patient_age" => $patient_age
  562.             , "patient_sex" => $patient_sex
  563.             , "patient_ethnicity" => $patient_ethnicity});
  564.        
  565.         # Create unique Incident Patient relationship
  566.         ($neo4p_query, $last_known_query) =
  567.             ($create_incident_patient_rel_query
  568.             , $create_incident_patient_rel_cql);
  569.            
  570.         &run_query(
  571.             $last_known_query,
  572.             $neo4p_query,
  573.             { "incident_id" => $incident_id
  574.             , "patient_age" => $patient_age
  575.             , "patient_sex" => $patient_sex
  576.             , "patient_ethnicity" => $patient_ethnicity });
  577.        
  578.         # Create unique Degree Of Harm node
  579.         ($neo4p_query, $last_known_query) =
  580.             ($create_incident_doh_query
  581.             , $create_incident_doh_cql);
  582.            
  583.         &run_query(
  584.             $last_known_query,
  585.             $neo4p_query,
  586.             { "code" => "PD09"
  587.             , "degree_of_harm" => $degree_of_harm });
  588.        
  589.         # Create unique Degree Of Harm relationship
  590.         ($neo4p_query, $last_known_query) =
  591.             ($create_incident_doh_rel_query
  592.             , $create_incident_doh_rel_cql);
  593.            
  594.         &run_query(
  595.             $last_known_query,
  596.             $neo4p_query,
  597.             { "incident_id" => $incident_id
  598.             , "degree_of_harm" => $degree_of_harm});
  599.        
  600.         REST::Neo4p->commit
  601.             or die("Could not commit transaction " . $neo4p_query->errstr);
  602.        
  603.         my $results = REST::Neo4p->_tx_results; # Flush results
  604.            
  605.     } catch {
  606.        
  607.         my $current_row_number = $row->row_number() + 1;
  608.         say STDERR "Failure at row number $current_row_number in $input_file : $_.";
  609.         say STDERR "Last known query $last_known_query."
  610.             if (defined($last_known_query));
  611.            
  612.         if (defined($neo4p_query) and defined($neo4p_query->errstr)) {
  613.             say STDERR "Last known neo4p query error: " , $neo4p_query->errstr;
  614.         }
  615.  
  616.         foreach my $required_column (@required_columns) {
  617.             if (defined($header_map{$required_column}) and
  618.                 defined($values[$header_map{$required_column}])) {
  619.                 say STDERR "Column $required_column : " , $values[$header_map{$required_column}];
  620.             }
  621.         }
  622.        
  623.         REST::Neo4p->rollback if defined($need_rollback);
  624.     };
  625.  
  626.     unless ($row->row_number() % $track_count) {
  627.         say "Processed " , $row->row_number() , " rows. " , scalar localtime;
  628.     }
  629. }
  630.  
  631. say "Completed processing loop " , scalar localtime if defined($debug);
  632.  
  633. sub run_query {
  634.     my ($cql, $query, $params) = @_;
  635.    
  636.     if (defined($debug) && $debug eq '10') {
  637.        
  638.         my $cql_with_params = $cql;
  639.         my %params = %{$params};
  640.        
  641.         foreach my $key (keys(%params)) {
  642.             $cql_with_params =~ s/\{ $key \}/'$params{$key}'/;
  643.         }
  644.        
  645.         say "Attempting to execute statement $cql_with_params "
  646.             , scalar localtime;
  647.     }
  648.        
  649.     $query->execute($params)
  650.         or die("Could not execute query $cql");
  651.        
  652.     my $response = $query->fetch
  653.         or die("Could not fetch response for $cql");
  654. }
Advertisement
Add Comment
Please, Sign In to add comment