Home » Archived » BIRT » Writing to table from external application(trouble write to a table from a Java app)
Writing to table from external application [message #818025] |
Sat, 10 March 2012 21:04  |
Eclipse User |
|
|
|
I have a java application that queries a database and returns a bunch of values in a certain format. I verified that everything writes to the Netbeans console window without any problems, but when I try to use BIRT to create a PDF of the same data some of it doesn't show up. The PDF gets created and my program seems to pass all of the static values (too a couple of grid controls) without any problems, but when I try to print to a table nothing gets printed.
I created the table and set it to use a scripted data source. The columns are: [Food Description][Amount][Calories][Carbohydrates][Proteins][Fat]
Then from my Java program I am just looping the following code to try to populate the table. In theory, each iteration through my loop was supposed to write 3 rows into the table. Any ideas? Thanks.
String FD = "";
String AMT = "";
String CAL = "";
String Carbs = "";
String PT = "";
String FT = "";
FD = proteinArray[0];
AMT = proteinArray[1];
CAL = proteinArray[2];
Carbs = proteinArray[3];
PT = proteinArray[4];
FT = proteinArray[5];
runTask.setParameterValue("FD", FD);
runTask.setParameterValue("AMT", AMT);
runTask.setParameterValue("CAL", CAL);
runTask.setParameterValue("Carbs", Carbs);
runTask.setParameterValue("PT", PT);
runTask.setParameterValue("FT", FT);
FD = carbArray[0];
AMT = carbArray[1];
CAL = carbArray[2];
Carbs = carbArray[3];
PT = carbArray[4];
FT = carbArray[5];
runTask.setParameterValue("FD", FD);
runTask.setParameterValue("AMT", AMT);
runTask.setParameterValue("CAL", CAL);
runTask.setParameterValue("Carbs", Carbs);
runTask.setParameterValue("PT", PT);
runTask.setParameterValue("FT", FT);
FD = vegArray[0];
AMT = vegArray[1];
CAL = vegArray[2];
Carbs = vegArray[3];
PT = vegArray[4];
FT = vegArray[5];
runTask.setParameterValue("FD", FD);
runTask.setParameterValue("AMT", AMT);
runTask.setParameterValue("CAL", CAL);
runTask.setParameterValue("Carbs", Carbs);
runTask.setParameterValue("PT", PT);
runTask.setParameterValue("FT", FT);
|
|
|
Re: Writing to table from external application [message #819198 is a reply to message #818025] |
Mon, 12 March 2012 11:59   |
Eclipse User |
|
|
|
Brad,
What are your parameter types for the report. For example:
runTask.setParameterValue("FD"....
What is the parameter type for FD in the report design?
Jason
On 3/10/2012 9:04 PM, Brad Rarick wrote:
> I have a java application that queries a database and returns a bunch of
> values in a certain format. I verified that everything writes to the
> Netbeans console window without any problems, but when I try to use BIRT
> to create a PDF of the same data some of it doesn't show up. The PDF
> gets created and my program seems to pass all of the static values (too
> a couple of grid controls) without any problems, but when I try to print
> to a table nothing gets printed.
>
> I created the table and set it to use a scripted data source. The
> columns are: [Food
> Description][Amount][Calories][Carbohydrates][Proteins][Fat]
>
> Then from my Java program I am just looping the following code to try to
> populate the table. In theory, each iteration through my loop was
> supposed to write 3 rows into the table. Any ideas? Thanks.
>
>
> String FD = "";
> String AMT = "";
> String CAL = "";
> String Carbs = "";
> String PT = "";
> String FT = "";
> FD = proteinArray[0];
> AMT = proteinArray[1];
> CAL = proteinArray[2];
> Carbs = proteinArray[3];
> PT = proteinArray[4];
> FT = proteinArray[5];
> runTask.setParameterValue("FD", FD);
> runTask.setParameterValue("AMT", AMT);
> runTask.setParameterValue("CAL", CAL);
> runTask.setParameterValue("Carbs", Carbs);
> runTask.setParameterValue("PT", PT);
> runTask.setParameterValue("FT", FT);
> FD = carbArray[0];
> AMT = carbArray[1];
> CAL = carbArray[2];
> Carbs = carbArray[3];
> PT = carbArray[4];
> FT = carbArray[5];
> runTask.setParameterValue("FD", FD);
> runTask.setParameterValue("AMT", AMT);
> runTask.setParameterValue("CAL", CAL);
> runTask.setParameterValue("Carbs", Carbs);
> runTask.setParameterValue("PT", PT);
> runTask.setParameterValue("FT", FT);
> FD = vegArray[0];
> AMT = vegArray[1];
> CAL = vegArray[2];
> Carbs = vegArray[3];
> PT = vegArray[4];
> FT = vegArray[5];
> runTask.setParameterValue("FD", FD);
> runTask.setParameterValue("AMT", AMT);
> runTask.setParameterValue("CAL", CAL);
> runTask.setParameterValue("Carbs", Carbs);
> runTask.setParameterValue("PT", PT);
> runTask.setParameterValue("FT", FT);
>
>
>
>
|
|
|
Re: Writing to table from external application [message #819357 is a reply to message #819198] |
Mon, 12 March 2012 16:33   |
Eclipse User |
|
|
|
They are both strings.
In my Java app I have the elements in each string array being set to a string variable. The code has changed slightly as to what is above as I have been messing with it on and off all day.
String proteinArray[] = protein.split(delims);
String carbArray[] = carb.split(delims);
String vegArray[] = veg.split(delims);
String FD = "";
String AMT = "";
String CAL = "";
String Carbs = "";
String PT = "";
String FT = "";
FD = proteinArray[0];
AMT = proteinArray[1];
CAL = proteinArray[2];
Carbs = proteinArray[3];
PT = proteinArray[4];
FT = proteinArray[5];
runTask.setParameterValue("Food Description", FD);
runTask.setParameterValue("Amount", AMT);
runTask.setParameterValue("Calories", CAL);
runTask.setParameterValue("Carbohydrates", Carbs);
runTask.setParameterValue("Protein", PT);
runTask.setParameterValue("Fat", FT);
I then created 6 report parameters called "Food Description", "Amount", "Calories", "Carbohydrates", "Protein", and "Fat". These are all of datatype String and hidden.
I then bound the 6 report parameters to the 6 data set parameters for the table. The binding properties of one of the columns looks like...
Data Set: None
Data Column Binding
-------------------
Name: Food Description
Display Name: Food Description
Data Type: String
Expression: params["Food Description"].value
|
|
|
Re: Writing to table from external application [message #819399 is a reply to message #819357] |
Mon, 12 March 2012 17:49   |
Eclipse User |
|
|
|
Can you post the report design?
Jason
On 3/12/2012 4:33 PM, Brad Rarick wrote:
> They are both strings.
>
> In my Java app I have the elements in each string array being set to a
> string variable. The code has changed slightly as to what is above as I
> have been messing with it on and off all day.
>
> String proteinArray[] = protein.split(delims);
> String carbArray[] = carb.split(delims);
> String vegArray[] = veg.split(delims);
> String FD = "";
> String AMT = "";
> String CAL = "";
> String Carbs = "";
> String PT = "";
> String FT = "";
> FD = proteinArray[0];
> AMT = proteinArray[1];
> CAL = proteinArray[2];
> Carbs = proteinArray[3];
> PT = proteinArray[4];
> FT = proteinArray[5];
>
> runTask.setParameterValue("Food Description", FD);
> runTask.setParameterValue("Amount", AMT);
> runTask.setParameterValue("Calories", CAL);
> runTask.setParameterValue("Carbohydrates", Carbs);
> runTask.setParameterValue("Protein", PT);
> runTask.setParameterValue("Fat", FT);
>
> I then created 6 report parameters called "Food Description", "Amount",
> "Calories", "Carbohydrates", "Protein", and "Fat". These are all of
> datatype String and hidden.
>
> I then bound the 6 report parameters to the 6 data set parameters for
> the table. The binding properties of one of the columns looks like...
>
> Data Set: None
>
> Data Column Binding
> -------------------
> Name: Food Description
> Display Name: Food Description
> Data Type: String
> Expression: params["Food Description"].value
|
|
| | | | | |
Re: Writing to table from external application [message #821595 is a reply to message #821021] |
Thu, 15 March 2012 11:27   |
Eclipse User |
|
|
|
Still trying to get this to work...
Since each row in my table contains 6 items (Description, Amount, Calories, Carbs, Protein, and Fat) I figured it would be easier to store a list in each hashmap (which cleaned up my code alot).
So now I have 1 hashmap with 3 lists in it (protein, carb, and veg). Each list is supposed to write one row in my table.
So my Java program has this...
map.put("protein", proteinList);
map.put("carb", carbList);
map.put("veg", vegList);
runTask.getAppContext().put("Meal", map);
How do you tell the report to use a list to populate a row?
I assume the the iterater code will be the same:
apphm = ReportContext.getAppContext().get("Meal");
mealSet = apphm.entrySet();
iter = mealSet.iterator();
What would the fetch code look like?
if(iter.hasNext()) {
myIter = iter.next();
//code to populate one row with a list
return true;
} else {
return false;
}
Then how would you keep the table rows from being overwritten? My map is going to be populated a number of times because it runs in a loop. Is there a way to tell Birt no to overwrite rows that are already written too?
|
|
|
Re: Writing to table from external application [message #821799 is a reply to message #821595] |
Thu, 15 March 2012 16:58   |
Eclipse User |
|
|
|
I am not sure what you mean. Why not use some code similar to:
//Java Side
Object Cols[6];
ArrayList myrows = new ArrayList();
//Description, Amount, Calories, Carbs, Protein, and Fat
Cols[0] = DescriptionValRow1;
Cols[1] = AmountValRow1;
Cols[2] = CaloriesValRow1;
..
..
..
myrows.add(Cols);
Cols[0] = DescriptionValRow2;
Cols[1] = AmountValRow2;
Cols[2] = CaloriesValRow2;
..
..
..
myrows.add(Cols);
..
..
..
runTask.getAppContext().put("myrows", myrows);
//Report Side
//Scripted data set
open method
myrows = reportContext().get("myrows");
ii=0;
fetch method
if( ii < myrows.size() ){
var currentrow = myrows.get(ii);
row["Description"] = currentrow[0];
row["Amount"] = currentrow[1];
row["Calories"] = currentrow[2];
..
..
..
i++;
}else{
return false;
}
Jason
On 3/15/2012 11:27 AM, Brad Rarick wrote:
> Still trying to get this to work...
>
> Since each row in my table contains 6 items (Description, Amount,
> Calories, Carbs, Protein, and Fat) I figured it would be easier to store
> a list in each hashmap (which cleaned up my code alot).
>
> So now I have 1 hashmap with 3 lists in it (protein, carb, and veg).
> Each list is supposed to write one row in my table.
> So my Java program has this...
>
> map.put("protein", proteinList);
> map.put("carb", carbList);
> map.put("veg", vegList);
>
> runTask.getAppContext().put("Meal", map);
>
>
>
> How do you tell the report to use a list to populate a row?
>
> I assume the the iterater code will be the same:
>
> apphm = ReportContext.getAppContext().get("Meal");
> mealSet = apphm.entrySet();
> iter = mealSet.iterator();
>
>
> What would the fetch code look like?
>
>
> if(iter.hasNext()) { myIter = iter.next();
> //code to populate one row with a list
> return true;
> } else {
> return false;
> }
>
>
> Then how would you keep the table rows from being overwritten? My map is
> going to be populated a number of times because it runs in a loop. Is
> there a way to tell Birt no to overwrite rows that are already written too?
>
|
|
| | | |
Re: Writing to table from external application [message #824524 is a reply to message #824487] |
Mon, 19 March 2012 15:20   |
Eclipse User |
|
|
|
Brad,
Try changing Object Cols[6]; to an arraylist as well.
ArrayList Cols;
ArrayList myrows = new ArrayList();
//Description, Amount, Calories, Carbs, Protein, and Fat
Cols.add(0,DescriptionValRow1);
Cols.add(1,Amount....
Then currentrow should be an array list object and you can then do:
row["Food Description"] = currentrow.get(0);
Jason
On 3/19/2012 2:29 PM, Brad Rarick wrote:
> That cured the open method, but the fetch method still has some
> problems. I think the problem lies in the fact that currentrow is a var
> and can't be accessed with indexes.
>
>
> if(i < master2.length){
> var currentrow = master2[i];
> row["Food Description"] = currentrow[0];
> row["Amount"] = currentrow[1];
> row["Calories"] = currentrow[2];
> row["Carbohydrates"] = currentrow[3]
> row["Protein"] = currentrow[4]
> row["Fat"] = currentrow[5]
> i++;
> }else{
> return false;
> }
>
>
> Error:
>
> A BIRT exception occurred. See next exception for more information.
> Error evaluating Javascript expression. Script engine error: Java class
> "java.lang.String" has no public instance field or method named "0".
> (/report/data-sets/script-data-set[@id="219"]/method[@name="fetch"]#3)
> Script source:
> /report/data-sets/script-data-set[@id="219"]/method[@name="fetch"],
> line: 0, text:
> __bm_FETCH()
> org.eclipse.birt.data.engine.core.DataException: Fail to execute script
> in function __bm_FETCH(). Source:
> ------
> " + if(i < master2.length){
> var currentrow = master2[i];
> row["Food Description"] = currentrow[0];
> row["Amount"] = currentrow[1];
> row["Calories"] = currentrow[2];
> row["Carbohydrates"] = currentrow[3]
> row["Protein"] = currentrow[4]
> row["Fat"] = currentrow[5]
> i++;
> }else{
> return false;
> }
> + "
> -----
>
|
|
|
Re: Writing to table from external application [message #824556 is a reply to message #824524] |
Mon, 19 March 2012 16:23   |
Eclipse User |
|
|
|
Well that sounded good in theory The same exact error still occurs. It doesn't like the index.
The current code of the program is below:
ArrayList<String> columns = new ArrayList<String>();
ArrayList<String> master = new ArrayList<String>();
for(int a=1; a<size; a++){
String protein = my_meals.get(index);
index++;
String carb = my_meals.get(index);
index++;
String veg = my_meals.get(index);
index++;
String proteinArray[] = protein.split(delims);
String carbArray[] = carb.split(delims);
String vegArray[] = veg.split(delims);
columns.add(0, proteinArray[0]);
columns.add(1, proteinArray[1]);
columns.add(2, proteinArray[2]);
columns.add(3, proteinArray[3]);
columns.add(4, proteinArray[4]);
columns.add(5, proteinArray[5]);
master.addAll(columns);
columns.add(0, carbArray[0]);
columns.add(1, carbArray[1]);
columns.add(2, carbArray[2]);
columns.add(3, carbArray[3]);
columns.add(4, carbArray[4]);
columns.add(5, carbArray[5]);
master.addAll(columns);
columns.add(0, vegArray[0]);
columns.add(1, vegArray[1]);
columns.add(2, vegArray[2]);
columns.add(3, vegArray[3]);
columns.add(4, vegArray[4]);
columns.add(5, vegArray[5]);
master.addAll(columns);
}
Object master2[] = master.toArray();
runTask.getAppContext().put("master", master2);
|
|
|
Re: Writing to table from external application [message #824672 is a reply to message #824556] |
Mon, 19 March 2012 19:23   |
Eclipse User |
|
|
|
Take a look at the beforefactory and the scripted dataset on the
attached report.
Jason
On 3/19/2012 4:23 PM, Brad Rarick wrote:
> Well that sounded good in theory :) The same exact error still occurs.
> It doesn't like the index.
>
> The current code of the program is below:
>
>
> ArrayList<String> columns = new ArrayList<String>();
> ArrayList<String> master = new ArrayList<String>();
> for(int a=1; a<size; a++){
> String protein = my_meals.get(index);
> index++;
> String carb = my_meals.get(index);
> index++;
> String veg = my_meals.get(index);
> index++;
> String proteinArray[] = protein.split(delims);
> String carbArray[] = carb.split(delims);
> String vegArray[] = veg.split(delims);
> columns.add(0, proteinArray[0]);
> columns.add(1, proteinArray[1]);
> columns.add(2, proteinArray[2]);
> columns.add(3, proteinArray[3]);
> columns.add(4, proteinArray[4]);
> columns.add(5, proteinArray[5]);
> master.addAll(columns);
> columns.add(0, carbArray[0]);
> columns.add(1, carbArray[1]);
> columns.add(2, carbArray[2]);
> columns.add(3, carbArray[3]);
> columns.add(4, carbArray[4]);
> columns.add(5, carbArray[5]);
> master.addAll(columns);
> columns.add(0, vegArray[0]);
> columns.add(1, vegArray[1]);
> columns.add(2, vegArray[2]);
> columns.add(3, vegArray[3]);
> columns.add(4, vegArray[4]);
> columns.add(5, vegArray[5]);
> master.addAll(columns);
> }
> Object master2[] = master.toArray();
> runTask.getAppContext().put("master", master2);
>
|
|
|
Re: Writing to table from external application [message #825155 is a reply to message #824672] |
Tue, 20 March 2012 10:43   |
Eclipse User |
|
|
|
I just want to say, I'm buying everyone a beer (or 5) if this ever works 
OK, looking over you code I have 3 questions
- Your code uses size() on the open script, but when I try to use it I get an error that says "has no public instance field or method named "size"". Can I use array.length instead? Does it make a difference?
- Since I set all of my report values inside of my Java app (and write to the report API), do I need any code at all in the beforefactory method?
- If a sample of my master arraylist looks like the following:
Fish, tuna, light, canned in oil, drained solids
28.0
56
0
8
2
GARDENBURGER, Homestyle Classic Veggie Burger
66.0
113
7
12
6
Onions, frozen, whole, unprepared
321.0
113
27
3
0
Could my fetch method be something like this?
if(i < len){
var currentrow = master[i];
row["Food Description"] = currentrow[i];
i++;
row["Amount"] = currentrow.item[i];
i++;
row["Calories"] = currentrow.item[i];
i++;
row["Carbohydrates"] = currentrow.item[i];
i++;
row["Protein"] = currentrow.item[i];
i++;
row["Fat"] = currentrow.item[i];
i++;
}else{
return false;
}
|
|
| | | | |
Re: Writing to table from external application [message #825389 is a reply to message #825366] |
Tue, 20 March 2012 15:54  |
Eclipse User |
|
|
|
The error is below. The fetch doesn't recognize master as being an array.
A BIRT exception occurred. See next exception for more information.
Error evaluating Javascript expression. Script engine error: Java class "java.lang.String" has no public instance field or method named "0". (/report/data-sets/script-data-set[@id="219"]/method[@name="fetch"]#3)
Script source: /report/data-sets/script-data-set[@id="219"]/method[@name="fetch"], line: 0, text:
__bm_FETCH()
org.eclipse.birt.data.engine.core.DataException: Fail to execute script in function __bm_FETCH(). Source:
------
" + if(i < len){
var currentrow = master[i];
row["Food Description"] = currentrow[i];
i++;
row["Amount"] = currentrow[i];
i++;
row["Calories"] = currentrow[i];
i++;
row["Carbohydrates"] = currentrow[i];
i++;
row["Protein"] = currentrow[i];
i++;
row["Fat"] = currentrow[i];
i++;
}else{
return false;
}
+ "
-----
The current state of the report is attached. There really isn't much to it.
The data coming in is one arraylist of Strings called master. As mentioned before, the contents of master mimic what is below. Every 6 indexes in the arraylist will be a row.
index value
----- -----
0 Fish, tuna, light, canned in oil, drained solids
1 28.0
2 56
3 0
4 8
5 2
6 GARDENBURGER, Homestyle Classic Veggie Burger
7 66.0
8 113
9 7
10 12
11 6
12 Onions, frozen, whole, unprepared
13 321.0
14 113
15 27
16 3
17 0
|
|
|
Goto Forum:
Current Time: Wed Jul 23 05:16:57 EDT 2025
Powered by FUDForum. Page generated in 0.50123 seconds
|