Saturday 28 September 2013

Add custom componet in ADF RTE

Attribute toolboxLayout in af:richTextEditor gives flexibility to add our own custom component in RTE. Using this attribute we can also remove default components of RTE.

Adding custom button :

<af:richTextEditor label="Custom button" toolboxLayout="all myBtn" id="rte1">
        <f:facet name="myBtn">
        <af:commandButton text="Submit" id="cb0"/>
        </f:facet> 
</af:richTextEditor>


Refrence : http://docs.oracle.com/cd/E21043_01/apirefs.1111/e12419/tagdoc/af_richTextEditor.html


*Comments and feedback are most welcomed, Thanks.

Thursday 12 September 2013

How to Export adf table record in xls file using exportCollectionActionListener tag


Data of the collection components in ADF such as table, tree or treeTable can easily be exported in the form of xls file using ADF exportCollectionActionListener tag. It allows action events to export data from the colleection component.

<af:exportCollectionActionListener/> id attribute will refer to the collection component Id.

Find the sample code snippet for exporting department table records.

<af:panelCollection id="pc1" styleClass="AFStretchWidth">
            <f:facet name="toolbar">
              <af:toolbar id="t2">
                <af:commandToolbarButton text="Export" id="ctb1">
                  <af:exportCollectionActionListener type="excelHTML"
                                                     exportedId="table"
                                                     filename="DepartmentExcel"/>
                </af:commandToolbarButton>
              </af:toolbar>
            </f:facet>

            <af:table
id="table" ....
            ..................................  ...................
            ............ >
 </af:panelCollection>

By default the extension of the file downloaded will be xls. We can also export the record in doc format by just giving name of a file such as DepartmentExcel.doc in the attribute filename.




*Comments and feedback are most welcomed, Thanks.

Wednesday 11 September 2013

How to select current row in adf table and perform delete operation

ADF table component is used to display the VO records in tabular form. It has its own many in-built features such as sorting, filtering and we can add other features like detach, manage columns, exporting data in excel file etc using af:panelCollection and toolbar facet component.

It also have row selection both single/multiple. Row selection is very important in respect of performing operations of particular rows.

In this blog I will show how we can get the current row of the table, fetching the primary key of the record and performing the delete operation using java code.

Let us assume our table is having a binding attribute mapped to a bean class and having rowSelection attribute set to "single".

e.g : rowSelection="single" binding="#{pageFlowScope.department.departmentTable}"

We will add a column in the table, which will have a command button and a actionListener mapped to bean class.

e.g : <af:column headerText="Action" id="c5">
            <af:commandButton text="Delete" id="cb1"
                              actionListener="#{pageFlowScope.department.deleteRecord}"/>
          </af:column>

