Sunday, December 9, 2012

ADF Upload Blob File in Table Column


Create Table with Blob Column

create table xx_attachment(fileId Number, file_name VARCHAR2(255),
                                          file_content_type varchar2(100),file_data blob,
                                         CONSTRAINT xx_attach_pk PRIMARY KEY (fileId));



Create EO,VO add it to AM.

Create below variable and accessor inside Managed Bean or Backing Bean.

import org.apache.myfaces.trinidad.model.UploadedFile;
...
    private UploadedFile _file;   
   
    public UploadedFile getFile() {
        return _file;
    }

    public void setFile(UploadedFile file) {
        _file = file;
    }



On JSF Page(jspx/jsf) or Page Fragment(jsff)
1)From DataControl unde XxAttachmentVO1 --> Operations --> Drag CreateInsert method and drop as ADF Button.   (When you click this Button it creates Blank row).

2)From Common Components drag inputFile(af:inputFile) component and drop on page.
Make sure  you put value as  #{<yourMangedBean>.file}

<af:inputFile label="Upload File" id="if1"
                                  value="#{<yourMangedBean>.file}"/>
3)From Common Components drag Button and drop after inputFile component.
Write below code on Button action Listner.

<af:commandButton text="Upload" id="cb11"
                                      action="#{<yourManagedBean.uploadAttach}"/>

4)Add Commit action method in Bindings.

5)For Page Form make sure usesUpload property is True.
<af:form id="f1" usesUpload="true">


import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.event.PopupFetchEvent;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.Row;
import oracle.jbo.domain.BlobDomain;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import javax.faces.event.ActionEvent;
...
    public String uploadAttach() {
        // Add event code here...
        //BindingContainer bindings = (BindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
    
        UploadedFile myfile = (UploadedFile)this.getFile();
       
        BindingContext bindingctx = BindingContext.getCurrent();
        BindingContainer bindings = bindingctx.getCurrentBindingsEntry();
        DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
        DCIteratorBinding iter = bindingsImpl.findIteratorBinding("XxAttachmentVO1Iterator");
       
        Row row = iter.getCurrentRow();
        // Upload File into Blob Column
        row.setAttribute("FileData", createBlobDomain(myfile));
       
        // File Name
        String fileName = (String)myfile.getFilename();
        row.setAttribute("FileName", fileName);
       
        // File Content/MIME Type
        String fileContentType = (String)myfile.getContentType();
        row.setAttribute("FileContentType", fileContentType);
      
        //Commit Transaction
        OperationBinding method = bindings.getOperationBinding("Commit"); 
        method.execute();
       
        return null;
    }

    private BlobDomain createBlobDomain(UploadedFile file) {
        InputStream in = null;
        BlobDomain blobDomain = null;
        OutputStream out = null;

        try {
            in = file.getInputStream();

            blobDomain = new BlobDomain();
            out = blobDomain.getBinaryOutputStream();
            byte[] buffer = new byte[8192];
            int bytesRead = 0;

            while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
                out.write(buffer, 0, bytesRead);
            }

            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.fillInStackTrace();
        }

        return blobDomain;
    }






To download file.
http://hasamali.blogspot.com/2011/09/download-file-in-oracle-adf-gui.html



ADF Multi Select Options (af:selectMany)

ADF Code Corner
https://blogs.oracle.com/jdevotnharvest/entry/how_to_access_selected_rows


http://myadfnotebook.blogspot.com/2010/09/getting-string-value-or-item-code-of.html




Create Master Detail Table
create table xx_users(user_id NUMBER,user_name VARCHAR2(100),
                                  CONSTRAINT xx_users_pk PRIMARY KEY (user_Id));
create table  xx_roles(role_id NUMBER,role_name VARCHAR2(100),
                                   CONSTRAINT xx_roles_pk PRIMARY KEY (role_Id));
Create EO,VO for above 2 tables.



create table xx_user_roles(user_role_id NUMBER,user_id NUMBER,role_name VARCHAR2(100),
                    CONSTRAINT xx_user_roles_pk PRIMARY KEY (user_role_Id),
                    CONSTRAINT xx_user_roles_fk1
                      FOREIGN KEY (user_id)
                      REFERENCES xx_users(user_id));
Create ReadOnly VO based on Above table.

Add All VO instances into AppModule.

From DataControl Add CreateInsert Operation for XxUsersVo1 as Button on Page.
From DataControl XxUsersVo1 as ADF Form.

From Data Control add XxRolesLOVVO1 as ADF Select Many Choice
as
              <af:selectManyChoice value="#{bindings.XxRolesLOVVO1.inputValue}"
                                   label="#{bindings.XxRolesLOVVO1.label}"
                                   id="smc1">
                <f:selectItems value="#{bindings.XxRolesLOVVO1.items}"
                               id="si1"/>
              </af:selectManyChoice>



