Referencing a global variable in forloop from Apache Velocity - velocity

I am having trouble with having a formatter within a forloop in Apache Velocity.
#set( $array = ["10000", "3000", "13.456", "1111.13"] )
<ul>
#foreach( $a in $array)
<li>$formatter.print($a)</li>
#end
</ul>
This would be evaluated and print the original expression as a string 4 times
$formatter.print($a)
$formatter.print($a)
$formatter.print($a)
$formatter.print($a)
instead of
10,000.00
3,000.00
13.456
1,111.13
However it seems to work fine with the formatter outside of the scope from the forloop
<p>$formatter.print(123456)</p>
This would work as usual
Can anyone helps me figuring out how to reference a property (in this case $formatter) within a for loop ?

This can happen when one of the following condition is true:
1) The model passed to the velocity does not have the variable "formatter"
2) The method print is returning null or it does not exist
3) The method print accepts a parameter of the wrong type. Try to pass Object...
The following code works for me (notice that I am using an array of double and not any more an array of string):
package test;
import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
public class VelocityHelloWorld
{
public static void main( String[] args )
throws Exception
{
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
VelocityContext context = new VelocityContext();
context.put("formatter", new Formatter());
Template t = ve.getTemplate( "helloworld.vm" );
StringWriter writer = new StringWriter();
t.merge( context, writer );
System.out.println(writer.toString());
}
}
Velocity:
#set( $array = [10.00 , 20.00, 13.456, 1111.13] )
<ul>
#foreach( $a in $array)
<li>$formatter.print($a)</li>
#end
</ul>
Formatter:
package test;
public class Formatter {
public String print (Object d) {
String s = d.getClass().getName() + ": " + d.toString();
return s;
}
}
The template shows the same behaviour like yours if I substitute in
print (Double d)
Double with Float.
Long story short... I think that you probably need to check the parameter passed to your method.
Of course I think you should use Double and create the array as a list of double and not as a list of strings.

Try ${myref.doIt($var)} syntax to reference context variable. This ensures Velocity does not parse context names incorrectly within strings.

Related

Data is written to BigQuery but not in proper format

