/*
 *  Demo of COSS Relationship Service
 *  Copyright (C) 1998 Karel Gardas
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Send comments and/or bug reports to:
 *                 mico@informatik.uni-frankfurt.de
 *  or to my private e-mail:
 *                 gardask@alpha.inf.upol.cz
 */


#include <string.h>
#include <fstream.h>
#include "Person.h"
#include "Person_impl.h"


//#define DEBUG 1


Person_impl::Person_impl ()
{
}


Person_impl::Person_impl (CORBA::Object_ptr obj)
  : Person_skel (obj)
{
  char s[1000];
#if DEBUG							    
 cout << "  <Person_impl> restoring id " << obj->_ident () << "\n";
#endif
  ifstream in (obj->_ident ());
  assert (in);

  in >> apid;
  in >> ayear;
  in >> amonth;
  in >> aday;
  in >> s;
  afirst_name = CORBA::string_dup (s);
  //for (int i=0; i<1000; i++)
  //  tmp_str[i] = 0;
  in >> s;
  asurname = CORBA::string_dup (s);
#if DEBUG
  cout << "  <Person_impl> " << apid << ", " << ayear << ", " << amonth 
       << ", " << aday << ", " << afirst_name << ", " << asurname << "\n";
#endif
    in.close ();  
}

Person_impl::Person_impl (CORBA::Long xpid, CORBA::Long xyear, 
			  CORBA::Long xmonth, CORBA::Long xday)
{
  apid = xpid;
  ayear = xyear;
  amonth = xmonth;
  aday = xday;
  afirst_name = CORBA::string_dup ("");
  asurname = CORBA::string_dup ("");
}

Person_impl::Person_impl (CORBA::Long xpid, CORBA::Long xyear, 
			  CORBA::Long xmonth, CORBA::Long xday, 
			  const char* xfirst, const char* xsur)
{
  apid = xpid;
  ayear = xyear;
  amonth = xmonth;
  aday = xday;
  afirst_name = CORBA::string_dup (xfirst);
  asurname = CORBA::string_dup (xsur);
#if DEBUG
  cout << "  <Person_impl> constructor with name:";
  cout << apid << " ";
  cout << ayear << " ";
  cout << amonth << " ";
  cout << aday << " ";
  cout << afirst_name << " ";
  cout << asurname << endl;
#endif

}

CORBA::Boolean
Person_impl::_save_object ()
{
#if DEBUG
  cout << "  <Person_impl> saving id " << _ident () << "\n";
#endif
  ofstream out (_ident ());
  assert (out);

  out << apid << "\n";
  out << ayear << "\n";
  out << amonth << "\n";
  out << aday << "\n";
  out << afirst_name << "\n";
  out << asurname << "\n";
#if DEBUG
  cout << "  <Person_impl> wrote OK!\n";
  cout << "  <Person_impl> " << apid << ", " << ayear << ", " << amonth << ", "
       << aday << ", " << afirst_name << ", " << asurname << "\n";
#endif
 
  out.close ();
  return TRUE;
}


CORBA::Long
Person_impl::pid ()
{
  return apid;
}

CORBA::Long
Person_impl::year ()
{
  return ayear;
}

CORBA::Long
Person_impl::month ()
{
  return amonth;
}

CORBA::Long
Person_impl::day ()
{
  return aday;
}

char*
Person_impl::first_name ()
{
  return CORBA::string_dup (afirst_name);
}

void
Person_impl::first_name (const char* value)
{
#if DEBUG
  cout << "  <Person_impl> first_name (" << value << ");\n";
#endif
  afirst_name = CORBA::string_dup (value);
#if DEBUG
  cout << "  <Person_impl> first_name (...) -> OK!\n";
#endif
}

char*
Person_impl::surname ()
{
#if DEBUG
  cout << "  <Person_impl> surname () -> " << asurname << "\n";
#endif
  // I must use CORBA::string_dup, becouseof crash!!!
  return CORBA::string_dup (asurname);
  //  return asurname;
}

void
Person_impl::surname (const char* value)
{
  asurname = CORBA::string_dup (value);
}


void 
Person_impl::destroy ()
{
  CORBA::BOA_var boa = _boa();
  CORBA::ORB_var orb = _orb();

  boa->deactivate_obj (this);
  orb->shutdown (TRUE);
}


PersonFactory_impl::PersonFactory_impl ()
{
#if DEBUG
  cout << "  <Person_impl> PersonFactory_impl ();\n";
#endif
}


Person_ptr
PersonFactory_impl::create_noname ()
{
  CORBA::BOA_var boa = _boa ();
  CORBA::ORB_ptr orb = _orb ();

  Person_ptr p = new Person_impl ();
  boa->deactivate_obj (p);

  return Person::_duplicate (p);
}


Person_ptr
PersonFactory_impl::create (CORBA::Long p, CORBA::Long y, CORBA::Long m,
			    CORBA::Long d)
{
  CORBA::BOA_var boa = _boa ();
  CORBA::ORB_ptr orb = _orb ();

  Person_ptr person = new Person_impl (p, y, m, d);
  boa->deactivate_obj (person);

  return Person::_duplicate (person);
}


Person_ptr
PersonFactory_impl::create_with_name (CORBA::Long p, CORBA::Long y, 
				      CORBA::Long m, CORBA::Long d, 
				      const char* f, const char* s)
{
#if DEBUG
  cout << "  <Person_impl> create_with_name (...);\n";
#endif

  CORBA::BOA_var boa = _boa ();
  CORBA::ORB_ptr orb = _orb ();

  Person_ptr person = new Person_impl (p, y, m, d, f, s);
  boa->deactivate_obj (person);

  return Person::_duplicate (person);
}
 

void 
PersonFactory_impl::destroy ()
{
  CORBA::BOA_var boa = _boa();
  CORBA::ORB_var orb = _orb();

  boa->deactivate_obj (this);
  orb->shutdown (TRUE);
}


CORBA::Boolean
PersonLoader::restore (CORBA::Object_ptr obj)
{
#if DEBUG
  cout << "  <Person_impl> PersonLoader::restore (...);\n";
#endif
  if (!strcmp (obj->_repoid(), "IDL:Person:1.0")) {
#if DEBUG
    cout << "  <Person_impl> new Person_impl (obj);\n";
#endif
    new Person_impl (obj);
    return TRUE;
  }
  cout << "cannot restore " << obj->_repoid() << " objects" << endl;
  return FALSE;
  
}

  




