package org.thdl.roster.om;


import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.util.Criteria;
import org.apache.torque.util.Transaction;

   
      
   
/**
 * This class was autogenerated by Torque on:
 *
 * [Wed May 14 19:01:42 EDT 2003]
 *
 * You should not use this class directly.  It should not even be
 * extended all references should be to ResearchInterestDiscipline
 */
public abstract class BaseResearchInterestDiscipline extends BaseObject
{
    /** The Peer class */
    private static final ResearchInterestDisciplinePeer peer =
        new ResearchInterestDisciplinePeer();

                  
        /**
         * The value for the id field
         */
        private Integer id;
              
        /**
         * The value for the discipline_id field
         */
        private Integer discipline_id;
              
        /**
         * The value for the research_interest_id field
         */
        private Integer research_interest_id;
              
        /**
         * The value for the relevance field
         */
        private Integer relevance;
      
      
        /**
         * Get the Id
         *
         * @return Integer
         */
        public Integer getId()
        {
            return id;
        }

                                            
        /**
         * Set the value of Id
         *
         * @param v new value
         */
        public void setId(Integer v) 
        {
          


         if (!ObjectUtils.equals(this.id, v))
        {
             this.id = v;
            setModified(true);
        }

                  
                       }


        /**
         * Get the DisciplineId
         *
         * @return Integer
         */
        public Integer getDisciplineId()
        {
            return discipline_id;
        }

                                                      
        /**
         * Set the value of DisciplineId
         *
         * @param v new value
         */
        public void setDisciplineId(Integer v) throws TorqueException
        {
          


         if (!ObjectUtils.equals(this.discipline_id, v))
        {
             this.discipline_id = v;
            setModified(true);
        }

                                          
                if (aDiscipline != null && !ObjectUtils.equals(aDiscipline.getId(), v))
                {
            aDiscipline = null;
        }
          
                       }


        /**
         * Get the ResearchInterestId
         *
         * @return Integer
         */
        public Integer getResearchInterestId()
        {
            return research_interest_id;
        }

                                                      
        /**
         * Set the value of ResearchInterestId
         *
         * @param v new value
         */
        public void setResearchInterestId(Integer v) throws TorqueException
        {
          


         if (!ObjectUtils.equals(this.research_interest_id, v))
        {
             this.research_interest_id = v;
            setModified(true);
        }

                                          
                if (aResearchInterest != null && !ObjectUtils.equals(aResearchInterest.getId(), v))
                {
            aResearchInterest = null;
        }
          
                       }


        /**
         * Get the Relevance
         *
         * @return Integer
         */
        public Integer getRelevance()
        {
            return relevance;
        }

                                            
        /**
         * Set the value of Relevance
         *
         * @param v new value
         */
        public void setRelevance(Integer v) 
        {
          


         if (!ObjectUtils.equals(this.relevance, v))
        {
             this.relevance = v;
            setModified(true);
        }

                  
                       }


 
     
   
             
   
       private ResearchInterest aResearchInterest;

