// Do not edit this file!  It was generated by Melati POEM's DSD preprocessor.

package org.melati.util.test.generated;


import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import org.melati.poem.AccessPoemException;
import org.melati.poem.CachedSelection;
import org.melati.poem.Column;
import org.melati.poem.Field;
import org.melati.poem.JdbcPersistent;
import org.melati.poem.NoSuchRowPoemException;
import org.melati.poem.ValidationPoemException;
import org.melati.poem.util.EmptyEnumeration;
import org.melati.util.test.Node;
import org.melati.util.test.NodeTable;
import org.melati.util.test.TreeDatabaseTables;


/**
 * Melati POEM generated abstract base class for a <code>Persistent</code> 
 * <code>Node</code> Object.
 *
 * see org.melati.poem.prepro.TableDef#generatePersistentBaseJava 
 */
public abstract class NodeBase extends JdbcPersistent {


 /**
  * Retrieves the Database object.
  * 
  * see org.melati.poem.prepro.TableDef#generatePersistentBaseJava 
  * @return the database
  */
  public TreeDatabaseTables getTreeDatabaseTables() {
    return (TreeDatabaseTables)getDatabase();
  }


 /**
  * Retrieves the  <code>NodeTable</code> table 
  * which this <code>Persistent</code> is from.
  * 
  * see org.melati.poem.prepro.TableDef#generatePersistentBaseJava 
  * @return the NodeTable
  */
  public NodeTable getNodeTable() {
    return (NodeTable)getTable();
  }

  private NodeTable _getNodeTable() {
    return (NodeTable)getTable();
  }

  // Fields in this table 
 /**
  * id 
  */
  protected Integer id;
 /**
  * name - Contact Name 
  */
  protected String name;
 /**
  * parent - Parent of this Node 
  */
  protected Integer parent;


 /**
  * Retrieves the <code>Id</code> value, without locking, 
  * for this <code>Node</code> <code>Persistent</code>.
  *
  * see org.melati.poem.prepro.FieldDef#generateBaseMethods 
  * @return the Integer id
  */
  public Integer getId_unsafe() {
    return id;
  }


 /**
  * Sets the <code>Id</code> value directly, without checking, 
  * for this Node <code>Persistent</code>.
  * 
  * see org.melati.poem.prepro.FieldDef#generateBaseMethods 
  * @param cooked  the pre-validated value to set
  */
  public void setId_unsafe(Integer cooked) {
    id = cooked;
  }

 /**
  * Retrieves the Id value, with locking, for this 
  * <code>Node</code> <code>Persistent</code>.
  * 
  * Generated by org.melati.poem.prepro.AtomFieldDef#generateBaseMethods 
  * @throws AccessPoemException 
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights 
  * @return the value of the field <code>Id</code> for this 
  *         <code>Node</code> <code>Persistent</code>  
  */

  public Integer getId()
      throws AccessPoemException {
    readLock();
    return getId_unsafe();
  }


 /**
  * Sets the <code>Id</code> value, with checking, for this 
  * <code>Node</code> <code>Persistent</code>.
  * 
  * Generated by org.melati.poem.prepro.AtomFieldDef#generateBaseMethods  
  * @param cooked  a validated <code>int</code> 
  * @throws AccessPoemException 
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights
  * @throws ValidationPoemException 
  *         if the value is not valid
  */
  public void setId(Integer cooked)
      throws AccessPoemException, ValidationPoemException {
    _getNodeTable().getIdColumn().
      getType().assertValidCooked(cooked);
    writeLock();
    setId_unsafe(cooked);
  }

 /**
  * Sets the <code>Id</code> value, with checking, for this 
  * <code>Node</code> <code>Persistent</code>.
  * 
  * Generated by org.melati.poem.prepro.IntegerFieldDef#generateBaseMethods 
  * @param cooked  a validated <code>int</code>
  * @throws AccessPoemException 
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights
  * @throws ValidationPoemException 
  *         if the value is not valid
  */

  public final void setId(int cooked)
      throws AccessPoemException, ValidationPoemException {
    setId(new Integer(cooked));
  }


 /**
  * Retrieves the <code>Id</code> value as a <code>Field</code>
  * from this <code>Node</code> <code>Persistent</code>.
  * 
  * see org.melati.poem.prepro.FieldDef#generateFieldCreator 
  * @throws AccessPoemException 
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights
  * @return the Integer id
  */
  public Field<Integer> getIdField() throws AccessPoemException {
    Column<Integer> c = _getNodeTable().getIdColumn();
    return new Field<Integer>((Integer)c.getRaw(this), c);
  }


 /**
  * Retrieves the <code>Name</code> value, without locking, 
  * for this <code>Node</code> <code>Persistent</code>.
  *
  * see org.melati.poem.prepro.FieldDef#generateBaseMethods 
  * @return the String name
  */
  public String getName_unsafe() {
    return name;
  }


 /**
  * Sets the <code>Name</code> value directly, without checking, 
  * for this Node <code>Persistent</code>.
  * 
  * see org.melati.poem.prepro.FieldDef#generateBaseMethods 
  * @param cooked  the pre-validated value to set
  */
  public void setName_unsafe(String cooked) {
    name = cooked;
  }

 /**
  * Retrieves the Name value, with locking, for this 
  * <code>Node</code> <code>Persistent</code>.
  * Field description: 
  *   Contact Name 
  * 
  * Generated by org.melati.poem.prepro.AtomFieldDef#generateBaseMethods 
  * @throws AccessPoemException 
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights 
  * @return the value of the field <code>Name</code> for this 
  *         <code>Node</code> <code>Persistent</code>  
  */

