/*
 *  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 <fstream.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <mico/CosRelationships.h>
#include "Employment.h"
#include "Employment_impl.h" 
#include <mico/Relationship_impl.h>


//#define DEBUG 1


EmployerRole_impl::EmployerRole_impl ()
  : Role_impl ()
{
}



EmployerRole_impl::EmployerRole_impl (CORBA::Object_ptr obj)
  : Role_impl (obj),
    Employment::EmployerRole_skel (obj)
{
#if DEBUG
  cout << "  <EmployerRole_impl> constructor (restorer)\n";
#endif
}



EmployerRole_impl::EmployerRole_impl (CosRelationships::RelatedObject_ptr obj,
				      CORBA::Long card)
  : Role_impl (obj, card),
    Employment::EmployerRole_skel ()
{
#if DEBUG
  cout << "  <EmployerRole_impl> constructor with params\n";
#endif
}



EmployeeRole_impl::EmployeeRole_impl ()
  : Role_impl ()
{
}



EmployeeRole_impl::EmployeeRole_impl (CORBA::Object_ptr obj)
  : Role_impl (obj), 
    Employment::EmployeeRole_skel (obj) 
{
#if DEBUG
  cout << "  <EmployeeRole_impl> constructor (restorer)\n";
#endif
}



EmployeeRole_impl::EmployeeRole_impl (CosRelationships::RelatedObject_ptr obj,
				    CORBA::Long card)
  : Role_impl (obj, card),
    Employment::EmployeeRole_skel ()
{
#if DEBUG
  cout << "  <EmployeeRole_impl> constructor with params\n";
#endif
}


Employment_impl::Employment_impl ()
  : //IdentifiableObject_impl (), 
    Relationship_impl ()
{
}



Employment_impl::Employment_impl (CORBA::Object_ptr obj)
  : Relationship_impl (obj),
    Employment::Relationship_skel (obj)
{
  CORBA::ORB_ptr orb = _orb();
  char s[1000];
 
#if DEBUG
  cout << "  <Employment_impl> constructor (obj)\n";
  cout << "  <Employment_impl> ident: " << obj->_ident () << "\n";
#endif
  
  ifstream in (obj->_ident ());
  assert (in);

  CORBA::Boolean is_in = FALSE;
  while (!is_in) {
    in >> s;
#if DEBUG
    cout << s;
#endif
    if (strcmp (s, "---Employment_impl---") == 0) {
      is_in = TRUE;
#if DEBUG
      cout << "  <Employment_impl> is in!\n";
#endif
    }
  }
  in >> s;  // --since--
#if DEBUG
  cout << "  <Employment_impl> since\n";
#endif
  in >> tsince.year;
  in >> tsince.month;
  in >> tsince.day;
#if DEBUG
  cout << "  <Employment_impl> to\n";
#endif
  in >> s; // --to--
  in >> tto.year;
  in >> tto.month;
  in >> tto.day;
  in.close ();
  
}


Employment_impl::Employment_impl (CosRelationships::NamedRoles nr)
  : Relationship_impl (nr),
    Employment::Relationship_skel ()
{
}



CORBA::Boolean
Employment_impl::_save_object ()
{
  CORBA::ORB_ptr orb = _orb(); 

#if DEBUG
  cout << "  <Employment_impl> _save_object (): " << _ident () << "\n";
  //sleep (5);
  cout << "  <Employment_impl> _save_object (): " 
       << Relationship_impl::_ident () << "\n";
#endif

  Relationship_impl::_save_object ();


  ofstream out (_ident (), ios::app);

  assert (out);
  out << "---Employment_impl---\n";
  out << "-since-\n";
  out << tsince.year << "\n";
  out << tsince.month << "\n";
  out << tsince.day << "\n";
  out << "-to-\n";
  out << tto.year << "\n";
  out << tto.month << "\n";
  out << tto.day << "\n";
  out.close ();

  return TRUE;

}


/*
CORBA::Boolean
Employment_impl::xsave (ofstream* out)
{
  (*out) << "---Employment_impl---\n";
  (*out) << "-since-\n";
  (*out) << tsince.year << "\n";
  (*out) << tsince.month << "\n";
  (*out) << tsince.day << "\n";
  (*out) << "-to-\n";
  (*out) << tto.year << "\n";
  (*out) << tto.month << "\n";
  (*out) << tto.day << "\n";
}
*/ 


Employment::Date
Employment_impl::since ()
{
  return tsince;
}


void
Employment_impl::since (const Employment::Date& x)
{
  tsince = x;
}


Employment::Date 
Employment_impl::to ()
{
  return tto;
}


void 
Employment_impl::to (const Employment::Date& x)
{
  tto = x;
}



CORBA::Boolean
EmployerRoleLoader::restore (CORBA::Object_ptr obj)
{
#if DEBUG
  cout << "  <EmployerRoleLoader> Restoring role: " << obj->_repoid() << "\n";
#endif
  //  if (!strcmp (obj->_repoid(), "IDL:omg.org/CosRelationships/Role:1.0")) {
  if (!strcmp (obj->_repoid(), "IDL:omg.org/Employment/EmployerRole:1.0")) {
#if DEBUG
    cout << "  <EmployerRoleLoader> Creating new EmployerRole_impl:\n";
#endif
    new EmployerRole_impl (obj);
#if DEBUG
    cout << "  <EmployerRoleLoader> Restoring done!\n";
#endif
    return TRUE;
  }

#if DEBUG
  cout << "  <EmployerRoleLoader> cannot restore " << obj->_repoid() 
       << " objects" << endl;
#endif

  return FALSE;
}



CORBA::Boolean
EmployeeRoleLoader::restore (CORBA::Object_ptr obj)
{
#if DEBUG
  cout << "  <EmployeeRoleLoader> Restoring role: " << obj->_repoid() << "\n";
#endif
  // if (!strcmp (obj->_repoid(), "IDL:omg.org/CosRelationships/Role:1.0")) {
  if (!strcmp (obj->_repoid(), "IDL:omg.org/Employment/EmployeeRole:1.0")) {
#if DEBUG
    cout << "  <EmployeeRoleLoader> Creating new EmployeeRole_impl:\n";
#endif
    new EmployeeRole_impl (obj);
#if DEBUG
    cout << "  <EmployeeRoleLoader> Restoring done!\n";
#endif
    return TRUE;
  }

#if DEBUG
  cout << "  <EmployeeRoleLoader> cannot restore " << obj->_repoid() 
       << " objects" << endl;
#endif

  return FALSE;
}




CORBA::Boolean
EmploymentLoader::restore (CORBA::Object_ptr obj)
{
#if DEBUG
  cout << "  <EmploymentLoader> Restoring relationship: " << obj->_repoid() 
       << "\n";
#endif
  if (!strcmp (obj->_repoid(), "IDL:omg.org/Employment/Relationship:1.0")) {
#if DEBUG
    cout << "  <EmploymentLoader> Creating new Employment_impl:\n";
#endif
    new Employment_impl (obj);
#if DEBUG
    cout << "  <EmploymentLoader> Restoring done!\n";
#endif
    return TRUE;
  }

#if DEBUG
  cout << "  <EmploymentLoader> cannot restore " << obj->_repoid() 
       << " objects" << endl;
#endif

  return FALSE;
}



