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 Address
 */
public abstract class BaseAddress extends BaseObject
{
    /** The Peer class */
    private static final AddressPeer peer =
        new AddressPeer();

                  
        /**
         * The value for the id field
         */
        private Integer id;
              
        /**
         * The value for the address field
         */
        private String address;
              
        /**
         * The value for the city field
         */
        private String city;
              
        /**
         * The value for the region field
         */
        private String region;
              
        /**
         * The value for the zip field
         */
        private String zip;
              
        /**
         * The value for the country_id field
         */
        private Integer country_id;
      
      
        /**
         * Get the Id
         *
         * @return Integer
         */
        public Integer getId()
        {
            return id;
        }

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


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

                  
                                                  
              // update associated ContactInfo
              if (collContactInfos != null)
              {
                  for (int i = 0; i < collContactInfos.size(); i++)
                  {
                      ((ContactInfo) collContactInfos.get(i))
                          .setAddressId(v);
                  }
              }
                                   }


        /**
         * Get the Address
         *
         * @return String
         */
        public String getAddress()
        {
            return address;
        }

                                            
        /**
         * Set the value of Address
         *
         * @param v new value
         */
        public void setAddress(String v) 
        {
          


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

                  
                       }


        /**
         * Get the City
         *
         * @return String
         */
        public String getCity()
        {
            return city;
        }

                                            
        /**
         * Set the value of City
         *
         * @param v new value
         */
        public void setCity(String v) 
        {
          


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

                  
                       }


        /**
         * Get the Region
         *
         * @return String
         */
        public String getRegion()
        {
            return region;
        }

                                            
        /**
         * Set the value of Region
         *
         * @param v new value
         */
        public void setRegion(String v) 
        {
          


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

                  
                       }


        /**
         * Get the Zip
         *
         * @return String
         */
        public String getZip()
        {
            return zip;
        }

                                            
        /**
         * Set the value of Zip
         *
         * @param v new value
         */
        public void setZip(String v) 
        {
          


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

                  
                       }


        /**
         * Get the CountryId
         *
         * @return Integer
         */
        public Integer getCountryId()
        {
            return country_id;
        }

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


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

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


 
     
   
             
   
       private Country aCountry;

    /**
     * Declares an association between this object and a Country object
     *
     * @param v Country
     * @throws TorqueException
     */
    public void setCountry(Country v) throws TorqueException
    {
           if (v == null)
        {
                         setCountryId((Integer)null);
                    }
        else
        {
            setCountryId(v.getId());
        }
           aCountry = v;
    }

                 
    /**
     * Get the associated Country object
     *
     * @return the associated Country object
     * @throws TorqueException
     */
    public Country getCountry() throws TorqueException
    {
        if (aCountry == null && (!ObjectUtils.equals(this.country_id, null)))
        {
              aCountry = CountryPeer.retrieveByPK(SimpleKey.keyFor(this.country_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.
               Country obj = CountryPeer.retrieveByPK(this.country_id);
               obj.addAddresss(this);
             */
        }
        return aCountry;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey.  e.g.
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setCountryKey(ObjectKey key) throws TorqueException
    {
    
                                        setCountryId(new Integer(((NumberKey) key).intValue()));
                    }
    
                
      
    /**
     * Collection to store aggregation of collContactInfos
     */
    protected List collContactInfos;

    /**
     * Temporary storage of collContactInfos to save a possible db hit in
     * the event objects are add to the collection, but the
     * complete collection is never requested.
     */
    protected void initContactInfos()
    {
        if (collContactInfos == null)
        {
            collContactInfos = new ArrayList();
        }
    }

    /**
     * Method called to associate a ContactInfo object to this object
     * through the ContactInfo foreign key attribute
     *
     * @param l ContactInfo
     * @throws TorqueException
     */
    public void addContactInfo(ContactInfo l) throws TorqueException
    {
        getContactInfos().add(l);
        l.setAddress((Address) this);
    }

    /**
     * The criteria used to select the current contents of collContactInfos
     */
    private Criteria lastContactInfosCriteria = null;

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getContactInfos(new Criteria())
     *
     * @throws TorqueException
     */
    public List getContactInfos() throws TorqueException
    {
        if (collContactInfos == null)
        {
            collContactInfos = getContactInfos(new Criteria(10));
        }
        return collContactInfos;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Address has previously
     * been saved, it will retrieve related ContactInfos from storage.
     * If this Address is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     *
     * @throws TorqueException
     */
    public List getContactInfos(Criteria criteria) throws TorqueException
    {
        if (collContactInfos == null)
        {
            if (isNew())
            {
               collContactInfos = new ArrayList();
            }
            else
            {
                   criteria.add(ContactInfoPeer.ADDRESS_ID, getId() );
                   collContactInfos = ContactInfoPeer.doSelect(criteria);
            }
        }
        else
        {
            // criteria has no effect for a new object
            if (!isNew())
            {
                // the following code is to determine if a new query is
                // called for.  If the criteria is the same as the last
                // one, just return the collection.
                   criteria.add(ContactInfoPeer.ADDRESS_ID, getId());
                   if (!lastContactInfosCriteria.equals(criteria))
                {
                    collContactInfos = ContactInfoPeer.doSelect(criteria);
                }
            }
        }
        lastContactInfosCriteria = criteria;

        return collContactInfos;
    }

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getContactInfos(new Criteria(),Connection)
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getContactInfos(Connection con) throws TorqueException
    {
        if (collContactInfos == null)
        {
            collContactInfos = getContactInfos(new Criteria(10), con);
        }
        return collContactInfos;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Address has previously
     * been saved, it will retrieve related ContactInfos from storage.
     * If this Address is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getContactInfos(Criteria criteria, Connection con)
            throws TorqueException
    {
        if (collContactInfos == null)
        {
            if (isNew())
            {
               collContactInfos = new ArrayList();
            }
            else
            {
                     criteria.add(ContactInfoPeer.ADDRESS_ID, getId());
                     collContactInfos = ContactInfoPeer.doSelect(criteria, con);
             }
         }
         else
         {
             // criteria has no effect for a new object
             if (!isNew())
             {
                 // the following code is to determine if a new query is
                 // called for.  If the criteria is the same as the last
                 // one, just return the collection.
                     criteria.add(ContactInfoPeer.ADDRESS_ID, getId());
                     if (!lastContactInfosCriteria.equals(criteria))
                 {
                     collContactInfos = ContactInfoPeer.doSelect(criteria, con);
                 }
             }
         }
         lastContactInfosCriteria = criteria;

         return collContactInfos;
     }

     

     
      
         
          
                    
                
        
        
    
      
      
              
                            
                
        
        
    
      
      
              
                            
                
        
        
      



     
    
    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("Address");
            fieldNames.add("City");
            fieldNames.add("Region");
            fieldNames.add("Zip");
            fieldNames.add("CountryId");
            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("Address"))
    {
              return getAddress();
          }
            if (name.equals("City"))
    {
              return getCity();
          }
            if (name.equals("Region"))
    {
              return getRegion();
          }
            if (name.equals("Zip"))
    {
              return getZip();
          }
            if (name.equals("CountryId"))
    {
              return getCountryId();
          }
            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(AddressPeer.ID))
        {
              return getId();
          }
            if (name.equals(AddressPeer.ADDRESS))
        {
              return getAddress();
          }
            if (name.equals(AddressPeer.CITY))
        {
              return getCity();
          }
            if (name.equals(AddressPeer.REGION))
        {
              return getRegion();
          }
            if (name.equals(AddressPeer.ZIP))
        {
              return getZip();
          }
            if (name.equals(AddressPeer.COUNTRY_ID))
        {
              return getCountryId();
          }
            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 getAddress();
          }
            if (pos == 2)
    {
              return getCity();
          }
            if (pos == 3)
    {
              return getRegion();
          }
            if (pos == 4)
    {
              return getZip();
          }
            if (pos == 5)
    {
              return getCountryId();
          }
                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(AddressPeer.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())
            {
                AddressPeer.doInsert((Address) this, con);
                setNew(false);
            }
            else
            {
                AddressPeer.doUpdate((Address) this, con);
            }
        }

                                    
                
          if (collContactInfos != null)
          {
              for (int i = 0; i < collContactInfos.size(); i++)
              {
                  ((ContactInfo) collContactInfos.get(i)).save(con);
              }
          }
                  alreadyInSave = false;
      }
      }


                        
    
    

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

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    public void setPrimaryKey(String key) throws TorqueException
    {
                    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 Address copy() throws TorqueException
    {
        return copyInto(new Address());
    }

    protected Address copyInto(Address copyObj) throws TorqueException
    {
        copyObj.setId(id);
        copyObj.setAddress(address);
        copyObj.setCity(city);
        copyObj.setRegion(region);
        copyObj.setZip(zip);
        copyObj.setCountryId(country_id);

  copyObj.setNew(false);
                                    
                
        List v = getContactInfos();
        for (int i = 0; i < v.size(); i++)
        {
            ContactInfo obj = (ContactInfo) v.get(i);
            copyObj.addContactInfo(obj.copy());
            ((Persistent) v.get(i)).setNew(true);
        }
                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 AddressPeer getPeer()
    {
        return peer;
    }
}
