Tuesday, July 8, 2014

Implementing Auto Suggest in ADF

Took help from http://www.oracle.com/technetwork/developer-tools/jdev/autosuggest-090094.html
Requirement is to auto suggest users while using input field . In this case, there is an inputbox for employee's first name (using the HR schema) where upon entering the character, popup suggests user for the list of employees with the first name starting with the character entered as shown in the figure.


View Layer:
1. Create a jspx page with input box, and add the clientListener attributes to the input box

2. define the af:popup segment in the jspx with selectOneListbox option. Function written on keyup and click of input box would populate the List box in this case

Model Layer:
1. Create the EmployeeVO with View Criteria as:


    and write the java method to populate the Employee List(can be found in the code attached)
2. The backing bean would fetch the List from AppMod using method binding:


Run the jspx and result would be as expected :)


Code can be found @ Auto Suggest


Tuesday, July 1, 2014

How to Upload file in ADF Application

Upload file functionality is implemented using af:inputFile component which provides user with a dialog to select the input file.

File selected can be accessed using vcl of the inputfile which provides the
org.apache.myfaces.trinidad.model.UploadedFile object of the selected file:

using the file object, user can get the attributes like fileName, InputStream etc as:

        UploadedFile file = (UploadedFile) event.getNewValue();
        if (file != null)
        {
          fileName=file.getFilename();
          try {
                inputstream=file.getInputStream();
          } catch (IOException e) {
                e.printStackTrace();
          }

     

User would use this inputstream to upload the file in any given location as(can be written as button action):
        OutputStream outputStream = null;
        try{
            outputStream = new FileOutputStream(new File("C:\\JDeveloper\\mywork\\uploadedFiles\\"+fileName));     //hard coded for now
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = inputstream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
            }
            System.out.println("Done!");
        }catch(Exception e){
            e.printStackTrace();
        }


Attached code can be downloaded from here


Friday, June 6, 2014

ERROR: No j2ee modules detected in EAR archive. Deployment aborted : Resolved!!!

While Deploying the Application using build.xml file, i encountered this issue :
  <exception msg="== ERROR: No j2ee modules detected in EAR archive. Deployment aborted. ==">
    <![CDATA[
    oracle.jdeveloper.deploy.VetoableDeployException: == ERROR: No j2ee modules detected in EAR archive. Deployment aborted. ==
  at oracle.jdevimpl.deploy.ear.EarDeployerListener.willDeploy(EarDeployerListener.java:113)
  at oracle.jdevimpl.deploy.fwk.ListenerSupport.fireWillDeploy(ListenerSupport.java:198)
  at oracle.jdeveloper.deploy.common.AbstractDeployer.deploy(AbstractDeployer.java:88)
  at oracle.jdevimpl.deploy.fwk.WrappedDeployer.deployImpl(WrappedDeployer.java:39)
  at oracle.jdeveloper.deploy.common.AbstractDeployer.deploy(AbstractDeployer.java:94)
  at oracle.jdevimpl.deploy.fwk.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:457)
  at oracle.jdeveloper.deploy.DeploymentManager.deploy(DeploymentManager.java:217)
  at oracle.jdevimpl.deploy.common.ModulePackagerImpl.deployProfiles(ModulePackagerImpl.java:86)
  at oracle.jdeveloper.deploy.common.ModulePackager.deployProfiles(ModulePackager.java:63)
  at oracle.jdeveloper.deploy.common.ModulePackager.deployImpl(ModulePackager.java:52)
  at oracle.jdeveloper.deploy.common.AbstractDeployer.deploy(AbstractDeployer.java:94)
  at oracle.jdevimpl.deploy.fwk.WrappedDeployer.deployImpl(WrappedDeployer.java:39)
  at oracle.jdeveloper.deploy.common.AbstractDeployer.deploy(AbstractDeployer.java:94)
  at oracle.jdeveloper.deploy.common.BatchDeployer.deployImpl(BatchDeployer.java:82)
  at oracle.jdeveloper.deploy.common.AbstractDeployer.deploy(AbstractDeployer.java:94)
  at oracle.jdevimpl.deploy.fwk.WrappedDeployer.deployImpl(WrappedDeployer.java:39)
  at oracle.jdeveloper.deploy.common.AbstractDeployer.deploy(AbstractDeployer.java:94)
  at oracle.jdevimpl.deploy.fwk.WrappedDeployer.deployImpl(WrappedDeployer.java:39)
  at oracle.jdeveloper.deploy.common.AbstractDeployer.deploy(AbstractDeployer.java:94)
  at oracle.jdevimpl.deploy.fwk.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:457)
  at oracle.jdevimpl.deploy.fwk.DeploymentManagerImpl$1.run(DeploymentManagerImpl.java:315)


    ]]>
    </exception>
    <status exitcode="4">FAILED</status>
  </deploy-task>
....
...

This issue occurs, if you do not specify the in the Application Assemble in the deployment profile as shown in the screen shot

Make sure you specify the Application Assemble in the deployment profile as per the Application's need

Thursday, May 22, 2014

Using Sequence in ADF

The mentioned example demonstrates a scenario where EmployeeID attribute is supposed to be populated from sequence names emp_seq

1. Make sure that the EntityIMPL class has been defined for the entity to be used(EmployeeEO in this case)

