<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
<!DOCTYPE database SYSTEM "http://jakarta.apache.org/turbine/dtd/database.dtd">
<!-- $Id: ... $ -->

<!--
   file:///home/max/download/torque/torque-doc/tutorial/step2.html
	There are several items of importance to note. The database element's
	name attribute must be the same as the database name specified by the
	databaseUrl property in project.properties; likewise, the run-time
	properties (described in the next section) should also reflect this
	value. Failure to do so will prevent Torque from creating your database
	tables (if instructed to do so) or prevent your object model from
	working properly.
-->
<database
  name="codexrerum"
  defaultIdMethod="native"
  defaultJavaNamingMethod="nochange">


<!--
  ParentID:
    Items can have other items as their parents.
    This is used to reflect situations such as harddrive X is
    currently used in server Y.
-->
<table name="Item">
	<column name="ItemID" required="true" primaryKey="true" type="INTEGER" autoIncrement="true"/>
	<column name="Name" required="true" type="VARCHAR" size="100"/>
	<column name="InventoryNumber" required="true" type="VARCHAR" size="100"/>
	<!--
	    Business-Logic: Either "ParentID" OR "LocationID" can be NULL (but not both):
		In the first level of the hierarchy, there is no ParentID,
		so it has to be NULL, so we can't set 'required="true"' for
		the ParentID-column. Nevertheless, a hierarch-top-level-entry
		must have a location, so it LocationID not allowed to be NULL
		too in this case. (Example: The machine "AQUA".)
		In lower levels of an item-hierarchy, ParentID is set to the
		direct parent. In such a case, the item belongs to its parent,
		in which case it has the same location as its parent. This is
		represented by LocationID=NULL. (Example: A harddrive belonging
		to the machine AQUA.)
	-->
	<column name="ParentID" type="INTEGER"/>
	<column name="LocationID" type="INTEGER"/>
	<column name="StatusID" required="true" type="INTEGER"/>
	<column name="SpeciesID" required="true" type="INTEGER"/>
	<column name="CreatedByID" required="true" type="INTEGER"/>
	<column name="LastModifiedByID" required="true" type="INTEGER"/>
	<column name="DateCreated" required="true" type="DATE"/>
	<column name="DateModified" required="true" type="DATE"/>
	<column name="DatePurchased" required="true" type="DATE"/>
	<column name="SerialNumber" type="VARCHAR" size="50"/>
	<column name="Price" type="FLOAT"/>
	<column name="Memo" type="LONGVARCHAR"/>
	<!--
		Torque's "LONGVARCHAR" reflects MySQL's "TEXT".
	-->
	
	<index name="idx_itemid">
		<index-column name="ItemID"/>
	</index>
	<index name="idx_invnum">
		<index-column name="InventoryNumber"/>
	</index>
	<index name="idx_parentid">
		<index-column name="ParentID"/>
	</index>
	<index name="idx_statusid">
		<index-column name="StatusID"/>
	</index>
	<index name="idx_locationid">
		<index-column name="LocationID"/>
	</index>
	<index name="idx_speciesid">
		<index-column name="SpeciesID"/>
	</index>
	<index name="idx_createdbyid">
		<index-column name="CreatedByID"/>
	</index>
	<index name="idx_lastmodifiedbyid">
		<index-column name="LastModifiedByID"/>
	</index>

	<foreign-key foreignTable="Item" name="fk_parent">
		<reference local="ParentID" foreign="ItemID"/>
	</foreign-key>
	<foreign-key foreignTable="Status" name="fk_status">
		<reference local="StatusID" foreign="StatusID"/>
	</foreign-key>
	<foreign-key foreignTable="Location" name="fk_location">
		<reference local="LocationID" foreign="LocationID"/>
	</foreign-key>
	<foreign-key foreignTable="Species" name="fk_species">
		<reference local="SpeciesID" foreign="SpeciesID"/>
	</foreign-key>
	<foreign-key foreignTable="User" name="fk_user_created">
		<reference local="CreatedByID" foreign="UserID"/>
	</foreign-key>
	<foreign-key foreignTable="User" name="fk_user_modified">
		<reference local="LastModifiedByID" foreign="UserID"/>
	</foreign-key>

	<unique name="unq_invnum">
		<unique-column name="InventoryNumber"/>
	</unique>
