<?php

/**
 * @file
 * Contains functions used to install/uninstall tripal_core.
 */

/**
 * Implementation of hook_install().
 *
 * @ingroup tripal_core
 */
function tripal_core_install() {

  // make the data directory for this module
  $data_dir = file_directory_path() . "/tripal";
  if (!file_check_directory($data_dir, FILE_CREATE_DIRECTORY)) {
    $message = "Cannot create directory $data_dir. This module may not ".
               "behave correctly without this directory.  Please  create ".
               "the directory manually or fix the problem and reinstall.";
    drupal_set_message(check_plain($message), 'error');
    watchdog('tripal_core', $message, array(), WATCHDOG_ERROR);
  }

  // create the tables that manage materialized views and jobs
  drupal_install_schema('tripal_core');

}

/**
 *  Update for Drupal 6.x, Tripal 0.4
 *  This update
 *   - adjusts the materialized view by adding status, comment and mv_schema columns
 *   - changes the specs of mv_table, mv_specs and indexed
 *   - creates the tripal_custom_tables table
 *
 * @ingroup tripal_feature
 */
function tripal_core_update_6000() {
  // add additional columsn to the tripal_mviews table
  db_add_field($ret, 'tripal_mviews', 'status', array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE));
  db_add_field($ret, 'tripal_mviews', 'comment', array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE));
  db_add_field($ret, 'tripal_mviews', 'mv_schema', array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE));
  db_change_field($ret, 'tripal_mviews', 'mv_table', 'mv_table', array('type' => 'varchar', 'length' => 128, 'not NULL' => FALSE));
  db_change_field($ret, 'tripal_mviews', 'mv_specs', 'mv_specs', array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE));
  db_change_field($ret, 'tripal_mviews', 'indexed', 'indexed', array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE));

  // create the custom tables table
  $ret = array();
  $schema = tripal_core_custom_tables_schema();
  db_create_table($ret, 'tripal_custom_tables', $schema['tripal_custom_tables']);

  $ret = array(
    '#finished' => 1,
  );

  return $ret;
}

/**
 * Implementation of hook_schema().
 *
 * @ingroup tripal_core
 */
function tripal_core_schema() {

  // get the schemas defined by this install file
  $schema = tripal_core_get_schemas();

  // if this module is already installed and enabled, then we want to provide
  // the schemas for all of the custom tables.  This will allow Views to
  // see the schemas.  We check if the module is installed because during
  // installation we don't want to make these custom tables available as we don't
  // want them created in the Drupal database.  The custom tables go in the
  // Chado database.
  if (db_table_exists('tripal_custom_tables')) {
    $sql = 'SELECT * FROM {tripal_custom_tables}';
    $q = db_query($sql);
    while ($custom = db_fetch_object($q)) {
      $schema[$custom->table_name] = unserialize($custom->schema);
    }
  }

  return $schema;
}

/**
 * Implementation of hook_uninstall().
 *
 * @ingroup tripal_core
 */
function tripal_core_uninstall() {
  drupal_uninstall_schema('tripal_core');
}

/**
 * This function simply defines all tables needed for the module to work
 * correctly.  By putting the table definitions in a separate function we
 * can easily provide the entire list for hook_install or individual
 * tables for an update.
 *
 * @ingroup tripal_core
 */
function tripal_core_get_schemas() {
  $schema = array();

  // get all the various schema parts and join them together
  $temp = tripal_core_jobs_schema();
  foreach ($temp as $table => $arr) {
    $schema[$table] = $arr;
  }
  $temp = tripal_core_mviews_schema();
  foreach ($temp as $table => $arr) {
    $schema[$table] = $arr;
  }
  $temp = tripal_core_custom_tables_schema();
  foreach ($temp as $table => $arr) {
    $schema[$table] = $arr;
  }

  return $schema;
}

/**
 * Describes the Tripal Materialized View (tripal_mviews) table
 * This table keeps track of all materialized views created by Tripal and stored in chado
 *
 * @ingroup tripal_core
 */
