Monday 20 June 2016

Changing the VO query at run time programmatically

Use the below method to modify the query of a view object run time programmatically.
    public void modfiy(ViewObjectImpl voImpl, String newQueryString){
        voImpl.setFullSqlMode(voImpl.FULLSQL_MODE_AUGMENTATION);
        voImpl.closeRowSet();
        voImpl.setWhereClause(null);
        voImpl.setWhereClauseParams(null);
        voImpl.setQuery(newQueryString);
       

        }

Programmatically reading the entity state in Oracle ADF

Entity row has two states one is associated to the transaction and the other is corresponding to the database(post state).
Use getEntityState() to read the Entity state corresponding to the transaction and getPostState() to read the Entity state corresponding to the DB.

Points to be remembered
When you create a new Entity row both the getEntityState() and getPostState() gives you the status as Entity.STATUS_NEW.
When you post the EO to the DB(Without Committing the transaction) the getEntityState() gives you the Entity.STATUS_NEW, whereas the getPostState() returns you the Entity.STATE_UNMODIFIED.
When the transaction is committed the state both returns the Entity_STATE_UNMODIFIED.

You can observe the above points using the below code. Place the below code in the Application Module Implementation class.
Create a EO based on the Department table.
    public void findEntityState() {
        //Get defnition object
        EntityDefImpl departmentEODef = DepartmentEOImpl.getDefinitionObject();
        //Create the entiy instance in the current transaction
        DepartmentEOImpl newDept1 = (DepartmentEOImpl) departmentEODef.createInstance2(this.getDBTransaction(), null);
        newDept1.setDepartmentId(100);
        newDept1.setDepartmentName("Oracle Fusion");
        newDept1.getPostState() // NEW
        newDept1.getEntityState() // NEW
        //Post changes to DB
        getDBTransaction().postChanges();
        newDept1.getPostState() // UNMODIFIED
        newDept1.getEntityState() //NEW
        //Commit the Transaction
        getDBTransaction().commit();
        newDept1.getPostState() // UNMODIFIED
        newDept1.getEntityState() // UNMODIFIED

    }

Friday 17 June 2016

Programmatically handling the Entity Objects in Oracle ADF

Entity Object is n object oriented representation of data base objects like table. It represents a table or view. It encapsulates the properties of a Row like the validation rules, security and presenting values. It is used to perform all the DML operations.
Files related to Entity Object
  1. Entity definition XML file- Defines the structure of  the entity object. It acts as a template for the creation of entity objects.
  2. Entity Object class - It represents an entity instance. It wraps the business logic for a row like having default values at the time of row creation, lock() method to lock the row and doDML() method to perform DML operations.
  3. Entity Collection Class- Caches the rows particular to a DB transaction.

Use the below code to create the entity rows. You can have it in the Application module implementation classes.
Creating a new entity row
public void createEntityObject(){
EntityDefImpl sampleEODef=sampleEOImpl.getDefinitionObject();//Here get the definition object using the EOImpl like EntityImpl
SampleEOImpl newEOInstance=sampleEODef.createInstance2(this.getDBTransaction(), null);
//The entity status is STATUS_NEW now and qualifies to participate in a transaction.
//Use the newEOInstance object to set the attribute values.
}
Finding an entity row and updating it
public void updateEntity(String primaryKey){
//Key creation by passing the primary key
Key key= SampleEOImpl.createPrimaryKey(primaryKey);
//get the definition object
//The row status here is STATUS_UNMODIFIED now
EntityEOImpl rowTobeUpdated =
(DepartmentEOImpl)SampleEOImpl.
getDefinitionObject().
findByPrimaryKey(getDBTransaction(), key);
rowTobeUpdated .setAttributes()
//The row status here is STATUS_MODIFIED and qualifies for row the transaction.
}
Removing an entity row
The below method takes a key attribute value in a row as input and performs find and delete operations.
public void removeEntity(Integer key){
            //Key creation by passing the key
            Key key= SampleEOImpl.createPrimaryKey(key);
            EntityEOImpl rowTobeDelted =
             (SampleEOImpl)SampleEOImpl.
             getDefinitionObject().
            findByPrimaryKey(getDBTransaction(), key);
           //Row status is STATUS_UNMODIFIED
           rowTobeDelted.remove();
          //Row status is STATUS_DELETED
 }

Committing a transaction
     public void commitTransaction(){
       getDBTransaction().commit();

      }

Launching the Oracle ADF application from the Oracle EBS

The use case is to launch the ADF application from the Oracle EBS suite. The below process guides you to achieve it.
Create a jsp page Example.jsp and pace it under the $OA_HTML. This JSP will retrieve the URL                             defined in the EBS function and redirects to the ADF application.
          Example.jsp
          <%
           String redirectURL= request.getParameter("URL"); // This we will define in the function
            response.sendRedirect(redirectURL);
           %>
