/*
 *  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 <mico/CosRelationships.h>
#include <mico/Relationship_impl.h>
#include <mico/RelationshipFactory_impl.h>
#include "Ownership_impl.h"
#include "Employment_impl.h"
#include "Containment_impl.h"
#include "Reference_impl.h"
#include "NamedRoleTypesHelper.h"


int counter = 0;


int main( int argc, char *argv[] )
{
#if DEBUG
  cout << "  <RelationshipFactoryServer> Start...\n";
#endif

  CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "mico-local-orb" );
  CORBA::BOA_var boa = orb->BOA_init (argc, argv, "mico-local-boa");
  
  if (!boa->restoring()) 
    {
#if DEBUG
      cout << "  <RelationshipFactoryServer> Creating NamedRoleTypesHelper:\n";
#endif
      NamedRoleTypesHelper* nrth = new NamedRoleTypesHelper (orb, boa);
      
      //nrth->length (1);
      nrth->add ("IDL:omg.org/CosRelationships/Role:1.0", "ar1");
      nrth->add ("IDL:omg.org/CosRelationships/Role:1.0", "ar2");
      //nrth->add ("IDL:Bank:1.0");
      //nrth->add ("IDL:omg.org/CORBA/Object:1.0");
      //nrth->add ("IDL:Person:1.0");

      NamedRoleTypesHelper* 
	nrth_ownership = new NamedRoleTypesHelper (orb, boa);
      nrth_ownership->add ("IDL:omg.org/Ownership/OwnerRole:1.0", "owner");
      nrth_ownership->add ("IDL:omg.org/Ownership/OwnedByRole:1.0", 
			   "owned_thing");


      NamedRoleTypesHelper* 
	nrth_employment = new NamedRoleTypesHelper (orb, boa);
      nrth_employment->add ("IDL:omg.org/Employment/EmployerRole:1.0", 
			   "employer");
      nrth_employment->add ("IDL:omg.org/Employment/EmployeeRole:1.0", 
			   "employee");

      NamedRoleTypesHelper* 
	nrth_containment = new NamedRoleTypesHelper (orb, boa);
      nrth_containment->add ("IDL:omg.org/Containment/ContainsRole:1.0", 
			   "container");
      nrth_containment->add ("IDL:omg.org/Containment/ContainedInRole:1.0", 
			   "contained_thing");

      NamedRoleTypesHelper* 
	nrth_reference = new NamedRoleTypesHelper (orb, boa);
      nrth_reference->add ("IDL:omg.org/Reference/ReferencesRole:1.0", 
			   "thing_with_references");
      nrth_reference->add ("IDL:omg.org/Reference/ReferencedByRole:1.0", 
			   "referenced_thing");

 
#if DEBUG
      cout << "  <RelationshipServer> Creating factory:\n";
#endif

      CORBA::ORB::ObjectTag_var tag1 
	= CORBA::ORB::string_to_tag ("Relationship_impl");
      CORBA::ORB::ObjectTag_var tag2
        = CORBA::ORB::string_to_tag ("Ownership_impl");
      CORBA::ORB::ObjectTag_var tag3
        = CORBA::ORB::string_to_tag ("Employment_impl");
      CORBA::ORB::ObjectTag_var tag4
        = CORBA::ORB::string_to_tag ("Containment_impl");
      CORBA::ORB::ObjectTag_var tag5
        = CORBA::ORB::string_to_tag ("Reference_impl");


      RelationshipFactory_impl<Relationship_impl>* f1
	= new RelationshipFactory_impl<Relationship_impl>
	(tag1, 2, nrth->get_named_role_types (), "RelationshipServer");

      RelationshipFactory_impl<Ownership_impl>* f2 
	= new RelationshipFactory_impl<Ownership_impl>
	(tag2, 2, nrth_ownership->get_named_role_types (), 
	 "OwnershipServer");

      RelationshipFactory_impl<Employment_impl>* f3 
	= new RelationshipFactory_impl<Employment_impl>
	(tag3, 2, nrth_employment->get_named_role_types (), 
	 "EmploymentServer");
                                       
      RelationshipFactory_impl<Containment_impl>* f4 
	= new RelationshipFactory_impl<Containment_impl>
	(tag4, 2, nrth_containment->get_named_role_types (), 
	 "ContainmentServer");

      RelationshipFactory_impl<Reference_impl>* f5 
	= new RelationshipFactory_impl<Reference_impl>
	(tag5, 2, nrth_reference->get_named_role_types (), 
	 "ReferenceServer");
                                       

     // RelationshipFactory_impl* factory = new RelationshipFactory_impl;
      counter++;
#if DEBUG
      cout << "  <RelationshipServer> Register to BOA...done.\n";
      cout << "  <RelationshipServer> counter: " << counter << "\n";
#endif
      boa->impl_is_ready (CORBA::ImplementationDef::_nil() );

      orb->run ();
      CORBA::release (f1);
      CORBA::release (f2);
      CORBA::release (f3);

    }
  else
    {
#if DEBUG
      cout << "  <RelationshipFactoryServer> can't creating Relationship on shared server\n";
#endif
      boa->obj_is_ready (0, 0);
      orb->run ();
    }
#if DEBUG
  cout << "  <RelationshipFactoryServer> END!\n";
#endif
  return 0;
}

