Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Report 7B :- Validating Parameter Range
nuraniuscc
Hi,
I have 2 parameters, namely, Begin Date and End Date. The one validation I need to do is ensure that the difference between those 2 dates is a maximum of 6 months.
How do I do that?
Where can I put in my Custom error message? OR
Is there a default error message?
Please explain step by step, So I can use it rightaway.
Thanks
Nurani Sivakumar
Find more posts tagged with
Comments
bhanley
You can validate parameters in any of the scripting events available to you throughout the report's life cycle. The initialize event of the report design is one option. From here you will be able to get the parameter's value using the following code:
reportContext.getParameterValue("pParameterName");
If you need to store the upper and lower limits on the range you are evaluating, try using hidden parameters on the report. Your users will not see them and you can easily override them at run time should the range change over time. Then access the hidden parameters in the scripting block jsut as you access the public parameters.
Once you have evaluated the parameters, you can choose to act on the results of the evaluation however you see fit. One option would be to have your report contain two elements. The default element is what is seen should the parameters pass validation. The other element is an error condition shown to the user should they supply invalid parameters. Make the error condition a hidden element. Should the validation routine fail, hide the default element in the report and make the error description element visible.
Hope this helps.
nuraniuscc
Hi,
I have 2 dates in the format (yyyymm). So I have 2 fields called Begin Date
and End Date.
Can I say directly like this and Does BIRT know to compute them as Dates?
I was thinking like 200902 - 200812 should be 2 months...Right?
reportContext.getParameterValue("End Date") - reportContext.getParameterValue("Begin Date");
Thanks
Nurani Sivakumar
mwilliams
Nurani,
You will have to use date functions to figure the time. i.e. DateTimeSpan.months(startDate,endDate) would return how many months as a number.
bhanley
You need to tell BIRT that the parameters are dates. Remember, BIRT treats all parameters as strings (under the covers it all boils down to XML). Here is some code. In the code you will see straight Java Script used to parse the difference in Days between two dates and also the "DateTimeSpan" convenience operator to determine months. <br />
<br />
Good Luck<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var start = Date.parse(reportContext.getParameterValue("pStartDate"));
var end = Date.parse(reportContext.getParameterValue("pEndDate"));
//Set 1 day in milliseconds
var one_day = 1000*60*60*24;
var days = (end - start) / one_day;
var months = DateTimeSpan.months(reportContext.getParameterValue("pStartDate"), reportContext.getParameterValue("pEndDate"));
reportContext.setParameterValue("pDays", days + " days have passed");
reportContext.setParameterValue("pMonths", months + " months have passed");</pre>
nuraniuscc
Hi,
I tried the above code, using my Date variables. My Final Date is in CCYYMM format. So, the user would enter Jan 2008 which is basically saying 200801.
BIRT doeesn't seem to like the way I am using it and it gives error as given below. (Java.lang.Illegalargumentexception) Please help on what needs to be done to fix this.
DataException: A BIRT exception occurred: Error evaluating Javascript expression. Script engine error: Wrapped java.lang.IllegalArgumentException: Cannot convert 2010.0 to java.util.Date (<inline>#13) (<inline>#13) Script source: <inline>, line: 1, text:__bm_beforeOpen(). See next exception for more information.Error evaluating Javascript expression. Script engine error: Wrapped java.lang.IllegalArgumentException: Cannot convert 2010.0 to java.util.Date (<inline>#13) (<inline>#13) Script source: <inline>, line: 1, text:__bm_beforeOpen() at org.eclipse.birt.data.engine.core.DataException.wrap(DataException.java:118) at org.eclipse.birt.data.engine.script.ScriptEvalUtil.evaluateJSAsExpr(ScriptEvalUtil.java:718) at org.eclipse.birt.data.engine.script.JSMethodRunner.runScript(JSMethodRunner.java:77) at org.eclipse.birt.report.engine.script.internal.DtEScriptExecutor.handleJS(DtEScriptExecutor.java:71) ... 71 moreCaused by: org.eclipse.birt.core.exception.CoreException: Error evaluating Javascript expression. Script engine error: Wrapped java.lang.IllegalArgumentException: Cannot convert 2010.0 to java.util.Date (<inline>#13) (<inline>#13) Script source: <inline>, line: 1, text:__bm_beforeOpen() at org.eclipse.birt.core.script.JavascriptEvalUtil.wrapRhinoException(JavascriptEvalUtil.java:299) at org.eclipse.birt.core.script.JavascriptEvalUtil.evaluateRawScript(JavascriptEvalUtil.java:102) at org.eclipse.birt.core.script.JavascriptEvalUtil.evaluateScript(JavascriptEvalUtil.java:134) at org.eclipse.birt.data.engine.script.ScriptEvalUtil.evaluateJSAsExpr(ScriptEvalUtil.java:714) ... 73 moreCaused by: org.mozilla.javascript.WrappedException: Wrapped java.lang.IllegalArgumentException: Cannot convert 2010.0 to java.util.Date (<inline>#13) (<inline>#13) at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1757) at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:170) at org.mozilla.javascript.FunctionObject.call(FunctionObject.java:474) at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:76) at org.mozilla.javascript.gen.c6._c1(<inline>:13) at org.mozilla.javascript.gen.c6.call(<inline>) at org.mozilla.javascript.optimizer.OptRuntime.callName0(OptRuntime.java:108) at org.mozilla.javascript.gen.c7._c0(<inline>:1) at org.mozilla.javascript.gen.c7.call(<inline>) at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393) at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834) at org.mozilla.javascript.gen.c7.call(<inline>) at org.mozilla.javascript.gen.c7.exec(<inline>) at org.eclipse.birt.core.script.JavascriptEvalUtil.evaluateRawScript(JavascriptEvalUtil.java:95) ... 75 moreCaused by: java.lang.IllegalArgumentException: Cannot convert 2010.0 to java.util.Date (<inline>#13) at org.mozilla.javascript.Context.toType(Context.java:1722) at org.eclipse.birt.core.script.NativeDateTimeSpan.toDate(NativeDateTimeSpan.java:79) at org.eclipse.birt.core.script.NativeDateTimeSpan.jsStaticFunction_months(NativeDateTimeSpan.java:113) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:155) ... 87 moreCaused by: org.mozilla.javascript.EvaluatorException: Cannot convert 2010.0 to java.util.Date (<inline>#13) at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:109) at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1030) at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1086) at org.mozilla.javascript.Context.reportRuntimeError2(Context.java:1056) at org.mozilla.javascript.NativeJavaObject.reportConversionError(NativeJavaObject.java:896) at org.mozilla.javascript.NativeJavaObject.coerceTypeImpl(NativeJavaObject.java:556) at org.mozilla.javascript.Context.jsToJava(Context.java:1705) at org.mozilla.javascript.Context.toType(Context.java:1719) ... 94 more
bhanley
It looks to me like the date is only a year, and it is in Integer form. Is this the case? If you only need to manipulate years, you can do that without converting to a date format. The Date conversion is going to look for a full date value to convert. You will likely also need to convert the numericv to a string to have all of the pieces come together.
nuraniuscc
Ok,
The objective here is to find the difference between 2 "Dates" which are in the format CCYYMM (No Day). Since BIRT is looking for a Date to use the Date function, I guess we can do the following.
1) Plug in the default value for the Day as the Begin Day of the Month. (01)
So, the Date will be treated as a String. Right?
I don't want to treat them as Integer, Because that mayn not be consistent
when we are trying to subtract them as Dates.
So, Once I do Step 1, then I can use the function as you recommended.
Please comment
Thanks
Nurani Sivakumar
bhanley
I did not mean to imply that the engine cannot handle different date formats, only that your argument was not valid. Looking at the stack trace, the error when trying to convert to the Date is cited as "Cannot convert 2010.0". Try getting the String value of that variable and not the integer value. is it a numeric in the parameter? You can actually set the parameter up to store a Date type if that helps get things functional.<br />
<br />
If you need to accept the non-standard value (Year and Month only) than simply build out your Date variable using the parameterized constructor.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var startDate = new Date(2009, 1, 13); // Year, Month, Day (Month ais zero-based!!!!)</pre>
<br />
It should be pretty easy to split your date value and add a 1 for the day to all constructors. Then pass the dates to the DateTimeSpan functions built into BIRT.