Skip to end of metadata
Go to start of metadata

And finally, after all the preparation work, we can implement the actual TableSource Interface.

The important parts are the begin(), end() and the readNext() methods. At the begin() we do all that is required to start reading - we open the file. Then we get a couple of readNext() calls until we do not return any data back anymore. Normally, this method returns the String in XML format, with <AWA_BatchWrapper> as root node, inside are one or many <AWA_ROW> elements and each row uses the column names as XML elements for each value. But to avoid that encoding in XML and DI decoding it, we also implemented the Interface "Delimited" thus return the data in delimited format. And at the end, we close the file.

package com.acta.adapter.sf;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import com.acta.adapter.sdk.AdapterEnvironment;
import com.acta.adapter.sdk.AdapterException;
import com.acta.adapter.sdk.Delimited;
import com.acta.adapter.sdk.OperationEnvironment;
import com.acta.adapter.sdk.RecoverableOperationAdapterException;
import com.acta.adapter.sdk.Table;
import com.acta.adapter.sdk.TableSource;

public class SFReadTable implements TableSource, Delimited
{
    private OperationEnvironment  _adapterOperationEnvironment ;
    private AdapterEnvironment    _adapterEnvironment ;
    private SFAdapter           _adapter ;
    private SFSession           _session ;
    private FileNode              _fileNode ; // adapter defined metadata
    private BufferedReader reader;
    //---------------------------------------------------
    // Automatically configured properties (not from Designer)
    //---------------------------------------------------
    private String               _recordSeparator = "\r\n";
    private String               _fieldSeparator = ",";

    //------------------------------------------------
    // Configurable Properties
    // Only batch size
    //---------------------------------------------------
    private int batchSize = 1 ;

    /**
     * Note, batch size is not required by the interface.
     */
    public int getBatchSize ( )
    {
        return batchSize ;
    }
    /**
     *
     */
    public void setBatchSize ( int batch )
    {
        batchSize = batch ;
    }

    public void metadata ( Object metadata )
    {
        _fileNode = (FileNode)metadata ;
    }

    public String  getDateFormat ()
    {
        return "yyyy.mm.dd hh24:mi:ss" ;
    }

    public int getRecordFormat ()
    {
        return Table.RecordFormatDelimeted ;
    }


    public String  getRowDelimiter ()
    {
        return _recordSeparator ;
    }

    public String getColumnDelimiter ()
    {
        return _fieldSeparator ;
    }

    public String getTextDelimiter ()
    {
        return "" ;
    }

    public String getEscapeChar ()
    {
        return "" ;
    }


    //------------------------------------------------
    // Interface implementations
    //------------------------------------------------

    /**
     *
     */
    public void initialize ( OperationEnvironment adapterOperationEnvironment )
    {
        _adapterOperationEnvironment  = adapterOperationEnvironment ;
        _adapter                      = (SFAdapter)_adapterOperationEnvironment.getAdapter() ;
        _adapterEnvironment           = _adapterOperationEnvironment.getAdapterEnvironment() ;
        _session                      = (SFSession)adapterOperationEnvironment.getSession() ;
    }

    public void start() throws AdapterException
    {

    }

    public void stop()  throws AdapterException
    {
    }

    public void begin() throws AdapterException
    {
        if ( null == _fileNode )
            throw new AdapterException ( "Metadata object is not set." ) ;
        String _fullFileName = _session.getDirectory() + _fileNode.getFileName() ;
        File f = new File ( _fullFileName ) ;
        int flen = (int)f.length() ;
        if ( f.exists() )
        {
            if (f.isFile()) {
             try
             {
                 reader = new BufferedReader(new FileReader(f)) ;
             }
             catch ( Exception e )
             {
                 throw new AdapterException ( e, "Cannot read input file " + _fullFileName + ". " ) ;
             }
            }
            else
                throw new AdapterException ( _fullFileName + " is a directory, not a file." ) ;
        }
        else
            throw new AdapterException ( "File " +  _fullFileName + " does not exist." ) ;
    }

    public void end() throws AdapterException
    {
        try {
            reader.close();
        } catch (IOException e) {
            throw new AdapterException( e ) ;
        }
    }

    public String readNext() throws AdapterException, RecoverableOperationAdapterException
    {

        int recordsProcessed = 0;
        StringBuffer resultRow = new StringBuffer() ;
        try
        {
            for ( int i = 0; i < batchSize; i++ )
            {
                String line = reader.readLine();
                if ( null != line )
                {
                    resultRow.append(convertLineToAscii(line));
                    resultRow.append(_recordSeparator);
                    recordsProcessed++ ;
                }
                else
                    break ;
            }
        }
        catch ( Exception e )
        {
            throw new AdapterException ( e ) ;
        }

        if ( recordsProcessed > 0 )
            return resultRow.toString() ;
        else
            return "" ;
    }

    private String convertLineToAscii(String line) {
        return line;
    }
}

Although we said the file to be of complex structure, here we expect it to be comma separated already. We read one line like "val1,val2,val3" and return it exactly in this format in each readNext() call.



  • No labels