/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

// TestPreReq.cpp : implementation file
//

#include "stdafx.h"
#include "SctsGUI.h"
#include "TestPreReq.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTestPreReq dialog


CTestPreReq::CTestPreReq(SctsTestAccount *_testAccount,CSemaphore *_semaphore,CWnd* pParent /*=NULL*/)
	: CDialog(CTestPreReq::IDD, pParent),testAccount(_testAccount),semaphore(_semaphore)
{
	//{{AFX_DATA_INIT(CTestPreReq)
	m_prereq = _T("");
	//}}AFX_DATA_INIT
}


void CTestPreReq::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestPreReq)
	DDX_Text(pDX, IDC_PREREQ, m_prereq);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTestPreReq, CDialog)
	//{{AFX_MSG_MAP(CTestPreReq)
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestPreReq message handlers

BOOL CTestPreReq::OnInitDialog() 
{
	SctsDevice *device;
	SctsDeviceDB *deviceDB;
	SctsSystemDB *systemDB;
	SctsDataItem *dataItem;
	SctsDataItemID dataItemID;
	SctsTestPreReq *testCasePreReq;
	SctsTestPreReq preReq;
	std::list<SctsTestCase *>::iterator testCaseIt;
	std::map <std::string,SctsDeviceDB *>::iterator ddbIt;
	int testCaseID;
	int adds;
	char *tempStr;
	char buffer[10];

	CDialog::OnInitDialog();
	CString content;
	m_prereq="";
	device=testAccount->GetTestDevice();
	for (ddbIt=device->databases.begin();ddbIt!=device->databases.end();++ddbIt)
	{
		adds=0;
		deviceDB=ddbIt->second;
		m_prereq+="Prerequisites for database : ";
		m_prereq+=deviceDB->targetName;
		systemDB=deviceDB->GetSystemDB();
		std::list <SctsDataItemID>::iterator sourceAddsIt=testAccount->sourceAdds[systemDB].begin();
		std::list <SctsDataItemID>::iterator targetAddsIt=testAccount->targetAdds[systemDB].begin();
		/*
		The sourceAddsList and targetAddsList refer to dataItemIDs in the test account's
		systemDBs. To get the dataItemID having the same content in the automaton
		account's systemDB :
		1. Find the dataItem in the test account's systemDB
		2. Find the dataItem with similar content in the automaton account's systemDB
		
		Pre-requisites must be carried out in the same order as they occur in the test
		cases of the test group, also with in a test case replaces are done before
		deletes.
		*/

		for (testCaseIt=testAccount->nextTestGroup->testCases.begin();testCaseIt!=testAccount->nextTestGroup->testCases.end();testCaseIt++)
		{
 			/* Find the test cases pre-req */
			testCaseID=(*testCaseIt)->GetID();
			if (testAccount->sctsIsServer)
				testCasePreReq=sctsClientTestCasePreReqs;
			else
				testCasePreReq=sctsServerTestCasePreReqs;
			for (;testCasePreReq->id && testCasePreReq->id!=testCaseID;++testCasePreReq);
			sctsMemcpy(&preReq,testCasePreReq,sizeof(SctsTestPreReq));
			
			/* Forward the Adds */
			adds+=preReq.add;
			
			/* Forward replaces and deletes on data items added by the source */
			while (preReq.replaceOfSource || preReq.deleteOfSource)
			{
				SCTSASSERT(sourceAddsIt!=testAccount->sourceAdds[systemDB].end());
				dataItemID=*(sourceAddsIt++);
				dataItem=systemDB->Find(dataItemID);
				if (preReq.replaceOfSource)
				{
					m_prereq+="\r\n\tReplace data item (LUID : ";
					--preReq.replaceOfSource;
				}
				else
				{
					m_prereq+="\r\n\tDelete data item (LUID : ";
					--preReq.deleteOfSource;
				}
				tempStr=deviceDB->GetTUID(dataItemID);
				m_prereq+=tempStr;
				sctsFree(tempStr);
				m_prereq+=") :\r\n\t\t";

				tempStr=deviceDB->GetContentCODEC()->Build(*dataItem->content);
				content=tempStr;
				sctsFree(tempStr);
				content.Replace("\n","\n\t\t");
				m_prereq+=content;
				delete dataItem;
			}
			
			/* Forward replaces and deletes on data items added by the target */
			while (preReq.replaceOfTarget || preReq.deleteOfTarget)
			{
				SCTSASSERT(targetAddsIt!=testAccount->targetAdds[systemDB].end());
				dataItemID=*(targetAddsIt++);
				dataItem=systemDB->Find(dataItemID);
				if (preReq.replaceOfTarget)
				{
					m_prereq+="\r\n\tReplace data item (LUID : ";
					--preReq.replaceOfTarget;
				}
				else
				{
					m_prereq+="\r\n\tDelete data item (LUID : ";
					--preReq.deleteOfTarget;
				}
				tempStr=deviceDB->GetTUID(dataItemID);
				m_prereq+=tempStr;
				sctsFree(tempStr);
				m_prereq+=") :\r\n\t\t";

				tempStr=deviceDB->GetContentCODEC()->Build(*dataItem->content);
				content=tempStr;
				sctsFree(tempStr);
				content.Replace("\n","\n\t\t");
				m_prereq+=content;
				delete dataItem;
			}
		}
		if (adds)
		{
			m_prereq+="\r\n\tAdd ";
			m_prereq+=itoa(adds,buffer,10);
			m_prereq+=" data items";
		}
		m_prereq+="\r\n\r\n";
	}
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CTestPreReq::OnDestroy() 
{
	CDialog::OnDestroy();
	/* Release the semaphore */
	if (semaphore) semaphore->Unlock();
}

void CTestPreReq::OnClose() 
{
	CDialog::OnClose();
	DestroyWindow();
}

void CTestPreReq::OnOK() 
{
	CDialog::OnOK();
	DestroyWindow();
}