function tripal_core_mviews_schema() {
  $schema = array();

  $schema['tripal_mviews'] = array(
    'fields' => array(
      'mview_id'      => array('type' => 'serial', 'unsigned' => TRUE, 'not NULL' => TRUE),
      'name'          => array('type' => 'varchar', 'length' => 255, 'not NULL' => TRUE),
      'modulename'    => array('type' => 'varchar', 'length' => 50, 'not NULL' => TRUE, 'description' => 'The module name that provides the callback for this job'),
      'mv_table'      => array('type' => 'varchar', 'length' => 128, 'not NULL' => FALSE),
      'mv_specs'      => array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE),
      'mv_schema'     => array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE),
      'indexed'       => array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE),
      'query'         => array('type' => 'text', 'size' => 'normal', 'not NULL' => TRUE),
      'special_index' => array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE),
      'last_update'   => array('type' => 'int', 'not NULL' => FALSE, 'description' => 'UNIX integer time'),
      'status'        => array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE),
      'comment'       => array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE),
    ),
    'indexes' => array(
      'mview_id' => array('mview_id')
    ),
    'unique keys' => array(
      'mv_table' => array('mv_table'),
      'mv_name' => array('name'),
    ),
    'primary key' => array('mview_id'),
  );

  return $schema;
}

/**
 * Describes the Tripal Jobs (tripal_jobs) table
 * This table keeps track of all tripal jobs including their current status and is used
 * by tripal_launch_jobs to determine which jobs need to be run
 *
 * @ingroup tripal_core
 */
function tripal_core_jobs_schema() {
  $schema = array();
  $schema['tripal_jobs'] = array(
    'fields' => array(
      'job_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not NULL' => TRUE),
      'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not NULL' => TRUE, 'description' => 'The Drupal userid of the submitee'),
      'job_name' => array('type' => 'varchar', 'length' => 255, 'not NULL' => TRUE),
      'modulename' => array('type' => 'varchar', 'length' => 50, 'not NULL' => TRUE, 'description' => 'The module name that provides the callback for this job'),
      'callback' => array('type' => 'varchar', 'length' => 255, 'not NULL' => TRUE),
      'arguments' => array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE),
      'progress' => array('type' => 'int', 'unsigned' => TRUE, 'default' => 0, 'not NULL' => FALSE, 'description' => 'a value from 0 to 100 indicating percent complete'),
      'status' => array('type' => 'varchar', 'length' => 50, 'not NULL' => TRUE),
      'submit_date' => array('type' => 'int', 'not NULL' => TRUE, 'description' => 'UNIX integer submit time'),
      'start_time' => array('type' => 'int', 'not NULL' => FALSE, 'description' => 'UNIX integer start time'),
      'end_time' => array('type' => 'int', 'not NULL' => FALSE, 'description' => 'UNIX integer end time'),
      'error_msg' => array('type' => 'text', 'size' => 'normal', 'not NULL' => FALSE),
      'pid' => array('type' => 'int', 'unsigned' => TRUE, 'not NULL' => FALSE, 'description' => 'The process id for the job'),
      'priority' => array('type' => 'int', 'unsigned' => TRUE, 'not NULL' => TRUE, 'default' => '0', 'description' => 'The job priority'),
      'mlock' => array('type' => 'int', 'unsigned' => TRUE, 'not NULL' => FALSE, 'description' => 'If set to 1 then all jobs for the module are held until this one finishes'),
      'lock' => array('type' => 'int', 'unsigned' => TRUE, 'not NULL' => FALSE, 'description' => 'If set to 1 then all jobs are held until this one finishes'),
    ),
    'indexes' => array(
      'job_id' => array('job_id'),
      'job_name' => array('job_name')
    ),
    'primary key' => array('job_id'),
  );

  return $schema;
}

/**
 * Describes the Tripal Custom Tables (tripal_custom_tables) table
 * This keeps track of tables created by Tripal and stored in chado that may or may not
 * also be materialized views.
 *
 * @ingroup tripal_core
 */
function tripal_core_custom_tables_schema() {
  $schema = array();
  $schema['tripal_custom_tables'] = array(
    'fields' => array(
      'table_id' => array('type' => 'serial', 'unsigned' => TRUE, 'not NULL' => TRUE),
      'table_name' => array('type' => 'varchar', 'length' => 255, 'not NULL' => TRUE),
      'schema' => array('type' => 'text', 'not NULL' => TRUE),
    ),
    'indexes' => array(
      'table_id' => array('table_id'),
    ),
    'primary key' => array('table_id'),
  );

  return $schema;
}