Posted by: michaelverzijl | 27/02/2012

ODI 11.1.1.6 Working with Groovy editor

One of the new enhancements of Oracle Data Integrator is a Groovy Editor.
From the documentation:

The Groovy editor provides a single environment for creating, editing, and executing Groovy scripts within the ODI Studio context. It provides all standard features of a code editor such as syntax highlighting and common code editor commands.

Building interfaces through the ODI SDK was also available in previous releases, but you needed external tooling like Eclipse Editor for this.

Once ODI Studio is opnend you can find the Editor on the Tools menu (Tools -> Groovy -> New Script):

So what can you do with this editor?

With this editor you can create scripts to populate the ODI repository. A complete SDK can be installed while installing ODI to your machine.

In this post I will be using the Groovy Editor to create an interface between a source table and a staging table.
Because I was unfamiliar with Groovy I did some research and used these sites:

So before we start the script we need to import classes provided by Oracle:

// Context operators:
import oracle.odi.domain.topology.OdiContext
import oracle.odi.domain.topology.finder.IOdiContextFinder

// Folder operators:
import oracle.odi.domain.project.OdiFolder
import oracle.odi.domain.project.finder.IOdiFolderFinder

// Interface operators:
import oracle.odi.domain.project.OdiInterface

// Interface helpers:
import oracle.odi.interfaces.interactive.support.InteractiveInterfaceHelperWithActions
import oracle.odi.interfaces.interactive.support.targetkeychoosers.TargetKeyChooserPrimaryKey
import oracle.odi.interfaces.interactive.support.mapping.automap.AutoMappingComputerLazy
import oracle.odi.interfaces.interactive.support.mapping.matchpolicy.MappingMatchPolicyLazy
import oracle.odi.interfaces.interactive.support.actions.InterfaceActionSetTargetDataStore
import oracle.odi.interfaces.interactive.support.aliascomputers.AliasComputerDoubleChecker
import oracle.odi.interfaces.interactive.support.clauseimporters.ClauseImporterLazy
import oracle.odi.interfaces.interactive.support.actions.InterfaceActionAddSourceDataStore
import oracle.odi.interfaces.interactive.support.actions.InterfaceActionOnTargetDataStoreComputeAutoMapping;

// Transaction operators:
import oracle.odi.core.persistence.transaction.support.DefaultTransactionDefinition;
import oracle.odi.core.persistence.transaction.ITransactionDefinition;
import oracle.odi.core.persistence.transaction.ITransactionManager;
import oracle.odi.core.persistence.transaction.ITransactionStatus;

// Data set:
import oracle.odi.domain.project.interfaces.DataSet;

// Data store:
import oracle.odi.domain.model.OdiDataStore;
import oracle.odi.domain.model.finder.IOdiDataStoreFinder;

For this project I defined 2 vars:

// Define VARS:
def project = new String("Staging") // What is the target project?
def folder = new String("Source_1") // Which folder in target project?

Just like the blog from Oracle, a file is created which specifies the interface/source etc:

def file = new File('e:/interfaces.txt')
def s = 0

The file looks like this:

INT_ORDERS,SOURCE,ORDERS,STAGING,ORDERS

So now for the Real code:


// Get default context:
OdiContext context = ((IOdiContextFinder)odiInstance.getTransactionalEntityManager().getFinder(OdiContext.class)).findDefaultContext();

// Get Folder:
Collection odiFolders = ((IOdiFolderFinder)odiInstance.getTransactionalEntityManager().getFinder(OdiFolder.class)).findByName(folder);
if (odiFolders.size() == 0)
{
  println("Error: cannot find folder "+folder+" in project "+project)
}
OdiFolder odiFolder = (OdiFolder) (odiFolders.toArray()[0]);

// Print default settings:
println ("Context used = " + context.getName())
println ("Folder for interface = " + odiFolder.getName())

file.eachLine { line ->
        s++
        toks = line.split(",")

        if (toks.length != 5)
        {
          println("Error in input, line: "+s)
        }
        else
        {
          iname = toks[0]
          srcmodel = toks[1]
          srctab = toks[2]
          tgtmodel = toks[3]
          tgttab = toks[4]

          // Transactie:
          ITransactionDefinition txnDef = new DefaultTransactionDefinition()
          ITransactionManager tm = odiInstance.getTransactionManager()
          ITransactionStatus txnStatus = tm.getTransaction(txnDef)

          OdiInterface odiInterface = new OdiInterface(odiFolder, iname, context);

          InteractiveInterfaceHelperWithActions interactiveHelper = new InteractiveInterfaceHelperWithActions(odiInterface, odiInstance, odiInstance.getTransactionalEntityManager());

          DataSet dataSet = odiInterface.getDataSets().get(0);
          OdiDataStore odiDatastore1 = ((IOdiDataStoreFinder)odiInstance.getTransactionalEntityManager().getFinder(OdiDataStore.class)).findByName(srctab, srcmodel);
          OdiDataStore targetDatastore = ((IOdiDataStoreFinder)odiInstance.getTransactionalEntityManager().getFinder(OdiDataStore.class)).findByName(tgttab, tgtmodel);

          interactiveHelper.performAction(new InterfaceActionAddSourceDataStore(odiDatastore1, dataSet, new AliasComputerDoubleChecker(), new ClauseImporterLazy(), new AutoMappingComputerLazy()));
          interactiveHelper.performAction(new InterfaceActionSetTargetDataStore(targetDatastore, new MappingMatchPolicyLazy(), new AutoMappingComputerLazy(), new AutoMappingComputerLazy(), new TargetKeyChooserPrimaryKey()));

          interactiveHelper.performAction(new InterfaceActionOnTargetDataStoreComputeAutoMapping())
          interactiveHelper.computeSourceSets()
          interactiveHelper.preparePersist()

          tm.commit(txnStatus)
        }
}

So what have we done?

We created an interface between two datastores with the default Knowledge Modules selected.
Building default interfaces is now as easy as running a script within Oracle Data Integrator.

A log file is also created

context used = Development
Folder for interface = Source_1
Interface INTFC1 created

Note
I’m not a Groovy/Java expert so I’ve done my best. Tips? Please help me!


Responses

  1. Hi Michael

    Good to see you using the groovy console, it’s a great way for accelerated development.

    Cheers
    David


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

Join 119 other followers

%d bloggers like this: