/**
 * @param $form
 * @param $form_state
 * @throws Exception
 */
function gd_sync_import_update_form_submit ( $form, &$form_state ) {
    try {
        $content = json_decode($form_state['values']['content']);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new Exception('Invalid JSON');
        }

        $importContext = new GD\Sync\Import\ImportContext(array('datasourceName'=>$form_state['values']['datasourceName'],'operation'=>'update'));
        $importStream = new GD\Sync\Import\ImportStream();
        $importStream->set(null,$content);

        $importController = new \GD\Sync\Import\ImportController();
        $importController->import($importStream,$importContext);

        drupal_set_message('Datasource Updated Successfully');
    } catch ( Exception $e ) {
        LogHelper::log_error($e);
        drupal_set_message($e->getMessage(),'error');
    }
}
/**
 * @param $datamartId
 * @param $export
 * @return services_error|stdClass
 * @throws Exception
 */
function gd_sync_resource_sync ( $datasourceName, $export ) {
    try {
        if (is_string($export)) {
            $content = json_decode($export);
        } else {
            $content = $export;
        }

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new Exception('Invalid JSON');
        }

        $importContext = new GD\Sync\Import\ImportContext(array('datasourceName'=>$datasourceName,'operation'=>'update'));
        $importStream = new GD\Sync\Import\ImportStream();
        $importStream->set(null,$content);

        $importController = new \GD\Sync\Import\ImportController();
        $importController->import($importStream,$importContext);

        $apiObject = new stdClass();
        $apiObject->name = $datasourceName;
        $apiObject->messages = gd_get_session_messages();
        return $apiObject;
    } catch ( Exception $e ) {
        return gd_admin_ui_service_exception_handler($e);
    }
}