</table>


<table name="Status">
	<column name="StatusID" required="true" primaryKey="true" type="INTEGER" autoIncrement="true"/>
	<column name="Name" required="true" type="VARCHAR" size="100"/>
	<column name="Description" type="VARCHAR" size="100"/>
	
	<index name="idx_statusid">
		<index-column name="StatusID"/>
	</index>
	<index name="id_name">
		<index-column name="Name"/>
	</index>

	<unique name="unq_name">
		<unique-column name="Name"/>
	</unique>
</table>


<table name="Location">
	<column name="LocationID" required="true" primaryKey="true" type="INTEGER" autoIncrement="true"/>
	<column name="Name" required="true" type="VARCHAR" size="100"/>
	<column name="Description" type="VARCHAR" size="100"/>
	
	<index name="idx_locationid">
		<index-column name="LocationID"/>
	</index>

	<unique name="unq_name">
		<unique-column name="Name"/>
	</unique>
</table>


<!--
  ParentID:
    (Categories) Species can have other categories as their parents.
    This is used to build a hierarchy of (categories) species, such as:
       * Subnet Inventory
          * Hardware
	     * Harddrives
	     * Printeres
	  * Software
-->
<!--
	You can't use "Category" as table- (?and/or?) column-name,
	as Torque will create Java-code with methods such as
	"BaseItem.getCategory()" (with Category being referenced
	by a foreign-key in table Item).
	Nevertheless, BaseItem extends Torque's "BaseObject", which
	already holds a method "getCategory()".
	Trying to use such a table will result in Eclipse complaining
	about the "return type of BaseItem.getCategory() being
	incompatible to BaseObject.getCategory()".
-->
<table name="Species">
	<column name="SpeciesID" required="true" primaryKey="true" type="INTEGER" autoIncrement="true"/>
	<column name="Name" required="true" type="VARCHAR" size="100"/>
	<column name="Description" type="VARCHAR" size="100"/>
	<column name="ParentID" type="INTEGER"/>
	
	<index name="idx_speciesid">
		<index-column name="SpeciesID"/>
	</index>
	<index name="idx_name">
		<index-column name="Name"/>
	</index>
	<index name="idx_parentid">
		<index-column name="ParentID"/>
	</index>
	
	<foreign-key foreignTable="Species" name="fk_parent">
		<reference local="ParentID" foreign="SpeciesID"/>
	</foreign-key>
	
	<unique name="unq_name">
		<unique-column name="Name"/>
	</unique>
</table>


<table name="User">
	<column name="UserID" required="true" primaryKey="true" type="INTEGER" autoIncrement="true"/>
	<column name="Username" required="true" type="VARCHAR" size="50"/>
	<column name="FirstName" required="true" type="VARCHAR" size="50"/>
	<column name="LastName" required="true" type="VARCHAR" size="50"/>
	<column name="Password" required="true" type="VARCHAR" size="50"/>
	<column name="eMail" required="true" type="VARCHAR" size="150"/>
	<column name="CreateDate" required="true" type="DATE"/>
	<column name="LastLogin" required="true" type="DATE"/>
	<column name="LastModified" required="true" type="DATE"/>
	<column name="Deleted" required="true" type="BOOLEANINT"/>

	<index name="idx_id">
		<index-column name="UserID"/>
	</index>
	<index name="idx_name">
		<index-column name="Username"/>
	</index>

	<unique name="unq_name">
		<unique-column name="Username"/>
	</unique>
</table>


</database>


