Home
Analytics
Parameter Date validation and reformating
Ducharme
I am a newbie to BIRT and may not be stating the right question but here goes. I need to do the following:
Q1) Take a Start_Date and End_Date and ensure that the Start_Date is <= End_Date. These dates need to work in an Oracle 10g query.
I have set up the dates as strings and have the queries working. I added the following code to the validate event handler:
if (params["Start_Date"].value > params["End_Date"].value){
false;
}
else{
true;
}
I tried some tests with different data:
- 10/1/2008 (start date) and 10/2/2008 (end date): everything is fine
- 10/3/2008 (start date) and 10/2/2008(end date): error shown
- 1/1/2009 (start date) and 10/2/2008(end date): NO error shown
When running a simple test, (no query execution, just parameter check), I get an error box saying "org.eclipse.birt.report.service.api.ReportServiceException: Required parameter Start_Date is not set"
Not exactly useful. This brings up 2 points.
- how do I print out a useful message?
- The string test data comparison does not seem to work correctly.
This implies the value of the strings is not easily compared in that form even though javascript says to use the ">" operators. So what to do from that?
- I could try to manipulate the string in some form so I can compare them
- I could input the dates in Date format ( I tried this using short date formating with the above validate() event handler). The comparison so see if 01/01/09 is greater than 10/01/08 also indicated that it was not - returned true. Is this a bug?
I am running Eclipse Version: 3.3.1
Build id: M20070921-1145
I then converted the event handler to the following code:
BirtComp.lessOrEqual(params["Start_Date"].value, params["End_Date"].value);
This worked with the test data shown above - so that seems to take care of the comparison when using Date data type. It does not work when using Strings as input.
So what? If I input this in date format, it is not usable in the Oracle query. Therefore, what is the actual javaScript code to put in the validate to create a valid Oracle Date from the JavaScript Date (or a generated string).
I am thinking something silly like:
- create 2 hidden parameters SD and ED
- read Start_Date (date) and End_Date (date)
- do the comparison - if a problem, setup the output message and send false.
- if okay, make a string out of the passed in Date
- set the params["SD"] = string start date and do the same for the ED.
- use the SD and ED in the query.
I therefore, created the following script in validate for Start_Date
if (BirtComp.lessOrEqual(params["Start_Date"].value, params["End_Date"].value)){
var curdate = new Date(params["Start_Date"]);
var mday = curdate.getDate();
var month = curdate.getMonth();
var year = getFullYear();
params["SD"] = month+"/"+mday+"/"+year;
curdate = new Date(params["End_Date"]);
mday = curdate.getDate();
month = curdate.getMonth();
year = getFullYear();
params["ED"] = month+"/"+mday+"/"+year;
true;
}
else {
false;
}
Unfortunately, all I get is the mystery error message and I cannot tell what is wrong.
So, Finally. What is the best way to enter and compare the dates and then, if necessary, convert them to strings so I can use Oracle to_date() to use them in queries. I would appreciate actual code for the JavaScript if possible.
Thank you
Find more posts tagged with
Comments
mwilliams
Hi Ducharme,
I have not tried this at all, but one thing you could possibly do is use string parameters. Then in the validate script, change the 2 string dates to date objects with the something like the following javascript.
df = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd");
df2 = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd");
myStartDate = df.parse(params["StartDate"].value);
myEndDate = df2.parse(params["EndDate"].value);
Then do your comparison of these date values with the way that worked for you. This way, your parameters stay in string format for you to pass to you Oracle database. Again, I have not tested this, so I'm not 100% sure it'll work, but it's worth a shot. Let me know.
Ducharme
I tried the following in the event handler after setting Start_Date and End_Date to strings. It did not work, says Start_Date is not set.
df = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd");
df2 = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd");
myStartDate = df.parse(params["Start_Date"].value);
myEndDate = df2.parse(params["End_Date"].value);
if (myStartDate <= myEndDate){
true;
}else{
false;
}
Also tried:
df = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd");
df2 = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd");
myStartDate = df.parse(params["Start_Date"].value);
myEndDate = df2.parse(params["End_Date"].value);
BirtComp.lessOrEqual(myStartDate, myEndDate);
That did not work either. I think the idea of converting to in internal date for the comparison while passing in a string makes the most sense - just how to do it?
Also how about the output message - any way to change?
Ducharme
The following does not fail:
df = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd");
true;
Only when this is added does it fail:
df = new Packages.java.text.SimpleDateFormat("yyyy-MM-dd");
myStartDate = df.parse("10/01/2008");
true;
Ducharme
The following logic does work.
importPackage(Packages.java.text);
pos = new ParsePosition(0);
df = new SimpleDateFormat("yyyy-MM-dd");
myStartDate = df.parse("10/01/2008", pos);
true;
Next step will be to go back to the complete code and see what happens.
mwilliams
Ducharme,
I'm going to have to test this myself to try to figure it out. I'll let you know if I get it. As for the error message, you could write a custom parameter page to handle your parameters and output whatever you want. You would also be able to use this jsp page to validate the parameters if you went that route.
mwilliams
Ducharme,
Looks like you may have found a way to get my suggestion to work while I was typing my last message. Let me know if it does. If not, I'll do some testing and see if I can get it. Good luck!
Ducharme
Well, not so good.
Code now looks like:
importPackage(Packages.java.text);
df = new SimpleDateFormat("yyyy-MM-dd");
df2 = new SimpleDateFormat("yyyy-MM-dd");
pos = new ParsePosition(0);
myStartDate = df.parse(params["Start_Date"].value, pos);
pos1 = new ParsePosition(0);
myEndDate = df2.parse(params["End_Date"].value, pos1);
if (BirtComp.lessOrEqual(myStartDate, myEndDate)) {
true;
} else {
false;
}
This always passes no matter the data. I also tried for the last lines to be:
if (myStartDate <= myEndDate) {
true;
} else {
false;
}
and
BirtComp.lessOrEqual(myStartDate, myEndDate);
Any ideas?
Ducharme
Some more information.
With the following logic:
importPackage(Packages.java.text);
df = new SimpleDateFormat("yyyy-MM-dd");
df2 = new SimpleDateFormat("yyyy-MM-dd");
pos = new ParsePosition(0);
s1 = params["Start_Date"].value;
s1="10/02/2008";
myStartDate = df.format(s1, pos);
pos1 = new ParsePosition(0);
s2 = params["End_Date"].value;
s2="10/03/2008";
myEndDate = df2.format(s2, pos1);
params["SD"].value=="10/02/2008";
params["ED"].value="10/03/2008";
BirtComp.lessOrEqual(myStartDate, myEndDate);
It prints out the sting even though the SD and ED are date types. However, if I replace the code to set SD and ED with:
params["SD"].value=myStartDate;
params["ED"].value=myEndDate;
nothing is printed. Is this really generating a date?
mwilliams
Ducharme,<br />
<br />
This seems to work for me.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
df = new Packages.java.text.SimpleDateFormat("MM/dd/yyyy");
df2 = new Packages.java.text.SimpleDateFormat("MM/dd/yyyy");
myStartDate = df.parse(params["StartDate"].value);
myEndDate = df2.parse(params["EndDate"].value);
if (BirtComp.lessOrEqual(myStartDate, myEndDate)){
true;
}
else{
false;
}
</pre>
<br />
I put it in the validate method for the EndDate parameter. Try it out and let me know.
Ducharme
That works for me also. Thank you very much.
mwilliams
Ducharme,
No problem. Glad I could help.
bhattinilesh
It worked fine, please let me know how can we pop up error message in case condition (start date > end date) fails?
mwilliams
Hi bhattinilesh,<br />
<br />
Using the validate parameter script should cause a default message to pop up. To do your own custom messaging, you'd need to create your own parameter jsp page. Take a look at the following for help with getting started with that.<br />
<br />
<a class='bbc_url' href='
http://www.birt-exchange.com/devshare/deploying-birt-reports/298-2-2-birt-tag-library/#description'>2.2
BIRT Tag Library - Tips & Tricks - BIRT Exchange</a>