/*
 *  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/Role_impl.h>
#include <mico/RoleFactory_impl.h>
#include "Ownership_impl.h"
#include "Employment_impl.h"
#include "Containment_impl.h"
#include "Reference_impl.h"
#include "InterfaceDefsHelper.h"

//#define DEBUG 1

int counter = 0;


int main( int argc, char *argv[] )
{
  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 << "  <RoleFactoryServer> Creating InterfaceDefsHelper:\n";
#endif
    InterfaceDefsHelper* idefs = new InterfaceDefsHelper (orb, boa);
    
    //idefs->length (1);
    idefs->add ("IDL:Account:1.0");
    idefs->add ("IDL:Bank:1.0");
    //idefs->add ("IDL:omg.org/CORBA/Object:1.0");
    idefs->add ("IDL:Person:1.0");

    InterfaceDefsHelper* idefs_owner = new InterfaceDefsHelper (orb, boa);
    idefs_owner->add ("IDL:Person:1.0");
    InterfaceDefsHelper* idefs_ownedby = new InterfaceDefsHelper (orb, boa);
    idefs_ownedby->add ("IDL:Account:1.0");
    idefs_ownedby->add ("IDL:Documents/Text:1.0");
    idefs_ownedby->add ("IDL:Documents/Figure:1.0");


    InterfaceDefsHelper* idefs_employer = new InterfaceDefsHelper (orb, boa);
    idefs_employer->add ("IDL:Person:1.0");
    InterfaceDefsHelper* idefs_employee = new InterfaceDefsHelper (orb, boa);
    idefs_employee->add ("IDL:Person:1.0");

    InterfaceDefsHelper* idefs_contains = new InterfaceDefsHelper (orb, boa);
    idefs_contains->add ("IDL:Documents/Figure:1.0");
    idefs_contains->add ("IDL:Documents/Text:1.0");
    InterfaceDefsHelper* idefs_containedin = new InterfaceDefsHelper(orb, boa);
    idefs_containedin->add ("IDL:Documents/Figure:1.0");
    idefs_containedin->add ("IDL:Documents/Text:1.0");

    InterfaceDefsHelper* idefs_references = new InterfaceDefsHelper (orb, boa);
    idefs_references->add ("IDL:Documents/Figure:1.0");
    idefs_references->add ("IDL:Documents/Text:1.0");
    InterfaceDefsHelper* idefs_referencedby = new InterfaceDefsHelper(orb,boa);
    idefs_referencedby->add ("IDL:Documents/Figure:1.0");
    idefs_referencedby->add ("IDL:Documents/Text:1.0");


#if DEBUG
    cout << "  <RoleFactoryServer> Creating factory:\n";
#endif
  
    CORBA::ORB::ObjectTag_var tag1 = CORBA::ORB::string_to_tag ("Role_impl");
    CORBA::ORB::ObjectTag_var 
      tag2 = CORBA::ORB::string_to_tag ("OwnerRole_impl");
    CORBA::ORB::ObjectTag_var 
      tag3 = CORBA::ORB::string_to_tag ("OwnedByRole_impl");
    CORBA::ORB::ObjectTag_var 
      tag4 = CORBA::ORB::string_to_tag ("EmployerRole_impl");
    CORBA::ORB::ObjectTag_var 
      tag5 = CORBA::ORB::string_to_tag ("EmployeeRole_impl");
    CORBA::ORB::ObjectTag_var 
      tag6 = CORBA::ORB::string_to_tag ("ContainsRole_impl");
    CORBA::ORB::ObjectTag_var 
      tag7 = CORBA::ORB::string_to_tag ("ContainedInRole_impl");
    CORBA::ORB::ObjectTag_var 
      tag8 = CORBA::ORB::string_to_tag ("ReferencesRole_impl");
    CORBA::ORB::ObjectTag_var 
      tag9 = CORBA::ORB::string_to_tag ("ReferencedByRole_impl");


    RoleFactory_impl<Role_impl>* factory 
      = new RoleFactory_impl<Role_impl> 
      (tag1, 10, 0, idefs->get_interfaces (), 
       "RelationshipIteratorServer", 
       "",
       "RoleServer");

    RoleFactory_impl<OwnerRole_impl>* f2 
      = new RoleFactory_impl<OwnerRole_impl> 
      (tag2, 9999, 0, idefs_owner->get_interfaces (), 
       "RelationshipIteratorServer", 
       "IDL:omg.org/Ownership/Relationship:1.0",
       "OwnerRoleServer");

    RoleFactory_impl<OwnedByRole_impl>* f3 
      = new RoleFactory_impl<OwnedByRole_impl> 
      (tag3, 1, 0, idefs_ownedby->get_interfaces (), 
       "RelationshipIteratorServer", 
       "IDL:omg.org/Ownership/Relationship:1.0",
       "OwnedByRoleServer");

    RoleFactory_impl<EmployerRole_impl>* f4 
      = new RoleFactory_impl<EmployerRole_impl> 
      (tag4, 9999, 0, idefs_employer->get_interfaces (), 
       "RelationshipIteratorServer", 
       "",
       "EmployerRoleServer");

    RoleFactory_impl<EmployeeRole_impl>* f5 
      = new RoleFactory_impl<EmployeeRole_impl> 
      (tag5, 1, 0, idefs_employee->get_interfaces (), 
       "RelationshipIteratorServer", 
       "",
       "EmployeeRoleServer");


    RoleFactory_impl<ContainsRole_impl>* f6 
      = new RoleFactory_impl<ContainsRole_impl> 
      (tag6, 9999, 0, idefs_contains->get_interfaces (), 
       "RelationshipIteratorServer", 
       "",
       "ContainsRoleServer");

    RoleFactory_impl<ContainedInRole_impl>* f7 
      = new RoleFactory_impl<ContainedInRole_impl> 
      (tag7, 1, 0, idefs_containedin->get_interfaces (), 
       "RelationshipIteratorServer", 
       "",
       "ContainedInRoleServer");

    RoleFactory_impl<ReferencesRole_impl>* f8 
      = new RoleFactory_impl<ReferencesRole_impl> 
      (tag8, 9999, 0, idefs_references->get_interfaces (), 
       "RelationshipIteratorServer", 
       "",
       "ReferencesRoleServer");

    RoleFactory_impl<ReferencedByRole_impl>* f9 
      = new RoleFactory_impl<ReferencedByRole_impl> 
      (tag9, 9999, 0, idefs_referencedby->get_interfaces (), 
       "RelationshipIteratorServer", 
       "",
       "ReferencedByRoleServer");

    
                                       
#if DEBUG
    cout << "  <RoleFactoryServer> Register to BOA...done.\n";
#endif
    boa->impl_is_ready (CORBA::ImplementationDef::_nil() );
  }
  else {
    cout << "can't restoring to shared server!!!\n";
    exit (1);
  }
  
  orb->run ();
  
  cout << "  <RoleFactoryServer> END!\n";
  return 0;
}

