Groovy Or Java?

Groovy Or Java?

When I asked a colleague at Big Red in Utrecht what Groovy was, he gave me a book, and then he mentioned; ah well you know, you can just write Java and execute it as Groovy.

So basically I don't particular get the specific syntax of Groovy.

I just use my good old Eclipse (or IntelliJ) write Java in it, remove the package declaration, omit the main function and execute it in Groovy.

Here is the trick; so I have written a class in Java and want to excute it in ODI; so I;

  • Remove the package declaration "package test;"
  • Remove the reference to the OdiInstance; it is instantiated in ODI Studio automagically. "final OdiInstance odiInstance = null;"
  • Remove the overloading of the Groovy method println

"void println(String msg) {

System.out.println(msg);

}"

  • And remove the class declaration "public class Jodi{"
  • and the closing "}"

Below we have the Java Class and underneath that the resulting Groovy script we can execute in ODI.

Java Code

package test;

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;


import oracle.odi.core.OdiInstance;

import oracle.odi.domain.mapping.Mapping;

import oracle.odi.domain.mapping.finder.IMappingFinder;

import oracle.odi.domain.project.OdiFolder;

import oracle.odi.domain.project.finder.IOdiFolderFinder;


public class Jodi{

public void findMappingsInFolderLike(final String like) {

String project = "SOME_PROJECT";

IOdiFolderFinder folderFinder = (IOdiFolderFinder) odiInstance.getTransactionalEntityManager()

.getFinder(OdiFolder.class);

Collection<OdiFolder> odiFolders = folderFinder.findByProject(project);

List<String> folders = new ArrayList<String>();

for (OdiFolder odiFolder : odiFolders) {

folders.add(odiFolder.getName());

}

for (String folder : folders) {

IMappingFinder mapFinder = (IMappingFinder) odiInstance.getTransactionalEntityManager()

.getFinder(Mapping.class);

Collection<Mapping> mappings = mapFinder.findByProject(project, folder);

for (Mapping m : mappings) {

if (m.getName().contains(like.replace("%", "").replace("*", ""))) {

String message = "Found mapping %s in folder %s.";

println(String.format(message, m.getName(), m.getParentFolder().getName()));

}

}

}

}

void deleteThisMethod() {

findMappingsInFolderLike("CONTACT");

}

void println(String msg) {

System.out.println(msg);

}

final OdiInstance odiInstance = null;

}

Groovy Code

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

import oracle.odi.core.OdiInstance;

import oracle.odi.domain.adapter.topology.ITechnology;

import oracle.odi.domain.mapping.IMapComponent;

import oracle.odi.domain.mapping.Mapping;

import oracle.odi.domain.mapping.component.AggregateComponent;

import oracle.odi.domain.mapping.finder.IMappingFinder;

import oracle.odi.domain.project.OdiFolder;

import oracle.odi.domain.project.finder.IOdiFolderFinder;

import oracle.odi.domain.topology.OdiTechnology;

import oracle.odi.domain.topology.finder.IOdiTechnologyFinder;

public void findMappingsInFolderLike(final String like) {

String project = "SOME_PROJECT";

IOdiFolderFinder folderFinder = (IOdiFolderFinder) odiInstance

.getTransactionalEntityManager().getFinder(OdiFolder.class);

Collection<OdiFolder> odiFolders = folderFinder.findByProject(project);

List<String> folders = new ArrayList<String>();

for (OdiFolder odiFolder : odiFolders) {

folders.add(odiFolder.getName());

}

for (String folder : folders) {

IMappingFinder mapFinder = (IMappingFinder) odiInstance

.getTransactionalEntityManager().getFinder(Mapping.class);

Collection<Mapping> mappings = mapFinder.findByProject(project,

folder);

for (Mapping m : mappings) {

if(m.getName().contains(like.replace("%", "").replace("*", ""))){

String message = "Found mapping %s in folder %s.";

println(String.format(message,m.getName(),m.getParentFolder().getName()));

}

}

}

}

findMappingsInFolderLike("CONTACT");

So why use Java to write Groovy?

Basically there are thousands of good Java editors (with code completion and error detection, syntax highlighting among others),

and only a handful of Groovy editors.

In Java simple patterns are far easier to implement like the one in the example: recursion.

But hey, it's just my 2 cents on it.

See you in the next one!