package edu.vt.marian.Document;

import java.io.*;
import java.net.*;
import java.util.*;

import edu.vt.marian.common.*;


/**
    A "variable field" in a US MARC record that contains a conference name:
	that is, a 111, 411, 611, 711, or 811 field.
    @author	Robert France

*/

public class MarcConfNameField extends PresentableMarcVarField
{
    /** Labels for subfields reported in a long description.
    */
    private final static String conference_name_labels = "abcdeknpqt";


    /**
        Create a MarcConfNameField object with no subfields.
        @param	id -- the variable field id.
        @param	xMap -- used to convert ANSEL to (e.g.) XML.
        @param	debug -- used for debugging
    */      
    public MarcConfNameField(int id, EntityMap xMap, Debug debug)
    {
        super(id, xMap, debug);
        isConfName = true;
     }


    /**
        "Short" presentation for a conference name variable field:  use 'a' and 'b'
              subfields.
        @param	markupType    see edu.vt.marian.common.DigInfObj
        @param	out	A BufferedWriter (presumably String or OutputStream) to present on.
        @return    OK -- everything jake.
        <BR>    IO_ERROR or PARSE_ERROR -- problems.
    */      
    public int presentShort(int markupType, BufferedWriter out) throws IOException
    {
		int Err;
        switch ( markupType )
        {
         default:
            debug.dumpTrace("MarcConfNameField.presentShort(): Unexpected markup type " +
                            markupType + ":  treating as ASCII.");
            // Fall through:

         case DigInfObj.XML:
         case DigInfObj.SGML:
         case DigInfObj.HTML:
         case DigInfObj.ASCII:
         case DigInfObj.ANSEL:
            Enumeration subfld = subfields.elements();
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if ( ( sf.getLabel() == 'a' ) || ( sf.getLabel() == 'b' ) )
                   if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                if ( subfld.hasMoreElements() )
                   out.write(' ');
            } } catch( NoSuchElementException e) {};
        }
     return( ReturnCodes.OK );
    }


    /**
        "Long" presentation for a conference name variable field.
        @param	markupType    see edu.vt.marian.common.DigInfObj
        @param	out	A BufferedWriter (presumably String or OutputStream) to present on.
        @return    OK -- everything jake.
        <BR>    IO_ERROR or PARSE_ERROR -- problems.
    */      
    public int presentLong(int markupType, BufferedWriter out) throws IOException 
    {
		int Err;
        Enumeration subfld = subfields.elements();
        switch ( markupType )
        {
         case DigInfObj.XML:	// Use default.
         case DigInfObj.SGML:
	        return( super.presentLong(markupType, out) );

         case DigInfObj.HTML:
            // Presentation:  make the (unique) 'a' (meeting name) subfield a
            //  hot link using itself as (approximate) query text.
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if (sf.getLabel() == 'a')
                {
                    // Build URL.
                    StringWriter sw = new StringWriter();
                    BufferedWriter bsw = new BufferedWriter( sw );
                    if ( (Err = sf.present(DigInfObj.ANSEL, bsw))
                              != ReturnCodes.OK )
                        return( Err );
 			        bsw.flush();
                    String queryString = "text1_type=Conference as Author/text1=" +
                                            sw.toString();
                    presentUrl(queryString, out);
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    out.write("</A>");
                }
                else if (sf.getLabel() == '4')
                {
                    out.write("[");
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    out.write("]");
                }
                else
                {
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                }
                if ( subfld.hasMoreElements() )
                   out.write(' ');
            } } catch( NoSuchElementException e) {};
            return( ReturnCodes.OK );



         default:
            debug.dumpTrace("MarcConfNameField.presentLong(): Unexpected markup type " +
                            markupType + ":  treating as ASCII.");
            // Fall through:

         case DigInfObj.ASCII:
         case DigInfObj.ANSEL:
            try { while ( true )
            {
                MarcSubField sf = (MarcSubField) subfld.nextElement();
                if (sf.getLabel() == '4')
                {
                    out.write("[");
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                    out.write("]");
                }
                else
                {
                    if ( (Err = sf.present(markupType, out)) != ReturnCodes.OK )
                        return( Err );
                }
                if ( subfld.hasMoreElements() )
                   out.write(' ');
            } } catch( NoSuchElementException e) {};
            return( ReturnCodes.OK );
        }
    }

}