  public String getName()
      throws AccessPoemException {
    readLock();
    return getName_unsafe();
  }


 /**
  * Sets the <code>Name</code> value, with checking, for this 
  * <code>Node</code> <code>Persistent</code>.
  * Field description: 
  *   Contact Name 
  * 
  * Generated by org.melati.poem.prepro.AtomFieldDef#generateBaseMethods  
  * @param cooked  a validated <code>int</code> 
  * @throws AccessPoemException 
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights
  * @throws ValidationPoemException 
  *         if the value is not valid
  */
  public void setName(String cooked)
      throws AccessPoemException, ValidationPoemException {
    _getNodeTable().getNameColumn().
      getType().assertValidCooked(cooked);
    writeLock();
    setName_unsafe(cooked);
  }


 /**
  * Retrieves the <code>Name</code> value as a <code>Field</code>
  * from this <code>Node</code> <code>Persistent</code>.
  * 
  * see org.melati.poem.prepro.FieldDef#generateFieldCreator 
  * @throws AccessPoemException 
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights
  * @return the String name
  */
  public Field<String> getNameField() throws AccessPoemException {
    Column<String> c = _getNodeTable().getNameColumn();
    return new Field<String>((String)c.getRaw(this), c);
  }


 /**
  * Retrieves the <code>Parent</code> value, without locking, 
  * for this <code>Node</code> <code>Persistent</code>.
  *
  * see org.melati.poem.prepro.FieldDef#generateBaseMethods 
  * @return the Integer parent
  */
  public Integer getParent_unsafe() {
    return parent;
  }


 /**
  * Sets the <code>Parent</code> value directly, without checking, 
  * for this Node <code>Persistent</code>.
  * 
  * see org.melati.poem.prepro.FieldDef#generateBaseMethods 
  * @param cooked  the pre-validated value to set
  */
  public void setParent_unsafe(Integer cooked) {
    parent = cooked;
  }

 /**
  * Retrieves the Table Row Object ID. 
  *
  * Generated by org.melati.poem.prepro.ReferenceFieldDef#generateBaseMethods 
  * @throws AccessPoemException  
  *         if the current <code>AccessToken</code> 
  *         does not confer read access rights 
  * @return the TROID as an <code>Integer</code> 
  */

  public Integer getParentTroid()
      throws AccessPoemException {
    readLock();
    return getParent_unsafe();
  }


 /**
  * Sets the Table Row Object ID. 
  * 
  * Generated by org.melati.poem.prepro.ReferenceFieldDef#generateBaseMethods 
  * @param raw  a Table Row Object Id 
  * @throws AccessPoemException  
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights
  */
  public void setParentTroid(Integer raw)
      throws AccessPoemException {
    setParent(raw == null ? null : 
        getTreeDatabaseTables().getNodeTable().getNodeObject(raw));
  }


 /**
  * Retrieves the <code>Parent</code> object referred to.
  *  
  * Generated by org.melati.poem.prepro.ReferenceFieldDef#generateBaseMethods 
  * @throws AccessPoemException  
  *         if the current <code>AccessToken</code> 
  *         does not confer read access rights 
  * @throws NoSuchRowPoemException  
  *         if the <code>Persistent</code> has yet to be allocated a TROID 
  * @return the <code>Parent</code> as a <code>Node</code> 
  */
  public Node getParent()
      throws AccessPoemException, NoSuchRowPoemException {
    Integer troid = getParentTroid();
    return troid == null ? null :
        getTreeDatabaseTables().getNodeTable().getNodeObject(troid);
  }


 /**
  * Set the Parent.
  * 
  * Generated by org.melati.poem.prepro.ReferenceFieldDef#generateBaseMethods 
  * @param cooked  a validated <code>Node</code>
  * @throws AccessPoemException  
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights 
  */
  public void setParent(Node cooked)
      throws AccessPoemException {
    _getNodeTable().
      getParentColumn().
        getType().assertValidCooked(cooked);
    writeLock();
    if (cooked == null)
      setParent_unsafe(null);
    else {
      cooked.existenceLock();
      setParent_unsafe(cooked.troid());
    }
  }


 /**
  * Retrieves the <code>Parent</code> value as a <code>Field</code>
  * from this <code>Node</code> <code>Persistent</code>.
  * 
  * see org.melati.poem.prepro.FieldDef#generateFieldCreator 
  * @throws AccessPoemException 
  *         if the current <code>AccessToken</code> 
  *         does not confer write access rights
  * @return the Integer parent
  */
  public Field<Integer> getParentField() throws AccessPoemException {
    Column<Integer> c = _getNodeTable().getParentColumn();
    return new Field<Integer>((Integer)c.getRaw(this), c);
  }

  private CachedSelection<Node> parentNodes = null;
  /** References to this Node in the Node table via its parent field.*/
  @SuppressWarnings("unchecked")
  public Enumeration<Node> getParentNodes() {
    if (getTroid() == null)
      return new EmptyEnumeration<Node>();
    else {
      if (parentNodes == null)
        parentNodes =
          getTreeDatabaseTables().getNodeTable().getParentColumn().cachedSelectionWhereEq(getTroid());
      return parentNodes.objects();
    }
  }


  /** References to this Node in the Node table via its parent field, as a List.*/
  public List<Node> getParentNodeList() {
    return Collections.list(getParentNodes());
  }



}

