package mit.mitid ;

public class SEARCH
implements java.io.Serializable
{
	public static final int SEARCH_MATCH_VALUE = 1 ;
	public static final int SEARCH_LIST_VALUE = 2 ;
	public static final int SEARCH_NONE_VALUE = 3 ;
	public static final String SEARCH_MATCH_STATUS = "Exact match" ;
	public static final String SEARCH_LIST_STATUS = "Possible match" ;
	public static final String SEARCH_NONE_STATUS = "No match"  ;

	public static final String OPTION_DEFAULT = "S_DEFAULT" ;
	public static final String OPTION_EXACT_ON = "S_EXACT_ON" ;
	public static final String OPTION_EXACT_OFF = "S_EXACT_OFF" ;
	public static final String OPTION_SOUND_ON = "S_SOUND_ON" ;
	public static final String OPTION_SOUND_OFF = "S_SOUND_OFF" ;

	String firstName = null ;
	public String getFirstName() { return this.firstName ; }
	public void setFirstName( String firstName ) { this.firstName = firstName ; }

	String middleName = null ;
	public String getMiddleName() { return this.middleName ; }
	public void setMiddleName( String middleName ) { this.middleName = middleName ; }

	String lastName = null ;
	public String getLastName() { return this.lastName ; }
	public void setLastName( String lastName ) { this.lastName = lastName ; }

	String ssn = null ;
	public String getSsn() { return this.ssn ; }
	public void setSsn( String ssn ) { this.ssn = ssn ; }

	String dob = null ;
	public String getDob() { return this.dob ; }
	public void setDob( String dob ) { this.dob = dob ; }

	String mitid = null ;
	public String getMitid() { return this.mitid ; }
	public void setMitid( String mitid ) { this.mitid = mitid ; }

	String option = OPTION_DEFAULT ;
	public String getOption() { return this.option ; }
	public void setOption( String str ) { this.option = str ; }

	private int serverResponse = 0 ;
	public int getServerResponse() { return this.serverResponse ; }
	public void setServerResponse( int serverResponse ) { this.serverResponse = serverResponse ; }

	private int numRecords = 0 ;
	public int getNumRecords() { return this.numRecords ; }
	public void setNumRecords( int numRecords ) { this.numRecords = numRecords ; }

	private int msgOffset = 0 ;
	public int getMsgOffset() { return msgOffset ; }
	public void setMsgOffset( int msgOffset ) { this.msgOffset = msgOffset ; }

	private String[][] matchList = null ;
	public String[][] getMatchList() { return this.matchList ; }
	public void setMatchList( String[][] matchList ) { this.matchList = matchList ; }

	private int numFields = 6 ;
	private String listMitid = null ;
	private String listFirstName = null ;
	private String listMiddleName = null ;
	private String listLastName = null ;
	private String listSource = null ;
	private String listLocation = null ;

	private String status = null ;
	public String getStatus() { return this.status ; }
	public void setStatus( String status ) { this.status = status ; }

	private String statusMessage = null ;
	public String getStatusMessage() { return this.statusMessage ; }
	public void setStatusMessage( String statusMessage ) { this.statusMessage = statusMessage ; }

	private String serverTag = null ;
	public String getServerTag() { return this.serverTag ; }
	public void setServerTag( String serverTag ) { this.serverTag = serverTag ; }

	public String serialize()
	{
		try
		{
			return
				( mit.mitid.PROTOCOL.SEARCH + mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
				+ fixNull( getFirstName() ) + mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
				+ fixNull( getMiddleName() ) + mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
				+ fixNull( getLastName() ) + mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
				+ fixNull( getSsn() ) + mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
				+ fixNull( getDob() ) + mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
				+ fixNull( getMitid() ) + mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
				+ fixNull( getOption() ) + mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
				) ;
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
		}
		return null ;
	}

	private String fixNull( String str )
	{
		if( null == str )
		{
			return "" ;
		}
		return str ;
	}

	private void resetTableVariables()
	{
		listMitid = "" ;
		listFirstName = "" ;
		listMiddleName = "" ;
		listLastName = "" ;
		listSource = "" ;
		listLocation = "" ;
	}

	public void deserialize( String str )
	{
		try
		{
			resetTableVariables() ;
			setServerResponse( 0 ) ;
			setNumRecords( 0 ) ;
			setMsgOffset( 0 ) ;
			setMatchList( null ) ;
			setStatus( null ) ;
			setStatusMessage( null ) ;
			setServerTag( null ) ;

			java.util.StringTokenizer stRow
			= new java.util.StringTokenizer
				( str
				, mit.mitid.PROTOCOL.ROW_TERMINATOR_STRING
				) ;
			deserializeFirstRow( stRow.nextToken() ) ;
			setServerTag( stRow.nextToken() ) ;
			if( Integer.parseInt( mit.mitid.PROTOCOL.ERROR ) != serverResponse )
			{
				switch( serverResponse )
				{
				case SEARCH_MATCH_VALUE:
					setStatus( SEARCH_MATCH_STATUS ) ;
					break ;
				case SEARCH_LIST_VALUE:
					setStatus( SEARCH_LIST_STATUS ) ;
					for( int i = 0 ; getNumRecords() != i ; i ++ )
					{
						deserializeAMatch( stRow.nextToken() ) ;
						addToMatchList( i ) ;
					}
					setStatusMessage( mit.mitid.PROTOCOL.NUMRECORDS + Integer.toString( getNumRecords() ) ) ;
					break ;
				case SEARCH_NONE_VALUE:
					setStatus( SEARCH_NONE_STATUS ) ;
					setStatusMessage( stRow.nextToken() ) ;
					break ;
				}
			}
			else
			{
				java.util.StringTokenizer st
				= new java.util.StringTokenizer
					( str
					, mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
					+ mit.mitid.PROTOCOL.ROW_TERMINATOR_STRING
					) ;

				st.nextToken() ;
				st.nextToken() ;
				st.nextToken() ;
				st.nextToken() ;
				setStatus( st.nextToken() ) ;
			}
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
		}
	}

	private void deserializeFirstRow( String firstRowString )
	{
		java.util.StringTokenizer st
		= new java.util.StringTokenizer
			( firstRowString
			, mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING
			) ;
		if( st.hasMoreTokens() ) { setServerResponse( Integer.parseInt( st.nextToken() ) ) ; }
		if( st.hasMoreTokens() ) { String unk1 = st.nextToken() ; }
		if( st.hasMoreTokens() ) { setNumRecords( Integer.parseInt( st.nextToken() ) ) ; }
		if( st.hasMoreTokens() ) { setMsgOffset( Integer.parseInt( st.nextToken() ) ) ; }
	}

	private void deserializeAMatch( String match )
	{
		resetTableVariables() ;

		String temp = match ;
		int index = temp.indexOf( mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING ) ;
		if( -1 != index )
		{
			listMitid = temp.substring( 0 , index ) ;
			temp = temp.substring( index + 1 ) ;
			index = temp.indexOf( mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING ) ;
			if( -1 != index )
			{
				listFirstName = temp.substring( 0 , index ) ;
				temp = temp.substring( index + 1 ) ;
				index = temp.indexOf( mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING ) ;
				if( -1 != index )
				{
					listMiddleName = temp.substring( 0 , index ) ;
					temp = temp.substring( index + 1 ) ;
					index = temp.indexOf( mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING ) ;
					if( -1 != index )
					{
						listLastName = temp.substring( 0 , index ) ;
						temp = temp.substring( index + 1 ) ;
						index = temp.indexOf( mit.mitid.PROTOCOL.FIELD_DELIMITER_STRING ) ;
						if( -1 != index )
						{
							listSource = temp.substring( 0 , index ) ;
							index += 1 ;
							if( index < temp.length() )
							{
								listLocation = temp.substring( index + 1 ) ;
							}
						}
					}
				}
			}
		}
	}

	private void addToMatchList( int row )
	{
		if( null == getMatchList() )
		{
			setMatchList( new String[ getNumRecords() ][ numFields ] ) ;
		}
		if( null != getMatchList() )
		{
			matchList[ row ][ 0 ] = listMitid ;
			matchList[ row ][ 1 ] = listFirstName ;
			matchList[ row ][ 2 ] = listMiddleName ;
			matchList[ row ][ 3 ] = listLastName ;
			matchList[ row ][ 4 ] = listSource ;
			matchList[ row ][ 5 ] = listLocation ;
		}		
	}

}