Conditional writing to CSV file with Scriptella - export-to-csv

I'am working on an ETL script that reads data from an DB, manipulates it using Java and finally writes it to a CSV file. My script looks like this:
<query connection-id="db">
SELECT COLUMN_A AS A, COLUMN_B AS B
FROM DATABASE_TABLE
<script connection-id="java">
System.setProperty("a",String.valueOf(get("A")));
System.setProperty("b",String.valueOf(get("B")));
</script>
<script connection-id="csv_out">
$a,$b
</script>
</query>
The scripts is working in a way that I get one row written to the CSV file for each row in the query result. But what I need is a way to create more or less rows than the query result has delivered. Therefore I want a conditional output to the CSV file, based on values set in the Java part:
<script connection-id="java">
System.setProperty("a",String.valueOf(get("A")));
System.setProperty("b",String.valueOf(get("B")));
</script>
// only create line when 'a' has specific value, e.g. '1'
<script connection-id="csv_out" if="$a == 1">
$a,$b
</script>
This approach does not work. It writes nothing to the CSV, even if I set the value of 'a' explicitely to 1. So what is the right way to do conditional script execution in Scriptella? Or is there an even better solution?

Got it. You just have to use the name of the varibale, without the '$' symbol.
// only create line when 'a' has specific value, e.g. 1
<script connection-id="csv_out" if="a == 1">
$a,$b
</script>
For String values you have to use quotes around the comparison String.
// only create line when 'a' has specific value, e.g. 'ok'
<script connection-id="csv_out" if="a == 'ok'">
$a,$b
</script>

Related

Source a value from custom record field to a field in employee record

I need to source a value from a custom record field (type: decimal) to a field in employee record(type: decimal). when the custom record is created. Is there any way I can implement this in workflow or should I go for the script?
If I choose the script which script I need to write?
Actually, I am new to script.so can anyone show me some sample?
Sounds very much like business logic, and thus shouldn't be done in a template... but, maybe you have no choice, so you can write a function like this:
<#--
Returns if two sequences (Java List-s or Java arrays usually) are equal.
Items at the same index must have common comparable type, and no item
can be null, or else you may get an error!
-->
<#function sequencesEqual s1, s2>
<#if s1?size != s2?size>
<#return false>
</#if>
<#list s1 as i1>
<#if i1 != s2[i1?index]>
<#return false>
</#if>
</#list>
<#return true>
</#function>
Then you can do things like <#if sequencesEqual(foo, bar)>...</#if>.

How to dynamically add a script to the page using watir-webdriver

