Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Is it possible to check the existence of a context parameter, like I'd check a key in the global map?
I'm trying to avoid having to create context parameters for every variable I might possibly check for.
Yes, you can do this like:
if ( context.containsKey("myParameter") ) { // myParameter is a context variable } else { // myParameter is not a context variable }
You can reference the context object in the fields of most components or in a tJava or tJavaRow if you need custom logic.
How would I access the value without getting an unresolved compile error?
@cyberpunkspike wrote:
How would I access the value without getting an unresolved compile error?
Like I showed you above:
if ( context.containsKey("myParameter") ) { System.out.println((String)context.myParameter); } else { // myParameter is not a context variable }
context.myparameter would be unresolved.
You're checking for the existence of a contextkey at runtime, but retrieving from the corresponding field a value at compilation time.
This is a slightly different way of doing this. FYI you can look at the generated code to work most of this stuff out if you have enough Java experience.
PropertiesWithType pwt = (PropertiesWithType)context; if ( pwt.getProperty("myParameter")!=null ) { if(pwt.getContextType("myParameter").compareToIgnoreCase("ID_STRING")==0){ System.out.println(pwt.getProperty("myParameter")); }else if(pwt.getContextType("myParameter").compareToIgnoreCase("ID_INTEGER")==0){ System.out.println(pwt.getProperty("myParameter")); } } else { // myParameter is not a context variable }
There is a class constructed inside the Talend job called PropertiesWithType. This has all of the information you will need, even the type information so that you know how to deal with it. The code I have written above is just a basic example, you should be able to reconstruct this to do what you want.
@cyberpunkspike wrote:
context.myparameter would be unresolved.
You're checking for the existence of a contextkey at runtime, but retrieving from the corresponding field a value at compilation time.
Yes, that is the way to do it. If the context parameter does not exist at run time, my code does not try to access it so there will be no error.
If you post more specifics on your use case maybe we could help you more.
@nfz11 your code would lead to compilation errors I'm afraid. If you try to use a context variable (in the normal way) that does not exist in a job, it will not compile.
Try this code in a job without the context variable "bob".....
if(1==0){ System.out.println(context.bob); }
The code inside the if condition will never run as 1 will never equal 0. However the code inside is still compiled.
@rhall wrote:
@nfz11 your code would lead to compilation errors I'm afraid. If you try to use a context variable (in the normal way) that does not exist in a job, it will not compile.
Try this code in a job without the context variable "bob".....
if(1==0){ System.out.println(context.bob); }The code inside the if condition will never run as 1 will never equal 0. However the code inside is still compiled.
Oh OK thanks, I did not know that. I thought it worked as it does in Java.
It does work as it does in Java. You can try the same code (but replacing "context.bob" with another variable that has not been declared in your code) in a small Java class.