/////////////////////////////////////////////////////////////////////////////
// Name:        dbmodelentity.cc
// Purpose:     Database Objects
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: dbmodelentity.cc,v 1.2 2004/01/01 13:56:19 horakdan Exp $
// Copyright:   (c) Daniel Horak
// Licence:     GPL
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWindows headers
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <wx/ogl/ogl.h>
#include "config.h"
#include "xml.h"
#include "dbobject.h"
#include "dbmodelentity.h"
#include "dbmodelrelation.h"
#include "dbmodel.h"
#include "dbentity.h"
#include "dbrelation.h"
#include "project.h"

DBModelEntity::DBModelEntity(DataDesignerProject *project, DataDesignerContainer *container)
	:DBObject(DBModelEntityType, "modelentity", project, container),
	m_xpos(100), m_ypos(100), m_entity(NULL)
{
}

wxDialog *DBModelEntity::Editor(bool edit)
{
	return new DBModelEntityEditor(this, edit);
}

void DBModelEntity::LoadXmlNode(wxXmlNode *node)
{
	wxString number;
	
	if (node->GetName() == m_typestr) {
		m_name	= node->GetPropVal("name", wxEmptyString);
		number	= node->GetPropVal("xpos", "100");
		m_xpos	= wxAtoi(number);	
		number	= node->GetPropVal("ypos", "100");
		m_ypos	= wxAtoi(number);	
	} else {
		wxLogMessage("wrong type '%s'", node->GetName().c_str());
	}
	
	// must set pointer to the real entity
	m_entity = (DBEntity *)GetProject()->m_top_entities->GetObjectByName(m_name);
	if (m_entity && m_entity->GetType() == DBEntityType)
		m_entity->AddModelEntity(this);
	else
		wxLogMessage("DBModelEntity::LoadXmlNode - cannot find real entity for " + m_name);

	m_project->SetItemText(m_treeitemid, m_name);

	// look for associated relations and add them to the model
	DBRelation      *rel = NULL;
	wxTreeItemId    child, modelid;
	long            cookie;
	DBModel		*model;
	DBObject	*mrel;
	
	modelid = m_project->GetParent(m_project->GetParent(m_treeitemid));
	model = (DBModel *)(((DataDesignerItemData *)m_project->GetItemData(modelid))->GetObject());
	
	child = m_project->GetFirstChild(m_project->m_top_relations->GetTreeItemId(), cookie);
	while (child.IsOk()) {
		rel = (DBRelation *)(((DataDesignerItemData *)m_project->GetItemData(child))->GetObject());
		if (rel && rel->GetType() == DBRelationType) {
			wxLogMessage("DBModelEntity::LoadXmlNode - relation " + rel->GetName());
			if ((m_name == rel->m_parent || m_name == rel->m_child)
				&& m_container->GetObjectByName(rel->m_parent) && m_container->GetObjectByName(rel->m_child))
			{
				wxLogMessage("DBModelEntity::LoadXmlNode - adding");
				mrel = model->m_relations->CreateObject();
				if (mrel) {
					mrel->m_name = rel->GetName();
					mrel->AppendItem();
				}
			}
		}
		child = m_project->GetNextChild(m_project->m_top_relations->GetTreeItemId(), cookie);
	}
}

wxXmlNode *DBModelEntity::GetXmlNode()
{
	wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, m_typestr);
	wxString number;
	
	node->AddProperty("name", m_name);
	number.Printf("%d", m_xpos);
	node->AddProperty("xpos", number);
	number.Printf("%d", m_ypos);
	node->AddProperty("ypos", number);
	
	return node;
}

wxTreeItemId DBModelEntity::AppendItem()
{
	wxTreeItemId	id = DBObject::AppendItem();
	
	return id;
}

void DBModelEntity::CreateShape()
{
	m_shape = new DBEntityShape(this);
	m_shape->Show(TRUE);
}

/*
 * Editor
 */
