<?php
require_once('objects/types.php');
require_once('objects/Entity.php');
require_once('objects/EntityList.php');
require_once('util/utilityFunctions.php');

$files = array(
               'eml-access.xsd',
               'eml-coverage.xsd',
               'eml-documentation.xsd',
               'eml-literature.xsd',
               'eml-party.xsd',
               'eml-project.xsd',
               'eml-resource.xsd',
               'eml-text.xsd',
               'tcsv101.xsd',
               'tdwg_basetypes.xsd',
#               'tdwg_dw_element.xsd',
               'tdwg_dw_geospatial.xsd',
               'veg-misc.xsd',
               'veg-organismobservation.xsd',
               'veg-plotobservation.xsd',
               'veg.xsd',
               'veg-plot.xsd');


$eList = new EntityList();
foreach($files as $file) {
  global $eList;

  $xmlDoc = new DOMDocument();
  $xmlDoc->load($file);
  $xpath = new DOMXPath($xmlDoc);
  $nodes = $xpath->query("child::node()",$xmlDoc->documentElement);
  foreach($nodes as $node) {
    $nodeName = preg_replace("/.*:/","",$node->nodeName);
    if(isIgnoreType($nodeName) || isPrimitiveType($node->getAttribute('name'))) {
      continue;
    }
    switch($nodeName) { 
      case 'group':
        if(!hasImportantNodes($node)) {
          die("Group nodes currently must define an entity. Group node in file: $file does not. Exiting.\n");
        }
      case 'simpleType':
      case 'complexType':
        $e = $eList->newEntity($node->getAttribute('name'),$node);
        break;
      case 'element':
        $eName = preg_replace("/\..*$/","",$file);
        $eName = preg_replace("/-/","_",$eName);
        $eParent = $eList->getEntityForReference($eName);

        $eParent->handleElementCase($node);
        break;
      default:
          die("Unsupported node type: $nodeName found in file: $file.\n");
    }
  }
}

$entities = $eList->getEntities();
print "#There are " . count($entities) . " defined entities.\n";
print "import string\n";
print "import xml.dom.minidom\n";
print "from random import choice\n";
print "from django.db import models\n\n";
print "def getRandom(length=8, chars=string.letters + string.digits):\n".
      "  return ''.join([choice(chars) for i in range(length)])\n\n".
      "def getRandomString():\n".
      "  return getRandom(45)\n\n".
      "def getRandomText():\n".
      "  return getRandom(450)\n\n".
      "def getRandomInt():\n".
      "  return int(getRandom(4,string.digits))\n\n".
      "def getRandomFloat():\n".
      "  return float(getRandom(4,string.digits))\n\n".
      "def getRandomBool():\n".
      "  return choice(['true','false'])\n\n".
      "#Not actually random, but not worth the time\n".
      "def getRandomDate():\n".
      "  return '2007-07-16'\n\n".
      "def getRandomTime():\n".
      "  return '08:30:00'\n\n";

$eList->printAll();

?>