Java Code snippet :

    public void deleteRecord(ActionEvent actionEvent) {
//Fetch current row data as per index
        FacesCtrlHierNodeBinding rowdata =            (FacesCtrlHierNodeBinding)departmentTable.getRowData(departmentTable.getRowIndex());
 //getting row and typecasting in RowImpl
        DeptVORowImpl rowImpl = (DeptVORowImpl)rowdata.getRow();
//Getting primary key of the record
        Number keyVal = rowImpl.getDepartmentId();
        DCBindingContainer dcBindings =
            (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
//Iterator binding on the page          
 DCIteratorBinding iteratorBinding = (DCIteratorBinding)dcBindings.get("DeptVOIterator");
        RowSetIterator rowSetIterator = iteratorBinding.getRowSetIterator();
        Key key = new Key(new Object[] { keyVal });
        Row row = rowSetIterator.findByKey(key, 1)[0];
        rowSetIterator.setCurrentRow(row);
//Removing the current record
rowSetIterator.removeCurrentRow();
}

After delete perform AM commit operation.

*Comments and feedback are most welcomed, Thanks.

Thursday 4 July 2013

Uploading Image file in ADF

Uploading files in ADF can be easily done using a component "inputFile".
Here in this blog I am sharing my experience of uploading an image file in ADF.

I will make following checks for the image :

1. Image with the extension other than PNG not allowed 
2. Size of the image should not be more than 150*60
3. Check the directory exist, if not create directory.

Code snippet below :

1. Code for jsff :

<af:panelGroupLayout id="pgl1" layout="horizontal">
        <af:inputFile id="inputFile"
                      required="true" label="upload"
                      requiredMessageDetail="Please select a file to upload"
                      value="#{pageFlowScope.uploadImage.uploadedFile}"
                      binding="#{pageFlowScope.uploadImage.inputFileBind}"/>
      <af:spacer id="sp1" width="10px"/>
        <af:commandImageLink text="Upload" id="upload"
                             partialTriggers="inputFile"
                             actionListener="#{pageFlowScope.uploadBanner.onUploadImage}"/>
      </af:panelGroupLayout>

2. Code of Java Class - UploadImage.java

package com.upload.image;


import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import javax.imageio.ImageIO;

import oracle.adf.view.rich.component.rich.input.RichInputFile;

import oracle.jbo.domain.Number;

import org.apache.myfaces.trinidad.context.RequestContext;
import org.apache.myfaces.trinidad.model.UploadedFile;


/**
 * Upload the image
 */

public class UploadImage {

    private UploadedFile uploadedFile = null;
    private RichInputFile inputFileBind;

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setInputFileBind(RichInputFile inputFileBind) {
        this.inputFileBind = inputFileBind;
    }

    public RichInputFile getInputFileBind() {
        return inputFileBind;
    }

    /**
     * On click of upload button

     * @param actionEvent
     */

    public void onUploadImage(ActionEvent actionEvent) {
        System.out.println("Entry - [UploadImage : onUploadImage]");
        InputStream inputStream = null;
        String image = null;
        try {
            System.out.println("[UploadJrnlUsers : onUploadFile] - uploaded image type : " +
                               uploadedFile.getContentType());
            inputStream = uploadedFile.getInputStream();
            System.out.println("[UploadJrnlUsers : onUploadFile] - uploaded image InputStream : " +
                               uploadedFile.getInputStream());          
            image = uploadedFile.getFilename();
            //Condition to check whether the image is png or not
            if ("image/png".equalsIgnoreCase(uploadedFile.getContentType())) {
                System.out.println("[UploadImage : onUploadImage] - File Name : " +
                                   uploadedFile.getFilename());
                System.out.println("[UploadImage : onUploadImage] - image : " + image);
                //Method call to upload png
                uploadPngFile(image, inputStream);
            } else {
                showADFDefaultPopup("Only .png file is allowed");
            }
            showADFDefaultPopup(image + " image has been uploaded successfully");
        } catch (IOException e) {
            e.getMessage();
        }
        System.out.println("Exit - [UploadImage : onUploadImage]");
    }

    private void uploadPngFile(String image, InputStream inputStream) throws IOException {
        String pathToStore = "C://myImages";
        Integer definedHeight = 150;
        Integer definedWidth = 60;
        //Reading image from inputStream
        BufferedImage bufferedImage = ImageIO.read(inputStream);
        if (checkDimensions(bufferedImage, definedHeight, definedWidth)) {
            File outputFile = new File(pathToStore + image);
            File directory = new File(pathToStore);
            if (directory.isDirectory()) {
                ImageIO.write(bufferedImage, "PNG", outputFile);
            } else {
                if (directory.mkdirs()) {
                    ImageIO.write(bufferedImage, "PNG", outputFile);
                    System.out.println("[UploadImage : onUploadImage] - Multiple file directory created");
                } else {
                    System.out.println("[UploadImage : onUploadImage] - Failed while creating multiple files");
                }
            }
        } else {
            showADFDefaultPopup("Maximum dimension allowed is " + definedHeight + "*" +
                                definedWidth + ".Please check the image used");
        }
    }

    private boolean checkDimensions(BufferedImage bufferedImage, int definedHeight,
                                    int definedWidth) {
        boolean accepted = false;
        int height = bufferedImage.getHeight();
        int width = bufferedImage.getWidth();
        System.out.println("[UploadImage : checkDimensions] - height : " + height);
        System.out.println("[UploadImage : checkDimensions] - width : " + width);
        if (definedHeight > height && definedWidth > width) {
            accepted = true;
        } else {
            accepted = false;
        }
        return accepted;
    }

    /**
     * Shows ADF default pop-up in the UI
     * @param text
     */

    private static void showADFDefaultPopup(String text) {
        FacesContext fcntx = FacesContext.getCurrentInstance();
        FacesMessage popup = new FacesMessage(FacesMessage.SEVERITY_INFO, "", text);
        fcntx.addMessage(null, popup);
    }
}

*Comments and feedback are most welcomed, Thanks.

Wednesday 3 April 2013

How to create a page in ADF



I must say this will be the most simplest blog I am planning to write today. No doubts many of you are definitely aware of how to create a jspx page in ADF. But I am writing this blog for those people who are very new to ADF or its there first day and they Google to find out that how they start with creating a simple page in ADF.
The idea of writing this blog came into my mind, when I found freshers in my project struggling creating a simple page in ADF.

Lets start with our work. First I would like to explain the design which I am going to follow to create an ADF page because there are many ways we can design a page. But I am going to explain the only way, which I have used in our project. I am using JDeveloper version 11.1.1.6.0

In this example I am going to create a sample Login page.

3 things will be mandatory in creating page -

a) Jsff (fragment) - It will contain the UI components such as text box, button, layout etc.
b) Taskflow - It will contain the jsff as a default activity.
c) Jspx - This page will contain taskflow as a region and it will be the final page.

Steps to create sample login application :

1. Select Fusion Web Application (ADF) :

 

Click next (until it get disabled) and finally click finish.

Below project structure will get created - 

2. Right click on View Controller project and select New .

A new Gallery window will open


3. Select JSF page Fragment and click OK


4. Repeat Step 2 - Select ADF task flow and click OK


5. Repeat Step 2 - select JSF page and click OK


6. Below structure will be created in ViewController –


7. Drag and drop the required components from component palette in your jsff page :


Sample code below :

<af:panelGroupLayout id="pgl1" layout="vertical">
      <af:inputText label="Email" id="it1" value="#{pageFlowScope.emailId}"
                    autoSubmit="true"/>
    <af:inputText label="Password" id="it2" value="#{pageFlowScope.password}"
                  autoSubmit="true" secret="true"/>
    <af:goButton text="Login" id="gb1"/>
  </af:panelGroupLayout>

Blue color - It states that these values or attributes are manually added using property Inspector.

8. Drag and drop your login.jsff page on your loginTF.xml task flow as show below.


Make sure to set it as a default activity.

9. Drag and drop your loginTF in your Login.jspx page as region.


10. Right click your Login.jspx page and select Run


Your first ADF page view will be displayed like below :


Note : The above blog will demonstrate the use of view controller project only. I have used bounded taskflow for the implementation and the application will currently not interacting with any BC component or managed bean.

*Comments and feedback are most welcomed, Thanks.