DBModelEntityEditor::DBModelEntityEditor(DBObject *object, bool edit)
	: wxDialog(NULL, -1, _("Simple entity"), wxDefaultPosition, wxSize(400,120)),
	m_object(object), m_edit(edit)
{
	wxString	**strings;
	int		idx;
	
	m_panel_general = new wxPanel(this);
	m_panel_button = new wxPanel(this);
	
	wxLayoutConstraints *c = new wxLayoutConstraints;
	c->top.SameAs	(this, wxTop);
	c->left.SameAs	(this, wxLeft);
	c->right.SameAs	(this, wxRight);
	c->bottom.SameAs(m_panel_button, wxTop);
	m_panel_general->SetConstraints(c);
	
	c = new wxLayoutConstraints;
	c->height.Absolute(40);
	c->left.SameAs	(this, wxLeft);
	c->right.SameAs	(this, wxRight);
	c->bottom.SameAs(this, wxBottom);
	m_panel_button->SetConstraints(c);

	
	idx = 0;

	strings = m_object->GetProject()->m_top_entities->ListNames();
	new wxStaticText(m_panel_general, -1, _("Entity"), wxPoint(10,10), wxSize(100,-1), wxALIGN_RIGHT);
//	c1 = new wxComboBox(m_panel_general, -1, wxEmptyString, wxPoint(120,10), wxSize(200,-1), 0, NULL, /*wxCB_SORT |*/ wxCB_READONLY);
	c1 = new wxChoice(m_panel_general, -1, wxPoint(120,10), wxSize(200,-1));
	while (strings[idx]) {
		// append iff entity is not already contained in model
		if (m_object->GetContainer()->GetObjectByName(*strings[idx]) == NULL)
			c1->Append(*strings[idx]);
		idx++;
	}
	delete strings;
	
	m_button_ok	= new wxButton(m_panel_button, wxID_OK, _("OK"), wxPoint(10,10), wxSize(60,-1));
	m_button_cancel	= new wxButton(m_panel_button, wxID_CANCEL, _("Cancel"), wxPoint(90,10), wxSize(60,-1));
	
	SetAutoLayout(TRUE);
	Layout();
	Center();
}

DBModelEntityEditor::~DBModelEntityEditor()
{
}

bool DBModelEntityEditor::TransferDataFromWindow()
{
	DBModelEntity	*object = (DBModelEntity *)GetObject();
	DBEntity		*ent_old, *ent_new;

	if (c1->GetStringSelection().IsEmpty())
		return FALSE;

	object->m_name 		= c1->GetStringSelection();
	
	ent_old 		= object->GetRealEntity();
	ent_new			= (DBEntity *)m_object->GetProject()->m_top_entities->GetObjectByName(object->m_name);
	
	if (ent_old != ent_new) {
		if (ent_old) {
			ent_old->DeleteModelEntity(object);
		}
		ent_new->AddModelEntity(object);
		
		object->SetRealEntity(ent_new);
	}
	
	return TRUE;
}

bool DBModelEntityEditor::TransferDataToWindow()
{
	DBModelEntity	*object = (DBModelEntity *)GetObject();
	
	c1->SetStringSelection(object->m_name);
	
	return TRUE;
}

/*
 * Container
 */
DBModelEntityContainer::DBModelEntityContainer(DataDesignerProject *project, const wxTreeItemId& parent)
	: DataDesignerContainer(project, parent, "modelentities")
{
}

DBObject *DBModelEntityContainer::CreateObject()
{
	return new DBModelEntity(GetProject(), this);
}

void DBModelEntityContainer::ShowList()
{
	SetList(new DBModelEntityListCtrl(GetProject()->GetSplitter(), this));
	
	DataDesignerContainer::AddObjectsToListAndShow();
}

/*
 * ObjectList
 */
DBModelEntityListCtrl::DBModelEntityListCtrl(wxWindow *parent, DataDesignerContainer *container)
	: DBObjectListCtrl(parent, container)
{
}

DBModelEntityListCtrl::~DBModelEntityListCtrl()
{
}

void DBModelEntityListCtrl::SetObject(long item, DBObject *object)
{
	DBModelEntity *attribute = (DBModelEntity *)object;
}


syntax highlighted by Code2HTML, v. 0.9.1