package com.wiley.compbooks.brose.chapter9.dsi;

import java.io.*;
import java.util.Calendar;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import com.wiley.compbooks.brose.chapter9.HelloWorld.*;

class GoodDayImpl 
    extends org.omg.PortableServer.DynamicImplementation 
{

    private String location;

    /** constructor */
    GoodDayImpl( String location ) 
    {
        this.location = location;
    }

    public String[] _all_interfaces(POA poa, byte[] objectID)
    {
	return new String[]{"IDL:com/wiley/compbooks/brose/chapter9/HelloWorld/GoodDay:1.0"};
    }

    /** operation implementation */

    public void invoke( ServerRequest request ) 
    {
        // check operation name

	if( !request.operation().equals("hello") ) 
        {
            throw new BAD_OPERATION();
        }

        // get local time of the server
        Calendar date = Calendar.getInstance();

	// create anys for hour and minute and insert values

	Time time = new Time( (short) date.get(Calendar.HOUR),
			      (short) date.get(Calendar.MINUTE));

	Any time_any = _orb().create_any();
	TimeHelper.insert( time_any, time );

	// create an any for the out parameter and insert
	// the time any

	Any param_any = _orb().create_any();
	param_any.insert_any( time_any );

	// create the list of named values and add our
	// parameter any

        NVList parameters = _orb().create_list(0);
        parameters.add_value("any_time", param_any, ARG_OUT.value );

	// set our parameter list 

        request.arguments( parameters );

	// create an any for the location and insert the value

        Any location_any = _orb().create_any();
	location_any.insert_string( location );

	// create an any for the operation result and insert 
	// the location any

	Any result_any = _orb().create_any();
	result_any.insert_any( location_any ); 
	
	// set the result and return

	request.set_result( result_any );
    }
}
