package com.wiley.compbooks.brose.chapter10.office;

import java.util.*;
import java.io.*;
import org.omg.CORBA.*;

public class Diary
    extends java.util.TreeMap
    implements java.io.Externalizable
{
    private static DateComparator comparator = new DateComparator();

    private transient Calendar calendar;
    private transient ORB orb;   

    /** this constructor is only for use by deserialization !
     * The object must be fully initialized by calling init().
     */

    public Diary()
    {
	super( comparator );
    }

    public Diary(ORB orb)
    {
	super( comparator );
	this.orb = orb;
	calendar = Calendar.getInstance();
    }

    public void readExternal(java.io.ObjectInput in)
	throws IOException
    {	
	try
	{
	    putAll( (java.util.TreeMap)((ObjectInputStream)in).readObject() );
	}
	catch( ClassNotFoundException c )
	{
	    throw new IOException( c.getMessage());
	}
	calendar = Calendar.getInstance();
    }

    /**
     * Init() must be called after deserializing a 
     * BuildingImpl object !
     */

    void init(ORB orb)
    {	
	this.orb = orb;

	Set keySet = keySet();
	HashMap currentDiary = new HashMap();
	keySet = keySet();

	for( Iterator iter = keySet.iterator(); iter.hasNext(); )
	{
	    java.lang.Object key = iter.next();
	    java.lang.Object value = get( key );

	    if( value instanceof String && 
		((String)value).startsWith("IOR:") )
	    {
		iter.remove();
		currentDiary.put( key, 
				  MeetingHelper.narrow( orb.string_to_object((String) value)));
	    }	    
	}
	putAll( currentDiary );
    }


    public void writeExternal(java.io.ObjectOutput out)
	throws IOException
    {
	/*
	 * For serialization, object references are transformed
	 * into strings
	 */

  	HashMap currentDiary = new HashMap();
  	Set keySet = keySet();

	for( Iterator iter = keySet.iterator(); iter.hasNext(); )
	{
	    java.lang.Object key = iter.next();
	    java.lang.Object value = get( key );
	    if( value instanceof org.omg.CORBA.Object )
	    {
		iter.remove();
		currentDiary.put( key, 
				  orb.object_to_string( (org.omg.CORBA.Object)value )
				  );
	    }
	}		
	putAll( currentDiary );
	((ObjectOutputStream)out ).writeObject( (java.util.TreeMap)super.clone() );
    }

    public Date currentDate()
    {
	calendar.setTime( new java.util.Date() );
	return new Date( calendar.get( Calendar.YEAR ),
			 Month.from_int( calendar.get( Calendar.MONTH )),
			 (short)calendar.get(Calendar.DAY_OF_MONTH ),
			 (short)calendar.get(Calendar.HOUR_OF_DAY ));
    }

    void enter(Date when, java.lang.Object what)
	throws AlreadyEngaged
    {
	if( get( when ) != null )
	{
	    throw new AlreadyEngaged();
	}
	put( when, what );
    }

    public void clearDay( int year, Month month, short day)
    {
	Date date = new Date( year, month, day, (short)0);
	for( int i = 0; i < 24; i++ )
	{
	    date.the_hour = (short)i;
	    remove( date );
	}
    }

    public java.lang.Object[] getDay( int year, Month month, short day)
    {
	Date fromDate = new Date( year, month, day, (short)0);
	Date toDate = new Date( year, month, day, (short)24);

	SortedMap submap = subMap(fromDate, toDate);
	java.lang.Object[] result = submap.values().toArray();	

	return result;
    }

    public java.lang.Object[] getToday()
    {
	Date current = currentDate();
	return getDay(current.the_year, current.the_month, current.the_day );
    }
}