I have some *.js script that I want to execute using watir-webdriver during my test runs. So the question is there any way to upload the script to the page using watir-webdriver?
You could use the execute_script method to add a script element with the src attribute as your js file. The method call would look like:
browser.execute_script(
"var the_script = document.createElement('script');
the_script.setAttribute('src','your_script.js');
document.head.appendChild(the_script);"
)
To see that it works, let us assume you have the page:
<html>
<body>
<input type="text" id="field" value="100">
</body>
</html>
If you try to execute jQuery on the page (the '$' in the script), an exception occurs because the page does not know what jQuery is:
field = browser.text_field
p browser.execute_script('return $(arguments[0]).val();', field)
#=> $ is not defined (Selenium::WebDriver::Error::JavascriptError)
If you add the jQuery script file (http://code.jquery.com/jquery-1.10.2.js), via execute_script, you will now be able to use jQuery:
browser.execute_script(
"var the_script = document.createElement('script');
the_script.setAttribute('src','http://code.jquery.com/jquery-1.10.2.js');
document.head.appendChild(the_script);"
)
field = browser.text_field
p browser.execute_script('return $(arguments[0]).val();', field)
#=> "100"

Are variables (var) supported in XSLTforms or betterForm?

I've prepared the following XForms document based on http://www.w3.org/MarkUp/Forms/wiki/XForms_2.0#The_var_element:
<?xml-stylesheet type="text/xsl" href="../xsltforms/xsltforms.xsl"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms">
<head>
<title>var-demo</title>
<xf:model>
<xf:instance>
<data xmlns="">
<PersonGivenName>Kilroy</PersonGivenName>
</data>
</xf:instance>
</xf:model>
</head>
<body>
<xf:output value="PersonGivenName"><xf:label>PersonGivenName=</xf:label></xf:output>
<xf:var name="pg_name" value="PersonGivenName"/>
<xf:output value="pg_name"><xf:label>pg_name=</xf:label></xf:output>
</body>
</html>
It is very simple: should output (I think) Kilroy twice, first read directly from the model, then putting into a variable and printing that variable.
But the output is only
PersonGivenName=Kilroy
pg_name=
I've tried this from eXist-db 2.0 and eXide version 2.0. The code above is processed by XSLTforms; omitting the first line, it's processed by betterForm - but the result is the same.
The examples after chapter "3.2.4.2 Variable Scope" in http://www.w3.org/MarkUp/Forms/wiki/XForms_2.0 suggest that a $ prefix is needed when the value of the variable is used, but both XSLTforms and betterForm throw exception on changing the last output to value="$pg_name". The code above seems to be syntactically correct, but the value of the variable is not printed out. Can anybody explain this?
Sorry, variables are not implemented yet in XSLTForms.
I frequently define a dedicated instance for "variables".
-Alain
Neither are they in betterFORM. You can use the pattern Alain describes or use a custom simple variable mechanism we've implemented before it occured in the spec:
We got a <xf:setvariable name="foo" value="bar"/> implementation. The resulting variable can then be used with the '$' notation in certain places or (more savely) by using bf:appContext('foo') XPath function.
Joern

How to pass variable to javascript function from <liferay-ui> tag?

I have wirtten following code but it does not gets executed
<liferay-ui:search-container-column-text>
<liferay-ui:input-checkbox param="<%=String.valueOf(item1.getUserId()) %>" formName="frmAllCompanyUserPanelDisplay" onClick="callAddEntry('String.valueOf(item1.getUserId())')" id="<%=String.valueOf(item1.getUserId()) %>"/>
</liferay-ui:search-container-column-text>
<script type="text/javascript">
function callAddEntry(Hello) {
alert("Hello " + Hello);
}
</script>
It gives me same output 'String.valueOf(item1.getUserId())' rather then fetching id dynamically, but when i use normal input checkbox every line of code executed fine? why?
can anyone help me out in solving this javascript problem.
Thanks in Advance.
Try following one.. need to use expression in java script function as follows
callAddEntry('<%=String.valueOf(item1.getUserId())%>')
More abut liferay have look into to www.liferaysavvy.com
<liferay-ui:input-checkbox param="<%=String.valueOf(item1.getUserId()) %>"
formName="frmAllCompanyUserPanelDisplay" onClick="callAddEntry('<%=String.valueOf(item1.getUserId())%>')"
id="<%=String.valueOf(item1.getUserId()) %>"/>

Using KnockoutJS template within jQuery template

I have a jQuery template, and I would like to use a KnockOutJS template within this.
I cannot make this work as this example illustrates: http://jsfiddle.net/maate/bwmcR/1/.
However, it DOES work when the KnockOutJS template itself is placed outside the scope of the jQuery template as in this example: http://jsfiddle.net/maate/bwmcR/2/.
It seems that the problem is related to the scope of the template data variables (for instance, I can access the ${test} variable within the subTemplate).
Does anyone know how to make this work?
Br. Morten
The first example you have is just not valid. Although you want to create a sub-template it has to be in a separate script tag. You just can't embed templates within each other, you have to create them one after the other.
WRONG:
<script id="superTemplate" type="text/html">
...
<script id="subTemplate" type="text/html">
...
</script>
</script>
RIGHT:
<script id="superTemplate" type="text/html">
...
</script>
<script id="subTemplate" type="text/html">
...
</script>
When you applied the subtemplate on the ul, you defined what data it should be using with the foreach, so you won't be able to read the test as it is not a property on an item.
If you want you can pass it as a templateOption so it will be available on the subtemplate too.
<ul data-bind="template: { name: 'subTemplate', foreach: items, templateOptions: { testValue: 'Value' } }"></ul>
This way it will be available on the subtemplate.
<span data-bind="text: $item.testValue"></span>
Also, I wouldn't use the default jQuery template tags, it is much nicer with data-binding.
<div id="body" data-bind="template:{name: 'superTemplate'}"></div>
It will do the same, more or less, in the end. You can take a look here : http://jsfiddle.net/bwmcR/18/