// CLEAN
// LISTCLEAN

/***************************************************************************
                          station.cpp  -  description
                             -------------------
    begin                : Mon Apr 15 2002
    copyright            : (C) 2002 by Ross Andrews
    email                : randrews@geekfu.org
***************************************************************************/


#include "station.h"

station::station(){
}

station::station(const station& other){
  *this=other;
}

const station& station::operator =(const station& other){
  if(&other==this)return *this;
  inclength=other.inclength;
  middeckmass=other.middeckmass;
  modules=other.modules;
  rack_overhead=other.rack_overhead;

  for(int n=0;n!=modules.size();n++)
    {
      modules[n].stn=this;
    }
  return *this;
}

station::station(MYSQL& conn){
#ifndef RELEASE
  cout<<"Entered station::station(MYSQL&)\n"<<flush;
#endif
  // Create space for modules.
  ResultSet res(&conn);
  res.query<<"select acronym from modules order by acronym asc";
  res.store();

  // Create modules array
  while(res.next())
    modules.push_back(module(conn,res["acronym"]));
  for(int n=0;n!=modules.size();n++)
    modules[n].stn=this;
#ifndef RELEASE
  cout<<"Returned from station::station(MYSQL&)\n"<<flush;
#endif
}

station::station(MYSQL& conn,string combo){
#ifndef RELEASE
  cout<<"Entered station::station(MYSQL& conn,string combo)\n"<<flush;
#endif
  *this=station(conn);

  // Get the rack data from the database
  ResultSet res(&conn);
  res.query<<"select acronym from racks order by idx asc";
  res.store();

  // Populate racks
  for(int n=0;n!=combo.length();n++)
    {
      res.seek(n);
      if(combo[n]==1 || combo[n]=='1')
	{
	  addrack(rack(conn,res["acronym"]));
#ifndef RELEASE
	  cout<<"\t\tAdded "<<res["acronym"]<<" to racks.\n";
#endif
	}
      if(combo[n]==2 || combo[n]=='2')
	{
	  addkeepalive(rack(conn,res["acronym"]));
#ifndef RELEASE
	  cout<<"\t\tAdded "<<res["acronym"]<<" to keepalives.\n";
#endif
	}
    }

#ifndef RELEASE
  cout<<"Returned from station::station(MYSQL& conn,string combo)\n"<<flush;
#endif
}

void station::addrack(rack r){
  int n=0;
  while(n<modules.size() && modules[n].getname()!=r.modname)n++;
  modules[n].addrack(r);
}

void station::addkeepalive(rack r){
  int n=0;
  while(n<modules.size() && modules[n].getname()!=r.modname)n++;
  modules[n].addkeepalive(r);
}

void station::addcombo(vector<rack> racks){
  for(int n=0;n!=racks.size();n++)
    addrack(racks[n]);
}

void station::addkeepalivecombo(vector<rack> racks){
  for(int n=0;n!=racks.size();n++)
    addkeepalive(racks[n]);
}

station::~station(){
}
