Thursday, October 29, 2009

Using Jasper Reports In ADF Applications

You can check this for step by step calling jasper report

http://sameh-nassar.blogspot.ie/2012/12/using-jasper-report-in-adf-application.html


One of the solutions for generating reports when you use JDeveloper is using Jasper Report.
Jasper report is some of libraries (.Jar files) and .xml or .jrxml file that represent your report.
One of the easy ways to write .xml file is using IReport program. IReport is a program that manage you to design your report graphically then save your report to xml or jrxml file. From JDeveloper after you add jasper reports libraries to your project you can read your report (.xml file) and print it as PDF file or Excel File or Jasper Viewer.
Jasper Report Libraries is:
1-commons-digester-1.7.jar
2-itext-1.4.5.jar
3-jasperreports-3.0.0.jar
4-poi-2.5.1-final-20040804.jar
5- jasper-compiler-jdt-5.5.15.jar

You Can Download IReport From :
To configure your application to use Jasper Reports:
1- Add jasper report libraries to your project.
2- In web.xml add this
< resource-ref>
< res-ref-name> jdbc/OracleDBConnectionDS < /res-ref-name>
< res-type> javax.sql.DataSource < /res-type>
< res-auth> Container < /res-auth>
< /resource-ref>
3- From backingbean write this method:
----------- Imports ----------------
import java.io.*;
import java.util.*;
import java.sql.Connection;import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.InitialContext;import javax.naming.NamingException;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;
----------------------------------------
public void printReport() throws FileNotFoundException, JRException, NamingException, SQLException, IOException {
InputStream input = new FileInputStream(new File("c:/yourJasperReport.xml"));
JasperDesign design = JRXmlLoader.load(input);
JasperReport report = JasperCompileManager.compileReport(design);
Map parameters = new HashMap();
// parameters.put("jasperParamenterName", "ParameterValue"); Used if you want to pass a parameter to a jasper report
InitialContext initialContext = new InitialContext();
DataSource ds = (DataSource)initialContext.lookup("java:comp/env/jdbc/OracleDBConnectionDS"); // get from your application module configuration
Connection conn = ds.getConnection();
JasperPrint print = JasperFillManager.fillReport(report, parameters, conn);
///// For printing report as PDF file ////////
ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream(); JasperExportManager.exportReportToPdfStream(print,byteArrayOutputStream);
out.write(byteArrayOutputStream.toByteArray());
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
//////// For printing report as excel file //////
OutputStream ouputStream = new FileOutputStream(new File("c:/yourReport.xls")); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, print); exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
exporterXLS.exportReport();
ouputStream.write(byteArrayOutputStream.toByteArray());
ouputStream.flush();
ouputStream.close();
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler c:/yourReport.xls");
///// for printing report as a jasper report viewer /////
JasperViewer.viewReport(print, true);
}
For More Information About Using Jasper Report :

Wednesday, October 28, 2009

Read Variable From PageDef

In your page definition you can define a variable and set a value to it and you can read this variable and retrieve its value from backingbean as :

1- goto your page definition and in the executables section right click in the variables then choose insert inside the variables ----> variable.
2- set the name as "sameh" and the type to "String" then Ok.
3- set a default value to this variable with "Great Man".
4- in your backingbean write this :

FacesContext facesContext = FacesContext.getCurrentInstance();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp = elFactory.createValueExpression(elContext, "#{bindings.sameh}", Object.class);
Object varValue = valueExp.getValue(elContext);
// varValue will be Great Man

Solving PermGem Errors

Locate the file: setDomainEnv.cmd which should be at:
[JDev Install Dir]\jdeveloper\system\system11.1.1.0.31.51.88\DefaultDomain\bin
There find the line:set MEM_MAX_PERM_SIZE=-XX:MaxPermSize=128m
and change it to:set MEM_MAX_PERM_SIZE=-XX:MaxPermSize=512m

Read From Excel File And Write To Excel File

You Can use Apache POI library with JARs file:
- poi-3.5-FINAL-20090928.jar
- poi-contrib-3.5-FINAL-20090928.jar
- poi-ooxml-3.5-FINAL-20090928.jar
- poi-scratchpad-3.5-FINAL-20090928.jar
You can download this from

http://www.filefactory.com/file/a07fc15/n/POIJars_rar

In you backingbean:


/////// ........................ Read From File ................
InputStream myxls = new FileInputStream("c:\\yourExcelFils.xls");
HSSFWorkbook wb = new HSSFWorkbook(myxls);
HSSFSheet sheet = wb.getSheetAt(0); // first sheet
HSSFRow row = sheet.getRow(1); // second row
HSSFCell cell = row.getCell((short)0); // first cell
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
System.out.println("The Cell was a String with value " + cell.getStringCellValue());
} else {
System.out.println ("The cell was nothing we're interested in");
}

/////// ........................ Write To File ................
HSSFWorkbook wb1 = new HSSFWorkbook();

HSSFSheet sheet = wb1.createSheet();
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);

FileOutputStream fileOut = new FileOutputStream("c:\\yourExcelFils.xls");
wb1.write(fileOut);
fileOut.close();

Using Skin In Your ADF Application

One of the important thing you can do in ADF Application is to enhance a look and feel of  the application. This can be done by using skins.

To use skins in your ADF Application you can follow these steps:

1- Make a new file with name (trinidad-skins.xml) under WEB-INF Folder.
2- Write this in trinidad-skins.xml






3-From tools --- > Preferences ----> Css Editor thin check to ADF Faces Extension (optional step).

4- Write a .css file under Web content folder (With name as you write in trinidad-skins.xml file. e.g. mySkin.css)


5-In mySkin.css file you should write a selectors that affect to an ADF component.
(for more information about selectors check this URL
http://jdevadf.oracle.com/adf-richclient-demo/docs/skin-selectors.html)

6- In trinidad-config.xml change skin-family to "mySkin" as:




This an example for using skins in ADF Application http://www.filefactory.com/file/a07fgc1/n/SkinApplication_rar

Read Resource Bundle From Backingbean

ResourceBundle rb = ResourceBundle.getBundle("ResourceBundleFileName",FacesContext.getCurrentInstance().getViewRoot().getLocale());
rb.getString("key");

Get / Set Browser localization

Set Browser Locale:
FacesContext ctx = FacesContext.getCurrentInstance();
UIViewRoot uiRoot = ctx.getViewRoot();
Locale locale = new Locale("en");// set browser as english
uiRoot.setLocale(locale);
-----------------------------------------------------------
Get Browser Locale
FacesContext ctx = FacesContext.getCurrentInstance();
UIViewRoot uiRoot = ctx.getViewRoot();
uiRoot.getLocale();

How To Get Application Module From Your BackingBean

There are two ways to generate application module from your backnigbean:

1- Get application module from iterator:
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding dciter = (DCIteratorBinding)bindings.get("YourView1Iterator");
ApplicationModule am=dciter.getViewObject().getApplicationModule();
--------------------------------------------------------------
2- ApplicationModule am = this.getAm();
private ApplicationModule getAm() {
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = fc.getELContext();
ValueExpression valueExp =
elFactory.createValueExpression(elContext, "#{data.AppModuleDataControl.dataProvider}",
Object.class);
return (ApplicationModule )valueExp.getValue(elContext);
}