On Button Pressed

    public void addRoles() {
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();
        JUCtrlListBinding allRolesList =
                 (JUCtrlListBinding) bindings.get("XxRolesLOVVO1");
          Object[] selVals = allRolesList.getSelectedValues();
          for (int i = 0; i < selVals.length; i++) {
            //Integer val = (Integer)selVals[i];
            String val = (String)selVals[i];
              System.out.println("Int Val "+val);

             // Insert into Table after getting selected Rows or your Code
              OperationBinding crRoleMethod = bindings.getOperationBinding("CreateInsertRoles"); 
              crRoleMethod.execute();
             
              DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
              DCIteratorBinding iter = bindingsImpl.findIteratorBinding("XxUserRolesVO2Iterator");

              Row row = iter.getCurrentRow();
              // Insert Role Name
              row.setAttribute("RoleName",val );
        
            //...
          }
    }


Use if you want to pull records based on Indices

    public void addRolesUsingIndices() {
        BindingContext bctx = BindingContext.getCurrent();
        BindingContainer bindings = bctx.getCurrentBindingsEntry();
        JUCtrlListBinding allRolesList =
                 (JUCtrlListBinding) bindings.get("XxRolesLOVVO1");
           int[] selVals = allRolesList.getSelectedIndices();
      
          for (int indx : selVals ) {
                  Row rw = allRolesList.getRowAtRangeIndex(indx);
                  Number id  = (Number)rw.getAttribute("RoleId");
                  String val = (String)rw.getAttribute("RoleName");
             
               // Insert into Table after getting selected Rows  or your Code
               OperationBinding crRoleMethod = bindings.getOperationBinding("CreateInsertRoles"); 
              crRoleMethod.execute();
             
              DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
              DCIteratorBinding iter = bindingsImpl.findIteratorBinding("XxUserRolesVO2Iterator");
   
              Row row = iter.getCurrentRow();
              // Insert Role Name
              row.setAttribute("RoleName",id+" "+val );
        
            //...
          }
    }






If the Choice List is Static eg.
            <af:selectManyCheckbox label="Roles" id="smc1"
                                   binding="#{backingBeanScope.Backing_xxUserCreation.smc1}"
                                   value="#{backingBeanScope.Backing_xxUserCreation.listValue}"
                                   autoSubmit="true">
              <af:selectItem label="Accountant" value="ACCOUNTANT" id="si3"
                             binding="#{backingBeanScope.Backing_xxUserCreation.si3}"/>
              <af:selectItem label="Sales Manager" value="SALES_MANAGER"
                             id="si1"
                             binding="#{backingBeanScope.Backing_xxUserCreation.si1}"/>
              <af:selectItem label="Finance Manager" value="FINANCE_MANAGER"
                             id="si2"
                             binding="#{backingBeanScope.Backing_xxUserCreation.si2}"/>
              <af:selectItem label="Buyer" value="BUYER" id="si4"
                             binding="#{backingBeanScope.Backing_xxUserCreation.si4}"/>
            </af:selectManyCheckbox>



Add below method call on Button Pressed

addRoles(listValue);


Add below code in Managed Bean or Backing Bean


    private Object[] listValue;
   
    public void setListValue(Object[] listvalue){
        this.listValue = listvalue;
    }
   
    public Object[] getListValue(){
        return listValue;
    }
   
    public String getListString(){
        return createString(listValue);
    }
   
    public String createString(Object[] arr){
        if(arr != null){
            String values = "[";
            for(int i=0;i<arr.length;i++) {
                values = values + arr[i];
                if (i <arr.length - 1)
                values = values + ",";
                }
        values = values + "]";
        return values;
       }
        return null;
     }


    public void addRoles(Object[] arr){
       
         BindingContext bindingctx = BindingContext.getCurrent();
         BindingContainer bindings = bindingctx.getCurrentBindingsEntry();
        
        if(arr != null){
            //String values = "[";
            for(int i=0;i<arr.length;i++) {
                //values = values + arr[i];
                System.out.println("Roles to be added "+arr[i]);
                    // Insert Row into USer Roles table
                    OperationBinding crRoleMethod = bindings.getOperationBinding("CreateInsertRoles"); 
                    crRoleMethod.execute();
                   
                   
                    DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
                    DCIteratorBinding iter = bindingsImpl.findIteratorBinding("XxUserRolesVO2Iterator");
                   
                    Row row = iter.getCurrentRow();
                    // Insert Role Name
                    row.setAttribute("RoleName",arr[i] );
                //if (i <arr.length - 1)
                //values = values + ",";
                }
        //values = values + "]";
       }
     }