/* example how to create dictionary from scratch */
/* we have to have DDL anyway   */
#include "DDLFileObj.h"
#include "DICFileObj.h"
#include <iostream.h>


#define FALSE 0
int main() {
  DICFileObj * fobjR = NULL;

  char *diags=NULL;
  ISTable *ddlformat;
  DDLFileObj * ddl = NULL;
  ISTable* format;
  CifString datablockName = "Dictionary";
  CifString cs;
  


  ddl = new DDLFileObj((char*)"ddl", WRITE_MODE, 80, "?", 1);
  ddlformat = new ISTable("ddlformat");
  ddl->Read((char*)"./test/ddl-mm",ddlformat, diags);
  cout<<"diags = ";
  if (diags != NULL)
  {
    cout<<diags;
  }
  cout<<endl;
/*
 Table format defines how save frames look like. This table
has three rows:
    dbName - is datablock name. This is the same datablock
name as datablock name in dictionary. If we have more than
one datablock, than we have to define this for every
datablock.
    type - this column defines where to put data from
particular table. It can have one of three values: data,
category, item. Data means that value from table (column
catName) will be put out of any save frame, as in data file.
"category"/"item" means that data from table will be put in
category/item save frame.
    catName - is name of table

example:
    dbName  = cif_mm.dic
    type          = category
    catName = category_key
this means that data from table category_key which is
belongs to cif_mm.dic dictionary (datablock) will be put in
category save frame.

    For every table it has to be defined one row in this
format table, otherwise this information will be lost. You
can just see them in tabulated written cif file.
*/


  format = new ISTable("format");
  format->AddColumn("dbName");
  format->AddColumn("type");
  format->AddColumn("catName");
  format->AddRow();
  format->UpdateCell(datablockName,0,format->GetLastRowIndex());
  cs = "category";
  format->UpdateCell(cs,1,format->GetLastRowIndex());
  format->UpdateCell(cs,2,format->GetLastRowIndex());

  format->AddRow();
  format->UpdateCell(datablockName,0,format->GetLastRowIndex());
  cs = "item";
  format->UpdateCell(cs,1,format->GetLastRowIndex());
  format->UpdateCell(cs,2,format->GetLastRowIndex());

  fobjR = new DICFileObj((char*)"./test/TESTFILE15", WRITE_MODE, 80, "?", 1);

  int colIndex;

  ISTable *tbl;
/*
    the following line describe how to add/update tables,
information, new definitions to the dictionary.
    Tables category and item have to exist, and have to have
all columns that are defined here, in this example
*/
  tbl = new ISTable("category");
  tbl->AddColumn("description");
  tbl->AddColumn("mandatory_code");
  tbl->AddColumn("id");
  tbl->AddColumn("implicit_key");

  tbl->AddRow();

  /* GetLastRowIndex() is different from GetNumRows() because some rows might be deleted,
     they actually are marked as deleted, but they are stil in a table
  */
  colIndex = tbl->GetColumnIndex("mandatory_code");
  cs="no";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("id");
  cs="category_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("description");
  cs="Description for category_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("implicit_key");
  tbl->UpdateCell(datablockName,colIndex,tbl->GetLastRowIndex());

  fobjR->WriteTable(tbl,datablockName.Text(),"category");

  tbl = new ISTable("item");
  tbl->AddColumn("name");
  tbl->AddColumn("category_id");
  tbl->AddColumn("mandatory_code");
  tbl->AddRow();
  colIndex = tbl->GetColumnIndex("name");
  cs="item_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("category_id");
  cs="category_1";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());

  colIndex = tbl->GetColumnIndex("mandatory_code");
  cs="no";
  tbl->UpdateCell(cs,colIndex,tbl->GetLastRowIndex());
  fobjR->WriteTable(tbl,datablockName.Text(),"item");

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

  fobjR->Write("./test/Test15.ocif");
  fobjR->Write("./test/Test15.dic",ddl,format);
  fobjR->CloseFile(0);
  if (fobjR) delete fobjR;
  exit(0);
}