Now we need to create a function in EBS that stores the URL of the ADF Application. In the below screen shot under the parameters section we need to pass the application URL.

In the Web HTML give the JSP name we created.


The JSP file must be compiled and the OA core server must be bounced for the JSP changes to take effect. Once compiled the class file for the JSP is created under the $COMMON_TOP/_Pages folder.


Now create a menu function(personalization) that calls the function you have created in the earlier steps. You can use the same function on any screen to launch the  ADF application.

Code for finding the Entity or Rows by using the Primary Key in ADF

Use the below process/code to find the Entity instance based on the primary key.
  1. Get the EntityDefinition object by using the EntityImpl class.
  2. Use createPrimaryKey() method available in the EntityImpl to create the key.
  3. Use the EntityDefImpl object findyByPrimaryKey method and pass the key you created in the previous step.
  4. The findByPrimaryKey method gives the EntityImpl, which is an representation of row.

 In this example we are considering the EmployeeEO and empId as primary key.
Private EmployeeEOImpl findEmpById(String empId){
EntityDefImpl empDef= EmployeeEOImpl.getDefinitionObject();//Finding the Defintion
Key empKey= EmployeeEOImpl.createPrimaryKey(new Integer(empId));//Creating the Key
Return (EmployeeEOImpl) empDef.findByPrimaryKey(getDBTransaction(),empKey);
}


Friday 10 June 2016

Changing the XML processing in Oracle ADF/ J developer when integrating with SOAP web service

When I am invoking a web service using SOAP in ADF applications the SOAP request and responses are not be displayed correctly. To make it work change the below weblogic-application.xml.
  1. Open the weblogic-application.xml available in the Application Resources --> Descriptors --> Meta-Inf folder
  2. Change the Document Builder Factory to javax.xml.parsers.DocumentBuilderFactory and Transformer factory to javax.xml.transform.TransformerFactory as shown below. Or if required you can change these configurations too.

Problem with Export to Excel Feature in Oracle 12c

I am trying export the table data to excel sheet using Export to Excel in 12c,it is giving the errors like <org.apache.myfaces.trinidad.component.UIXCollection> <UIXCollection> <_verifyComponentInContext> <COLLECTION_NOT_IN_CONTEXT>.
To make it work we can alternatively use the download listener(af:fileDownloadActionListener) and apache POI.
Follow the below process.
  1. Download the Apache POI jar and add it to project libraries.
  2. Create a page and drag and drop the view object as a table
  3. Drag and drop the button on the page and add a fileDownloadListener(af:fileDownloadActionListener) to the button
  4. Point the method attribute of the file download listener to a managed bean method.
//This is the code you will have in the page
<af:fileDownloadActionListener contentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                                   filename="EmployeeDtails" method="#{pageFlowScope.EmployesBean.downLoadExcel}"/> 
  1. Write the below code in the method that creates and downloads the excel file.
//This is the managed bean method
        public void downLoadExcel(FacesContext facesContext, OutputStream outputStream) {
                        try {
                                   HSSFWorkbook workbook = new HSSFWorkbook();
                                   HSSFSheet worksheet = null;
                               worksheet = workbook.createSheet("Employee Details");
                       DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
                                   DCIteratorBinding dcIteratorBindings = bindings.findIteratorBinding(iterName);
                       RowSetIterator rowSetIterator=  dcIteratorBindings.getRowSetIterator();
                       rowSetIterator=  dcIteratorBindings.getViewObject().createRowSetIterator("ExportToExcel");
                                   int i = 0;
                                   Row row=null;
                                    HSSFRow excelrow = null;
                                   while(rowSetIterator.hasNext()){
                                        row=rowSetIterator.next();
                                       if (i == 0) {
                                           excelrow = (HSSFRow) worksheet.createRow(i);
                                       int j = 0;                      
                                           for (String colName : row.getAttributeNames()) {                      
                                               HSSFCell cellA1 = excelrow.createCell(j);
                                                        cellA1.setCellValue(colName);
                                                       }
                                                    j++;
                                                  }
                                                   }
                      
                                       ++i;
                                       int j = 0;
                                       excelrow = worksheet.createRow(i);
                                       for (String colName : row.getAttributeNames()) {
                                                   HSSFCell cell = excelrow.createCell(j);
                                                   cell.setCellValue(row.getAttribute(colName).toString());
                                                   }  
                                               j++;
                                               workbook.write(outputStream);
                                               outputStream.flush();
                                               outputStream.close();
                                               rowSetIterator.closeRowSetIterator();
                                               logger.info("Export excel is finished");
                                               } catch (IOException e) {
                                               e.printStackTrace();
                                               }

                                           }

Reference 
http://www.techartifact.com/blogs/2013/08/generate-excel-file-in-oracle-adf-using-apache-poi.html#sthash.XzGOCOMf.dpbs