I'm writing data to BigQuery and successfully gets written there. But I'm concerned with the format in which it is getting written.
Below is the format in which the data is shown when I execute any query in BigQuery :
Check the first row, the value of SalesComponent is CPS_H but its showing 'BeamRecord [dataValues=[CPS_H' and In the ModelIteration the value is ended with a square braket.
Below is the code that is used to push data to BigQuery from BeamSql:
TableSchema tableSchema = new TableSchema().setFields(ImmutableList.of(
new TableFieldSchema().setName("SalesComponent").setType("STRING").setMode("REQUIRED"),
new TableFieldSchema().setName("DuetoValue").setType("STRING").setMode("REQUIRED"),
new TableFieldSchema().setName("ModelIteration").setType("STRING").setMode("REQUIRED")
));
TableReference tableSpec = BigQueryHelpers.parseTableSpec("beta-194409:data_id1.tables_test");
System.out.println("Start Bigquery");
final_out.apply(MapElements.into(TypeDescriptor.of(TableRow.class)).via(
(MyOutputClass elem) -> new TableRow().set("SalesComponent", elem.SalesComponent).set("DuetoValue", elem.DuetoValue).set("ModelIteration", elem.ModelIteration)))
.apply(BigQueryIO.writeTableRows()
.to(tableSpec)
.withSchema(tableSchema)
.withCreateDisposition(CreateDisposition.CREATE_IF_NEEDED)
.withWriteDisposition(WriteDisposition.WRITE_TRUNCATE));
p.run().waitUntilFinish();
EDIT
I have transformed BeamRecord into MyOutputClass type using below code and this also doesn't work:
PCollection<MyOutputClass> final_out = join_query.apply(ParDo.of(new DoFn<BeamRecord, MyOutputClass>() {
private static final long serialVersionUID = 1L;
#ProcessElement
public void processElement(ProcessContext c) {
BeamRecord record = c.element();
String[] strArr = record.toString().split(",");
MyOutputClass moc = new MyOutputClass();
moc.setSalesComponent(strArr[0]);
moc.setDuetoValue(strArr[1]);
moc.setModelIteration(strArr[2]);
c.output(moc);
}
}));
It looks like your MyOutputClass is constructed incorrectly (with incorrect values). If you look at it, BigQueryIO is able to create rows with correct fields just fine. But those fields have wrong values. Which means that when you call .set("SalesComponent", elem.SalesComponent) you already have incorrect data in the elem.
My guess is the problem is in some previous step, when you convert from BeamRecord to MyOutputClass. You would get a result similar to what you're seeing if you did something like this (or some other conversion logic did this for you behind the scenes):
convert BeamRecord to string by calling beamRecord.toString();
if you look at BeamRecord.toString() implementation you can see that you're getting exactly that string format;
split this string by , getting an array of strings;
construct MyOutputClass from that array;
Pseudocode for this is something like:
PCollection<MyOutputClass> final_out =
beamRecords
.apply(
ParDo.of(new DoFn() {
#ProcessElement
void processElement(Context c) {
BeamRecord record = c.elem();
String[] fields = record.toString().split(",");
MyOutputClass elem = new MyOutputClass();
elem.SalesComponent = fields[0];
elem.DuetoValue = fields[1];
...
c.output(elem);
}
})
);
Correct way of doing something like this is to call getters on the record instead of splitting its string representation, along these lines (pseudocode):
PCollection<MyOutputClass> final_out =
beamRecords
.apply(
ParDo.of(new DoFn() {
#ProcessElement
void processElement(Context c) {
BeamRecord record = c.elem();
MyOutputClass elem = new MyOutputClass();
//get field value by name
elem.SalesComponent = record.getString("CPS_H...");
// get another field value by name
elem.DuetoValue = record.getInteger("...");
...
c.output(elem);
}
})
);
You can verify something like this by adding a simple ParDo where you either put a breakpoint and look at the elements in the debugger, or output the elements somewhere else (e.g. console).
I was able to resolve this issue using below methods :
PCollection<MyOutputClass> final_out = record40.apply(ParDo.of(new DoFn<BeamRecord, MyOutputClass>() {
private static final long serialVersionUID = 1L;
#ProcessElement
public void processElement(ProcessContext c) throws ParseException {
BeamRecord record = c.element();
String strArr = record.toString();
String strArr1 = strArr.substring(24);
String xyz = strArr1.replace("]","");
String[] strArr2 = xyz.split(",");

Kohana: trying to dynamically change content (not the whole page)

I am struggling to get my head around this since too many hours and I need some help.:)
I have a website build on Kohana and want to dynamically change the content of some text when the user click one button or and another. Not sure if I am doing it the right way but this what I did so far (ho by the way I am new to this framework).
Controller:
class Controller_Homepage extends Controller_General {
public $template = "template/widepage";
public $textbuyer = array (
'text1' => "homepage.buyer.bigtext1", //transfering language variable.
'text2' => "homepage.buyer.bigtext2",
//with more ...
);
public $textseller = array (
'text1' => "homepage.seller.bigtext1",
'text2' => "homepage.seller.bigtext2",
'text3' => "homepage.seller.bigtext3",
//with more ...
);
public $thetext = array ("textbuyer"); //the defaul array is textbuyer
public function action_index() {
$this->content = View::factory("homepage")
->bind('pagetext', $thetext );
if ($this->request->method() === Request::POST) {
$post= $this->request->post();
if (isset($post['buyer'])){
$thetext = $textbuyer;//gives rrorException [ Notice ]: Undefined variable: textbuyer
// arr::overwrite($thetext, $textbuyer);
}else if(isset($post['seller'])){
$thetext = $textseller;
}
}
}
Section of my view to show how I use the variable in the view:
<div class="container_content">
<div>
<p id='sline'><?php echo $pagetext['text1']; ?></p>
</div>
<div>
Can't get the content of my array to the view and when I click on one of the two buttons this code gives me the following error: ErrorException [ Notice ]: Undefined variable: textbuyer. What I am doing wrong ? Why I get the error I mentionned ?
Thank you!
When you define the variables like this
public $textbuyer = ...
public $textseller = ...
public $thetext = ...
They are attributes of your class. And since they are, you need to call them via
$this->textbuyer
$this->textseller
$this->thetext
Just as you call methods inside the same class with $this->methodName() instead of methodName().
class Foo {
public $bar = "hello ";
public function foo() {
$bar = "world";
print $this->bar.$bar; // hello world
}
}
This would work just fine and you get the error because you never define $textbuyer (because you want to call $this->textbuyer).

How to get rid of StringTemplate warning "\n in string"

I'm using StringTemplate 4 to generate some Java source files.
The templates are really simple, e.g.:
QueryHandler(method) ::="
public class Obtenir<method.name>Handler extends QueryHandler\<List\<<method.name>Db>> implements IQueryHandler\<List\<<method.name>>>{
private IQuery\<List\<<method.name>Db>> query;
private <method.name>Converter converter;
#Inject
public Obtenir<method.name>Handler(IQuery\<List\<<method.name>Db>> query, <method.name>Converter converter, IStatisticsCollecter theStatsCollecter){
super(theStatsCollecter);
if(query == null){
throw new IllegalArgumentException(\"The query argument cannot be null\");
}
if(converter == null){
throw new IllegalArgumentException(\"Illegal argument for converter(null)\");
}
this.query = query;
this.converter = converter;
}
public List\<<method.name>> handle(Object... params) throws JdbcException {
final String method = \"obtenir<method.name>\";
DaoQueryStatusCallable status = new DaoQueryStatusCallable();
List\<<method.name>Db> result = invoke(query, status, method);
return converter.convert(result);
}
}
"
The code is even simpler:
STGroup group = new STGroupFile("src/main/resources/QueryHandler.stg");
ST wsTemplate = group.getInstanceOf("QueryHandler");
wsTemplate.add("method", m);
System.out.println(wsTemplate.render());
The template lines are separated by Unix EOLs (\n).
When I execute the code, StringTemplate is emitting a warning "QueryHandler.stg 1:25: \n in string".
The result is correct, but I'd still like to get rid of this message.
Anybody ever had this problem and knows how to solve it?
t() ::= "..." is meant only for single lines. Please use
t() ::= <<
...
>>
to get multi-line templates.
Ter

Issue with Antlr Stringtemplate V4 ArrayList iteration

heres my test in java
public class person
{
public String name;
public int age;
public String getName() { return name; }
public int getAge() { return age; }
}
In my function I create a number of person objects
and add it into an list
ArrayList<person> arr = new ArrayList<person>()
arr.add(person1);
arr.add(person2); etc etc
in the string template group file I have got
test(arr) ::= <<
<table>
$arr: {a|
<tr><td>$a.name$</td><td>$a.age$</td></tr>
}$
</table>
>>
this is called from my list template
list (arr) ::= <<
$test(arr)$
... and other page details etc
>>
in version4 for I get template not found message with some stack trace as follows
Caused by: java.lang.ClassCastException: java.util.ArrayList
at org.antlr.runtime.tree.RewriteRuleTokenStream.nextNode(RewriteRuleTokenStream.java:58)
at org.stringtemplate.v4.compiler.STParser.subtemplate(STParser.java:1563)
at org.stringtemplate.v4.compiler.STParser.mapTemplateRef(STParser.java:3692)
but I can list an array as follows and it prints the object representation in string format
(also I can use a map - ie key value pairs works ok too)
in string template
test(arr) :: =<<
<p>
$arr; seperator="</br>"$
</p>
>>
how do we iterate list of object to print field values using string template grop in V4
am I using it in a wrong format/syntax?
any help/points would be greateful
note: we set $ as delimiter using new STGroupString("", templateGroup, '$', '$');
You are missing the terminating $ on your expression: $a.age should be $a.age$

optimize a string.Format + replace

I have this function. The visual studio profile marked the line with string.Format as hot and were i spend much of my time.
How can i write this loop more efficiently?
public string EscapeNoPredicate(string sz)
{
var s = new StringBuilder(sz);
s.Replace(sepStr, sepStr + sepStr);
foreach (char v in IllegalChars)
{
string s2 = string.Format("{0}{1:X2}", seperator, (Int16)v);
s.Replace(v.ToString(), s2);
}
return s.ToString();
}
Instead of calculating s2s foreach v each time this method is called; you can store them precalculated. Of course I am assuming IllegalChars and seperator remains same.
In a string.format you can put objects, so (Int16)v is not needed. You can supply "v"