You are here

function drush_sf_import in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 6.2 sf_import/sf_import.drush.inc \drush_sf_import()

Import data from Salesforce

File

sf_import/sf_import.drush.inc, line 199
Drush integration for Salesforce Import module. Provides commands to import Salesforce data.

Code

function drush_sf_import($fieldmap_key) {

  // Initial checks
  if (!$fieldmap_key) {
    drush_die('Please provide a mapping to use');
    return;
  }
  _sf_import_drush_check_soap();
  $map = salesforce_api_fieldmap_load($fieldmap_key);
  if (empty($map)) {
    drush_die('Invalid fieldmap');
    return;
  }

  // Read options
  $where = drush_get_option('where', FALSE);
  $orderby = drush_get_option('order-by', FALSE);
  $start = intval(drush_get_option('start', 0));
  $count = intval(drush_get_option('count', -1));
  $no_new = drush_get_option('no-new', FALSE);
  $no_update = drush_get_option('no-update', FALSE);
  $check_date = drush_get_option('check-date', FALSE);

  // Read existing entries
  $query = db_select('salesforce_object_map', 's')
    ->fields('s', array(
    'oid',
    'sfid',
  ))
    ->condition('s.name', $map->name);
  $result = $query
    ->execute();
  while ($row = $result
    ->fetchAssoc()) {
    $existing[$row['sfid']] = $row['oid'];
  }

  // Read entries from salesforce
  $fields = array_keys($map->fields);
  if ($check_date && !in_array('LastModifiedDate', $fields)) {
    $fields[] = 'LastModifiedDate';
  }
  if (!in_array('Id', $fields)) {
    $fields[] = 'Id';
  }
  $soql = "\n    SELECT " . implode(', ', $fields) . "\n    FROM   " . $map->salesforce . "\n  ";
  if ($where) {
    $soql .= " WHERE " . $where;
  }
  if ($orderby) {
    $soql .= " ORDER BY " . $orderby;
  }
  try {
    $sf = salesforce_api_connect();
    if (!$sf) {
      throw new Exception('Unable to connect to Salesforce');
    }
    $query = $sf->client
      ->query($soql);
  } catch (Exception $e) {
    drush_die("Error while querying Salesforce : " . $e
      ->getMessage());
  }

  // Prepare for import
  $type = $map->drupal_entity;

  // Check to see if the fieldmap matches a Drupal entity.
  // If so, use the entity import function.
  if ($entity_info = entity_get_info($type)) {
    $type = 'entity';
  }
  $function = 'sf_' . $type . '_import';
  if (!function_exists($function)) {
    drush_die("Could not find import function for type " . $type);
  }

  /*
    $drupal_object = salesforce_api_fieldmap_objects_load('drupal', $map->drupal_entity);
    if ($count == -1) {
      $count = count($query->records) - $start;
    }
    if ($start < 0
          || ($start + $count) > count($query->records)) {
      drush_die("Start/count exceed possible rows. Number of rows in Salesfoce : " . count($query->records));
    }
  */

  // Run the import
  $created = 0;
  $updated = 0;
  $skipped = 0;
  $failed = 0;
  $imported = 0;
  while (!$query->done || count($query->records) > 0) {
    foreach ($query->records as $record) {

      // Check if we know this record
      $exists = isset($existing[$record->Id]);
      if ($exists && $no_update) {
        drush_print("Record Id {$record->Id} already exists, skipping");
        $skipped++;
        continue;
      }
      if (!$exists && $no_new) {
        drush_print("Record Id {$record->Id} does not exist, skipping");
        $skipped++;
        continue;
      }
      if ($type == 'node' && $exists && $check_date) {
        $changed = db_query("\n          SELECT changed\n          FROM   {node}\n          WHERE  nid = %d", $existing[$record->Id])
          ->fetchField();
        if ($changed >= strtotime($record->LastModifiedDate)) {
          drush_print("Record Id {$record->Id} has not been modified (date check)");
          $skipped++;
          continue;
        }
      }
      if ($exists) {
        $existing_oid = $existing[$record->Id];
      }
      else {
        $existing_oid = NULL;
      }
      $oid = FALSE;
      try {
        $oid = $function($record->Id, $map->name, $existing_oid);
      } catch (Exception $e) {
        drush_log("Error importing record {$record->Id} : " . $e
          ->getMessage(), 'error');
      }
      if (!$oid) {
        drush_log("Error importing record {$record->Id}", 'error');
        $failed++;
      }
      else {
        if (!$exists) {
          drush_print("Created object {$oid}");
          $created++;
        }
        else {
          drush_print("Updated object {$oid}");
          $updated++;
        }
        $imported++;
      }
    }
    unset($query->records);

    // get the next set of results
    if (!empty($query->queryLocator)) {
      try {
        $sf = salesforce_api_connect();
        if (!$sf) {
          throw new Exception('Unable to connect to Salesforce');
        }
        $query = $sf->client
          ->queryMore($query->queryLocator);
      } catch (Exception $e) {
        drush_print('Error: ' . $e
          ->getMessage());
        exit;
      }
    }

    // Report
    drush_print("Number of created objects : " . $created);
    drush_print("Number of updated objects : " . $updated);
    drush_print("Number of skipped objects : " . $skipped);
    drush_print("Number of failed imports : " . $failed);
  }
}