Pages

Tuesday, December 6, 2011

working with jasperreports.properties

jasperreports gives us numerous properties settings that we can customize at design or compile time and
modify/control the execution of reports.

Jasperreports has his own default settings and it reads it from its jar file jasperreports-x.x.x.jar with the file name default.jasperreports.properties, unless you specify differently. Property file gives us flexibility of changing its execution behavior at run time, it may get applied to all report or to specific report as well, all depend upon how you specify properties.
Following are the ways you can define the properties which will be read by jasper and work accordingly.

1. specify in system properties.
    if we want to load some specific properties then we can specify it system.setProperties(k,v) format and compiler will override it with default. used for global changes for all reports.
   
2. specify in jasperreports.properties file   
    create your own jasperreports.properties file (normally override default by copy and modify ) and place it in classpath so that compiler will override default properties. this will get applied to all reports getting compiled/executed so useful for global changes.
   
3. specify properties in template file.
    if you want to specify only for specific report and not for others there is way to specify properties at top of your report template so that those will be overridden by compiler with default properties. You can specify properties in template as below
   
   
there are few properties specifically for export can be set through exportmanager you use at time of exporting programmatically as exportmanager.setParameter(k,v) for example

property file has
net.sf.jasperreports.export.xls.one.page.per.sheet=false

same can be set at run-time programmatically in code using xlsExporter as below

xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);






hope this helps you.
Lastly, If possible before closing, click on ad to support me.

Wednesday, October 5, 2011

Calling Oracle Stored Procedure in JasperReports

In my previous article I have very briefly explained how  PLSQL has been supported in jasperreports. But it is not that easy when it comes to different databases. Here I will present how we can call stored procedure from jasperreports. For this sample I have used iReport  4.1.2 designer and Oracle database, it is supported in previous versions as well, may be with minor changes.
Well to begin with we need to create a procedure. That returns cursor. Here I am using a simple procedure with sample database available in oracle (HR).

Employee table


Procedure



