/*******************************************************************************
*                                                                       
*N  {accessingbandtablemetadata.c}  --  Example of accessing the properties of
*    the raster band table, sde_bnd_<raster_column_id>.
*                                                                       
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
* How to compile:: 
*       This program provides programming examples of 
*
*
*     1. Add {ibm ICU installation directory}/lib to your library paths
*	  2. Add {ibm ICU installation directory}/include to your include paths
*     3. Add {$SDEHOME}/lib to your library paths;
*     4. Compile and link with icuuc.lib and other needed SDE libraries
*E
*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*X  Legalese:
*
* Copyright � 2006-2007 ESRI
*
* All rights reserved under the copyright laws of the United States and 
* applicable international laws, treaties, and conventions.
*
* You may freely redistribute and use this sample code, with or without 
* modification, provided you include the original copyright notice and use 
* restrictions.  
*
* Disclaimer:  THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 
* WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ESRI OR 
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
* OR BUSINESS INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED 
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
* TORT ARISING IN ANY WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED 
* OF THE POSSIBILITY OF SUCH DAMAGE.
* 
* For additional information, contact:
* Environmental Systems Research Institute, Inc.
* Attn: Contracts and Legal Services Department
* 380 New York Street
* Redlands, California, 92373
* USA
* 
* email: contracts@esri.com
*
*E
****************************************************************************/

#include "sdetype.h"
#include "sderaster.h"
#include <string.h>
#include "raster_source.h"

void main(void) 
{
	SE_CONNECTION connection;
	SE_ERROR error;
	CHAR table_name[SE_MAX_TABLE_LEN], column_name[SE_MAX_COLUMN_LEN], name_value[64], *server,
		*instance, *database, *username, *password;

	SE_RASCOLINFO rastercolumn;
	SE_RASTERINFO raster;
	SE_RASBANDINFO *rasterbands;
	LONG rastercolumn_id, rc, raster_id, band_count;
	SHORT i;

	/* Set the ArcSDE server connection parameters */
	server = "jolex";
	instance = "9000";
	database = NULL;
	username = "mark";
	password = "mark";

	/* Connect to the ArcSDE server */
	rc = SE_connection_create (server, instance, database, username, password, &error, &connection);
	check_error (rc, connection, NULL, "SE_connection_create");

	/* Initialize the raster column info structure */
	rc = SE_rascolinfo_create (&rastercolumn);
	check_error (rc, NULL, NULL, "SE_rascolinfo_create");

	/* Set the table and column name */
	strcpy(table_name,"myrastertable");
	strcpy(column_name,"raster");

	/* Populate the raster column info structure based on the table and column name.*/
	rc = SE_rastercolumn_get_info_by_name (connection, table_name, column_name, rastercolumn);
	check_error (rc, connection, NULL, "SE_rastercolumn_get_info_by_name");

	/* Get the raster column id from the raster column info structure. */
	rc = SE_rascolinfo_get_id (rastercolumn, &rastercolumn_id);
	check_error (rc, NULL, NULL, "SE_rascolinfo_get_id");

	/* Free the raster column info structure */
	SE_rascolinfo_free (rastercolumn);

	/* Initialize the raster info structure */
	rc = SE_rasterinfo_create (&raster);
	check_error (rc, NULL, NULL, "SE_rasterinfo_create");

	/* Set the name of the raster. This value corresponds to a business table column value */
	strcpy (name_value,"europe");

	/* fetch the raster id, based on the business table name value */
	fetch_rasterid_on_raster_name (connection, table_name, name_value, &raster_id);

	if(raster_id == -1) 
	{

		printf("The raster called europe does not exist in the raster table.\n");
		return;
	}

	/* Populate the raster info structure based on the raster id */
	rc = SE_raster_get_info_by_id ( connection, rastercolumn_id, raster_id, raster);
	check_error (rc, connection, NULL, "SE_raster_get_info_by_id");

	/* Initialize and populate the raster band info structure based on the */
	/* raster info structure */
	rc = SE_raster_get_bands (connection, raster, &rasterbands, &band_count);
	check_error (rc, connection, NULL, "SE_raster_get_bands");

	/* Free the raster info structure */
	SE_rasterinfo_free (raster);

	/* Display the data of the rasterband info array */
	for (i=0; i<band_count; i++) 
		print_rasterbandinfo(connection,rasterbands[i]);

	/* Free the raster band info arrary */
	SE_rasterband_free_info_list (band_count,rasterbands);

	/* Release the connection to the ArcSDE Server. */
	SE_connection_free (connection);

	return;
}


/***************************************************/
/*                                                                                                  */
/*   fetch_rasterid_on_raster_name - Fetch the raster ID based      */
/*                                                     on the business table's            */
/*                                                     name value.                            */
/*                                                                                                   */
/*              arguments:                                                                     */
/*                                                                                                   */
/*              connection <SE_CONNECTION> - the connection     */
/*                                           handle.                                             */
/*              table_name <CHAR *>        - The table name               */
/*              name_value <CHAR *>        - The name value              */
/*              raster_id  <LONG *>        - The raster ID to                 */
/*                                           be returned.                                      */
/*                                                                                                   */
/****************************************************/

void fetch_rasterid_on_raster_name (SE_CONNECTION connection,
                                                     CHAR * table_name,
                                                     CHAR * name_value,
                                                     LONG *raster_id) 
{
	SE_STREAM stream;
	LONG rc;
	CHAR sqlstmt[128];
	SHORT raster_ind= SE_IS_NULL_VALUE;

	/* Initialize the SE_STREAM structure. */

	rc = SE_stream_create (connection, &stream);
	check_error (rc,connection,NULL,"SE_stream_create");

	/* Set up SQL statement */

	sprintf (sqlstmt,"select raster from %s where name = '%s'",table_name, name_value);

	/* prepare the SQL statement */

	rc = SE_stream_prepare_sql (stream, sqlstmt);
	check_error (rc, NULL, stream, "SE_stream_prepare_sql");

	/* Bind the raster_ID variable to receive the output of the stream  */

	rc = SE_stream_bind_output_column (stream, 1, (void *)raster_id, &raster_ind);
	check_error (rc, NULL, stream, "SE_stream_bind_output_column");

	/* Execute the query */

	rc = SE_stream_execute (stream);
	check_error (rc, NULL, stream, "SE_stream_execute");

	/* Fetch the result from the cursor */

	rc = SE_stream_fetch (stream);

	if (rc == SE_SUCCESS || rc == SE_FINISHED) {
		/* If the query sets the indicator to NULL */
		if (raster_ind == SE_IS_NULL_VALUE)
			 *raster_id = -1;
	}
	else
		check_error (rc, NULL, stream, "SE_stream_fetch");

	/* Release the SE_STREAM structure */

	rc = SE_stream_free (stream);
	check_error (rc,NULL, stream,"SE_stream_free");

	return;

} /* fetch_rasterid_on_raster_name */