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

                  
        /**
         * The value for the id field
         */
        private Integer id;
              
        /**
         * The value for the contact_name field
         */
        private String contact_name;
              
        /**
         * The value for the email field
         */
        private String email;
              
        /**
         * The value for the website field
         */
        private String website;
              
        /**
         * The value for the phone field
         */
        private Integer phone;
              
        /**
         * The value for the fax field
         */
        private Integer fax;
              
        /**
         * The value for the address_id field
         */
        private Integer address_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 Member
              if (collMembers != null)
              {
                  for (int i = 0; i < collMembers.size(); i++)
                  {
                      ((Member) collMembers.get(i))
                          .setContactInfoId(v);
                  }
              }
                                   }


        /**
         * Get the ContactName
         *
         * @return String
         */
        public String getContactName()
        {
            return contact_name;
        }

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


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

                  
                       }


        /**
         * Get the Email
         *
         * @return String
         */
        public String getEmail()
        {
            return email;
        }

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


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

                  
                       }


        /**
         * Get the Website
         *
         * @return String
         */
        public String getWebsite()
        {
            return website;
        }

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


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

                  
                       }


        /**
         * Get the Phone
         *
         * @return Integer
         */
        public Integer getPhone()
        {
            return phone;
        }

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


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

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


        /**
         * Get the Fax
         *
         * @return Integer
         */
        public Integer getFax()
        {
            return fax;
        }

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


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

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


        /**
         * Get the AddressId
         *
         * @return Integer
         */
        public Integer getAddressId()
        {
            return address_id;
        }

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


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

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


 
     
   
             
   
       private Address aAddress;

    /**
     * Declares an association between this object and a Address object
     *
     * @param v Address
     * @throws TorqueException
     */
    public void setAddress(Address v) throws TorqueException
    {
           if (v == null)
        {
                         setAddressId((Integer)null);
                    }
        else
        {
            setAddressId(v.getId());
        }
           aAddress = v;
    }

                 
    /**
     * Get the associated Address object
     *
     * @return the associated Address object
     * @throws TorqueException
     */
    public Address getAddress() throws TorqueException
    {
        if (aAddress == null && (!ObjectUtils.equals(this.address_id, null)))
        {
              aAddress = AddressPeer.retrieveByPK(SimpleKey.keyFor(this.address_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.
               Address obj = AddressPeer.retrieveByPK(this.address_id);
               obj.addContactInfos(this);
             */
        }
        return aAddress;
    }

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

    /**
     * Declares an association between this object and a Phone object
     *
     * @param v Phone
     * @throws TorqueException
     */
    public void setPhoneRelatedByPhone(Phone v) throws TorqueException
    {
           if (v == null)
        {
                         setPhone((Integer)null);
                    }
        else
        {
            setPhone(v.getId());
        }
           aPhoneRelatedByPhone = v;
    }

                 
    /**
     * Get the associated Phone object
     *
     * @return the associated Phone object
     * @throws TorqueException
     */
    public Phone getPhoneRelatedByPhone() throws TorqueException
    {
        if (aPhoneRelatedByPhone == null && (!ObjectUtils.equals(this.phone, null)))
        {
              aPhoneRelatedByPhone = PhonePeer.retrieveByPK(SimpleKey.keyFor(this.phone));
      
            /* 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.
               Phone obj = PhonePeer.retrieveByPK(this.phone);
               obj.addContactInfosRelatedByPhone(this);
             */
        }
        return aPhoneRelatedByPhone;
    }

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

    /**
     * Declares an association between this object and a Phone object
     *
     * @param v Phone
     * @throws TorqueException
     */
    public void setPhoneRelatedByFax(Phone v) throws TorqueException
    {
           if (v == null)
        {
                         setFax((Integer)null);
                    }
        else
        {
            setFax(v.getId());
        }
           aPhoneRelatedByFax = v;
    }

                 
    /**
     * Get the associated Phone object
     *
     * @return the associated Phone object
     * @throws TorqueException
     */
    public Phone getPhoneRelatedByFax() throws TorqueException
    {
        if (aPhoneRelatedByFax == null && (!ObjectUtils.equals(this.fax, null)))
        {
              aPhoneRelatedByFax = PhonePeer.retrieveByPK(SimpleKey.keyFor(this.fax));
      
            /* 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.
               Phone obj = PhonePeer.retrieveByPK(this.fax);
               obj.addContactInfosRelatedByFax(this);
             */
        }
        return aPhoneRelatedByFax;
    }

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

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

    /**
     * Method called to associate a Member object to this object
     * through the Member foreign key attribute
     *
     * @param l Member
     * @throws TorqueException
     */
    public void addMember(Member l) throws TorqueException
    {
        getMembers().add(l);
        l.setContactInfo((ContactInfo) this);
    }

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

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

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this ContactInfo has previously
     * been saved, it will retrieve related Members from storage.
     * If this ContactInfo is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     *
     * @throws TorqueException
     */
    public List getMembers(Criteria criteria) throws TorqueException
    {
        if (collMembers == null)
        {
            if (isNew())
            {
               collMembers = new ArrayList();
            }
            else
            {
                   criteria.add(MemberPeer.CONTACT_INFO_ID, getId() );
                   collMembers = MemberPeer.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(MemberPeer.CONTACT_INFO_ID, getId());
                   if (!lastMembersCriteria.equals(criteria))
                {
                    collMembers = MemberPeer.doSelect(criteria);
                }
            }
        }
        lastMembersCriteria = criteria;

        return collMembers;
    }

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getMembers(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 getMembers(Connection con) throws TorqueException
    {
        if (collMembers == null)
        {
            collMembers = getMembers(new Criteria(10), con);
        }
        return collMembers;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this ContactInfo has previously
     * been saved, it will retrieve related Members from storage.
     * If this ContactInfo 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 getMembers(Criteria criteria, Connection con)
            throws TorqueException
    {
        if (collMembers == null)
        {
            if (isNew())
            {
               collMembers = new ArrayList();
            }
            else
            {
                     criteria.add(MemberPeer.CONTACT_INFO_ID, getId());
                     collMembers = MemberPeer.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(MemberPeer.CONTACT_INFO_ID, getId());
                     if (!lastMembersCriteria.equals(criteria))
                 {
                     collMembers = MemberPeer.doSelect(criteria, con);
                 }
             }
         }
         lastMembersCriteria = criteria;

         return collMembers;
     }

        

     
      
         
          
                    
                
        
        
    
      
      
          
                    
                
        
        
    
      
      
          
                    
                
        
        
    
      
      
          
                    
                
        
        
    
      
      
          
                    
                
        
        
    
      
      
          
                    
                
        
        
      



     
    
    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("ContactName");
            fieldNames.add("Email");
            fieldNames.add("Website");
            fieldNames.add("Phone");
            fieldNames.add("Fax");
            fieldNames.add("AddressId");
            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("ContactName"))
    {
              return getContactName();
          }
            if (name.equals("Email"))
    {
              return getEmail();
          }
            if (name.equals("Website"))
    {
              return getWebsite();
          }
            if (name.equals("Phone"))
    {
              return getPhone();
          }
            if (name.equals("Fax"))
    {
              return getFax();
          }
            if (name.equals("AddressId"))
    {
              return getAddressId();
          }
            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(ContactInfoPeer.ID))
        {
              return getId();
          }
            if (name.equals(ContactInfoPeer.CONTACT_NAME))
        {
              return getContactName();
          }
            if (name.equals(ContactInfoPeer.EMAIL))
        {
              return getEmail();
          }
            if (name.equals(ContactInfoPeer.WEBSITE))
        {
              return getWebsite();
          }
            if (name.equals(ContactInfoPeer.PHONE))
        {
              return getPhone();
          }
            if (name.equals(ContactInfoPeer.FAX))
        {
              return getFax();
          }
            if (name.equals(ContactInfoPeer.ADDRESS_ID))
        {
              return getAddressId();
          }
            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 getContactName();
          }
            if (pos == 2)
    {
              return getEmail();
          }
            if (pos == 3)
    {
              return getWebsite();
          }
            if (pos == 4)
    {
              return getPhone();
          }
            if (pos == 5)
    {
              return getFax();
          }
            if (pos == 6)
    {
              return getAddressId();
          }
                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(ContactInfoPeer.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())
            {
                ContactInfoPeer.doInsert((ContactInfo) this, con);
                setNew(false);
            }
            else
            {
                ContactInfoPeer.doUpdate((ContactInfo) this, con);
            }
        }

                                    
                
          if (collMembers != null)
          {
              for (int i = 0; i < collMembers.size(); i++)
              {
                  ((Member) collMembers.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 ContactInfo copy() throws TorqueException
    {
        return copyInto(new ContactInfo());
    }

    protected ContactInfo copyInto(ContactInfo copyObj) throws TorqueException
    {
        copyObj.setId(id);
        copyObj.setContactName(contact_name);
        copyObj.setEmail(email);
        copyObj.setWebsite(website);
        copyObj.setPhone(phone);
        copyObj.setFax(fax);
        copyObj.setAddressId(address_id);

  copyObj.setNew(false);
                                    
                
        List v = getMembers();
        for (int i = 0; i < v.size(); i++)
        {
            Member obj = (Member) v.get(i);
            copyObj.addMember(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 ContactInfoPeer getPeer()
    {
        return peer;
    }
}
