assigning String to in-array for looping

hi all.,
im writing a Flow Service. im doing few steps to finish it. here im giving my steps:
1st : calling a Java Service which gives a NoOf Files and send it to pipeline out.
this OutPut Value is String in my Java Service and OUtPut also same.
2nd : i want to take that No Of Files and now my service has to loop thru my next steps those many No of times of my NoOfFiles value.
here im not getting how to assign that pipeLine value to in-aaray Value.
3rd…end.
can any one help me to resolve this
Thanks&Regard’s;
M Kartheek

Hi,

Keep in mind that the webMethods Flow loop is not the same as a Java loop - Flow expects an array of objects to loop over, rather than a numeric count of the number of times to loop.

In Java you tend to code array loops like this:
for (i=0; i<someArray.length; i++) {
// Do something with someArray[i] …
}

Whereas in Flow, you would code the same loop like this:
Loop over someArray {
// Do something with someArray … (NOTE: someArray implies someArray[i]!)
}

Inside the Flow loop construct, the array variable name points to the current (single) array element, rather than the whole array.

So, if your java can return a string array containing your results, then the easiest solution is simply to use that array as the in-array value to Flow’s loop construct.

If your java must return the number of results, and you have to call another method to extract each result individually, then an alternative approach would be to use Repeat, setting count to n-1 and “repeat on” to SUCCESS.

Both the Loop and Repeat constructs set a variable inside the loop which you can use to determine which loop iteration you are up to.

Hope this helps.

Cheers,

Steve Ovens

thank u Steve.,
now im trying with repeat. now problem is that just above the repeat
1: i written Java Service which gives the NoOfFiles–its working fine…
2: after this i done for repeat and i put that pipeline value in count and repeat-on as ‘SUCCESS’.
problem is when i run the FlowService cursor is not moving from JavaService eventhough Java Service giving the results also.
if i remove that Java Service and give some hard-coded value in count then flow is working fine.
please let me know if you have solution for this.
Regard’s
Kartheek

What do you mean by “FlowService cursor”?

Sounds like you want to do something to each of a list of files. Why don’t you have a service that returns the list of filenames as a StringList and then Flow Loop over the entries in the StringList?

Cheers,
Fred

thnk u guys for ur immediate replies.,
im taking the noOfFiles(not-listOfFiles) from a directory and then i have to repeat few steps till this noOfFiles-1(looping is total-1) times…
in repeat i stated count as %noOfFiles-1% and it is working fine. but when it comes to at end of the fileslist(noOfFiles) its throwing an
Exception: String Index out of Range:-1
please let me know if nyone has solution for this.
Thanks&Regard’s;
M Kartheek

Hi,

Sounds like the repeat on success looping over %count-1% is not doing what you expect. I suspect that it is not evaluating the “%count-1%” expression correctly, and is therefore going beyond the number of items in your list. I would guess that the string index out of range exception is being thrown by your java service when it is asked to do something to element # 11 in a 10-element list (for example). If you need to use repeat, I would recommend putting a Map step before the repeat to decrement the loop count variable, e.g.
=> pub.math:addInts ( Add -1 to count and store result in count)
=> Repeat on success, count is %count%

However, I agree with Fred that the best solution in your case sounds like returning the list of files into a String array and then looping over that using the Loop construct.

For example - create a Java service which takes a directoryName input parameter and returns the list of files (and directories) as a String array, e.g.:

IDataCursor pipelineCursor = pipeline.getCursor();
String directoryName = IDataUtil.getString( pipelineCursor, “directoryName”);
if ((directoryName == null) || !directoryName.equals("")) {
directoryName = “.”; // Default to current directory
}
String[] files = new java.io.File(directoryName).list();
IDataUtil.put( pipelineCursor, “files”, files );
pipelineCursor.destroy();

Then, write a Flow service which calls the directoryList Java service and then loops over “files” (as the in-array parameter), e.g.
=> invoke directoryList, storing result in variable “files”
=> Loop in-array = “files”
=> pub.flow:debugLog, message is “%files%”, variable substitution is selected.

As a simple example, I set a debugLog step to output the message “File # %$iteration% is [%files%]”, which resulted in output like this:
File # 1 is [Apache_license.txt]
File # 2 is [audit]
File # 3 is [bin]
etc.

You might also filter the list (in your Java service) to remove any directories, sort it alphabetically, convert to all uppercase, etc. etc.

Hope this helps.

Cheers,

Steve Ovens

thank u Steve .,
Thank you very much…now its working fine…actually problem was which u expected is correct->in count i given %count-1% . system coudnt able to find that value. can’t we do like that.?
other Query is: in the Dynamic Query if want like:
Ex: Select * from table-name where col1=? and col2=? and col3=’?’
how can we assign the value for col3. in DataBase col3 allows only Varchar.
if i give (‘value’) in pipeline and i remove codes in Query then its fine. it wont be any problem. but later i want to store same value in Data Base then it becomes ‘Value’ in DataBase which is not supposed to be…
Thanks&Regard’s;
M kartheek

Hi,

For a simple Select statement as in your example, you should not need the Dynamic SQL template, you should be able to just use a Select template.

For example, if the table has three columns:
COLUMN_A NUMERIC
COLUMN_B NUMERIC
COLUMN_C VARCHAR(32)
then you can construct a select statement using:
Select * from my_table where COLUMN_A=? AND COLUMN_B=? and COLUMN_C=?

There is no need to put quotes around the third question mark, regardless of the fact that the third column is a VARCHAR - the question mark is simply a placeholder for the variable you are going to pass in - JDBC (which is what the templates become when they are executed) takes care of the data type matching for you.

I hope this helps.

Cheers,

Steve Ovens

Hi
i have taken a stringlist in that iam taking ten names like(Ramesh,suresh,Rajesh,santu,josh,Ramesh,roshan,akhil,Ramesh,sadan).
so when i run this the Ramesh should be come 3 times in the o/p.
what is the service to be taken.

Rahul - What do you want to achieve. Your question is not so clear to ans.

Thanks,

Well, I understand your question here but you should have explained little more clear and precise.

PFA screen shots. You can also write this code by Java service, but flow service is simple and straight forward.

More questions?


IO.JPG