2. Over ride the create method as follows:




    /**
     * Add attribute defaulting logic in this method.
     * @param attributeList list of attribute names/values to initialize the row
     */
    protected void create(AttributeList attributeList) {
        super.create(attributeList);
        setEmployeeId(nextSequenceVal("EMP_SEQ"));
    }


    public Number nextSequenceVal(String seqName){
        SequenceImpl seq=new SequenceImpl(seqName,getDBTransaction());
        System.out.println("seqence no "+seq.getSequenceNumber()+"  "+seq.getSequenceNumber());
        return seq.getSequenceNumber();
    }

3. Use the corresponding VO as required on the jspx page

Tuesday, May 20, 2014

Get the new row of the ADF table while using insert operation

If you need to fetch the values of the new row being inserted to editable adf table, use following in the backing bean:


            CollectionModel tableModel = (CollectionModel) getEmployee().getValue();
            JUCtrlHierBinding adfModel = (JUCtrlHierBinding) tableModel.getWrappedData();
            DCIteratorBinding dciter = adfModel.getDCIteratorBinding();
            ViewObject voTableData = dciter.getViewObject();
            Row rowSelected = voTableData.getCurrentRow();
            String fName="";
            if(rowSelected.getAttribute("FirstName")!=null){
                fName=(String)rowSelected.getAttribute("FirstName");
                System.out.println(fName);
            }



getEmployee() : binding for the table
FirstName : the column of table 

Monday, May 19, 2014

Get the current selected row of af:table

In order to get the selected row attributes of af:table, follow these steps:


1. Bind the selectionListener property to a java method as:

<af:table value="#{bindings.EmployeesVO1.collectionModel}" var="row"
                  rows="#{bindings.EmployeesVO1.rangeSize}"
                  emptyText="#{bindings.EmployeesVO1.viewable ? 'No data to display.' : 'Access Denied.'}"
                  fetchSize="#{bindings.EmployeesVO1.rangeSize}"
                  rowBandingInterval="0"
                  selectedRowKeys="#{bindings.EmployeesVO1.collectionModel.selectedRow}"
                  selectionListener="#{EmployeeEdit.employeeSelectListener}"
                  rowSelection="single" id="t1">

2. Define the selectionListener in managed bean as:

    public void employeeSelectListener(SelectionEvent selectionEvent) {
        System.out.println("Selection event called");
     
        DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding dcItteratorBindings =bindings.findIteratorBinding("EmployeesVO1Iterator");
        GenericTableSelectionHandler.makeCurrent(selectionEvent);
        // Get an object representing the table and what may be selected within it
        ViewObject voTableData = dcItteratorBindings.getViewObject();
        
        // Get selected row
        Row rowSelected = voTableData.getCurrentRow();
        if(rowSelected.getAttribute("EmployeeId")!=null){
            System.out.println("Employee selecetd"+rowSelected.getAttribute("EmployeeId")+"    "+rowSelected.getAttribute("FirstName"));   
        }
    }


3. You need to create a custom class that would point the selection event to the current row, else no matter what is the selection the 1st row would be returned. Code for makeCurrent is as follows:

    public static void makeCurrent( 
    SelectionEvent selectionEvent){ 
    
    RichTable _table = (RichTable) selectionEvent.getSource(); 
    //the Collection Model is the object that provides the 
    //structured data 
    //for the table to render 
    CollectionModel _tableModel = (CollectionModel) _table.getValue(); 
    //the ADF object that implements the CollectionModel is 
    //JUCtrlHierBinding. It is wrapped by the CollectionModel API 
    JUCtrlHierBinding _adfTableBinding = (JUCtrlHierBinding) _tableModel.getWrappedData(); 
    //Acess the ADF iterator binding that is used with 
    //ADF table binding 
    DCIteratorBinding _tableIteratorBinding = _adfTableBinding.getDCIteratorBinding(); 
    
    //the role of this method is to synchronize the table component 
    //selection with the selection in the ADF model 
     Object _selectedRowData = _table.getSelectedRowData(); 
     //cast to JUCtrlHierNodeBinding, which is the ADF object 
     //that represents a row 
     JUCtrlHierNodeBinding _nodeBinding = 
     (JUCtrlHierNodeBinding) _selectedRowData; 
     //get the row key from the node binding and set it 
     //as the current row in the iterator 
     Key _rwKey = _nodeBinding.getRowKey(); 
     _tableIteratorBinding.setCurrentRowWithKey( 
     _rwKey.toStringFormat(true)); 
     } 

4. Run the code to see the output:





Tuesday, May 13, 2014

How to access the selected value in af:selectonechoice

Common problem with af:selectonechoice component is when trying to get the selected value it returns the index of the selected item.

I found a turn around for it. It can be done in multiple ways, one of which is explained here.

1. Make sure the attribute which is expected to be selected is defined in the page def as. For example, on the select of department i would like to fetch the location of the selected department:



2. Add the following code in vcl:

    public void deptVCL(ValueChangeEvent valueChangeEvent) {
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();
        AttributeBinding applicationNameBinding =   (AttributeBinding)bindings.getControlBinding("LocationId");
        String locatriondId= applicationNameBinding.getInputValue().toString();
        System.out.println("Old Location name   "+locatriondId);
        FacesContext contxt = FacesContext.getCurrentInstance();
        valueChangeEvent.getComponent().processUpdates(contxt);
        locatriondId = applicationNameBinding.getInputValue().toString();
        System.out.println("New Location name     " + locatriondId);
    }

3. The output in console is as follows: