Skip to end of metadata
Go to start of metadata

The Import class deals with all the capabilities of importing objects. It definies what kind of objects can be imported and what the class used for ImportByName will be. And it performs the actual import.

package com.acta.adapter.sf;

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

import com.acta.adapter.sdk.Adapter;
import com.acta.adapter.sdk.AdapterEnvironment;
import com.acta.adapter.sdk.AdapterException;
import com.acta.adapter.sdk.ImportByName;
import com.acta.adapter.sdk.MetadataImport;
import com.acta.adapter.sdk.MetadataNode;
import com.acta.adapter.sdk.Session;
import com.acta.metadata.AWColumn;
import com.acta.metadata.AWMetadata;
import com.acta.metadata.AWTableMetadata;
import com.acta.metadata.ResourceMetadata;

public class SFImport implements MetadataImport
{
    AdapterEnvironment  _adapterEnvironment = null ;
    SFAdapter         _adapter = null ;
    SFSession         _session = null ;

    public void initialize ( Adapter adapter, AdapterEnvironment adapterEnvironment, Session session )
    {
        _adapterEnvironment = adapterEnvironment ;
        _adapter = (SFAdapter)adapter ;
        _session = (SFSession)session ;
    }

    public Vector importMetadata ( MetadataNode node ) throws AdapterException
    {
        Vector v = new Vector() ;
        if ( node instanceof com.acta.adapter.sf.FileNode )
        {
            FileNode fn = (FileNode)node ;
            v.addElement( importTable( _session.getDirectory(), fn.getFileName() ) ) ;
        }
        else
            throw new AdapterException ( "Import is not supported for metadata " + node.getClass().getName() ) ;
        return v ;
    }

    public Vector importMetadata ( ImportByName r ) throws AdapterException
    {
        Vector resourceMetadata = new Vector() ;
        SFImportByName or = (SFImportByName)r ;

        resourceMetadata.addElement ( importTable( _session.getDirectory(), or.getMetadataName() ) ) ;
        return resourceMetadata ;
    }

    /**
     * Returns the user-defined object that implements the {@link SFImportByName TestImportByName} interface.
     * The returned object has all information needed for the "import by name"
     * performed with the {@link MetadataImport#importMetadata importMetadata ( ImportByName r )} method.
     */
    public ImportByName importByName ()
    {
        return new SFImportByName () ;
    }

    /**
     * Returns true because we can import tables.
     */
    public boolean isTables ()
    {
        return true ;
    }

    /**
     * Returns false because we cannot import documents.
     */
    public boolean isDocuments ()
    {
        return false;
    }

    /**
     * Returns false because we cannot import function calls.
     */
    public boolean isFunctionCalls ()
    {
        return false;
    }

    /**
     * Returns false because we cannot import messages.
     */
    public boolean isOutboundMessages ()
    {
        return false;
    }

    /**
     * Returns false because we cannot import message function calls.
     */
    public boolean isMessageFunctionCalls ()
    {
        return false;
    }

    /**
     *Helper
     */
    private AWMetadata importTable( String directory, String fileName ) throws AdapterException
    {
        AWTableMetadata awTable = new AWTableMetadata() ;
        awTable.setCanBeLoader(false);
        awTable.setCanBeReader(true);

        int columncounter = 1;
        try
        {
            File f = new File ( directory + fileName ) ;
            BufferedReader br = new BufferedReader ( new FileReader ( f ) ) ;
            String firstline = br.readLine(); // should have at least one
            br.close();

            int pos = firstline.indexOf(",");

            while (pos != -1) {
                columncounter++;
                pos = firstline.indexOf(",", pos+1);
            }
        }
        catch (IOException e) {
            // no file found
            throw new AdapterException(e.getMessage());
        }

        AWColumn [] awc = new AWColumn [columncounter] ;
        for ( int i = 0; i < columncounter; i++ )
        {
            awc[i] = new AWColumn();
            awc[i].setDatatype(AWColumn.AWT_VARCHAR);
            awc[i].setName("COL" + i);
            awc[i].setNullable(true);
            awc[i].setScale(0);
            awc[i].setLength(100);
            awc[i].setPrecision(100);
            awc[i].setDescription("Column " + i);
        }
        awTable.setColumns(awc);
        awTable.setPhysicalName( fileName );
        awTable.setTableDescription(fileName);
        awTable.setTableName(fileName);
        //
        ResourceMetadata ei = new ResourceMetadata() ;
        ei.setMetadataObject(new FileNode(awTable));
        awTable.setResourceMetadata(ei);
        return awTable;
    }

}

As we want to import tables, we need to return an object of type com.acta.metadata.AWTableMetadata. From the MetadataBrowsing or ImportByName dialog we will get one element to be imported and have to return the AWTableMetadata object with its columns. The AWTableMetadata will be written into the DI repository and is removed afterwards. It is only required for the import process, not for the execution!

For this reason, the AWMetadataTable object gets destroyed afterwards, the table is carrying one extra property - the ResourceMetadata - that object will be passed to the Adapter upon execution. So what do we need as information when executing the DataFlow? Actually, in our case, only the FileName. So we can reuse the FileNode class for that.

ResourceMetadata ei = new ResourceMetadata() ;
ei.setMetadataObject(new FileNode(awTable));
awTable.setResourceMetadata(ei);

This is a common trick to use the same MetadataNode class for both, the browsing and as ResourceMetadata.

 

 

  • No labels