//
// internals
//
void SuperMapApplySchemaCommand::AddSchema ()
{
	TRACE(_T("SuperMapApplySchemaCommand::AddSchema ()"));

	FdoPtr<SuperMapLpFeatureSchemaCollection> lpSchemas = mConnection->GetLpSchemas();
	UGC::UGDataSource* pDataSource = mConnection->GetDataSource();

	VALIDATE_POINTER(pDataSource);

	//创建一个逻辑物理模式(并把它自动加到逻辑物理模式集合中)
	FdoPtr<SuperMapLpFeatureSchema> newLpSchema = new SuperMapLpFeatureSchema(lpSchemas,mConnection,
		pDataSource,m_Schema,m_IgnoreStates);                   //把m_Schema内容加到pDataSource中去
}
void FaultSignalsDlg::vPrepareUi()
{
    VALIDATE_POINTER(m_pEcu);

    LinEcuProps ouEcuProps;
    m_pEcu->GetProperties(ouEcuProps);
    if ( ouEcuProps.m_eEcuType == eSlave )
    {
        if ( false == bAreEqual(m_fSlaveProtVersion , 2.1) )
        {
            return;
        }
    }
    std::map<ISignal*, ISignal*> ouSignsalList;
    m_pEcu->GetSignalList(eTx, ouSignsalList);
    QList<QVariant> ouColumnList;
    std::string strName;
    int nRow = 0;
    ui.tableSignalList->setRowCount(ouSignsalList.size());
    LINSignalProps ouSignalProps;
for ( auto itr : ouSignsalList )
    {
        itr.second->GetProperties(ouSignalProps);
        if ( ouSignalProps.m_ouSignalType == eSignalNormal )
        {
            itr.second->GetName(strName);
            ouColumnList.push_back(QVariant(strName.c_str()));
            ui.tableSignalList->InsertRow(nRow, ouColumnList);
            auto itr2 =  std::find(m_lstSignals.begin(), m_lstSignals.end(),
                                   itr.second->GetUniqueId());
            if ( m_lstSignals.end() != itr2)
            {
                ui.tableSignalList->item(nRow, 0)->setCheckState(Qt::Checked);
            }
            else
            {
                ui.tableSignalList->item(nRow, 0)->setCheckState(Qt::Unchecked);
            }
            ui.tableSignalList->item(nRow, 0)->setData(Qt::UserRole, QVariant(itr.second->GetUniqueId()));
            nRow++;
        }
        ouColumnList.clear();
    }
}
/// <summary>Gets the string value of the specified property. No conversion is
/// performed, thus the property must be FdoDataType_String or an exception
/// is thrown.</summary>
/// <param name="identifier">Input the property name.</param> 
/// <returns>Returns the string value</returns> 
FdoString* SuperMapFeatureReader::GetString (FdoString* identifier)
{
	if(NULL == m_pRecordset || NULL == identifier)
	{
		return NULL;
	}

	UGC::UGString strProperName = SuperMapUtil::WcharToString(identifier).c_str();
	UGC::UGVariant var;

	// added by zhoux 2007-11-28 for 文本数据集的TEXT_PROPERTY字段内容作为点数据集的文本字段

	UGC::UGDatasetVector* pDatasetVector = m_pRecordset->GetDataset();
	VALIDATE_POINTER(pDatasetVector);
	if((pDatasetVector->GetType() == UGC::UGDataset::Text) && (0 == wcscmp(identifier,TEXT_PROPERTY)))
	{
		UGC::UGGeoText* pGeoText = NULL;
		m_pRecordset->GetGeometry((UGC::UGGeometry*&)pGeoText); // pGeoText 需要在内部分配空间,需要释放
		if(pGeoText != NULL)  //对于文本子对象个数大于1的情况,将文本内容合并:
		{
			FdoStringP tmpString;
			m_CachedString = L"";
			for(FdoInt32 i = 0; i < pGeoText->GetSubCount(); ++i)
			{
				tmpString = SuperMapUtil::StringToWString(pGeoText->GetSub(i).GetText().Cstr()).c_str();
				m_CachedString += tmpString;
			}
			delete pGeoText;
			return m_CachedString.operator FdoString*( );	
		}
		else
		{
			return L"";
		}
	}
	// end added 

	// added by zhouxu 2008-4-28  支持复合数据集中的文本对象
	if((pDatasetVector->GetType() == UGC::UGDataset::CAD) && (0 == wcscmp(identifier,TEXT_PROPERTY)))
	{
		UGC::UGGeoText* pGeoText = NULL;
		UGC::UGGeometry* pGeometry = NULL;
		m_pRecordset->GetGeometry(pGeometry); // pGeometry 需要在内部分配空间,需要释放
		if(pGeometry->GetType() != UGC::UGGeometry::GeoText)
		{
			//对于复合数据集的非文本对象,此字段置为空
			return L"";
		}
		pGeoText = (UGC::UGGeoText*)pGeometry;
		if(pGeoText != NULL)  //对于文本子对象个数大于1的情况,将文本内容合并:
		{
			FdoStringP tmpString;
			m_CachedString = L"";
			for(FdoInt32 i = 0; i < pGeoText->GetSubCount(); ++i)
			{
				tmpString = SuperMapUtil::StringToWString(pGeoText->GetSub(i).GetText().Cstr()).c_str();
				m_CachedString += tmpString;
			}
			delete pGeoText;
			return m_CachedString.operator FdoString*( );	
		}
		else
		{
			return L"";
		}
	}
	// end added
	
	m_pRecordset->GetFieldValue(strProperName, var);
	if(var.GetType() == UGC::UGVariant::String)
	{
		/*char *strHeader = (char*)var.GetValue().pVal;
		strHeader += 4;
		m_CachedString = SuperMapUtil::StringToWString(strHeader).c_str();
		return m_CachedString.operator FdoString*( );*/	
		
		UGC::UGString StrVal = "";
		StrVal = var.ToString();
		m_CachedString = SuperMapUtil::StringToWString(StrVal.Cstr()).c_str();
		if(StrVal.GetLength() == 0)
		{
			TRACE(_T("调用 SuperMapFeatureReader::GetString()..字符串为空! \n"));
		}
		return m_CachedString.operator FdoString*( );
	}

	else
	{
		throw FdoException::Create(NlsMsgGet(SUPERMAP_UNEXPECTED_DATATYPE, "The field type is not string (%1$ls).", "GetString"));
	}
}
/// <summary>Gets the double-precision floating point value of the specified property. No
/// conversion is performed, thus the property must be FdoDataType_Double
/// or an exception is thrown.</summary>
/// <param name="identifier">Input the property name.</param> 
/// <returns>Returns the double floating point value</returns> 
double SuperMapFeatureReader::GetDouble (FdoString* identifier)
{
	if(NULL == m_pRecordset || NULL == identifier)
	{
		return 0;
	}

	UGC::UGString strProperName = SuperMapUtil::WcharToString(identifier).c_str();
	UGC::UGVariant var;

	// added by zhoux 2007-12-10 for 文本数据集的TEXT_SIZE_PROPERTY字段内容作为点数据集的文本大小字段

	UGC::UGDatasetVector* pDatasetVector = m_pRecordset->GetDataset();
	VALIDATE_POINTER(pDatasetVector);

	if((pDatasetVector->GetType() == UGC::UGDataset::Text) && (0 == wcscmp(identifier,TEXT_SIZE_PROPERTY)))
	{
		UGC::UGGeoText* pGeoText = NULL;
		m_pRecordset->GetGeometry((UGC::UGGeometry*&)pGeoText); // pGeoText 需要在内部分配空间,需要释放
		if(pGeoText != NULL)  //暂时不考虑文本子对象个数大于1的情况 ToDo:
		{
			UGC::UGTextStyle textStyle = pGeoText->GetTextStyle();     
			double retSize = textStyle.GetHeight(); // 取大小
			delete pGeoText;
			return retSize;	
		}
		else
		{
			return 0.0;
		}
	}
	// end added 

	// added by zhoux 2008-4-28 支持复合数据集的文本对象
	if((pDatasetVector->GetType() == UGC::UGDataset::CAD) && (0 == wcscmp(identifier,TEXT_SIZE_PROPERTY)))
	{
		UGC::UGGeoText* pGeoText = NULL;
		UGC::UGGeometry* pGeometry = NULL;
		m_pRecordset->GetGeometry(pGeometry); // pGeoText 需要在内部分配空间,需要释放
		if(pGeometry->GetType() != UGC::UGGeometry::GeoText)
		{
			//复合数据集中非文本对象的此字段值设为0.0
			return 0.0;
		}
		pGeoText = (UGC::UGGeoText*)pGeometry ;
		if(pGeoText != NULL)  //暂时不考虑文本子对象个数大于1的情况 ToDo:
		{
			UGC::UGTextStyle textStyle = pGeoText->GetTextStyle();     
			double retSize = textStyle.GetHeight(); // 取大小
			delete pGeoText;
			return retSize;	
		}
		else
		{
			return 0.0;
		}
	}
	// end added 

	m_pRecordset->GetFieldValue(strProperName, var);
	if(var.GetType() == UGC::UGVariant::Double)
	{
		double ret = var.GetValue().dVal;
		return ret;
	}
	else
	{
		throw FdoException::Create(NlsMsgGet(SUPERMAP_UNEXPECTED_DATATYPE, "The field type is not double (%1$ls).", "GetDouble"));
	}
}
void SuperMapLpPropertyDefinition::ConvertPhysicalToLogical(UGC::UGFieldInfo* fieldInfo)
{
	if(m_fieldInfo == NULL)
	{
		m_fieldInfo = fieldInfo;
	}
    VALIDATE_POINTER(m_fieldInfo);

	// 逻辑的FDO属性名及为物理数据集的字段名
	//FdoStringP pLogicalPropertyName = fieldInfo->m_strName;
	FdoStringP pLogicalPropertyName = SuperMapUtil::StringToWString(fieldInfo->m_strName.Cstr()).c_str(); //modified by majun, 支持汉字字段
	FdoString* logicalPropertyName = (FdoString*)pLogicalPropertyName;

	FdoString* logicalPropertyDescription = L"";
	
	// 创建逻辑属性
	m_logicalProperty = FdoDataPropertyDefinition::Create(logicalPropertyName, logicalPropertyDescription);

	// 属性字段物理属性转换
	FdoDataType data_type ;
	FdoInt32 nType = fieldInfo->m_nType; // ugc的字段类型
	//TRACE(_T("SuperMapLpPropertyDefinition::ConvertPhysicalToLogical..[%ls]...[%d]\n"), logicalPropertyName, nType);
	switch(nType)
	{
	case UGC::UGFieldInfo::Boolean:
		data_type = FdoDataType_Boolean;
		break;
	case UGC::UGFieldInfo::Byte:
	case UGC::UGFieldInfo::Char:
		data_type = FdoDataType_Byte;
		break;
	case UGC::UGFieldInfo::Integer:
		data_type = FdoDataType_Int16;
		break;
	case UGC::UGFieldInfo::Long:   
		data_type = FdoDataType_Int32;
		break;
	case UGC::UGFieldInfo::Single:
	case UGC::UGFieldInfo::Float:
		data_type = FdoDataType_Single;
		break;
	case UGC::UGFieldInfo::Double:
		data_type = FdoDataType_Double;
		break;
	case UGC::UGFieldInfo::Date:
	case UGC::UGFieldInfo::Time:
		data_type = FdoDataType_DateTime;
		break;
	case UGC::UGFieldInfo::Decimal:
		data_type = FdoDataType_Decimal;
		break;
	case UGC::UGFieldInfo::Binary:
	case UGC::UGFieldInfo::LongBinary:
	case UGC::UGFieldInfo::VarBinary:
	case UGC::UGFieldInfo::TimeStamp:
		data_type = FdoDataType_BLOB;
		break;
	case UGC::UGFieldInfo::Text:
	case UGC::UGFieldInfo::Memo:
	case UGC::UGFieldInfo::NVarChar:
		//data_type = FdoDataType_CLOB;
		data_type = FdoDataType_String; 
		break;
	default:
		return;  //其他可能类型字段暂不转换
	}


	m_logicalProperty->SetDataType(data_type);

	// 如果是字符串类型设置长度
	if (data_type == FdoDataType_String)
	{
		if(fieldInfo->m_nType == UGC::UGFieldInfo::NVarChar)
			m_logicalProperty->SetLength(fieldInfo->m_nSize * 2);
		else
			m_logicalProperty->SetLength(fieldInfo->m_nSize);
	}
		
	// 如果是十进制类型设置位数和精度
	if (data_type == FdoDataType_Decimal)
	{
		m_logicalProperty->SetPrecision(fieldInfo->m_nPrecision);
		m_logicalProperty->SetScale(fieldInfo->m_nScale);
	}

	FdoPtr<FdoClassDefinition> logicalClass = m_parentLpClass->GetLogicalClass(); //各种逻辑属性要加入此类中
		
	FdoPtr<FdoPropertyDefinitionCollection> logicalProperties = logicalClass->GetProperties();

	// 如果有重复字段名返回
	if (logicalProperties->Contains (m_logicalProperty->GetName ())) 
		return;

	// modified by zhouxu 2007-12-18
	// 对Sm开头的字段除了SmUserId以外都设为只读。/ 并且加为逻辑类的标识属性
	if((0 == wcscmp (L"SmGeometry", pLogicalPropertyName)) 
	|| (0 == wcscmp(L"SMGEOMETRY",pLogicalPropertyName))) //modified by zhouxu 2007-12-7 //added by majun ,要素类中去掉SmGeometry字段
	{
		return;
	}
	else if((0 == wcscmp(L"SmUserID", pLogicalPropertyName))
	|| (0 == wcscmp(L"SMUSERID", pLogicalPropertyName)))
	{
		m_logicalProperty->SetReadOnly(false);
		m_logicalProperty->SetIsAutoGenerated(false);	
		m_logicalProperty->SetNullable(false);
		logicalProperties->Add(m_logicalProperty);
	}
	else if((0 == wcscmp(L"Sm", pLogicalPropertyName.Mid(0,2)))
		|| (0 == wcscmp(L"SM", pLogicalPropertyName.Mid(0,2))))
	{
		m_logicalProperty->SetReadOnly(true);//m_logicalProperty->SetReadOnly(true);
		m_logicalProperty->SetIsAutoGenerated(true);
		m_logicalProperty->SetNullable(false);
		//// 作为一个标识属性
		//FdoPtr<FdoDataPropertyDefinitionCollection> pIdentityProperties = logicalClass->GetIdentityProperties();
		//VALIDATE_POINTER(pIdentityProperties);
		logicalProperties->Add(m_logicalProperty);
		//pIdentityProperties->Add(m_logicalProperty);
	}
	else
	{
		m_logicalProperty->SetReadOnly(false);
		m_logicalProperty->SetIsAutoGenerated(false);	
		m_logicalProperty->SetNullable(true);
		logicalProperties->Add(m_logicalProperty);
	}

}
UGC::UGDataSource* SuperMapConnection::GetDataSource()
{
	UGC::UGDataSource* ds = SuperMapConnectionPool::GetInstance()->GetDataSource(m_strConnectionSession);
	VALIDATE_POINTER(ds);
	return ds;
}