setVariableData to assign a Invoke Input Variable Collection from java embedding - java

I am using the below line in JAVA Embedding to assign value to a BPEL Invoke DB adapter input variable.
setVariableData("S2C_insert_InputVariable","TmpInvStoc3Collection","/ns8:TmpInvStoc3Collection/ns8:TmpInvStoc3/ns8:batchid","12345");
Now i want to put this statement in a while loop within java and want to repeat this for n iterations. I want to place a loop variable in the collection but I dont know how to do this.
I am looking for something like below.
setVariableData("S2C_insert_InputVariable","TmpInvStoc3Collection","/ns8:TmpInvStoc3Collection/ns8:TmpInvStoc3[$loop_variable]/ns8:batchid","12345");
Please let me know how to achieve this
Regards
Murali

This is based on the assumption that value is a function of i and cannot be calculated in the BPEL.
String qry = "";
for (int i = 0; i < n; i++)
{
value = SomeFunctionThatRequiresJavaRatherThanBPEL(i);
qry = "/ns8:TmpInvStoc3Collection/ns8:TmpInvStoc3[" + i + "]/ns8:batchid";
setVariableData("S2C_insert_InputVariable","TmpInvStoc3Collection",qry,value);
}
The code could be more efficient but it should do what you want.

Related

Java Mysql table data output formatting

Here is my MySql table:
I want to show the output of the query in commandline as below:
I have written the code below to loop but I am getting only the first row, What i have to modify ??
ResultSet rs2 = stmt.executeQuery(table_retrive);
String[] cols = new String[itemList.size()];
int[] rec =new int[itemList.size()];
for (int i = 0; i < itemList.size(); i++) {
while (rs2.next()) {
cols[i] =(String) itemList.get(i);
rec[i] = rs2.getInt(cols[i]);
System.out.println(rec[i]+" ");
}
}
Your two loops are wrong. Start at i=0 and then iterate once over the whole ResultSet, filling yor first array position. When this is done, i is incremented and you try to iterate the ResultSet a second time but the cursor is at the end of the ResultSet, so rs2.next() returns false and the code will not be executed.
So you have two Solutions:
Handle the loops correctly. Unfortunately I do not know, what you are trying to do anyways because this is some C-like code without OOP, which doesn't show semantics and then you have this itemList which seems to hold preset values and you read out of this list, which column to take for the i-th position. This seems odd. Maybe switching the loops does the desired: Start with the while and nest the for.
Reset the cursor of the ResultSet after the while with rs2.beforeFirst(). WARNING: This could throw a SQLFeatureNotSupportedException. Not all Databases can move the cursor backwards. This is of course a very ugly solution, since you should first parse the whole row a once.
Try to use printf() Or format() method. It is same as printf method in c lang. you can pass parameters and difference. Look at link1
And link 2
Example : System.out.printf("%d%5s%10d", 5,"|",10);
output : 5 | 10
Using this the I got all the values but in one row :
while (rs2.next()) {
for (int i = 0; i < itemList.size(); i++) {
cols[i] =(String) itemList.get(i);
rec[i] = rs2.getInt(cols[i]);
System.out.print(rec[i]+" ");
}
}
But I need to divide like the rows.
Usage of the inner loop is your problem.
You can enhance your code to remove the usage of the second loop in your code, it basically does nothing. You can loop over your result set and in the same loop using the incremented variable to persist the values accordingly.
The code shown half implemented in your question, hence it will be difficult to give you exactly what need to be done. Nevertheless, here's an attempt to resolve the problem for you:
while (rs2.next()) {
System.out.println(rs2.getInt(1) + "\t |" + rs2.getString(2) + "\t |" + rs2.getString(3));
}
Based on the column names from the table in the question, assuming that column2 and column3 are String's.
You can add the necessary details to this code to complete it according to your usecase, but I've just taken the example of showing a record in one line.
EDIT:
OP has his own way of programming, but to satisfy his question in the comment - this is how you can do it.
while (rs2.next()) {
for (int i = 0; i < itemList.size(); i++)
{
cols[i] =(String) itemList.get(i);
rec[i] = rs2.getInt(cols[i]);
System.out.print(rec[i]+"\t |");
}
System.out.println();
}

Dynamic addition & use of variable outside for loop- Selenium

Below is the scenario i am trying to automate:
Put all numerical values of the links in a Selenium Weblist & perform an addition and later to verify if the sum of count matches a fixed number.
The issue is that the numerical links returns a number engulfed in braces example:(20)(35)(16)(15)
I need to first trim these brackets & fetch only the numbers & then perform the addition i.e: 20+35+16+15
Later i need to assert the total against the number i.e: Assert.assertequals(sum,'86')
List<WebElement> lists=driver.findElements(By.cssSelector("span.ndocs"));
for (int i=0; i<lists.size(); ){
String trimmed_value=lists.get(i).getText();
trimmed_value=lists.get(i).getText().trim().substring(trimmed_value.indexOf("(") + 1);
trimmed_value=lists.get(i).getText().trim().substring(0, trimmed_value.indexOf(")"));
System.out.println(trimmed_value);
int numerical_value = Integer.parseInt(trimmed_value);
i++;
}
Till now i am able to get the elements, iterate them & able to remove the braces & get the numbers, I am stuck upon how to perform the addition operation & then do an Assert for the count.
Any help will be much appreciated here.
Try using below code.
Initialize a variable outside the method and add every trimmed_value to it as explained below.
import assertEquals(import static org.junit.Assert.assertEquals;)
int expected_value=86;
int numerical_value=0;
List<WebElement> lists = driver.findElements(By.cssSelector("span.ndocs"));
for (int i = 0; i < lists.size(); ) {
String trimmed_value = lists.get(i).getText();
trimmed_value = lists.get(i).getText().trim().substring(trimmed_value.indexOf("(") + 1);
trimmed_value = lists.get(i).getText().trim().substring(0, trimmed_value.indexOf(")"));
System.out.println(trimmed_value);
numerical_value =numerical_value+Integer.parseInt(trimmed_value);
i++;
}
assertEquals(expected_value, numerical_value);

