Sunday, December 9, 2012

Using Jasper Report In ADF Application (Step-by-Step)

In my previous post  http://sameh-nassar.blogspot.com/2009/10/using-jasper-reports-with-jdeveloper.html I explain how to use Jasper Report but because of many people send me many requests for explain this topic in more detail so I will explain it in step by step.

our example will make application with one screen this screen has a table of employees you should select an employee and send the selected employee id to jasper report to print the selected employee data.

Before we begin be sure of:

1- You download iReport-4.0.1 (you can get it from http://sourceforge.net/projects/ireport/files/iReport/iReport-4.0.1/) I download iReport-4.0.1-windows-installer.exe.

2- You have database has hr schema.

3- Setup JDeveloper 11g (any release).

Now we can begin ......

Step 1:

Setup iReport-4.0.1
Step 2:
Open JDeveloper and make new ADF Application (name it for example JasperApplication)

Step 3:

Connect your application to your database HR Schema by right click in your Model application ---> new ---> ADF Business Components ---> Business Components From Table
Add your connection and test the connection

Add one Entity Objects (Employees)
Add one Updatable View Objects
Then press finish, your application should be like this
Step 4:
Create new jspx page (name it for example employees.jspx)
In your ViewController project right click on Web Content folder then new --->JSF ---> JSF Page

From the Data Controls drag EmployeesView1 to your page as an ADF Read-only Table


From Row Selection Select Single Row
Add a button above a table (text it for example Run Report) and its action point to a method in a back bean (for example Back bean name is JasperBean.java)


To get the selected employee id:
    DCIteratorBinding empIter = (DCIteratorBinding) getBindings().get("EmployeesView1Iterator");
    String empId = empIter.getCurrentRow().getAttribute("EmployeeId").toString();

where EmployeesView1Iterator is the iterator name in the page definition and EmployeeId is the attribute name in the EmployeesView.

now we should pass the selected employee Id to jasper report so, you should make a map and set the parameter like
    Map m = new HashMap();
    m.put("employeeId", empId);// employeeId is a jasper parameter name

then you should call the jasper report like this method
 runReport("empReport.jasper", m);

where runReport is the method take jasper report name (empReport.jasper) and the map which hold the parameter

write this code in your JasperBean.java

--------------------------------------------------------------------------------------------------------------



--------------------------------------------------------------------------------------------------------

After you write this code you will find Jasper library missing

to get jasper libraries goto the folder which you setup iReport for example in this path
C:\Program Files (x86)\Jaspersoft\iReport-4.0.1\ireport\modules\ext
and get these libraries:
1- iText-2.1.7.jar
2- jasperreports-4.0.1.jar
3- jasperreports-fonts-4.0.1.jar

add this libraries to your ViewController project ---> Right click on the project ---> Project Properties--->Libraries And Classpath ---> Add JAR/Directory

Now your code should have no error

Now my Back bean code is :
----------------------------------------------------------------------------------------------------------------------



--------------------------------------------------------------------------------------------------------------------------
Now depending on my previous code we should make a report the name of this report should be "empReport" and has one parameter "employeeId"

Step 5:

Open iReport and make a connection to hr database but first ..... iReport has not oracle database driver so you use oracle database you should first add a ojdbc jar file to the classpath of iReport.

you can get ojdbc jar file in your middleware home like:
C:\Oracle\Middleware\wlserver_10.3\server\lib and search for ojdbc6.jar

In iReport goto Tools---> Options ---> Classpath and add ojdbc6.jar
Now make a new connection to hr schema
Now from File ---> New and make new Report
Choose any template or make it blank then press "Launch Report Wizard"

Write report name "empReport" and under your web content folder in ViewController project make new folder "reports" and save the report on this path


Press on Design query button and select your schema then drag Employees table
then press Ok you will find the report query will generated then press Next and shuttle the field you want to displayed in your report
Then press next then finish. You will find you report generated.
Goto the report query as shown:

and make new Parameter "employeeId"
add where clause to the query like:
WHERE EMPLOYEES."EMPLOYEE_ID"=to_number($P{employeeId})

Now you can preview your report by pressing on "Preview" button and enter for example employeeId = 200


Now compile your report to generate .jasper file in your application
If you goto your application you should something like this:
before you run be sure of two things:
1- any picture you use in jasper should be in the class path
2- You should define datasource in your weblogic "as we write in the code datasource is hrDS"


for how you can define data source in weblogic try this http://www.mediafire.com/file/g7odbc06rpad1aa/dataSourceInWeblogic.doc/file

Step 6:
run your application and select any employee then press on "Run Report" button
for example select employee Id= 104 then run report



you can download the application from this link JasperApplication



To open the report in new window:

1- In the .jspx right click on the af:document -->Facets - Document --> Meta Container 
2- Inside f:facet - metaContainer add af:resource with type="javascript" as:


inside af:resource you can add javascript function 

function newWindow()
{
  document.getElementById("f1").target = "_blank;targetfeatures=toolbar=no location=no directories=no menubar=no";
}

where "f1" is the af:form id.

So the structure will be:



To call this function add button or link and inside this link add clientLister as:





Thursday, May 17, 2012

Call Oracle Reports From Your ADF Application (Hide Report Parameters From URL)

Hello ADF Developers,

In my previous post http://sameh-nassar.blogspot.com/2010/07/call-oracle-reports-from-your-adf.html I explain a way to call oracle report but this way has a problem. The problem is the report parameters appear in the URL. In this post I will explain another way to call oracle report without displaying report parameters in URL.
to call oracle report follow this steps:
1- Make a new .jsp page (for example the page name is  report.jsp).

2- In faces-config.xml make a navigation rule to report.jsp with outcome "report".

3- Write this code inside the report.jsp page






4- Create ADF page (.jspx or .jsf page) then add button (this button will call the report) and in the button action write this code:

    





Wednesday, March 21, 2012

Apply MDS To Oracle ADF Application

What is MDS?
Metadata Services (MDS) is a feature provided by oracle to store customization and personalization information in a repository. The repository can either be stored in a database or in a file system as we will see.

for example if there is a screen has a table this table has 8 columns, and there are a user (User1) want to see only 4 columns and hide the others. When User1 login again he want to see last changes which he make (see only 4 columns), the user can see any changes he make in the screen only if you apply MDS to your application.

To apply MDS to your application follow this:

1.Pages should be (.jspx) not (.jsp).
2.In your project properties --> ADF View check on "Enable User Customizations" then select "Across Sessions Using MDS".



3.Create a new class like this



This class has two main functions:
- getName() which return the folder name that hold all users changes.
- getValue() which return the folder name for the login user. This folder should be unique for every login user and will saved inside the getName() folder.

4. In Application Resources --> Descriptors --> ADF META-INF open adf-config.xml.
5. In adf-config file go to MDS Configurations and add your class



6. In adf-config file go to View tab and add all Tags.



7. Now you apply MDS to your application.

8. If you run your application in Integrated Weblogic there will be no problem, you should only specify the path to store the users changes (changes will stored in file system). You will find this path in your Application properties --> Run --> MDS.



9. If you deploy your application in standalone weblogic server you should setup Repository Creation Utility (RCU). You can download it from
here
RCU will create a new schema in your database which hold all the changes.

10. Unzip RCU downloaded file and go to rcuHome --> BIN and run rcu.bat.



Press Next



Select Create then Press Next



Enter Database connection with sysdba username and password then press next

If any error message appear press ignore



Press OK



Select Metadata Services Then Press Next



Press OK



Enter the password for the newly created User in database then press Next. Then follow all other steps until Finish

11. After finishing installation of RCU you should configure you weblogic domain to the MDS Repository.

12. Open your weblogic Enterprise manager using URl like
http://yourServerIP:7001/em

13. Enter weblogic username and password

14. Right click in you Weblogic domain the choose "Metadata Repositories"



15. Press Register Button and Enter Database Connection then Press Query



You will find your repository information press on your repository and enter Repository Name and Schema Password Then Press OK.

16. Now you register your MDS to Your Weblogic Domain.


You can select it and choose the target.



17. Weblogic automatic create a data source for connecting to your MDS Repository. If you go to Weblogic Console (http://yourServerIp:7001/console) and go to Services --> Data Sources you will find a newly Data Source



18. Deploy your application directly from JDeveloper to your weblogic server the MDS Deployment Configuration will automatically opened with the configurations of your MDS Repository you should only enter the Position Name.