    /**
     * Declares an association between this object and a ResearchInterest object
     *
     * @param v ResearchInterest
     * @throws TorqueException
     */
    public void setResearchInterest(ResearchInterest v) throws TorqueException
    {
           if (v == null)
        {
                         setResearchInterestId((Integer)null);
                    }
        else
        {
            setResearchInterestId(v.getId());
        }
           aResearchInterest = v;
    }

                 
    /**
     * Get the associated ResearchInterest object
     *
     * @return the associated ResearchInterest object
     * @throws TorqueException
     */
    public ResearchInterest getResearchInterest() throws TorqueException
    {
        if (aResearchInterest == null && (!ObjectUtils.equals(this.research_interest_id, null)))
        {
              aResearchInterest = ResearchInterestPeer.retrieveByPK(SimpleKey.keyFor(this.research_interest_id));
      
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               ResearchInterest obj = ResearchInterestPeer.retrieveByPK(this.research_interest_id);
               obj.addResearchInterestDisciplines(this);
             */
        }
        return aResearchInterest;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey.  e.g.
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setResearchInterestKey(ObjectKey key) throws TorqueException
    {
    
                                        setResearchInterestId(new Integer(((NumberKey) key).intValue()));
                    }
 
   
             
   
       private Discipline aDiscipline;

    /**
     * Declares an association between this object and a Discipline object
     *
     * @param v Discipline
     * @throws TorqueException
     */
    public void setDiscipline(Discipline v) throws TorqueException
    {
           if (v == null)
        {
                         setDisciplineId((Integer)null);
                    }
        else
        {
            setDisciplineId(v.getId());
        }
           aDiscipline = v;
    }

                 
    /**
     * Get the associated Discipline object
     *
     * @return the associated Discipline object
     * @throws TorqueException
     */
    public Discipline getDiscipline() throws TorqueException
    {
        if (aDiscipline == null && (!ObjectUtils.equals(this.discipline_id, null)))
        {
              aDiscipline = DisciplinePeer.retrieveByPK(SimpleKey.keyFor(this.discipline_id));
      
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               Discipline obj = DisciplinePeer.retrieveByPK(this.discipline_id);
               obj.addResearchInterestDisciplines(this);
             */
        }
        return aDiscipline;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey.  e.g.
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setDisciplineKey(ObjectKey key) throws TorqueException
    {
    
                                        setDisciplineId(new Integer(((NumberKey) key).intValue()));
                    }
    
        
    
    private static List fieldNames = null;

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static synchronized List getFieldNames()
    {
      if (fieldNames == null)
      {
        fieldNames = new ArrayList();
            fieldNames.add("Id");
            fieldNames.add("DisciplineId");
            fieldNames.add("ResearchInterestId");
            fieldNames.add("Relevance");
            fieldNames = Collections.unmodifiableList(fieldNames);
      }
      return fieldNames;
    }

    /**
     * Retrieves a field from the object by name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    public Object getByName(String name)
    {
            if (name.equals("Id"))
    {
              return getId();
          }
            if (name.equals("DisciplineId"))
    {
              return getDisciplineId();
          }
            if (name.equals("ResearchInterestId"))
    {
              return getResearchInterestId();
          }
            if (name.equals("Relevance"))
    {
              return getRelevance();
          }
            return null;
    }
    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    public Object getByPeerName(String name)
    {
            if (name.equals(ResearchInterestDisciplinePeer.ID))
        {
              return getId();
          }
            if (name.equals(ResearchInterestDisciplinePeer.DISCIPLINE_ID))
        {
              return getDisciplineId();
          }
            if (name.equals(ResearchInterestDisciplinePeer.RESEARCH_INTEREST_ID))
        {
              return getResearchInterestId();
          }
            if (name.equals(ResearchInterestDisciplinePeer.RELEVANCE))
        {
              return getRelevance();
          }
            return null;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    public Object getByPosition(int pos)
    {
            if (pos == 0)
    {
              return getId();
          }
            if (pos == 1)
    {
              return getDisciplineId();
          }
            if (pos == 2)
    {
              return getResearchInterestId();
          }
            if (pos == 3)
    {
              return getRelevance();
          }
                return null;
    }

     


    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws Exception
     */
    public void save() throws Exception
    {
             save(ResearchInterestDisciplinePeer.getMapBuilder()
                .getDatabaseMap().getName());
     }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
     *
     * @param dbName
     * @throws TorqueException
     */
    public void save(String dbName) throws TorqueException
    {
        Connection con = null;
         try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }

     }

      /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
      /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    public void save(Connection con) throws TorqueException
    {
        if (!alreadyInSave)
      {
        alreadyInSave = true;



  
        // If this object has been modified, then save it to the database.
        if (isModified())
        {
            if (isNew())
            {
                ResearchInterestDisciplinePeer.doInsert((ResearchInterestDiscipline) this, con);
                setNew(false);
            }
            else
            {
                ResearchInterestDisciplinePeer.doUpdate((ResearchInterestDiscipline) this, con);
            }
        }

              alreadyInSave = false;
      }
      }


                
    
    

        /**
     * Set the PrimaryKey using ObjectKey.
     *
     * @param  id ObjectKey
     */
    public void setPrimaryKey(ObjectKey key)
        
    {
                    setId(new Integer(((NumberKey) key).intValue()));
            }

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    public void setPrimaryKey(String key) 
    {
                    setId(new Integer(key));
            }


    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
        return SimpleKey.keyFor(getId());
    }

 

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
     */
    public ResearchInterestDiscipline copy() throws TorqueException
    {
        return copyInto(new ResearchInterestDiscipline());
    }

    protected ResearchInterestDiscipline copyInto(ResearchInterestDiscipline copyObj) throws TorqueException
    {
        copyObj.setId(id);
        copyObj.setDisciplineId(discipline_id);
        copyObj.setResearchInterestId(research_interest_id);
        copyObj.setRelevance(relevance);

  copyObj.setNew(false);
      copyObj.setNew(true);

                      copyObj.setId((Integer)null);
                                return copyObj;
    }

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public ResearchInterestDisciplinePeer getPeer()
    {
        return peer;
    }
}
