///////////////////////////////////////////////////////////
// Author:			Stefan Liersch
// Contact:			stefan.liersch@googlemail.com,
//					stefan.liersch@ufz.de
//
// Date:			2008-04-11
// last modified:	2008-04-11
//
//---------------------------------------------------------
// DESCRIPTION
//---------------------------------------------------------
//
//---------------------------------------------------------
// ToDo
//---------------------------------------------------------
//
///////////////////////////////////////////////////////////

#include "pg_menu_db_soil_salinity_assessment.h"
#include "pgsql/pgdatabase.h"
#include "pgsql/pg_menu_db.h"
#include "saga_gui/saga_frame.h"		// location of class Cpgdatabase
										// using global *g_pSAGA_Frame pointer
#include "saga_gui/helper.h"			// using function MSG_General_Add()
#define MSG_ADD(s)	MSG_General_Add(s, true, true);


CSoilSalinityAssessment::CSoilSalinityAssessment()
: Cpg_menu_db_soil_salinity()
{
}

CSoilSalinityAssessment::~CSoilSalinityAssessment()
{
}



///////////////////////////////////////////////////////////////////////
//
//	EXPORT FUNCTIONS
//
///////////////////////////////////////////////////////////////////////


//void CSoilSalinityAssessment::Export_Shape_As_SoilSalinityBase_Map()
//{
//	CSG_Shapes* pShapes = Select_Shapefile_for_Export();
//	CSG_String outputName = Get_Shape_Output_Name(pShapes);
//
//	CSG_Shapes* pShapesOut = new CSG_Shapes(*pShapes);
//	pShapesOut->Set_Name(outputName.c_str());
//
//	Add_PK_Field_To_AttributeTable(pShapesOut);
//
//	Add_IDs_To_PK_Field(pShapesOut);
//
//	Create_External_Attributes(g_pSAGA_Frame->db->Cut_FileNameExtension(pShapesOut->Get_Name()).c_str());
//
//	// add new shape to SAGA workspace
//	g_pData->Add(pShapesOut);
//
//	// export new shape to PostgreSQL database
//	Cmenu_db menu_db;
//	menu_db._On_DB_ExportAsSimpleFeature(pShapesOut);
//}

//---------------------------------------------------------------------
//CSG_String CSoilSalinityAssessment::Get_Shape_Output_Name(CSG_Shapes *pShapes)
//{
//	CSG_String sname = "ssa_";
//	return(sname.Append(pShapes->Get_Name()));
//}
//---------------------------------------------------------------------

//void CSoilSalinityAssessment::Add_PK_Field_To_AttributeTable(CSG_Shapes *pShapesOut)
//{
//	// Add a column at the first position in the shape file attribute table
//	// that serves as primary key in the database
//	pShapesOut->Get_Table().Add_Field("FID",TABLE_FIELDTYPE_Int,0);
//}
//---------------------------------------------------------------------
//
//void CSoilSalinityAssessment::Create_External_Attributes(const char* tablename)
//{
//	CSG_String sql;
//	CSG_String name;
//	name.Append("_attr_ssa_"); // ssa = Soil Salinity Assessment
//	name.Append(tablename);
//
//	if ( g_pSAGA_Frame->db->CheckAccessibility(name.c_str()) ) {
//		g_pSAGA_Frame->db->DropTable(name.c_str());
//	}
//
//	sql.Append("CREATE TABLE ");
//	sql.Append(name);
//	sql.Append(" (FID int4 primary key, Year int4, Period varchar(1), Salinity varchar, Salinity_v float4,");
//	sql.Append("MS_LOW float4, MS_MEDIUM float4, MS_HIGH float4, Plant varchar)");
//	MSG_ADD(sql.c_str());
//
//	g_pSAGA_Frame->db->ExecSQL_wx(sql.c_str());
//}
//---------------------------------------------------------------------


///////////////////////////////////////////////////////////////////////
//
//	IMPORT FUNCTIONS
//
///////////////////////////////////////////////////////////////////////


void CSoilSalinityAssessment::Load_Map()
{
	CSG_Data_Object*	pObject = Select_Import_Map();

	if ( pObject != NULL ) {

		//Set_Feature_Name(pObject);

		Add_Specific_Fields(pObject);

		g_pData->Add(pObject);
		pObject->asSimpleFeature()->Set_Category(SF_CATEGORY_SoilSalinityMap);
	} else {
		delete pObject;
	}
}
//---------------------------------------------------------------------

CSG_String CSoilSalinityAssessment::Get_Feature_Name(CSG_Data_Object* pObject)
{
	CSG_String featurename = "ssa_"; // ssa = soil salinity assessment
	featurename.Append(pObject->Get_Name());
	return(featurename);
}
//---------------------------------------------------------------------

void CSoilSalinityAssessment::Set_Feature_Name(CSG_Data_Object* pObject)
{
	CSG_String featurename = Get_Feature_Name(pObject);
	// Set feature name 'ssa_' + original object name
	pObject->Set_Name(featurename.c_str());
	// Set attribute table name
	pObject->asSimpleFeature()->Get_Table().Set_Name(featurename.c_str());
}
//---------------------------------------------------------------------

void CSoilSalinityAssessment::Add_Specific_Fields(CSG_Data_Object* pObject)
{
	// add soil salinity specific columns
	pObject->asSimpleFeature()->Get_Table().Add_Field("Plant",TABLE_FIELDTYPE_String,1);
	pObject->asSimpleFeature()->Get_Table().Add_Field("MS_HIGH",TABLE_FIELDTYPE_Float,1);
	pObject->asSimpleFeature()->Get_Table().Add_Field("MS_MEDIUM",TABLE_FIELDTYPE_Float,1);
	pObject->asSimpleFeature()->Get_Table().Add_Field("MS_LOW",TABLE_FIELDTYPE_Float,1);
	pObject->asSimpleFeature()->Get_Table().Add_Field("Salinity_v",TABLE_FIELDTYPE_Float,1);
	pObject->asSimpleFeature()->Get_Table().Add_Field("Salinity",TABLE_FIELDTYPE_String,1);
	pObject->asSimpleFeature()->Get_Table().Add_Field("Mnth",TABLE_FIELDTYPE_String,1);
	pObject->asSimpleFeature()->Get_Table().Add_Field("Yr",TABLE_FIELDTYPE_Int,1);
}
//---------------------------------------------------------------------

