<?php
/**
 * @file
 * @todo Add file header description
 */

/**
 * Implementation of hook_requirements().
 */
function tripal_library_requirements($phase) {
  $requirements = array();
  if ($phase == 'install') {
    // make sure chado is installed
    if (!tripal_core_is_chado_installed()) {
      $requirements ['tripal_library'] = array(
        'title' => "tripal_library",
        'value' => "ERROR: Chado most be installed before this module can be enabled",
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $requirements;
}
/**
 * Implementation of hook_install().
 *
 * @ingroup tripal_library
 */
function tripal_library_install() {
  
  // create the module's data directory
  tripal_create_moddir('tripal_library');

  // add the materialized view
  tripal_library_add_mview_library_feature_count();
  
  // add cvterms
  tripal_library_add_cvterms();
}

/**
 * Implementation of hook_uninstall().
 *
 * @ingroup tripal_library
 */
function tripal_library_uninstall() {
  // Drop the MView table if it exists
  if ($mview_id = tripal_mviews_get_mview_id('library_feature_count')) {
    tripal_mviews_action("delete", $mview_id);
  }
}

/**
 * Implementation of hook_schema().
 *
 * @ingroup tripal_library
 */
function tripal_library_schema() {
  $schema['chado_library'] = array(
    'fields' => array(
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0
      ),
      'library_id' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0
      )
    ),
    'indexes' => array(
      'chado_library_idx1' => array('library_id')
    ),
    'unique keys' => array(
      'chado_library_uq1' => array('nid', 'vid'),
      'chado_library_uq2' => array('vid')
    ),
    'primary key' => array('nid'),
  );
  return $schema;
}

/**
 * @ingroup tripal_library
 */
function tripal_library_add_mview_library_feature_count(){
  $view_name = 'library_feature_count';
  $comment = 'Provides count of feature by type that are associated with all libraries';
  
  $schema = array(
    'table' => $view_name,
    'description' => $comment,
    'fields' => array(
      'library_id' => array(
        'type' => 'int',
        'not null' => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'num_features' => array(
        'type' => 'int',
        'not null' => TRUE,
      ),
      'feature_type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
    ),
    'indexes' => array(
      'library_feature_count_idx1' => array('library_id'),
    ),
  );
  
  $sql = "
    SELECT 
      L.library_id, L.name, 
      count(F.feature_id) as num_features, 
      CVT.name as feature_type 
    FROM library L 
      INNER JOIN library_feature LF  ON LF.library_id = L.library_id 
      INNER JOIN feature F           ON LF.feature_id = F.feature_id 
      INNER JOIN cvterm CVT          ON F.type_id     = CVT.cvterm_id 
    GROUP BY L.library_id, L.name, CVT.name
  ";
  
  tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
}

/**
 * @ingroup tripal_library
 */
function tripal_library_add_cvterms() {
  
  // Insert cvterm 'library_description' into cvterm table of chado
  // database. This CV term is used to keep track of the library
  // description in the libraryprop table.
  tripal_cv_add_cvterm(array('name' => 'library_description', 'def' => 'Description of a library'), 
    'tripal', 0, 1, 'tripal');
  
  // add cvterms for the map unit types
  tripal_cv_add_cvterm(array('name' => 'cdna_library','def' => 'cDNA library'), 
    'tripal_library_types', 0, 1, 'tripal');
  tripal_cv_add_cvterm(array('name' => 'bac_library','def' => 'Bacterial Artifical Chromsome (BAC) library'), 
    'tripal_library_types', 0, 1, 'tripal');
  tripal_cv_add_cvterm(array('name' => 'fosmid_library','def' => 'Fosmid library'), 
    'tripal_library_types', 0, 1, 'tripal');   
  tripal_cv_add_cvterm(array('name' => 'cosmid_library','def' => 'Cosmid library'), 
    'tripal_library_types', 0, 1, 'tripal');
  tripal_cv_add_cvterm(array('name' => 'yac_library','def' => 'Yeast Artificial Chromosome (YAC) library'), 
    'tripal_library_types', 0, 1, 'tripal');
  tripal_cv_add_cvterm(array('name' => 'genomic_library','def' => 'Genomic Library'), 
    'tripal_library_types', 0, 1, 'tripal');
}