Now follow steps to implement stored procedure in jasper report. 
Create a new blank report.
1.  Add PLSQL executer to jasperreports.properties. 
Select Menu Tools>>options>>query Executers 
Find that below entry is added in the properties, if you are using latest version it will be already there for older version you need to add it separately. Please note that for this you need a jar jasperreports-extensions-3.5.3.jar which will be available in 4.1.2 version.
Language =plsql
Factory Class =com.jaspersoft.jrx.query.PlSqlQueryExecuterFactory
Fields Provider Class =com.jaspersoft.ireport.designer.data.fieldsproviders.SQLFieldsProvider

    2.  Also go to classpath tab and make sure you added classess12/14/ojdbc.jar in classpath  

    3.  Make sure that you have created oracle datasource connection available.
    4.  Open  report Query , select Query Language = “plsql”  

    5.  Write your plsql query as below make sure you have  procedure call withing  curly braces “{}“
    {call emplist_proc($P{ORACLE_REF_CURSOR} ) }
    If you have your procedure accepting more input parameters you can have them passed as parameters.
    6.  If you notice that parameter “ORACLE_REF_CURSOR” is available in Available Parameter list we need to pass it as our OUT parameter for the stored procedure which will return cursor.  Also note that if you are using old versions you can create your own parameter name “cursor” with datatype as “java.sql.ResultSet”  and also make sure “use for prompt” is not selected.
    7.  Read fields may not work in some cases but its ok. You can manually add them if do not work.
    8.  Place the fields and you are done !. 

    9.  Click on preview and you will see that your report is generated.

















    10. lastly, if possible before closing, click on add to support me. Thanks

      Tuesday, September 20, 2011

      Understand Hashmap Better


      Many of us use Map in our code frequently. but very little of us know how the map is different than Array or linkedlist and works internally.
      Well, we will explore how HashMap works. all of us know that map stores key value pairs, and we also know that it is better to use map when we need frequent search an item in collection. lets take an example that if we have a 1000 words put in collection and we want to find if a given particular word is present in collection or not, it would be inefficient if we tried to find sequentially looping through all 1000 words and find the matching word.map uses hashing technique which more efficiently narrow down the search and match the word.
      .
      what is hashing then?
      .
      Hashing uses a technique called as hashcode. It is nothing but using some algorithm to map object data to some representative integer value, that help to narrow down the search for finding object in collection. lets find how this mechanism works.
      So to begin with we will take a simple example of small collection of strings like country name and its currency. assume that we have 5 countries and their currency need to be stored in collection/list. following is the data that we need to keep in map.

      .
      CountryCurrency
      IndiaRupee
      FranceEuro
      CubaPeso
      SwitzerlandSwiss Franc
      DenmarkKrone

      As I explained earlier that hascode uses a algorithm to find representative integer value of string, in our example we will take simple algorithm as count the length of string. so to put item in hashmap we will calculate it hashcode and put them in corresponding index.
      Now assume that map uses two arrays to store key and value pairs respectively in each array as shown below. every item will be placed based on its calculated hashcode as an index item.


      HashMap
      (hashcode=keylength) IndexKeysArrayValueArrays
      1
      2
      3
      4CubaPeso
      5
      6FranceEuro
      7DenmarkKrone
      8
      9
      10BangladeshTaka
      11SwitzerlandSwiss Franc

      So if we want to find Denmark item in list we will simply count its length which is (7) and get item out from hash table from index 7. so simple isn't it. it also works much faster than comparing with other string. you will wonder how this will work if we have more than one key with same hashcode? i.e. if we have collision? If you noticed that we have wasted few space in the list. you will also have a question now what if we have a word more than has 100 characters long and rest are small? we will end up using lot of waste space.

      .
      Java uses the same structure as we have seen above, but instead of keeping items in a array it has array referenced to linklist, that means every item in a array points to a linklist called bucket. Now, as you know that two unequal object in Java very much can have equal hashcode, since hashcode is same, bucket location would be same and collision occurs in HashMap, and HashMap uses a linked list to store data objects, so they will be stored in next node of linked list, to explain same example above assume that if we have to find Denmark in map, it will first find its hashcode which is 7 and then it will traverse the linklist at index 7 for matching correct pair of key with its value with method key.equals().


      .
      Now to avoid empty space in the array, map tries to evenly distribute objects in the list. so it takes the hashcode of the key object and applies its own logic to identify bucket location for storing.please note that it stores whole key value pair object in the linklist, so that it can find it easily afterwords while retrieving. Its makes more clear now how HashMap stores Key value pairs objects, but one doubts arise how HashMap handles if array size grows? objects are stored in bucket based on the length of array to evenly distribute the load so that retrieval should be faster, so how it works if hashmap exceeds its given threshold load factor?
      If the size of map exceeds given threshold defined by load factor i.e. if the load factor is .75%, it will try to re-size the map once it is filled by 75% java does this by creating new array of buckets by twice the size of previous map and then start putting all items to new map by calculating new bucket location, this method is called rehashing.
      There are still more things to explore in map. Now it is much better that we know how HashMap stores objects internally and has logic of retrieval.

      Lastly, If possible before closing, click on ad to support me.

      Wednesday, July 20, 2011

      Accessing parent window function from modal window

      if you create a modal dialog window and try to access any function from parent window, well its not possible, any browser will not allow you to access parent window functions unless it is a normal window. so the alternate possible solution is as below
      modal window can return a value to parent window's calling function, so based on its value you can either change parent window variables or call any function. It is only possible when your child modal window is closing.
      here it is how.

      // parent window JS
      retval= window.showModalDialog( "mynewpage.jsp", myArguments, "dialogHeight: 100px; dialogWidth: 400px; edge: Raised; center: Yes; resizable: Yes; status:Yes ; scroll:Yes;");
      resetTimer(retval);
      

      // in child window JS i.e. mynewpage.jsp
      function closeMe()
      {
      window.returnValue="60";
      window.close();
      }
      

      Wednesday, February 16, 2011

      ClassNotFoundException or NoClassDefFoundError.. what the hell is going on...

      many a times we face "ClassNotFoundException" or  "NoClassDefFoundError"  and then we wonder what the hell is going on... not able to figure out what went wrong... some times we even see that even though class file or jar was exist in classpath, classloader unable to find it...
      first try to understand that  "ClassNotFoundException" is thrown when specified class was not found in classpath by classloader, simply its a missing class in classpath or in a jar.
      "NoClassDefFoundError" is also similar but with bit difference that it is a runtime error that is thrown by JVM when classloader did not find class definition while loading class caused by  "ClassNotFoundException",  it means that class was present at compile time but does not present at run time". normally both (error and exception) will print the causing class name that was not found by the classloader.

      but if you come to notice that even though if class is present in classpath or in a jar and still classloader gives same error then try to look for dependent class imported inside the class that was thrown as "ClassNotFoundException"
      Also check is that class has some static block/class variable defined from other package or jar, then make sure even those class/jars are present in classpath


      in case of web applications or case where you use framworks like struts/spring or hibernate exceptions are wrapped and you may not come to know actual error that was causing the problem... some such scenarios could be

      1. in web applications classloader has child and parent classloaders where classes loaded by parent are accessable to child classloaders but not child to parent. so in such case need to know which classloader is loading the class. for e.g. if you have some class (or class in jar)from shared lib of application referring to web-inf/lib or folder...

      2. version compatibility. sometimes it happens that your application jdk is of lower version and jars created in higher version will create unsupported classversion error.. but thrown error could be "ClassNotFoundException"

      3. recheck the fully qualified class name that may have typo error, e.g. class.forname or applicationcontext.xml in case of spring where beans are defined.