
// AKAssmsDoc.cpp : implementation of the CAKAssmsDoc class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "AKAssms.h"
#endif

#include "AKAssmsDoc.h"
#include "CString_Ext.h"

#include <propkey.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAKAssmsDoc

IMPLEMENT_DYNCREATE(CAKAssmsDoc, CDocument)

BEGIN_MESSAGE_MAP(CAKAssmsDoc, CDocument)
END_MESSAGE_MAP()


// CAKAssmsDoc construction/destruction

CAKAssmsDoc::CAKAssmsDoc()
{
	// TODO: add one-time construction code here
}

CAKAssmsDoc::~CAKAssmsDoc()
{
}

///////////////////////////////////////////////////////////////////////
//  Properties

const CString&
CAKAssmsDoc::GetSQLstmt ( )
{
    return m_strSQLstmt;
}

///////////////////////////////////////////////////////////////////////
//  Operations

void
CAKAssmsDoc::DoTableSelectTop1000Rows ( WPARAM wParam, LPARAM lParam )
{
    P2PmsgMgr *pMgr = theApp.GetP2PmsgMgr ( );
    // Problematic
    try
    {
      SetModifiedFlag ( FALSE );
      P3PmsgNode oTable = pMgr->P2Pos2Node(wParam).r_Object();
      P3PmsgAttr oAttr  = oTable.r_Attr().r_Object();
      if ( !oAttr.Exists(_T("$Table")) ||
           !oAttr.Exists(_T("$Table"))    )
        return;

      // Build SELECT statement
      CString strSelect;
      strSelect  = _T("/****** Script for SelectTopNRows command from AKAssms  ******/\r\n");
      strSelect += _T("SELECT TOP 1000 " );

      P3PmsgNode oColumns = oTable.SelectNode(_T("Columns")).r_Object();
      P3PmsgCurs oCurs(oColumns);
      for ( int i = 0; oCurs.Goto(i); i++ )
      {
        if ( !oCurs.IsNode() )
          continue;
        CString strColumnName = oCurs.r_name().c_name();

        if ( i )
          strSelect += _T("      ,");
        strSelect += CString_Truncate ( strColumnName, _T(" (") );
        strSelect += _T("\r\n");
      }

      // Database details
      P3PmsgNode oNodeSQL = oAttr.SelectObject ( _T("SQL") );
//oNodeSQL.Print(stdout,1);
      strSelect += _T("  FROM ");
      strSelect += _T("[");
      strSelect += oNodeSQL["CATALOG_NAME"].c_str();
      strSelect += _T("].[");
      strSelect += oNodeSQL["SCHEMA_NAME"].c_str();
      strSelect += _T("].[");
      strSelect += oNodeSQL["name"].c_str();
      strSelect += _T("]");

      // Implementation
      m_strSQLstmt = strSelect;
      UpdateAllViews ( 0, 1 );
      POSITION pos = GetFirstViewPosition();
      CView *p = (CView *)this->GetNextView(pos);
      CMDIFrameWndEx *frame = (CMDIFrameWndEx *)p->GetParentFrame();
      frame->PostMessage(WM_COMMAND, ID_QUERY_EXECUTE, 0);
      return;
    }

    // Exceptions
    catch_pP2Pevent_Cancel
    catch_pCException_Cancel
    catch_ALL_Cancel
}

void
CAKAssmsDoc::DoViewSelectTop1000Rows ( WPARAM wParam, LPARAM lParam )
{
    P2PmsgMgr *pMgr = theApp.GetP2PmsgMgr ( );
    // Problematic
    try
    {
      SetModifiedFlag ( FALSE );
      P3PmsgNode oView = pMgr->P2Pos2Node(wParam).r_Object();
      P3PmsgAttr oAttr = oView.r_Attr().r_Object();
      if ( !oAttr.Exists(_T("$View")) ||
           !oAttr.Exists(_T("$View"))    )
        return;

      // Build SELECT statement
      CString strSelect;
      strSelect  = _T("/****** Script for SelectTopNRows command from AKAssms  ******/\r\n");
      strSelect += _T("SELECT TOP 1000 " );

      P3PmsgNode oColumns = oView.SelectNode(_T("Columns")).r_Object();
      P3PmsgCurs oCurs(oColumns);
      for ( int i = 0; oCurs.Goto(i); i++ )
      {
        if ( !oCurs.IsNode() )
          continue;
        CString strColumnName = oCurs.r_name().c_name();

        if ( i )
          strSelect += _T("      ,");
        strSelect += CString_Truncate ( strColumnName, _T(" (") );
        strSelect += _T("\r\n");
      }

      // Database details
      P3PmsgNode oNodeSQL = oAttr.SelectObject ( _T("SQL") );
//oNodeSQL.Print(stdout,1);
      strSelect += _T("  FROM ");
      strSelect += _T("[");
      strSelect += oNodeSQL["CATALOG_NAME"].c_str();
      strSelect += _T("].[");
      strSelect += oNodeSQL["SCHEMA_NAME"].c_str();
      strSelect += _T("].[");
      strSelect += oNodeSQL["name"].c_str();
      strSelect += _T("]");

      // Implementation
      m_strSQLstmt = strSelect;
      UpdateAllViews ( 0, 1 );
      POSITION pos = GetFirstViewPosition();
      CView *p = (CView *)this->GetNextView(pos);
      CMDIFrameWndEx *frame = (CMDIFrameWndEx *)p->GetParentFrame();
      frame->PostMessage(WM_COMMAND, ID_QUERY_EXECUTE, 0);
      return;
    }

    // Exceptions
    catch_pP2Pevent_Cancel
    catch_pCException_Cancel
    catch_ALL_Cancel
}

BOOL CAKAssmsDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}




// CAKAssmsDoc serialization

void CAKAssmsDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

#ifdef SHARED_HANDLERS

// Support for thumbnails
void CAKAssmsDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// Modify this code to draw the document's data
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// Support for Search Handlers
void CAKAssmsDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// Set search contents from document's data. 
	// The content parts should be separated by ";"

	// For example:  strSearchContent = _T("point;rectangle;circle;ole object;");
	SetSearchContent(strSearchContent);
}

void CAKAssmsDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = NULL;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != NULL)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CAKAssmsDoc diagnostics

#ifdef _DEBUG
void CAKAssmsDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CAKAssmsDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG


///////////////////////////////////////////////////////////////////////////////
//  CAKAssmsDoc commands
