Thursday, December 15, 2011

How to download Oracle ADF tables in PDF format


          At this task we are going to convert ADF BC tables into PDF files and make it downloadable to users. It looks like very easy but it is not. To successfully complete this task I had to use several libraries such as apache fop library.
 
  • What is Apache FOP library.....??



          FOP stands for Formatting Object Processor. Apache FOP is a Java application which reads a Formatting Object (FO) tree and renders the resulting pages to a specific output. Currently supported outputs are:
           PDF, PS, PCL, AFP, XML, Print, AWT and PNG, and to a lesser extend RFT and TXT.
          Developers use Apache FOP libraries to change the output file format very commonly because it’s free and open source. You can download the Apache FOP library as a JAR file from the below link. (There are different links to download FOP.jar; but most of the jar files do not contain all the libraries required for this conversion.)


  •  How to convert ADF BC tables into PDF files

           If we try to analyse the process goes here we can simply represent it as below.

          First of all we want to get the table data and write it into a XML file. We can do it through JDeveloper framework. There is an inbuilt method called “printXML” at ViewObjectImpl class. It will generate the XML to the VO data. So if we can access the ViewObjectImpl class anyway we can generate the XML file. <VO>Impl class is a one which extends the ViewObjectImpl class. So we can generate the <VO>Impl class and generate the XML file.

  • Implement the <VO>Impl and <Application_Module>Impl classes for your MODEL project. (We need those method at those classes to generate the XML file.)
    (<VO>.xml à Java à Java Classes à click on "pencil" icon.)

  • Create a Java class at “View Controller” project to create a managed-bean. We are going to do all the coding inside this bean. All the required codes are mentioned at the end of this article.
     
  • We can access the <VO>Impl class through this path:
    (FacesContext à ExternalContext à AdfFacesContext à HttpServletRequest à BindingContext à DCDataControl à ApplicationModule à AppModuleImpl à ViewObjectImpl)
     
  • When we are creating the Node object we have two options. You can select the one which suits your requirement. (refer code while "// Print the XML" )
    Parameter
    Description
    XML_OPT_ALL_ROWS
    All rows will be rendered in XML
    XML_OPT_ASSOC_CONSISTENT
    attribute accessors that return a RowSet should set the AssociationConsistent flag on, so that new (unposted) rows for the RowSet are also included in the output.
    XML_OPT_CHANGES_ONLY
    For internal framework use only.
    XML_OPT_LIMIT_RANGE
    Rows in the current range will be rendered in XML

  • Then we have to generate the PDF using  a style sheet. Before genarate PDF files you should have to build the style sheet. Then we can generate the output file using FOP libraries (we can select the output format). Output format depends on the MimeConstant you select. There are several output formats supported by FOP libraries as follows. (refer code while "// Generate PDF" )

    MIME Constant
    Output Format
    MIME_PDF
    pdf
    MIME_PLAIN_TEXT
    txt
    MIME_PNG
    png
    MIME_FOP_AREA_TREE
    xml
    MIME_POSTSCRIPT
    ps
    MIME_AFP
    afp
    MIME_TIFF
    tiff
  •   
  • Then insert a Command Button next to your ADF BC table. Create a web page backing bean and write an Action Method. Then set that action method as the “action” property of the created command button. Inside the action method we have to write some codes which will make the generated pdf file to be downloadable.
     
  • When user press the command button pdf file shold be generated and downloadable. To do that  we have to go through the Context layers of the framework, get the http servlet response and set the content type of the response.
     
  • Here we have set the content type as “application/pdf” because we have generated a pdf file. That parameter depends on your file type. (refer code while "// download the PDF" )

      
All the required codes are given below.....

 
// Access the <VO>Impl
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
AdfFacesContext context = AdfFacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
BindingContext ctx = DCUtil.getBindingContext(request);
DCDataControl dc = ctx.findDataControl("AppModuleDataControl");
ApplicationModule service = (ApplicationModule)dc.getDataProvider();
ApplicationModule am = service.findApplicationModule("AppModule");
AppModuleImpl amImpl = (AppModuleImpl)am;
ViewObjectImpl VOImlp = (EmpVOImpl)amImpl.findViewObject("EmpVO1");
HashMap viewDefMap = new HashMap();

// Print the XML
Node n = VOImlp.writeXML(XMLInterface.XML_OPT_ALL_ROWS, viewDefMap);
java.io.File file = new java.io.File(<path to save XML file>);
PrintWriter output = null;
try {  output = new java.io.PrintWriter(file);
          ((XMLNode)n).print(output);
} catch (Exception e) { System.out.println(e.getMessage());
} finally {
try {output.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } }

// Generate PDF
File xmlfile = new File(<path to generated XML file>);
File xsltfile = new File(<path to styleSheet>);
File pdffile = new File(<path to save PDF file>);
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
TransformerFactory factory = new TransformerFactoryImpl();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
transformer.setParameter("versionParam", "2.0");
Source src = new StreamSource(xmlfile);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
} finally { out.close(); }

