You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Copernico <en...@gmail.com> on 2015/08/27 19:17:39 UTC

Camel Velocity pass ArrayList

Hi!

I have a velocity template for send an email. In this template i send to our
clients a list of items (JPA Entities) as an arraylist (is a result of a
query which size may be diferent each time)

Dear ${headers.name}

You request ${headers.itemList.size} items

#foreach(${item} in ${headers.itemList})
 Item ${item.description} - ${item.price}          
#end

With Java DSL i put the headers

exchange.getIn().setHeader("name", client.getName());
exchange.getIn().setHeader("itemList", itemList());


But the testing email i receive, does not have the items list from the
arraylist (the foreach loop is not working) and camel is not replacing
${headers.itemList.size}

Dear George

The items

You request ${headers.itemList.size} items


Any Ideas? How i pass objects like a pojo, a jpa entity, an arraylist to the
velocity template and access to their properties?

Thanks, 







--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Velocity-pass-ArrayList-tp5771027.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel Velocity pass ArrayList

Posted by Raul Kripalani <ra...@evosent.com>.
I assume you mean a JMS queue. Is your class Serializable? If not, it'll
not get passed on.

Try making it Serializable and let us know.

Thanks,

*Raúl Kripalani*
Apache Camel PMC Member & Committer | Enterprise Architect, Open Source
Integration specialist
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk

On Fri, Aug 28, 2015 at 2:52 PM, Copernico <en...@gmail.com> wrote:

> OK. It's working now. I had to change my routerbuilder class. Before, i
> had a
> bean to query the items in the database, put the headers and send it to a
> queue. In another route, after consumed this, passed it to velocity
> component producer and send it with smtp.
>
> Now i pass the query items to the velocity template directly and then send
> it to the queue where the other route sends the email. I don't know why
> camel does not keep references to the object pased in the header.
>
>
>
>
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Camel-Velocity-pass-ArrayList-tp5771027p5771062.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Camel Velocity pass ArrayList

Posted by Copernico <en...@gmail.com>.
OK. It's working now. I had to change my routerbuilder class. Before, i had a
bean to query the items in the database, put the headers and send it to a
queue. In another route, after consumed this, passed it to velocity
component producer and send it with smtp.

Now i pass the query items to the velocity template directly and then send
it to the queue where the other route sends the email. I don't know why
camel does not keep references to the object pased in the header.








--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Velocity-pass-ArrayList-tp5771027p5771062.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel Velocity pass ArrayList

Posted by Raul Kripalani <ra...@evosent.com>.
The signature of .setHeader() is .setHeader(String headerName, Expression
expression).

Your itemList() method returns a List, so I'm pretty sure something's
missing here.

Are you sure that's the code you're running?

*Raúl Kripalani*
Apache Camel PMC Member & Committer | Enterprise Architect, Open Source
Integration specialist
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk

On Fri, Aug 28, 2015 at 12:33 PM, Copernico <en...@gmail.com> wrote:

> private List<Item> itemList(){
>   Query query = em.createNamedQuery("Items.findByDescription",
> Items.class);
>   query.setParameter("description", "something");
>
>   if(query.getResultList() != null && !query.getResultList().isEmpty()){
>       return query.getResultList();
>   }
>
>   return new ArrayList();
> }
>
>
> The function is working well: when i debugging the fuction it returns a
> list
> of item (is not empty). The problem is with adding the list to the header
> and using it in a foreach loop in velocity template.
>
> So the custion is how to use this array list in a foreach loop in velocity?
>
> Thanks
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Camel-Velocity-pass-ArrayList-tp5771027p5771052.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Camel Velocity pass ArrayList

Posted by Copernico <en...@gmail.com>.
private List<Item> itemList(){
  Query query = em.createNamedQuery("Items.findByDescription", Items.class);
  query.setParameter("description", "something");

  if(query.getResultList() != null && !query.getResultList().isEmpty()){
      return query.getResultList();
  }

  return new ArrayList();
}


The function is working well: when i debugging the fuction it returns a list
of item (is not empty). The problem is with adding the list to the header
and using it in a foreach loop in velocity template.

So the custion is how to use this array list in a foreach loop in velocity?

Thanks



--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Velocity-pass-ArrayList-tp5771027p5771052.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel Velocity pass ArrayList

Posted by Copernico <en...@gmail.com>.
The camel version is 2.15.2
I'm doing now a simple test to see if camel replaces de variables 

public class Item{
   private String description;
   public Item(){}
   public String getDescription(){return this.description;}
   public void setDescription(String description){this.description =
description;}
}