Count number of strings returned from Azure query

I am trying to count the number of rows from my azure database table in my android java code. Unfortunately there is no count() method built into the azure library. The closest thing to it is the includeInlineCount() method. I used the following line of code:
final MobileServiceList<Crime> result = mToDoTable.includeInlineCount().execute().get();
Which returns the value of the first column for each row. The value of result looks something like this:
[column1_row1_String, column1_row2_String, column1_row3_String]
How can I extract the number of strings from the value result?
According to the source code of Class MobileServiceList, you can try the code below using the method getTotalCount().
final MobileServiceList<Crime> result = mToDoTable.includeInlineCount().execute().get();
int count = result.getTotalCount();
TRY THIS..it will select all the elements of your table and then count it to return integer value.
int count = mToDoTable.execute().get().getTotalCount();
this will definitely give you your required answer.
There is no built in count() method which can directly count the number, you can try using includeTotalCount() instead, first query out all result and do the count. Below is an example in C#, hope it could help you make it out in Java:
var table = MobileService.GetTable<T> ();
var query = table.Take(0).IncludeTotalCount();
IList<T> results = await query.ToListAsync ();
long count = ((ITotalCountProvider)results).TotalCount;
Check this thread for details:How to get the row count from an azure database?

String naming convention in java

I am currently trying to make a naming convention. The idea behind this is parsing.
Lets say I obtain an xml doc. Everything can be used once, but these 2 in the code below can be submitted several times within the xml document. It could be 1, or simply 100.
This states that ItemNumber and ReceiptType will be grabbed for the first element.
ItemNumber1 = eElement.getElementsByTagName("ItemNumber").item(0).getTextContent();
ReceiptType1 = eElement.getElementsByTagName("ReceiptType").item(0).getTextContent();
This one states that it will grab the second submission if they were in their twice.
ItemNumber2 = eElement.getElementsByTagName("ItemNumber").item(1).getTextContent();
ReceiptType2 = eElement.getElementsByTagName("ReceiptType").item(1).getTextContent();
ItemNumber and ReceiptType must both be submitted together. So if there is 30 ItemNumbers, there must be 30 Receipt Types.
However now I would like to set this in an IF statement to create variables.
I was thinking something along the lines of:
int cnt = 2;
if (eElement.getElementsByTagName("ItemNumber").item(cnt).getTextContent();)
**MAKE VARIABLE**
Then make a loop which adds one to count to see if their is a third or 4th. Now here comes the tricky part..I need them set to a generated variable. Example if ItemNumber 2 existed, it would set it to
String ItemNumber2 = eElement.getElementsByTagName("ItemNumber").item(cnt).getTextContent();
I do not wish to make pre-made variable names as I don't want to code a possible 1000 variables if that 1000 were to happen.
KUDOS for anyone who can help or give tips on just small parts of this as in the naming convention etc. Thanks!
You don't know beforehand how many ItemNumbers and ReceiptTypes you'll get ? Maybe consider using two Lists (java.util.List). Here is an example.
boolean finished = ... ; // true if there is no more item to process
List<String> listItemNumbers = new ArrayList<>();
List<String> listReceiptTypes = new ArrayList<>();
int cnt = 0;
while(!finished) {
String itemNumber = eElement.getElementsByTagName("ItemNumber").item(cnt).getTextContent();
String receiptType = eElement.getElementsByTagName("ReceiptType").item(cnt).getTextContent();
listItemNumbers.add(itemNumber);
listReceiptTypes.add(receiptType);
++cnt;
// update 'finished' (to test if there are remaining itemNumbers to process)
}
// use them :
int indexYouNeed = 32; // for example
String itemNumber = listItemNumbers.get(indexYouNeed); // index start from 0
String receiptType = listReceiptTypes.get(indexYouNeed);

Pass very large variable from Java app to Javascript

What is the quickest way to export a very large string (several megabytes) from a Java app to on-page javascript? At the moment it's taking so long the browser grinds to a halt.
Here is the code I'm using to modify the DOM:
window = JSObject.getWindow(this);
document = (JSObject) window.getMember("document");
for (int i = 0; i < encHexFileUploadStr.length(); i++){
char c = encHexFileUploadStr.charAt(i);
document.eval("document.getElementById('encOutgoingData').value += '"+c+"';");
if (i % 100 == 0) document.eval("console.log("+i+");");
}
Before this I tried to just assign the encHexFileUploadStr variable directly in one go, that was no better.
Is there any good way to do this that isn't so slow?
Thanks!
I have not attempted to transfer the amount of data you are mentioning from a Java application to a webpage, however, if you are using document.eval the performance problem you are having is most likely due to the processing required on an eval statement. An alternative approach is to directly invoke a JavaScript method which performs the actual data update work. Here is an example of how you would go about using this approach:
Java
JSObject jso = JSObject.getWindow(this);
// invoke JavaScript method updateData with parameter encHexFileUploadStr
jso.call("updateData", new String[] { encHexFileUploadStr });
JavaScript
function updateData(s) {
document.getElementById('encOutgoingData').value = s;
}
I would combine Kris' suggestion with breaking the string in chunks of 1024 bytes or such and append to a string in JavaScript and at the end copy, in JavaScript, the string to the field
How do I optimize this method for breaking a string in chunks?
Also in the loop, make it a habit to do
for (int i = 0, n=something.length();i<n; i++){
rather than
for (int i = 0; i < something.length(); i++){

Categories

Resources