Browse Source

still working on the field generation for the data collection but web services integration is mostly complete

Shawna Spoor 7 years ago
parent
commit
b0dec477eb

+ 52 - 33
tripal/includes/TripalEntityCollection.inc

@@ -314,31 +314,40 @@ class TripalEntityCollection {
               // Use the api call to get the vocab for the site which will need to be parsed 
               // to find the accession and the download types available.
               $site_vocab = tripal_web_services_vocab_request($site_id);
-              foreach ($site_vocab as $item) {
-                if (is_array($item)) {
-                  foreach ($item as $vocab_term) {
-                    if (!empty($vocab_term['supportedProperty'])) {
-                      $vocab_supported_properties = $vocab_term['supportedProperty'];
-                      if (is_array($vocab_supported_properties)) {
-                        foreach ($vocab_supported_properties as $property) {
-                          if ($property['property'] === $field) {
-                            if (in_array('tripal_formatters', $property)) {
-                              $download_types = $property['tripal_formatters'];
-
-                              foreach ($download_types as $download_type) {
-                                $this->downloaders[$download_type] = $download_type;
-                                continue 7;
+              if (!empty($site_vocab)) {
+                // Because there are multiple sites available we need to differentiate the
+                // different cached vocab structures. 
+                $cache_name = 'tripal_web_services_vocab_' . $site_id['site_id'];
+                // Put it in the cache so we don't need to make repeated calls to it.                
+                cache_set($cache_name, $site_vocab);
+
+                // Now find the tripal formatters in the json data returned.
+                foreach ($site_vocab as $item) {
+                  if (is_array($item)) {
+                    foreach ($item as $vocab_term) {
+                      if (!empty($vocab_term['supportedProperty'])) {
+                        $vocab_supported_properties = $vocab_term['supportedProperty'];
+                        if (is_array($vocab_supported_properties)) {
+                          foreach ($vocab_supported_properties as $property) {
+                            if ($property['property'] === $field) {
+                              if (in_array('tripal_formatters', $property)) {
+                                $download_types = $property['tripal_formatters'];
+
+                                foreach ($download_types as $download_type) {
+                                  $this->downloaders[$download_type] = $download_type;
+                                  continue 7;
+                                }
                               }
                             }
                           }
                         }
-                      }
-                      else {
-                        if (in_array('tripal_formatters', $vocab_supported_properties)) {
-                          $download_types = $vocab_supported_properties['tripal_formatters'];
-                          foreach ($download_types as $download_type) {
-                            $this->downloaders[$download_type] = $download_type;
-                            continue 6;
+                        else {
+                          if (in_array('tripal_formatters', $vocab_supported_properties)) {
+                            $download_types = $vocab_supported_properties['tripal_formatters'];
+                            foreach ($download_types as $download_type) {
+                              $this->downloaders[$download_type] = $download_type;
+                              continue 6;
+                            }
                           }
                         }
                       }
@@ -348,17 +357,19 @@ class TripalEntityCollection {
               }
             }
             else {
-              $field_info = field_info_field_by_id($field);
-              if (!$field_info) {
-                continue;
+              foreach ($field_id as $field) {
+                $field_info = field_info_field_by_id($field);
+                if (!$field_info) {
+                  continue;
+                }
               }
+              $field_name = $field_info['field_name'];
+              $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
+              // API function
+              // All fields should support the Tab and CSV downloaders.
+              $downloaders = array();
+              $this->downloaders += tripal_get_field_field_formatters($field);
             }
-            $field_name = $field_info['field_name'];
-            $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
-            // API function
-            // All fields should support the Tab and CSV downloaders.
-            $downloaders = array();
-            $this->downloaders += tripal_get_field_field_formatters($field);
           }
         }
         else {
@@ -664,7 +675,16 @@ class TripalEntityCollection {
               // Use the api call to get the vocab for the site which will need to be parsed 
               // to find the accession and the download types available.
               if (!empty($site_id)) {
-                $site_vocab = tripal_web_services_vocab_request($site_id);
+                $cache_name = 'tripal_web_services_vocab_' . $site_id;
+                if ($cache = cache_get($cache_name)) {
+                  $site_vocab = $cache->data;
+                }
+                else {
+                  $site_vocab = tripal_web_services_vocab_request($site_id);
+                  if (!empty($site_vocab)){
+                    cache_set('tripal_web_services_vocab', $site_vocab);
+                  }
+                }
                 foreach ($site_vocab as $item) {
                   if (is_array($item)) {
                     foreach ($item as $vocab_term) {
@@ -745,8 +765,7 @@ class TripalEntityCollection {
         }
       }
     }
-    print_r($supported_fields);
-    $downloader = new $formatter($this->bundles, $this->ids, $supported_fields, $outfile, $this->user->uid);
+    $downloader = new $formatter($this->bundles, $this->ids, $supported_fields, $outfile, $this->user->uid, $this->collection_id);
     $downloader->write();
 
   }

+ 168 - 13
tripal/includes/TripalFieldDownloaders/TripalFieldDownloader.inc

@@ -18,6 +18,12 @@ abstract class TripalFieldDownloader {
    */
   protected $bundle_name = '';
 
+  /**
+   * The collection ID
+   */
+  protected $collection_id = NULL;  
+  
+
   /**
    * A set of entity IDs. 
    */
@@ -49,7 +55,7 @@ abstract class TripalFieldDownloader {
    *   a path.
    */
   public function __construct($bundle_name, $ids, $fields = array(),
-      $outfile_name, $uid) {
+      $outfile_name, $uid, $collection_id) {
 
     $user = user_load($uid);
     if (!$user) {
@@ -62,7 +68,8 @@ abstract class TripalFieldDownloader {
     $this->bundle_name = $bundle_name;
     $this->entity_ids = $ids;
     $this->fields = $fields;
-
+    $this->collection_id = $collection_id;
+    
     // Make sure the user directory exists
     $user_dir = 'public://tripal/users/' . $user->uid;
     if (!file_prepare_directory($user_dir, FILE_CREATE_DIRECTORY)) {
@@ -76,7 +83,7 @@ abstract class TripalFieldDownloader {
       return;
     }
 
-    $this->outfile = $user_dir. '/' . $outfile_name;
+    $this->outfile = $user_dir . '/' . $outfile_name;
   }
 
   /**
@@ -106,6 +113,7 @@ abstract class TripalFieldDownloader {
    * Creates the downloadable file.
    */
   public function write() {
+   // print_r("public function write \n \n ");
     global $user;
     $fh = fopen(drupal_realpath($this->outfile), "w");
 
@@ -119,29 +127,176 @@ abstract class TripalFieldDownloader {
       foreach ($this->bundle_name as $bundle) {
         // Set the single bundle name for getting the Header.
         $this->bundle_name = $bundle->bundle_name;
-        $headers = $this->getHeader();
+        /*$headers = $this->getHeader();
         if ($headers) {
           foreach ($headers as $line) {
             fwrite($fh, $line . "\r\n");
           }
-        }
-
+        }*/
+        print_r($this->bundle_name);
         // Determine if the entity is remote or local.
         if (strpos($this->bundle_name, 'bio_data_') !== 0) {
-          // Because this is a remote field we need to construct a fake entity.
-          /*
+          // Get all fields for that remote site/bundle name pairing, put them all in a fake entity
+          // Need Field_id, have bundle_name, need collection_id
+          // Return the bundles from the collection_bundle table.
+          // Get the site id to build the web services call
+          $remote_site_data = db_select('tripal_collection_bundle')
+            ->fields('tripal_collection_bundle', array('site_id', 'fields', 'ids'))
+            ->condition('collection_id', $this->collection_id, '=')
+            ->condition('bundle_name', $this->bundle_name, '=')
+            ->execute()
+            ->fetchAssoc();
+ 
+          $site_id = $remote_site_data['site_id'];
+          $remote_fields = unserialize($remote_site_data['fields']);
+          $remote_ids = unserialize($remote_site_data['ids']);
+          $remote_entities = [];
 
-          $lines = $this->formatEntity($entity_info);
-          foreach ($lines as $line) {
-            fwrite($fh, $line . "\r\n");
-          }*/
+          //Now we have the remote site info we need to check against the passed field and entity ids.
+          foreach ($remote_ids as $remote_id) {
+           // print_r("remote_id \n");
+           // print_r($remote_id);
+            foreach ($this->entity_ids as $entity_id) {
+              if (is_array($entity_id)) {
+                foreach ($entity_id as $entity) {
+                  if (is_array($entity)) {
+                    foreach ($entity as $single_id) {
+                      if ($remote_id == $single_id) {
+                        // Before a request can be made we need to get the entity type
+                        // '@id' contains the bundle_name, so look for that.
+                        if (!empty($remote_id)) {
+                          $cache_name = 'tripal_web_services_vocab_' . $site_id;
+                          if ($cache = cache_get($cache_name)) {
+                            $site_vocab = $cache->data;
+                          }
+                          else {
+                            $site_vocab = tripal_web_services_vocab_request($site_id);
+                            if (!empty($site_vocab)) {
+                              cache_set('tripal_web_services_vocab', $site_vocab);
+                            }
+                          }
+                          // Now we have the vocab we can look for the bundle_name in the @id field
+                          foreach ($site_vocab as $item) {
+                            if (is_array($item)) {
+                              foreach ($item as $vocab_term) {
+                                if (!empty($vocab_term['@id'])) {
+                                  if (strpos($vocab_term['@id'], $this->bundle_name) !== FALSE) {
+                                    $entity_type = $vocab_term['hydra:title'];
+                                  }
+                                }
+                              }
+                            }
+                          }
+                          //print_r($entity_info);
+                          //print_r("\n \n");
+                          // This entity needs to be pulled down and data grabbed.
+                          $query = $entity_type . '/' . $single_id;
+                          $remote_entity = tripal_web_services_remote_request($site_id, $query);
+                        }
+                      }
+                      
+                    }
+                  }
+                }
+              }
+              else {
+                if ($remote_id == $entity_id) {
+                  // Before a request can be made we need to get the entity type
+                  // '@id' contains the bundle_name, so look for that.
+                  if (!empty($remote_id)) {
+                    $cache_name = 'tripal_web_services_vocab_' . $site_id;
+                    if ($cache = cache_get($cache_name)) {
+                      $site_vocab = $cache->data;
+                    }
+                    else {
+                      $site_vocab = tripal_web_services_vocab_request($site_id);
+                      if (!empty($site_vocab)) {
+                        cache_set('tripal_web_services_vocab', $site_vocab);
+                      }
+                    }
+                    //print_r($site_vocab);
+                    // Now we have the vocab we can look for the bundle_name in the @id field
+                    foreach ($site_vocab as $item) {
+                      if (is_array($item)) {
+                        foreach ($item as $vocab_term) {
+                          if (!empty($vocab_term['@id'])) {
+                            if (strpos($vocab_term['@id'], $this->bundle_name) !== FALSE) {
+                              $entity_type = $vocab_term['hydra:title'];
+                            }
+                          }
+                        }
+                      }
+                    }
+                    //print_r($entity_info);
+                    // This entity needs to be pulled down and data grabbed.
+                    $query = $entity_type . '/' . $entity_id;
+                    $remote_entity = tripal_web_services_remote_request($site_id, $query);
+                  }
+                }
+              }
+            }
 
+            // Build the fields for the fake entity.
+            // Need the label of the field so we can pull the data from the json.
+            $fields = [];
+            $fields = $this->fields;
+            foreach ($fields as $field) {
+              foreach ($site_vocab as $item) {
+                if (is_array($item)) {
+                  foreach ($item as $vocab_term) {
+                    if (!empty($vocab_term) && is_array($vocab_term)) {
+                      foreach ($vocab_term as $key => $term) {
+                        if (is_array($$key)) {
+                          print_r($key);
+                          if ($key['property'] === $field) {
+                            if (in_array('hydra:title', $key)) {
+                              $fields[] = $key['hydra:title'];
+                            }
+                          }
+                        
+                        } 
+                        else {
+
+                          if ($term['property'] === $field) {
+                            if (in_array('hydra:title', $term)) {
+                              $fields[] = $$term['hydra:title'];
+                            }
+                          }
+                        }
+                      }
+                    }
+                  }
+                }
+              }
+            }
+
+            // Because this is a remote field we need to construct a fake entity.
+            $fake_tripal_entity = new stdClass();
+            $fake_tripal_entity->entityType = 'TripalEntity';
+            $fake_tripal_entity->entityInfo = [];
+            $fake_tripal_entity->id = $entity_id;
+            $fake_tripal_entity->type = 'TripalEntity';
+            $fake_tripal_entity->bundle = $this->bundle_name;
+            foreach ($fields as $name => $field) {
+              $fake_tripal_entity->$name = [
+                'und' => [
+                  '0' => [
+                    'value' => $field,
+                  ],
+                ],
+              ];
+            }
+            //print_r($fake_tripal_entity);
+            $lines = $this->formatEntity($fake_tripal_entity);
+            foreach ($lines as $line) {
+              fwrite($fh, $line . "\r\n");
+            }
+          }
         }
         else {
           foreach ($this->entity_ids[$bundle_id] as $entity_id) {
             if (is_array($entity_id)) {
               foreach ($entity_id as $single_entity) {
-
                 if (is_array($single_entity)) {
                   foreach ($single_entity as $entity) {
                     // If the field is from a remote entity then we need to load the info through web services. 

+ 9 - 2
tripal_ws/api/tripal_ws.api.inc

@@ -247,19 +247,26 @@ function tripal_web_services_vocab_request($site_id) {
  *   The query string. This string is added to the URL for the remote
  *   website.
  */
-function tripal_web_services_remote_request($remote_site, $query) {
+function tripal_web_services_remote_request($site_id, $query) {
   $ctype = $query;
   $qdata = '';
   if (preg_match('/\?/', $query)) {
     list($ctype, $qdata) = explode('?', $query);
   }
 
+  if ($site_id) {
+    $remote_site = db_select('tripal_sites', 'ts')
+      ->fields('ts')
+      ->condition('ts.id', $site_id)
+      ->execute()
+      ->fetchObject();
+  }
+
   // Build the URL to the remote web services.
   $ws_version = $remote_site->version;
   $ws_url = $remote_site->url;
   $ws_url = trim($ws_url, '/');
   $ws_url .= '/web-services/content/' . $ws_version . '/' . $ctype;
-
   // Build the Query and make and substitions needed.
   //dpm($ws_url . '?' . $query);
   $options = array(