tripal_bulk_loader.loader.inc 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * Add Loader Job Form
  8. *
  9. * This form is meant to be included on the node page to allow users to submit/re-submit
  10. * loading jobs
  11. *
  12. * @ingroup tripal_bulk_loader
  13. */
  14. function tripal_bulk_loader_add_loader_job_form($form, $form_state, $node) {
  15. $form = array();
  16. // --notify--
  17. if ($node->job_status == 'Loading...') {
  18. drupal_set_message(t("The Loading Summary only updates at the end of each constant set.
  19. Although records may have already been inserted, they won't be available until the
  20. current constant set is full loaded and no errors are encountered.", array()), 'warning');
  21. }
  22. $form['nid'] = array(
  23. '#type' => 'hidden',
  24. '#value' => $node->nid,
  25. );
  26. $form['file'] = array(
  27. '#type' => 'hidden',
  28. '#value' => $node->file
  29. );
  30. $form['job_id'] = array(
  31. '#type' => 'hidden',
  32. '#value' => $node->job_id,
  33. );
  34. $form['submit'] = array(
  35. '#type' => 'submit',
  36. '#value' => ($node->job_id) ? 'Re-Submit Job' : 'Submit Job',
  37. );
  38. $form['submit-cancel'] = array(
  39. '#type' => ($node->job_id)? 'submit' : 'hidden',
  40. '#value' => 'Cancel Job',
  41. );
  42. if ($node->keep_track_inserted) {
  43. $form['submit-revert'] = array(
  44. '#type' => ($node->job_id) ? 'submit' : 'hidden',
  45. '#value' => 'Revert',
  46. );
  47. }
  48. return $form;
  49. }
  50. /**
  51. * Add Loader Job Form (Submit)
  52. *
  53. * @ingroup tripal_bulk_loader
  54. */
  55. function tripal_bulk_loader_add_loader_job_form_submit($form, $form_state) {
  56. global $user;
  57. if (preg_match('/Submit Job/', $form_state['values']['op'])) {
  58. //Submit Tripal Job
  59. $job_args[1] = $form_state['values']['nid'];
  60. if (is_readable($form_state['values']['file'])) {
  61. $fname = basename($form_state['values']['file']);
  62. $job_id = tripal_add_job("Bulk Loading Job: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  63. // add job_id to bulk_loader node
  64. $success = db_update('tripal_bulk_loader')->fields(array(
  65. 'job_id' => $job_id,
  66. 'job_status' => 'Submitted to Queue'
  67. ))->condition('nid',$form_state['values']['nid'])->execute();
  68. }
  69. else {
  70. drupal_set_message(t("Can not open %file. Job not scheduled.", array('%file' => $form_state['values']['file'])));
  71. }
  72. }
  73. elseif (preg_match('/Re-Submit Job/', $form_state['values']['op'])) {
  74. tripal_jobs_rerun($form_state['values']['job_id']);
  75. $success = db_update('tripal_bulk_loader')->fields(array(
  76. 'job_status' => 'Submitted to Queue'
  77. ))->condition('nid',$form_state['values']['nid'])->execute();
  78. }
  79. elseif (preg_match('/Cancel Job/', $form_state['values']['op'])) {
  80. $success = db_update('tripal_bulk_loader')->fields(array(
  81. 'job_status' => 'Job Cancelled'
  82. ))->condition('nid',$form_state['values']['nid'])->execute();
  83. tripal_jobs_cancel($form_state['values']['job_id']);
  84. }
  85. elseif (preg_match('/Revert/', $form_state['values']['op'])) {
  86. // Remove the records from the database that were already inserted
  87. $resource = db_query('SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=:nid ORDER BY tripal_bulk_loader_inserted_id DESC', array(':nid' => $form_state['values']['nid']));
  88. while ($r = $resource->fetchObject()) {
  89. $ids = preg_split('/,/', $r->ids_inserted);
  90. db_query('DELETE FROM {'.$r->table_inserted_into.'} WHERE '.$r->table_primary_key.' IN ('.$r->ids_inserted.')')->execute();
  91. $result = db_query('SELECT true as present FROM {'.$r->table_inserted_into.'} WHERE '.$r->table_primary_key.' IN ('.$r->ids_inserted.')')->fetchObject();
  92. if (!$result->present) {
  93. drupal_set_message(t('Successfully Removed data Inserted into the %tableto table.', array('%tableto' => $r->table_inserted_into)));
  94. db_query('DELETE FROM {tripal_bulk_loader_inserted} WHERE tripal_bulk_loader_inserted_id=:id', array(':id' => $r->tripal_bulk_loader_inserted_id))->execute();
  95. }
  96. else {
  97. drupal_set_message(t('Unable to remove data Inserted into the %tableto table!', array('%tableto' => $r->table_inserted_into)), 'error');
  98. }
  99. }
  100. // reset status
  101. $success = db_update('tripal_bulk_loader')->fields(array(
  102. 'job_status' => 'Reverted -Data Deleted'
  103. ))->condition('nid',$form_state['values']['nid'])->execute();
  104. }
  105. }
  106. /**
  107. * Tripal Bulk Loader
  108. *
  109. * This is the function that's run by tripal_launch_jobs to bulk load chado data.
  110. *
  111. * @param $nid
  112. * The Node ID of the bulk loading job node to be loaded. All other needed data is expected to be
  113. * in the node (ie: template ID and file)
  114. *
  115. * Note: Instead of returning a value this function updates the tripal_bulk_loader.status.
  116. * Errors are thrown through watchdog and can be viewed at admin/reports/dblog.
  117. *
  118. * @ingroup tripal_bulk_loader
  119. */
  120. function tripal_bulk_loader_load_data($nid, $job_id) {
  121. // ensure no timeout
  122. drupal_set_time_limit(0);
  123. // set the status of the job (in the node not the tripal jobs)
  124. db_update('tripal_bulk_loader')
  125. ->fields(array('job_status' => 'Loading...'))
  126. ->condition('nid', $nid)
  127. ->execute();
  128. $node = node_load($nid);
  129. print "Template: " . $node->template->name . " (" . $node->template_id . ")\n";
  130. $total_lines = trim(`wc --lines < $node->file`);
  131. print "File: " . $node->file . " (" . $total_lines . " lines)\n";
  132. //print "\nClearing all prepared statements from previous runs of this loader...\n";
  133. //tripal_core_chado_clear_prepared('_'.$node->nid.'_');
  134. // Prep Work ==================================================================================
  135. print "\nPreparing to load...\n";
  136. $loaded_without_errors = TRUE;
  137. // Generate default values array
  138. $default_data = array();
  139. $field2column = array();
  140. $record2priority = array();
  141. $tables = array();
  142. $template_array = $node->template->template_array;
  143. // first build the record2priority array
  144. foreach ($template_array as $priority => $record_array) {
  145. $record2priority[$record_array['record_id']] = $priority;
  146. }
  147. //
  148. foreach ($template_array as $priority => $record_array) {
  149. if (!is_array($record_array)) {
  150. continue;
  151. }
  152. // Add tables being inserted into to a list to be treated differently
  153. // this is used to acquire locks on these tables
  154. if (preg_match('/insert/', $record_array['mode'])) {
  155. $tables[$record_array['table']] = $record_array['table'];
  156. }
  157. // iterate through each of the fiels for the current record and
  158. // set the default_data array
  159. foreach ($record_array['fields'] as $field_index => $field_array) {
  160. $default_data[$priority]['table'] = $record_array['table'];
  161. $default_data[$priority]['mode'] = ($record_array['mode']) ? $record_array['mode'] : 'insert';
  162. $default_data[$priority]['select_if_duplicate'] = ($record_array['select_if_duplicate']) ? $record_array['select_if_duplicate'] : 0;
  163. $default_data[$priority]['update_if_duplicate'] = ($record_array['update_if_duplicate']) ? $record_array['update_if_duplicate'] : 0;
  164. $default_data[$priority]['disabled'] = ($record_array['disable']) ? $record_array['disable'] : 0;
  165. $default_data[$priority]['optional'] = ($record_array['optional']) ? $record_array['optional'] : 0;
  166. $default_data[$priority]['select_optional'] = ($record_array['select_optional']) ? $record_array['select_optional'] : 0;
  167. $default_data[$priority]['record_id'] = $record_array['record_id'];
  168. $default_data[$priority]['required'][$field_array['field']] = $field_array['required'];
  169. $one = $default_data[$priority];
  170. if (isset($field_array['regex'])) {
  171. $default_data[$priority]['regex_transform'][$field_array['field']] = $field_array['regex'];
  172. }
  173. $two = $default_data[$priority];
  174. if (preg_match('/table field/', $field_array['type'])) {
  175. $default_data[$priority]['values_array'][$field_array['field']] = '';
  176. $default_data[$priority]['need_further_processing'] = TRUE;
  177. $field2column[$priority][$field_array['field']] = $field_array['spreadsheet column'];
  178. }
  179. elseif (preg_match('/constant/', $field_array['type'])) {
  180. $default_data[$priority]['values_array'][$field_array['field']] = $field_array['constant value'];
  181. }
  182. elseif (preg_match('/foreign key/', $field_array['type'])) {
  183. $default_data[$priority]['values_array'][$field_array['field']] = array();
  184. $default_data[$priority]['need_further_processing'] = TRUE;
  185. $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['record'] = $field_array['foreign key'];
  186. // Add in the FK / Referral table
  187. $fk_priority = $record2priority[$field_array['foreign key']];
  188. $fk_table = $template_array[$fk_priority]['table'];
  189. $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['table'] = $fk_table;
  190. // Add in the FK / Referral field
  191. // for backwards compatibility we need to get the FK relationship to find
  192. // out what field we're joining on. For templates created using a
  193. // previous version it was assumed that the FK field was always the field to join
  194. if (!array_key_exists('foreign field', $field_array)) {
  195. $tbl_description = tripal_core_get_chado_table_schema($record_array['table']);
  196. foreach ($tbl_description['foreign keys'] as $key_table => $key_array) {
  197. if ($key_table == $fk_table) {
  198. foreach ($key_array['columns'] as $left_field => $right_field) {
  199. if ($left_field == $field_array['field']) {
  200. $field_array['foreign field'] = $right_field;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. $default_data[$priority]['values_array'][$field_array['field']]['foreign record']['field'] = $field_array['foreign field'];
  207. }
  208. else {
  209. print 'WARNING: Unsupported type: ' . $field_array['type'] . ' for ' . $table . '.' . $field_array['field'] . "!\n";
  210. }
  211. $three = $default_data[$priority];
  212. } // end of foreach field
  213. } //end of foreach record
  214. ///////////////////////////////////////////////
  215. // For each set of constants
  216. ///////////////////////////////////////////////
  217. print "Loading...\n";
  218. $original_default_data = $default_data;
  219. $group_index = 0;
  220. $total_num_groups = sizeof($node->constants);
  221. foreach ($node->constants as $group_id => $set) {
  222. // revert default data array for next set of constants
  223. $default_data = $original_default_data;
  224. $group_index++;
  225. // Add constants
  226. if (!empty($set)) {
  227. print "Constants:\n";
  228. foreach ($set as $priority => $record) {
  229. foreach ($record as $field_id => $field) {
  230. print "\t- " . $field['chado_table'] . '.' . $field['chado_field'] . ' = ' . $field['value'] . "\n";
  231. if ($default_data[$priority]['table'] == $field['chado_table']) {
  232. if (isset($default_data[$priority]['values_array'][$field['chado_field']])) {
  233. if (isset($field2column[$priority][$field['chado_field']])) {
  234. $field2column[$priority][$field['chado_field']] = $field['value'];
  235. }
  236. else {
  237. $default_data[$priority]['values_array'][$field['chado_field']] = $field['value'];
  238. }
  239. }
  240. else {
  241. print "ERROR: Template has changed after constants were assigned!\n";
  242. tripal_bulk_loader_throw_error('Template has changed after constants were assigned', array(), TRIPAL_NOTICE);
  243. exit(1);
  244. }
  245. }
  246. else {
  247. print "ERROR: Template has changed after constants were assigned!\n";
  248. tripal_bulk_loader_throw_error('Template has changed after constants were assigned', array(), TRIPAL_NOTICE);
  249. exit(1);
  250. }
  251. }
  252. }
  253. }
  254. // Open File
  255. print "\tPreparing to load the current constant set...\n";
  256. print "\t\tOpen File...\n";
  257. try {
  258. $file = new SplFileObject($node->file, 'r');
  259. }
  260. catch (Exception $e) {
  261. tripal_bulk_loader_throw_error('Could not open file %file',
  262. array('%file' => $node->file), TRIPAL_ERROR);
  263. return;
  264. }
  265. // Set defaults
  266. $header = '';
  267. if (preg_match('/(t|true|1)/', $node->file_has_header)) {
  268. $file->next();
  269. $header = $file->current();
  270. }
  271. $num_records = 0;
  272. $num_lines = 0;
  273. $num_errors = 0;
  274. $interval = intval($total_lines * 0.0001);
  275. if ($interval == 0) {
  276. $interval = 1;
  277. }
  278. // Start Transaction
  279. $savepoint = '';
  280. switch (variable_get('tripal_bulk_loader_transactions', 'row')) {
  281. case "none":
  282. break;
  283. case "all":
  284. print "\t\tStart Transaction...\n";
  285. $TRANSACTION = db_transaction();
  286. $transactions = TRUE;
  287. break;
  288. case "row":
  289. print "\t\tStart Transaction...\n";
  290. $TRANSACTION = db_transaction();
  291. $transactions = TRUE;
  292. $new_transaction_per_row = TRUE;
  293. break;
  294. }
  295. // Disable triggers
  296. /**
  297. $triggers_disabled = FALSE;
  298. if ($transactions AND variable_get('tripal_bulk_loader_disable_triggers', TRUE)) {
  299. print "\t\tDefer Constraints...\n";
  300. $triggers_disabled = TRUE;
  301. chado_query("SET CONSTRAINTS ALL DEFERRED");
  302. }
  303. */
  304. // Acquire Locks
  305. /**
  306. if ($transactions) {
  307. print "\t\tAcquiring Table Locks...\n";
  308. $lockmode = variable_get('tripal_bulk_loader_lock', 'ROW EXCLUSIVE');
  309. foreach ($tables as $table) {
  310. print "\t\t\t$lockmode for $table\n";
  311. chado_query("LOCK TABLE ".$table." IN ".$lockmode." MODE");
  312. }
  313. }
  314. */
  315. print "\tLoading the current constant set...\n";
  316. tripal_bulk_loader_progress_bar(0, $total_lines);
  317. while (!$file->eof()) {
  318. $file->next();
  319. $raw_line = $file->current();
  320. $raw_line = trim($raw_line);
  321. if (empty($raw_line)) {
  322. continue;
  323. } // skips blank lines
  324. $line = explode("\t", $raw_line);
  325. $num_lines++;
  326. // update the job status every 1% of lines processed for the current group
  327. if ($node->job_id and $num_lines % $interval == 0) {
  328. // percentage of lines processed for the current group
  329. $group_progress = round(($num_lines / $total_lines) * 100);
  330. tripal_bulk_loader_progress_bar($num_lines, $total_lines);
  331. // percentage of lines processed for all groups
  332. // <previous group index> * 100 + <current group progress>
  333. // --------------------------------------------------------
  334. // <total number of groups>
  335. // For example, if you were in the third group of 3 constant sets
  336. // and had a group percentage of 50% then the job progress would be
  337. // (2*100 + 50%) / 3 = 250%/3 = 83%
  338. $job_progress = round(((($group_index - 1) * 100) + $group_progress) / $total_num_groups);
  339. tripal_job_set_progress($node->job_id, $job_progress);
  340. }
  341. $data = $default_data;
  342. // iterate through each record and process the line
  343. $data_keys = array_keys($data);
  344. foreach ($data_keys as $priority) {
  345. $options = array(
  346. 'field2column' => $field2column,
  347. 'record2priority' => $record2priority,
  348. 'line' => $line,
  349. 'line_num' => $num_lines,
  350. 'group_index' => $group_index,
  351. 'node' => $node,
  352. 'nid' => $node->nid,
  353. );
  354. // execute all records that are not disabled
  355. $no_errors = FALSE;
  356. if (array_key_exists($priority, $data) and
  357. array_key_exists('disabled', $data[$priority]) and
  358. $data[$priority]['disabled'] == 0) {
  359. $no_errors = process_data_array_for_line($priority, $data, $default_data, $options);
  360. }
  361. else {
  362. // set status to true for skipped records
  363. $no_errors = TRUE;
  364. }
  365. tripal_bulk_loader_progress_file_track_job($job_id, $no_errors);
  366. $failed = FALSE;
  367. if ($no_errors == FALSE) {
  368. // Encountered an error
  369. if ($transactions) {
  370. $TRANSACTION->rollback();
  371. }
  372. tripal_bulk_loader_finish_loading($node->nid, FALSE);
  373. break;
  374. }
  375. } // end of foreach table in default data array
  376. tripal_bulk_loader_progress_file_track_job($job_id, FALSE, TRUE);
  377. if ($failed) {
  378. $TRANSACTION->rollback();
  379. tripal_bulk_loader_finish_loading($node->nid, FALSE);
  380. break;
  381. }
  382. else {
  383. // Row inserted successfully
  384. if ($transactions && $new_transaction_per_row) {
  385. // commit current transaction and start a new one
  386. unset($TRANSACTION);
  387. $TRANSACTION = db_transaction();
  388. }
  389. }
  390. } //end of foreach line of file
  391. // END Transaction
  392. if ($transactions) {
  393. unset($TRANSACTION);
  394. }
  395. if ($failed) {
  396. $loaded_without_errors = FALSE;
  397. break;
  398. }
  399. tripal_bulk_loader_progress_bar($total_lines, $total_lines);
  400. tripal_bulk_loader_progress_file_track_job($job_id, FALSE, FALSE, TRUE);
  401. } //end of foreach constant set
  402. tripal_bulk_loader_finish_loading($node->nid, $loaded_without_errors);
  403. }
  404. /**
  405. * Process the data array for a given line
  406. *
  407. * @param $addt
  408. * Requires: field2column', 'record2priority', 'line', 'line_num',
  409. * 'group_index', 'node', 'nid'
  410. *
  411. * @ingroup tripal_bulk_loader
  412. */
  413. function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
  414. //$time_start = microtime(true);
  415. $table_data = $data[$priority];
  416. $addt = (object) $addt;
  417. $no_errors = TRUE;
  418. $table = $table_data['table'];
  419. $values = $table_data['values_array'];
  420. // populate the values array with real value either from the input data file line
  421. // or from the foreign key / referral record
  422. if (array_key_exists('need_further_processing', $table_data) and $table_data['need_further_processing']) {
  423. if (array_key_exists($priority, $addt->field2column)) {
  424. $values = tripal_bulk_loader_add_spreadsheetdata_to_values($values, $addt->line, $addt->field2column[$priority]);
  425. }
  426. $values = tripal_bulk_loader_add_foreignkey_to_values($table_data, $values, $data, $addt->record2priority, $addt->nid, $priority, $default_data);
  427. }
  428. $values = tripal_bulk_loader_regex_tranform_values($values, $table_data, $addt->line);
  429. if (!$values) {
  430. //tripal_bulk_loader_throw_error('Line ' . $addt->line_num . ' Regex:<pre>' . print_r($values, TRUE) . print_r($table_data, TRUE) . '</pre>' . '</pre>', array(), TRIPAL_NOTICE);
  431. }
  432. // get the table description
  433. $table_desc = tripal_core_get_chado_table_schema($table);
  434. if (!$table_desc) {
  435. $msg = "Tripal does not know about the table named '%table'. If this is a custom table,
  436. please define it first";
  437. tripal_bulk_loader_throw_error($msg, array('%table' => $table), TRIPAL_ERROR);
  438. $data[$priority]['error'] = TRUE;
  439. return;
  440. }
  441. // Check that template required fields are present. if a required field is
  442. // missing and this
  443. // is an optional record then just return. otherwise raise an error
  444. $skip_optional = 0;
  445. foreach ($table_data['required'] as $field => $required) {
  446. if ($required) {
  447. // check if the field has no value (or array is empty)
  448. if (!isset($values[$field]) or
  449. (is_array($values[$field]) and count($values[$field]) == 0)) {
  450. // check if the record is optional. For backwards compatiblity we need to
  451. // check if the 'mode' is set to 'optional'
  452. if ($table_data['optional'] or preg_match('/optional/', $table_data['mode']) or
  453. $table_data['select_optional']) {
  454. $skip_optional = 1;
  455. // set the values array to be empty since we all required fields are
  456. // optional and we can't do a select/insert so we don't want to keep
  457. // the values if this record is used in a later FK relationship.
  458. $values = array();
  459. }
  460. else {
  461. $msg = "Line " . $addt->line_num . ' "' . $table_data['record_id'] .
  462. '" (' . $table_data['mode'] . ') Missing template required value: ' . $table . '.' . $field;
  463. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_WARNING);
  464. $data[$priority]['error'] = TRUE;
  465. $no_errors = FALSE;
  466. }
  467. }
  468. }
  469. }
  470. // for an insert, check that all database required fields are present in the values array
  471. // we check for 'optional' in the mode for backwards compatibility. The 'optional'
  472. // mode used to be a type of insert
  473. if (!$skip_optional and (preg_match('/insert/', $table_data['mode']) or
  474. preg_match('/optional/', $table_data['mode']))) {
  475. // Check all database table required fields are set
  476. $fields = $table_desc['fields'];
  477. foreach ($fields as $field => $def) {
  478. // a field is considered missing if it cannot be null and there is no default
  479. // value for it or it is not of type 'serial'
  480. if (array_key_exists('not null', $def) and $def['not null'] == 1 and // field must have a value
  481. !array_key_exists($field, $values) and // there is not a value for it
  482. !array_key_exists('default', $def) and // there is no default for it
  483. strcmp($def['type'], 'serial') != 0) { // it is not a 'serial' type column
  484. $msg = "Line " . $addt->line_num . ' ' . $table_data['record_id'] .
  485. ' (' . $table_data['mode'] . ') Missing Database Required Value: ' . $table . '.' . $field;
  486. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_ERROR);
  487. $data[$priority]['error'] = TRUE;
  488. $no_errors = FALSE;
  489. }
  490. }
  491. }
  492. // add updated values array into the data array
  493. $data[$priority]['values_array'] = $values;
  494. // if there was an error already -> don't insert
  495. if (array_key_exists('error', $data[$priority]) and $data[$priority]['error']) {
  496. tripal_bulk_loader_throw_error('Skipping processing of %table due to previous errors',array('%table'=>$table),TRIPAL_NOTICE);
  497. return $no_errors;
  498. }
  499. // skip optional fields
  500. if ($skip_optional) {
  501. tripal_bulk_loader_throw_error('Skipping an optional record (%record)',array('%record'=>$table_data['record_id']),TRIPAL_NOTICE);
  502. return $no_errors;
  503. }
  504. // check if it is already inserted
  505. if (array_key_exists('inserted', $table_data) and $table_data['inserted']) {
  506. tripal_bulk_loader_throw_error('Skipping %record since it is already inserted',array('%record'=>$table_data['record_id']),TRIPAL_NOTICE);
  507. return $no_errors;
  508. }
  509. // check if it is already selected, if so, just get the value stored in
  510. // the default_data array
  511. if (array_key_exists('selected', $table_data) and $table_data['selected']) {
  512. $data[$priority]['values_array'] = $default_data[$priority]['values_array'];
  513. tripal_bulk_loader_throw_error('%record was already selected thus we are just returning the values previously selected.',array('%record'=>$table_data['record_id']),TRIPAL_NOTICE);
  514. return $no_errors;
  515. }
  516. // make sure we have some value in the select_if_duplicate and update_if_duplicate options
  517. if (!array_key_exists('select_if_duplicate', $table_data)) {
  518. $table_data['select_if_duplicate'] = 0;
  519. }
  520. if (!array_key_exists('update_if_duplicate', $table_data)) {
  521. $table_data['update_if_duplicate'] = 0;
  522. }
  523. // if "select if duplicate" is enabled then check to ensure unique constraint is not violated.
  524. // If it is violated then simply return, the record already exists in the database.
  525. // We check for "insert_unique" for backwards compatibilty but that mode no longer exists
  526. $data[$priority]['is_duplicate'] = 0;
  527. if (preg_match('/insert_unique/', $table_data['mode']) or
  528. $table_data['select_if_duplicate'] == 1 or
  529. $table_data['update_if_duplicate'] == 1) {
  530. $options = array('is_duplicate' => TRUE, 'print_errors' => TRUE);
  531. $duplicate = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, $options);
  532. // if this is a duplicate then substitute the values in the table_data array so
  533. // that for future records that may depend on this one, they can get the values needed
  534. if ($duplicate and is_array($duplicate) and count($duplicate) == 1) {
  535. $dup_record = $duplicate[0];
  536. // save the duplicate record for later. If this is an update_if_duplicate
  537. // then we'll need this record as the match
  538. $data[$priority]['is_duplicate'] = (array) $dup_record;
  539. // if all we have is one field then we will just use the value returned
  540. // rather than create an array of values. This way it will prevent
  541. // the tripal_core_chado_(select|insert|update) from recursing on
  542. // foreign keys and make the loader go faster.
  543. if (count((array) $dup_record) == 1) {
  544. foreach ($dup_record as $key => $value) {
  545. $data[$priority]['values_array'] = $value;
  546. }
  547. }
  548. // if we have multiple fields returned then we need to set the values
  549. // the new array.
  550. else {
  551. // convert object to array
  552. $new_values = array();
  553. foreach ($dup_record as $key => $value) {
  554. $new_values[$key] = $value;
  555. }
  556. $data[$priority]['values_array'] = $new_values;
  557. }
  558. // return if this is a select_if_duplicate
  559. if ($table_data['select_if_duplicate'] == 1) {
  560. tripal_bulk_loader_throw_error('Simply returning values for %record since it was already inserted',array('%record'=>$table_data['record_id']),TRIPAL_NOTICE);
  561. return $no_errors;
  562. }
  563. }
  564. }
  565. else {
  566. # TODO: what to do if there are more than one value returned when
  567. # checking for a duplicate?
  568. }
  569. if (!preg_match('/select/', $table_data['mode'])) {
  570. // Use prepared statement?
  571. if (variable_get('tripal_bulk_loader_prepare', TRUE)) {
  572. $options = array('statement_name' => 'record_' . $addt->nid . '_' . $priority);
  573. if (($addt->line_num > 1 && $addt->group_index == 1) OR $addt->group_index > 1) {
  574. //$options['is_prepared'] = TRUE;
  575. }
  576. }
  577. else {
  578. $options = array();
  579. }
  580. // Skip tripal_core_chado_insert() built-in validation?
  581. if (variable_get('tripal_bulk_loader_skip_validation', FALSE)) {
  582. $options['skip_validation'] = TRUE;
  583. }
  584. if ($table_data['update_if_duplicate'] == 1) {
  585. if (array_key_exists('statement_name', $options)) {
  586. $options['statement_name'] = 'upd_' . $options['statement_name'];
  587. }
  588. // This should have been set on the first round of inserts for this record
  589. $match = $data[$priority]['is_duplicate'];
  590. // However, sometimes there is a pre-existing record before the loader starts
  591. // Thus check that this value is set and if not, then generate a match array
  592. // based on the unique keys for this record.
  593. if (empty($match)) {
  594. $match = array();
  595. // First check to see if we have fields for the primary key
  596. foreach ($table_desc['primary key'] as $k_field) {
  597. if (!empty($values[$k_field])) {
  598. $match[$k_field] = $values[$k_field];
  599. }
  600. }
  601. // Otherwise check the fields that are part of the unique key
  602. if (empty($match)) {
  603. foreach ($table_desc['unique keys'] as $u_keys) {
  604. foreach ($u_keys as $u_field) {
  605. if (!empty($values[$u_field])) {
  606. $match[$u_field] = $values[$u_field];
  607. }
  608. }
  609. }
  610. }
  611. }
  612. if (!empty($match)) {
  613. // Now we need to check if it already exists via a select
  614. $results = tripal_core_chado_select($table, array_keys($table_desc['fields']), $match, array('print_errors' => TRUE));
  615. // If not then insert
  616. if (empty($results)) {
  617. $options['statement_name'] = 'ins_'.$options['statement_name'];
  618. $options['print_errors'] = TRUE;
  619. $record = tripal_core_chado_insert($table, $values, $options);
  620. }
  621. else {
  622. $options['return_record'] = TRUE;
  623. $options['print_errors'] = TRUE;
  624. $record = tripal_core_chado_update($table, $match, $values, $options);
  625. }
  626. }
  627. else {
  628. $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' .
  629. $table_data['mode'] . ') Unable to update record since none of the unique key or primary key fields were available ' .
  630. ' where values:' . print_r($values, TRUE);
  631. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_ERROR);
  632. $data[$priority]['error'] = TRUE;
  633. $no_errors = FALSE;
  634. }
  635. }
  636. else {
  637. $options['print_errors'] = TRUE;
  638. $record = tripal_core_chado_insert($table, $values, $options);
  639. }
  640. // if the insert was not successful
  641. if (!$record) {
  642. $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' .
  643. $table_data['mode'] . ') Unable to insert record into ' . $table .
  644. ' where values:' . print_r($values, TRUE);
  645. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_ERROR);
  646. $data[$priority]['error'] = TRUE;
  647. $no_errors = FALSE;
  648. }
  649. // if the insert was succesful
  650. else {
  651. // if mode=insert_once then ensure we only insert it once
  652. if (preg_match('/insert_once/', $table_data['mode'])) {
  653. $default_data[$priority]['inserted'] = TRUE;
  654. }
  655. // add to tripal_bulk_loader_inserted
  656. if ($addt->node->keep_track_inserted) {
  657. $insert_record = db_query(
  658. "SELECT * FROM {tripal_bulk_loader_inserted} WHERE table_inserted_into=:table AND nid=:nid",
  659. array(
  660. ':table' => $table,
  661. 'nid' => $addt->nid
  662. ))->fetchObject();
  663. if ($insert_record) {
  664. $insert_record->ids_inserted .= ',' . $record[$table_desc['primary key'][0] ];
  665. drupal_write_record('tripal_bulk_loader_inserted', $insert_record, 'tripal_bulk_loader_inserted_id');
  666. //print 'Update: '.print_r($insert_record,TRUE)."\n";
  667. //return $no_errors;
  668. }
  669. else {
  670. $insert_record = array(
  671. 'nid' => $addt->nid,
  672. 'table_inserted_into' => $table,
  673. 'table_primary_key' => $table_desc['primary key'][0],
  674. 'ids_inserted' => $record[ $table_desc['primary key'][0] ],
  675. );
  676. //print 'New: '.print_r($insert_record,TRUE)."\n";
  677. $success = drupal_write_record('tripal_bulk_loader_inserted', $insert_record);
  678. //return $no_errors;
  679. }//end of if insert record
  680. }// end of if keeping track of records inserted
  681. // substitute the values array for the primary key if it exists
  682. // and is a single field
  683. if (array_key_exists('primary key', $table_desc)) {
  684. if (count($table_desc['primary key']) == 1) {
  685. $pkey_field = $table_desc['primary key'][0];
  686. $data[$priority]['values_array'] = $record[$pkey_field];
  687. }
  688. }
  689. else {
  690. //add changes back to values array
  691. $data[$priority]['values_array'] = $record;
  692. $values = $record;
  693. }
  694. } //end of if insert was successful
  695. }
  696. // perform a select
  697. else {
  698. // get the matches for this select
  699. $matches = array();
  700. if (is_array($values) and count($values) > 0) {
  701. $matches = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, array('print_errors' => TRUE));
  702. }
  703. // if the record doesn't exist and it's not optional then generate an error
  704. if (count($matches) == 0) {
  705. // No record on select
  706. if ($table_data['select_optional'] != 1) {
  707. $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') No Matching record in ' . $table . ' where values:' . print_r($values, TRUE);
  708. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_ERROR);
  709. $data[$priority]['error'] = TRUE;
  710. $no_errors = FALSE;
  711. }
  712. // there is no match and select optional is turned on, so we want to set
  713. // the values to empty for any records with an FK relationship on this one
  714. else {
  715. $data[$priority]['values_array'] = NULL;
  716. }
  717. }
  718. // if we have more than one record matching and this select isn't optional then fail
  719. if (count($matches) > 1) {
  720. if ($table_data['select_optional'] != 1) {
  721. $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') Too many matching records in ' . $table . ' where values:' . print_r($values, TRUE);
  722. tripal_bulk_loader_throw_error($msg, array(), TRIPAL_WARNING);
  723. $data[$priority]['error'] = TRUE;
  724. $no_errors = FALSE;
  725. }
  726. // there are too many matches and this is an optional select so set
  727. // the values to empty for any records with an FK relationship on this one
  728. else {
  729. $data[$priority]['values_array'] = NULL;
  730. }
  731. }
  732. // if mode=select_once then ensure we only select it once
  733. if (preg_match('/select_once/', $table_data['mode'])) {
  734. $default_data[$priority]['selected'] = TRUE;
  735. // save the pkey
  736. if (array_key_exists('primary key', $table_desc)) {
  737. $new_values = array();
  738. foreach ($matches[0] as $key => $value) {
  739. $new_values[$key] = $value;
  740. }
  741. $default_data[$priority]['values_default'] = $new_values;
  742. }
  743. }
  744. }
  745. return $no_errors;
  746. }
  747. /**
  748. * This function adds the file data to the values array
  749. *
  750. * @param $values
  751. * The default values array -contains all constants
  752. * @param $line
  753. * An array of values for the current line
  754. * @param $field2column
  755. * An array mapping values fields to line columns
  756. * @return
  757. * Supplemented values array
  758. *
  759. * @ingroup tripal_bulk_loader
  760. */
  761. function tripal_bulk_loader_add_spreadsheetdata_to_values($values, $line, $field2column) {
  762. foreach ($values as $field => $value) {
  763. if (is_array($value)) {
  764. continue;
  765. }
  766. $column = $field2column[$field] - 1;
  767. if ($column < 0) {
  768. continue;
  769. }
  770. if (preg_match('/\S+/', $line[$column])) {
  771. $values[$field] = $line[$column];
  772. }
  773. else {
  774. unset($values[$field]);
  775. }
  776. }
  777. return $values;
  778. }
  779. /**
  780. * Handles foreign keys in the values array.
  781. *
  782. * Specifically, if the value for a field is an array then it is assumed that the array contains
  783. * the name of the record whose values array should be substituted here. Thus the foreign
  784. * record is looked up and the values array is substituted in.
  785. *
  786. * @ingroup tripal_bulk_loader
  787. */
  788. function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $data, $record2priority, $nid, $priority, $default_data) {
  789. // iterate through each field in the $values arrray and
  790. // substitute any values for FK / referring fields
  791. foreach ($values as $field => $value) {
  792. // if the field value is an array then it is an FK
  793. if (is_array($value)) {
  794. // get the name and priority of the foreign record
  795. $foreign_record = $value['foreign record']['record'];
  796. $foreign_priority = $record2priority[$foreign_record];
  797. $foreign_table = $value['foreign record']['table'];
  798. $foreign_field = $value['foreign record']['field'];
  799. // get the values of the foreign record and substitute those for the values
  800. $foreign_values = $data[$foreign_priority]['values_array'];
  801. // check to see if we have any default values in the $default_data array
  802. // these were populated from select statements that only need to run once
  803. // so we can reuse the values from those previous selects.
  804. if (array_key_exists($foreign_priority, $default_data) and
  805. array_key_exists('values_default', $default_data[$foreign_priority]) and
  806. array_key_exists($foreign_field, $default_data[$foreign_priority]['values_default'])) {
  807. $values[$field] = $default_data[$foreign_priority]['values_default'][$foreign_field];
  808. continue;
  809. }
  810. // if the field in the Referral records is in a FK relationship with
  811. // this field then we can simply keep the value we have
  812. $tbl_description = tripal_core_get_chado_table_schema($table_array['table']);
  813. if ($tbl_description and
  814. array_key_exists('foreign keys', $tbl_description) and
  815. array_key_exists($foreign_table, $tbl_description['foreign keys']) and
  816. array_key_exists($field, $tbl_description['foreign keys'][$foreign_table]['columns']) and
  817. $foreign_field == $tbl_description['foreign keys'][$foreign_table]['columns'][$field]) {
  818. if (sizeof($foreign_values) == 1 && isset($foreign_values->currval)) {
  819. $foreign_values = $foreign_values->currval;
  820. }
  821. $values[$field] = $foreign_values;
  822. }
  823. // if the field in the Referral records is not in an FK relationship
  824. // with this field then we we have to get the requested value, we must
  825. // return only a single value
  826. else {
  827. // if the current value of the referral records is a non-array then this
  828. // is the primary key, we can use it to select the value we need.
  829. $fk_description = tripal_core_get_chado_table_schema($foreign_table);
  830. if (!is_array($foreign_values)) {
  831. // if we have a value then use it to get the field we need
  832. if ($foreign_values) {
  833. $fvalues = array($fk_description['primary key'][0] => $foreign_values);
  834. $columns = array($foreign_field);
  835. $options = array('statement_name' => 'pk_' . $foreign_table);
  836. $options['print_errors'] = TRUE;
  837. $record = tripal_core_chado_select($foreign_table, $columns, $fvalues, $options);
  838. if ($record) {
  839. $values[$field] = $record[0]->$foreign_field;
  840. }
  841. else {
  842. unset($values[$field]);
  843. }
  844. }
  845. // if we don't have a value then there's nothing we can do so
  846. // set this value to nothing as well
  847. else {
  848. unset($values[$field]);
  849. }
  850. }
  851. // if the current value is an array and our field is not in it, then
  852. // we need to select a value for our field.
  853. else {
  854. $fvalues = $foreign_values;
  855. $columns = array($foreign_field);
  856. $options = array('statement_name' => 'blk_' . $nid . $priority . $foreign_table);
  857. $options['print_errors'] = TRUE;
  858. $record = tripal_core_chado_select($foreign_table, $columns, $fvalues, $options);
  859. if ($record) {
  860. $values[$field] = $record[0]->$foreign_field;
  861. }
  862. else {
  863. unset($values[$field]);
  864. }
  865. } // end else from: if (!is_array($foreign_values) ...
  866. } // end else from: if ($tbl_description ...
  867. } // end if(is_array($value)) ...
  868. } // end foreach ($values ...
  869. // return the updated field values
  870. return $values;
  871. }
  872. /**
  873. * Uses a supplied regex to transform spreadsheet values
  874. *
  875. * @param $values
  876. * The select/insert values array for the given table
  877. * @param $table_data
  878. * The data array for the given table
  879. *
  880. * @ingroup tripal_bulk_loader
  881. */
  882. function tripal_bulk_loader_regex_tranform_values($values, $table_data, $line) {
  883. if (!array_key_exists('regex_transform', $table_data) or
  884. empty($table_data['regex_transform']) or
  885. !array_key_exists('regex_transform', $table_data) or
  886. !is_array($table_data['regex_transform'])) {
  887. return $values;
  888. }
  889. //watchdog('T_bulk_loader','Regex Transformation:<pre>'.print_r($table_data['regex_transform'], TRUE).'</pre>', array(), TRIPAL_NOTICE);
  890. foreach ($table_data['regex_transform'] as $field => $regex_array) {
  891. if (!array_key_exists('replace', $regex_array) or
  892. !array_key_exists('pattern', $regex_array) or
  893. !is_array($regex_array['replace'])) {
  894. continue;
  895. }
  896. // Check for <#column:\d+#> notation
  897. // if present replace with that column in the current line
  898. foreach ($regex_array['replace'] as $key => $replace) {
  899. if (preg_match_all('/<#column:(\d+)#>/', $replace, $matches)) {
  900. foreach ($matches[1] as $k => $column_num) {
  901. $replace = preg_replace('/' . $matches[0][$k] .'/', $line[$column_num-1], $replace);
  902. }
  903. $regex_array['replace'][$key] = $replace;
  904. }
  905. }
  906. // do the full replacement
  907. $old_value = $values[$field];
  908. $new_value = preg_replace($regex_array['pattern'], $regex_array['replace'], $old_value);
  909. $values[$field] = $new_value;
  910. if ($values[$field] === '') {
  911. unset($values[$field]);
  912. }
  913. //print 'Now:'.$values[$field]."\n";
  914. }
  915. return $values;
  916. }
  917. /**
  918. * Flattens an array up to two levels
  919. * Used for printing of arrays without taking up much space
  920. *
  921. * @ingroup tripal_bulk_loader
  922. */
  923. function tripal_bulk_loader_flatten_array($values) {
  924. $flattened_values = array();
  925. foreach ($values as $k => $v) {
  926. if (is_array($v)) {
  927. $vstr = array();
  928. foreach ($v as $vk => $vv) {
  929. if (drupal_strlen($vv) > 20) {
  930. $vstr[] = $vk . '=>' . drupal_substr($vv, 0, 20) . '...';
  931. }
  932. else {
  933. $vstr[] = $vk . '=>' . $vv;
  934. }
  935. }
  936. $v = '{' . implode(',', $vstr) . '}';
  937. }
  938. elseif (drupal_strlen($v) > 20) {
  939. $v = drupal_substr($v, 0, 20) . '...';
  940. }
  941. $flattened_values[] = $k . '=>' . $v;
  942. }
  943. return implode(', ', $flattened_values);
  944. }
  945. /**
  946. * Used to display loader progress to the user
  947. *
  948. * @ingroup tripal_bulk_loader
  949. */
  950. function tripal_bulk_loader_progress_bar($current=0, $total=100, $size=50) {
  951. $new_bar = FALSE;
  952. $mem = memory_get_usage();
  953. // First iteration
  954. if ($current == 0) {
  955. $new_bar = TRUE;
  956. fputs(STDOUT, "Progress:\n");
  957. }
  958. // Percentage round off for a more clean, consistent look
  959. $percent = sprintf("%.02f", round(($current/$total) * 100, 2));
  960. // percent indicator must be four characters, if shorter, add some spaces
  961. for ($i = strlen($percent); $i <= 4; $i++) {
  962. $percent = ' ' . $percent;
  963. }
  964. $total_size = $size + $i + 3 + 2;
  965. $place = 0;
  966. // if it's not first go, remove the previous bar
  967. if (!$new_bar) {
  968. for ($place = $total_size; $place > 0; $place--) {
  969. // echo a backspace (hex:08) to remove the previous character
  970. //echo "\x08";
  971. }
  972. }
  973. // output the progess bar as it should be
  974. // Start with a border
  975. echo '[';
  976. for ($place = 0; $place <= $size; $place++) {
  977. // output "full" spaces if this portion is completed
  978. if ($place <= ($current / $total * $size)) {
  979. echo '|';
  980. }
  981. else {
  982. // Otherwise empty space
  983. echo '-';
  984. }
  985. }
  986. // End with a border
  987. echo ']';
  988. // end a bar with a percent indicator
  989. echo " $percent%. ($current of $total) Memory: $mem\r";
  990. // if it's the end, add a new line
  991. if ($current == $total) {
  992. echo "\n";
  993. }
  994. }
  995. /**
  996. * Keep track of progress in file rather then database
  997. *
  998. * This provides an alternative method to keep track of progress that doesn't require the
  999. * database. It was needed because you can't switch databases within a transaction...
  1000. * Waiting until the end of a constant set is much too long to wait for any indication
  1001. * that things are working.
  1002. *
  1003. * Each line represents a line processed in the loading file. Each period (.) represents
  1004. * a successfully inserted record.
  1005. *
  1006. * @param $job_id
  1007. * The ID of the current tripal job
  1008. * @param $record_added
  1009. * A boolean indicated whether a record was added successfully
  1010. * @param $line_complete
  1011. * A boolean indicating whether the current line is finished
  1012. * @param $close
  1013. * A boolean indicating that the file should be closed
  1014. *
  1015. * @ingroup tripal_bulk_loader
  1016. */
  1017. function tripal_bulk_loader_progress_file_track_job($job_id, $record_added, $line_complete = FALSE, $close = FALSE) {
  1018. // retrieve the file handle
  1019. $file_handle = variable_get('tripal_bulk_loader_progress_file_handle', NULL);
  1020. // open file for reading if not already
  1021. if (!$file_handle) {
  1022. $file_handle = fopen('/tmp/tripal_bulk_loader_progress-'. $job_id . '.out', 'w');
  1023. variable_set('tripal_bulk_loader_progress_file_handle', $file_handle);
  1024. }
  1025. if ($record_added) {
  1026. fwrite($file_handle, '.');
  1027. }
  1028. if ($line_complete) {
  1029. fwrite($file_handle, "\n");
  1030. }
  1031. // close the file if finished
  1032. if ($close) {
  1033. fclose($file_handle);
  1034. variable_set('tripal_bulk_loader_progress_file_handle', NULL);
  1035. }
  1036. }
  1037. function tripal_bulk_loader_throw_error($msg, $args, $severity) {
  1038. tripal_core_report_error(
  1039. 'tripal_bulk',
  1040. $severity,
  1041. $msg,
  1042. $args,
  1043. array('print' => TRUE)
  1044. );
  1045. }
  1046. function tripal_bulk_loader_finish_loading($nid, $loaded_without_errors) {
  1047. // set the status of the job (in the node not the tripal jobs)
  1048. if ($loaded_without_errors) {
  1049. $status = 'Loading Completed Successfully';
  1050. }
  1051. else {
  1052. $status = 'Errors Encountered';
  1053. }
  1054. db_update('tripal_bulk_loader')
  1055. ->fields(array('job_status' => $status))
  1056. ->condition('nid', $nid)
  1057. ->execute();
  1058. if(!$loaded_without_errors) {
  1059. drush_set_error('BULK_LOAD_FAILED', 'Execution aborted due to errors.');
  1060. exit();
  1061. }
  1062. }