If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Wednesday, October 17, 2012

Cocoon flowscript gotcha

Today I got a pretty hard to debug issue on my plate. To give more insight into the complexity let me explain the chain of events happening:
  • client side javascript doing a POST to a cocoon pipeline
  • flowscript calling another pipeline to fetch JSON from XMLDB
  • send a response to the zipserializer 
So there is a lot going on in those 3 simple events.  The easy part is we post some values from a form to a server side action.

POST parameters:
id=PH3030AL

But the id PH303AL is the identifier of a chemical content XML file. What we really need is the identifiers of the 1-to-many relationship with salesitems. We can retrieve those by executing an xquery.  We wrote a custom XQuery Generator that takes any request parameters and injects them dynamically into the XQuery as parameters.

So let's take a look at an action mapped to an XQuery:

<map:match pattern="xquery/getSalesItems">
  <map:generate src="xquery/chemicalcontent/getSalesItems.xquery" type="queryStringXquery"/>
  <map:serialize type="xml"/>
</map:match>

The only thing needed is mapping some match pattern to the xquery and the XQuery generator will execute the xquery after injecting any requests parameters provided.

So far so good.  So if we now invoke following URL:
http://localhost:8888/search/xquery/getSalesItems?id=PH3030AL
we get back following JSON response

[{"id": "PH3030AL","nc12s": ["934063085115"]}]

But from flowscript we needed to call this pipeline and this is were the problem occurred.

var output = new Packages.java.io.ByteArrayOutputStream();
var requestString = new Collection(cocoon.request.getParameterValues("id")).stringJoin(function(id) {return "id=" + id}, "&");
var uri = "xquery/getSalesItems?" + requestString;
cocoon.processPipelineTo(uri, null, output);

When we printed the output to the console we got following JSON output:
[{"id": "PH3030AL","nc12s": ["934063085115"]},{"id": "PH3030AL","nc12s": ["934063085115"]}]
To keep a long story short. We should not add the request string another time for invoking another pipeline in the same request as the POST parameters were already present and we ended up with duplicate request parameters. Changing the URI to the one below fixed our bug.
var uri = "xquery/getSalesItems";
 

No comments:

Post a Comment