In emailgeneratorbean

Item item = new Item()
item.setDescription("this is a description");
exchange.getIn().setHeader("item", item);

The velocity template 

The item description is ${headers.item.getDescription()}

But ${item.getDescription()} is not been replaced

So i adding Velocity Context.

In emailgeneratorbean

Item item = new Item()
item.setDescription("this is a description");

VelocityContext context = new VelocityContext();
context.put("item", item);

exchange.getIn().setHeader("CamelVelocityContext", context);

The velocity template 

The item description is ${item.getDescription()}

But still is not working

I guess the problem is that if i pass any object (pojo, jpa entitiy) in the
header of the exchange, camel is not replacing it correctly with velocity.
In the las test when i pass the VelocityContext camel is ignoring it.

I read velocity documentation about using objects as variables and the
expresion ${item.getDescription()} is correct as also are correct
$item.description or ${item.description}.

The fact is that camel is not recognosing and object in the header or is not
passing it correctly to velocity, but if i put something like 

exchange.getIn().setHeader("name", "Copernico");

An in template

Hola ${headers.name}

y gets

Hola Copernico









--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Velocity-pass-ArrayList-tp5771027p5771059.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel Velocity pass ArrayList

Posted by Raul Kripalani <ra...@evosent.com>.
Ah, of course, I missed that. Sifting through emails too quickly ;-)

What version of Camel are you on?

Could JPA lazy loading be the culprit here? Can you switch to eager loading?

*Raúl Kripalani*
Apache Camel PMC Member & Committer | Enterprise Architect, Open Source
Integration specialist
http://about.me/raulkripalani | http://www.linkedin.com/in/raulkripalani
http://blog.raulkr.net | twitter: @raulvk

On Fri, Aug 28, 2015 at 1:08 PM, Copernico <en...@gmail.com> wrote:

> Yes. I'm sure. The code is working. All this code is inside a bean not in
> my
> route builder class. From the route builder, i generate the email, pass the
> template to velocity and send email.
>
> The code is working because i send the email to myself and i can see the
> body of the email as i said before:
>
> Template
>
> *
> Dear ${headers.name}
>
> You request ${headers.itemList.size} items
>
> #foreach(${item} in ${headers.itemList})
>  Item ${item.description} - ${item.price}
> #end *
>
> Email
> *
> Dear George
>
> You request ${headers.itemList.size} items
>
> /(the item list should go here)/
> *
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Camel-Velocity-pass-ArrayList-tp5771027p5771054.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: Camel Velocity pass ArrayList

Posted by Copernico <en...@gmail.com>.
Yes. I'm sure. The code is working. All this code is inside a bean not in my
route builder class. From the route builder, i generate the email, pass the
template to velocity and send email. 

The code is working because i send the email to myself and i can see the
body of the email as i said before: 

Template

*
Dear ${headers.name}

You request ${headers.itemList.size} items

#foreach(${item} in ${headers.itemList})
 Item ${item.description} - ${item.price}          
#end *

Email
*
Dear George

You request ${headers.itemList.size} items 

/(the item list should go here)/
*




--
View this message in context: http://camel.465427.n5.nabble.com/Camel-Velocity-pass-ArrayList-tp5771027p5771054.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Camel Velocity pass ArrayList

Posted by Raul Kripalani <ra...@evosent.com>.
Could you please paste the itemList() function?

Thanks.
On 27 Aug 2015 18:17, "Copernico" <en...@gmail.com> wrote:

> Hi!
>
> I have a velocity template for send an email. In this template i send to
> our
> clients a list of items (JPA Entities) as an arraylist (is a result of a
> query which size may be diferent each time)
>
> Dear ${headers.name}
>
> You request ${headers.itemList.size} items
>
> #foreach(${item} in ${headers.itemList})
>  Item ${item.description} - ${item.price}
> #end
>
> With Java DSL i put the headers
>
> exchange.getIn().setHeader("name", client.getName());
> exchange.getIn().setHeader("itemList", itemList());
>
>
> But the testing email i receive, does not have the items list from the
> arraylist (the foreach loop is not working) and camel is not replacing
> ${headers.itemList.size}
>
> Dear George
>
> The items
>
> You request ${headers.itemList.size} items
>
>
> Any Ideas? How i pass objects like a pojo, a jpa entity, an arraylist to
> the
> velocity template and access to their properties?
>
> Thanks,
>
>
>
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Camel-Velocity-pass-ArrayList-tp5771027.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>