// download the PDF
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();
File file = new File(dir1, pdffile1);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
          response.reset();
          response.setContentType("application/pdf");
          response.setContentLength((int)file.length());
response.setHeader("Content-disposition","inline;filename=\""+pdffile1+"\"");
output = new BufferedOutputStream(response.getOutputStream(),DEFAULT_BUFFER_SIZE);
          byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
          int length;
          while ((length = input.read(buffer)) > 0) {
              output.write(buffer, 0, length); }
          output.flush(); } finally { close(output); close(input); }
facesContext.responseComplete();

      

CHEERS.......!!

Sunday, November 27, 2011

How to change windows7 start-up-screen


          At windows7 it is little bit complicated to change the start-up-screen (not like 2000 or xp). Let's see how it happens...


     1.     Click on start button and enter the word 'regedit' at the search bar.

     2.     Then follow the below mentioned path to access the OEMBackground property.
computer --> HKEY_LOCAL_MACHINE --> SOFTWARE --> Microsoft --> Windows --> CurrentVersion --> Authentication --> LogonUI --> Background


     3.     Then select the property called OEMBackground and double-click it. Change its value to 1.



 
     4.     Click on start button and enter the text '%windir%\system32\oobe' at the search bar.
(this will direct you to the folder "C:\Windows\System32\oobe")

     5.     Create a folder called 'info' inside the oobe floder.

     6.     Go inside the info folder you have created.
Create a folder called 'backgrounds' inside your info folder.

     7.     Then select the image you want to set as your start-up-screen (image size should be less than 256KB).
Copy that image into the backgrounds folder you have already created.

     8.     Rename that image as 'backgroundDefault.jpg'.

Now it is OK...
Use "Switch user" to see the difference.

Thursday, November 24, 2011

