DWORD 
Application_ClientConfigManager::visitUrl(Url* url, DWORD* minorError)
{
	DWORD status = CAPTURE_VISITATION_OK;
	stdext::hash_map<wstring, APPLICATION*>::iterator it;
	it = applicationsMap.find(url->getApplicationName());
	if(it != applicationsMap.end())
	{	
		// Download file to temp directory if required

		FileDownloader* downloader = new FileDownloader();
		wstring file = url->getUrl().substr(url->getUrl().find_last_of(L"\\"));
		DWORD downloadStatus = 0;
		if((downloadStatus = downloader->Download(url->getUrl(), &file)) > 0)
		{
			*minorError = downloadStatus;
			return CAPTURE_VISITATION_NETWORK_ERROR;
		}


		PAPPLICATION pApplication = it->second;
		STARTUPINFO siStartupInfo;
		PROCESS_INFORMATION piProcessInfo;

		memset(&siStartupInfo, 0, sizeof(siStartupInfo));
		memset(&piProcessInfo, 0, sizeof(piProcessInfo));
		siStartupInfo.cb = sizeof(siStartupInfo);

		wstring processCommand = L"\"";
		processCommand += pApplication->path;
		processCommand += L"\" ";
		processCommand += url->getUrl();

		BOOL created = CreateProcess(NULL,(LPWSTR)processCommand.c_str(), 0, 0, FALSE,
			CREATE_DEFAULT_ERROR_MODE, 0, 0, &siStartupInfo,
			&piProcessInfo);

		if(created == FALSE)
		{
			status = CAPTURE_VISITATION_PROCESS_ERROR;
			*minorError = GetLastError();
		} else {
			Sleep(url->getVisitTime()*1000);
					
			bool processClosed = closeProcess(piProcessInfo.dwProcessId, minorError);
			if(!processClosed)
			{
				status = CAPTURE_VISITATION_PROCESS_ERROR;
			}
			Sleep(1000);
		}
	} else {
		status = CAPTURE_VISITATION_PROCESS_ERROR;
		*minorError = CAPTURE_PE_PROCESS_PATH_NOT_FOUND;
	}
	return status;
}
void
Application_ClientConfigManager::visitGroup(VisitEvent* visitEvent)
{
	DWORD status = CAPTURE_VISITATION_OK;
	stdext::hash_map<wstring, APPLICATION*>::iterator it;
	it = applicationsMap.find(visitEvent->getProgram());
	if(it != applicationsMap.end())
	{	
		const size_t size = visitEvent->getUrls().size();
		std::vector<ProcessHandler*> processHandlers(size);
		
		bool error = false;
		for(unsigned int i = 0; i < visitEvent->getUrls().size(); i++)
		{	
			// Download file to temp directory if required
			Url* url = visitEvent->getUrls()[i];
			wstring url_path = url->getUrl();
			if(it->second->downloadURL)
			{
				FileDownloader* downloader = new FileDownloader();
				wstring file = url->getUrl().substr(url->getUrl().find_last_of(L"/"));
				DWORD downloadStatus = 0;
				if((downloadStatus = downloader->Download(url->getUrl(), &file)) > 0)
				{
					visitEvent->setErrorCode(CAPTURE_VISITATION_NETWORK_ERROR);
					url->setMajorErrorCode(CAPTURE_VISITATION_NETWORK_ERROR);
					url->setMinorErrorCode(downloadStatus);
					continue;
				} else {
					url_path = file;
				}
			}

			PAPPLICATION pApplication = it->second;
			wstring cmd = pApplication->path;
			wstring param = url_path;

			ProcessHandler *ph = new ProcessHandler(cmd,param);
			processHandlers[i] = ph;

			ph->executeProcess();
			url->setProcessId(ph->getProcessId());

			if(i==0) 
			{
				Sleep(2000);
			}

			double maxWaitTimeInSec = 60;
			double waitTimeInSec = 0;

			bool isOpen = ph->isOpen();
			while(!isOpen && waitTimeInSec < maxWaitTimeInSec) 
			{
				Sleep(100);
				waitTimeInSec = waitTimeInSec + 0.1;
				isOpen = ph->isOpen();
			}
			if(waitTimeInSec >= maxWaitTimeInSec)
			{
				visitEvent->setErrorCode(CAPTURE_VISITATION_PROCESS_ERROR);
				url->setMajorErrorCode(CAPTURE_VISITATION_PROCESS_ERROR);
				url->setMinorErrorCode(GetLastError());
				error = true;
				printf("Error opening app");
			} else {
				;//printf("Successfully opened app");
			}
			
		}
				
		if(!error)
		{
			int sleepTimeInSec = visitEvent->getUrls()[0]->getVisitTime();
			Sleep(sleepTimeInSec*1000);
		}

		for(unsigned int i = 0; i < visitEvent->getUrls().size(); i++)
		{
			DWORD minorError = 0;
			Url* url = visitEvent->getUrls()[i];
			ProcessHandler *ph = processHandlers[i];
			ph->closeProcess();

			double maxWaitTimeInSec = 30;	
			double waitTimeInSec = 0;
			bool isOpen = ph->isOpen();
			while(isOpen && waitTimeInSec < maxWaitTimeInSec) 
			{
				Sleep(100);
				waitTimeInSec = waitTimeInSec + 0.1;
				isOpen = ph->isOpen();
			}

			if(waitTimeInSec >= maxWaitTimeInSec)
			{
				visitEvent->setErrorCode(CAPTURE_VISITATION_PROCESS_ERROR);
				url->setMajorErrorCode(CAPTURE_VISITATION_PROCESS_ERROR);
				url->setMinorErrorCode(minorError);
			} else {
				;//printf("Successfully closed app");
			}
		}
		//delete [] processHandlers;
	} else {
		visitEvent->setErrorCode(CAPTURE_VISITATION_PROCESS_ERROR);
		//url->setMajorErrorCode(CAPTURE_VISITATION_PROCESS_ERROR);
		//url->setMinorErrorCode(CAPTURE_PE_PROCESS_PATH_NOT_FOUND);
	}
}