සමුදුරු මාවතේ ගතකල සොඳුරු සැදෑවක්...

          මේ කතාවෙ මුල පටන් ගන්නෙ මෙහෙමයි... ඔන්න ඉතින් සරසවි ජීවිතේ තුන්වෙනි අවුරුද්ද ඉවර වෙලා අන්තිම අවුරුද්ද පටන් ගන්න කලින් අපිට මාස 3ක දිර්ඝ නිවාඩුවක් ලැබුන. මොරටුව සරසවිය කිවුවම ඉතින් නිවාඩු එහෙම ලැබෙනව හොරයි කියල ගොඩක් අය දන්නවනෙ. ඒකෙත් පරිඝණක ඉන්ජිනේරු අංශය කිවුවම ලොවෙත් නිවාඩුවක් නම් හම්බෙනව බොරු... ඒක නිසා මේ ලැබුන නිවාඩුව අපිට දෙයියො දීපු එකක් වගේ. හැබැයි එහෙම හිතුනෙ නිවාඩුව හම්බුන මුල් දවස්වල. නිවාඩු මුල් දවස් ටිකේ TV series, films බැලුව තිත්ත වෙනකන්ම. ඊට පස්සෙ ඉතින් කරන්න දෙයක් නෑ.. නිවාඩුවට බැන බැන ඉන්නව.. ඔය අතරෙ තමා අපේ කට්ටිය gym යන්න සෙට් වෙන්නෙ. ඒක නම් කාලය ගෙවන්න නියම විදියක්. එතනට ගිහිල්ලත් අපි වැඩියම කරන්නෙ කට්ටිය එක්ක පොඩි chat 1ක් දාගෙන ඉන්න එක තමා. මොකද gym එකට ඇතුල් වෙනකොටම අපිට එක එක ඇගේ-පතේ අමාරු හැදෙනවනෙ. 
          මේ සිද්ධිය වෙච්ච දවසෙ අපේ gym සෙට් එකේ එකෙක්ගෙ (සන්දීපගෙ) උපන් දිනේ.. අපි ඉතින් gym එකට ගොඩ උනේම "අද මොනාද ගෙනාවෙ" කියල අහගෙන. ඉතින් ලැබුන උත්තරේ බොහොම සතුටුදායක එකක්. ඒ තමා "අපි එහෙනම් හවසට කොහේ හරි යමු" කියන එක. මොනා උනත් සන්දීපගෙන් දතක් ගලවනව කියන එක එච්චර ලේසි දෙයක් නම් නෙවෙයි..
          ගෙදර ඇවිත කාල, බීල, ඇගේ පතේ අමාරු යන්නත් එක්ක පොඩි නින්දක් දැම්ම... මට ඇහැරුනේ අම්ම "පුතේ call එකක්"  කියල කියන සද්දෙට. නිදිමතේම call එක answer කරා. call කරේ සන්දීප. "මේ ඕයි.., එන්නෙ නැද්ද යන්න"  කියල ඇහුව. එතකොට තමා මට තේරුනේ මම කොච්චර වෙලාවක් නිදාගෙනද කියල. මම ටක් ගාල කිරි වීදුරුවකුත් එක හුස්මට ඇදල දාල car එක අරන් ගියා සන්දීපගෙ ගෙදරට.
          අපි දෙන්න විතරක් යන එකත් හරි නෑනෙ. ඒක නිසා යනගමන් මගින් සුනෙතුත් (අපේ gym සෙට් එකේ තව කෙනෙක්) එකතුකර ගත්ත. කෙලින්ම ගියේ town එකේ තියෙන dinner ගන්න කියාපු තැනකට. සන්දීපත් ඉතින් සල්ලි සල්ලි කියල බලන්නෙ නැතුව එලටම වියදම් කරා. අපි රෑ කෑම ගත්තෙ cricket match එකත් බලන ගමන්ම තමා. බලන්න දෙයක් නෑ ඉතින්; අපි කෑම කනව; අපේ team එක match එක කනව.
          කාල බීල බලනකොට වෙලාව ඉතුරුයි. එච්චර කලින් ගෙදර යන්නත් හොඳ නෑනෙ. කට්ටිය එක්ක කල්පනා කරා මොකද කරන්නෙ කියල. එතකොට තමා හිතුනෙ සමුදුරු මාවතේ ඇවිදින්න යන්න පුලුවන්නෙ කියල. සමුදුරු මාවත කියන්නෙ මුහුදු වෙරළ එකකම කිලෝමීටරයක් විතර දිගට හදල තියෙන පදික වේදිකාවක් වගේ දෙයක්. මේකෙ මුහුද පැත්තට වෙන්න දිගටම තියෙන්නෙ බැම්මක්. ඉතින් රෑකට, හවස් වරුවකට මේ පැත්තෙ ආවම  ඈත මුහුද, තරු පිරිනු අහස දිහා බලාගෙන, මුහුදු හුලං වදින ගමන් ඕන තරම් වෙලා කතා කර කර ඉන්න පුලුවන්. දැන් සමුදුරු මාවත කියල කිවුවට මේකට ඉස්සර කියන්නෙ "වැල්ල පාර" කියල. ගාලු පාරෙ, ගාල්ලෙ ඉදල චුට්ටක් මාතර පැත්තට එනකොට තියෙන දිග වෙරළ තීරය ගැන තමයි මේ කියන්නෙ.
          අපි ඉතින් හොඳ තැනක් බලල වාඩි වෙලා සුපුරුදු පරිදි කතාව පටන් ගත්ත. සරසවි නිවාඩු උනත් අපිට පොඩි වැඩකට සරසවිය පැත්තට යන්න වෙලා තිබුන. එතනින් පටන් ගත්ත කතාව සරසවි මිතුරන්, සරසවි ඇඳුරන්, රාජ්‍ය අය-වැය.. වගේ ගොඩක් මාතෘකා දිගේ ඇදීගෙන ගියා. හෙට දිනයේ සරසවියේ පැවැත්වීමට තිබෙන "සරසවි ගී සිසිල" සංගීත ප්‍රසංගයත් මේ අතර උනා. මම දැකල, මට දැනිල තියෙන විදියට අපේ සරසවියෙ ශාස්ත්‍රීය ගීත ඇසීමට ලැදි බොහෝ දෙනෙක් ඉන්නව. ඒ අයගේ රසාස්වාදනය ඉල්ලක්ක කරගෙන පවත්වන සංගීත ප්‍රසංගයක් තමා මේක. මේ සොඳුරු කතා බහ අතරතුර අවට පරිසරයේ තිබූ සුන්දරත්වය රසවිදීමටත් අපි අමතක කරේ නෑ.. ඈතින් පේන "සාම චෛත්‍ය" (රූමස්සල කන්දේ පිහිටා ඇති මෙය ජපන්-ශ්‍රී ලංකා මිත්‍රත්ව පදනම මගින් ඉදිකරන ලද චෛත්‍යකි), ගාලු වරායට සේන්දුවන නැවු, ප්‍රදීපාගාරයෙන් විහිදෙන ආලෝකය ඒ අතර උනා...
          කොහොම කොහොම හරි පැයක හමාරක සුහද කතාබහ ඉවර කරන්න වෙලාව හරි බව අපිට තේරුනේ අපේ ගෙවල් වලින් එන දුරකතන ඇමතුම් නිසා.. ඉතින් කට්ටියම ගෙවල්වලට බස්සගෙන මමත් අපේ ගෙදරට සැපත් උනා.. 

මොනා උනත් සමුදුරු මාවතේ රෑකට තියෙන athal එක දන්නෙ එතනට ගියපු කෙනෙක්ම තමා...

Friday, November 18, 2011

How to download Oracle ADF tables in Excel format



          At a single web application there are several ADF tables which presents different data. Using the web front end we can read and refer those data. But if we have the ability to download those data in Excel format then it would be very easy to process those data and make some decisions.

         It is very easy to convert ADF tables to Excel format using the "exportCollectionActionListener" component. Let's see a very simple example.

  1. Create an ADF table using a VO (View Object)
  2. Then create a command button (or a command link) next to the ADF table. I name it as “Download Excel".
  3. After that drag and drop an “exportCollectionActionListener” component from component pallet on to that command button. Then set parameters as follows.

    • ExportedId: table or collection id you want to download.
    • Type: excelHTML (because we want to download an excel file)
    • filename: Name on the download file.
    • title: title of the download file.