Category Archives: snippets

How To in Jdeveloper ADF – To call a VO from another VO or from AM method


Scenario:
How to set an Attribute value from one VO into another VO?

Solution: 

//in you Application module Impl file have the follwing code written inside the method which will set the value for another vo
//lets say UploadView is the View object that you would want to set the value
ViewObject vo = findViewObject("tUploadView");
//if its not in Application Module, you will have to get the VO instance either from iterator if you are using this code in the bean
//get the rowset of UploadView
RowSet rs = vo.getRowSet();
//iterate through
while(rs.hasNext()){
//get the row
Row r = rs.next();
//set the value
r.setAttribute("ValueItem", value); //ValueItem is the Attribute that you want to set
}
}
Advertisements

How To in Jdeveloper ADF Tutorials – Add Date based on another Date


Scenario:

How to calculate the datefield by considering another datefield that’s entered in by the user.
If you have entered a datefield “Today” as 12/04/2012
Now the requirement is to calculate another datefield’s date as 12/04/2012 + 30 days?

Solution:

  • Have two fields in the page. inputDate1 and inputDate2 bounded to the value in the bean as date1 and date2.
  • set inputDate1 autosubmit=true, and inputDate2 partialTrigger is set to the id of the inputDate1

<af:inputDate label="Date 1" id="id1" value="#{bean.date1}" autoSubmit="true"/>
<af:inputDate label="Date 2" id="id2" value="#{bean.date2}" partialTriggers="id1"/>

in the bean for the date fields. have getter and setter

in the getter of the date2 field have the following code


private Date date1;
private Date date2;

public void setDate1(Date date1) {
this.date1 = date1;
}

public Date getDate1() {
return date1;
}

public void setDate2(Date date2) {
this.date2 = date2;
}

public Date getDate2() {
if(date1 != null){
Calendar cal =Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DATE, 30);
date2 = cal.getTime();
}
return date2;
}

The source code is downloaded from here, for Jdeveloper 11.1.2.2.0

How To in Jdeveloper ADF Tutorials – set and get SelectOneChoice values


Imagine that you have a ViewObject added to the Application Module and you want to add this view instance as a selectonechoice for the base VO in your UI.

Setup:

Lets have a setup as ‘DeliveryRuleView’ VO as a base datasource and for the column ‘CapTrgTyp’ you are going to add a LOV list of values coming from ‘DeliveryTriggerType’ VO

The following screenshot will show a VO ‘SystemDictionaryView’ added to the AM as a View Instance ‘DeliveryTriggerType’. The ‘SysemDictinaryView’ will list the values based on the ‘Subcategory’ provided during runtime.

To Map this to a select one choice in the UI. You will have to have the list binding created as shown in the screenshot.

  • The Base Data Source is selected as ‘DeliveryRuleView’ and the List Data Source is selected as ‘DeleiveryTriggerType’.
  • The Data Mpping is done against the ‘CaptrgType’ from ‘DeliveryRuleView’ and the ‘SubCategory’ attribute from the ‘DeliveryTriggerType’.
  • The List Binding will have another attribute ‘Value’ as a Display Attribute. The binding is named as Deliverytrigger’
  • Have an iterator defined for the ‘DeliveryTriggerType’ View instance from AM and name it as ‘DeliveryTriggerTypeIterator’

in the jsff page you will have the selectonechoice code as


<af:selectOneChoice label="Trigger" id="deliveryTrigger" value="#{pageFlowScope.backing_bean.deliveryTrigger}" required="true" autoSubmit="true">
<f:selectItems id="si131" value="#{bindings.DeliveryTrigger.items}"/>
selectOneChoice>

When you look closely, the value of the selectonechoice is mapped to the deliveryTrigger value in the managed bean and we have the selectItems bounded to the pagedef list bindings as ‘#{bindings.DeliveryTrigger.items}’

Create Operation

So during Creation of a record in ‘DeliveryRuleView’ the list will be populated from ‘DeliveryTriggerType’ instance. The user selects the option in the list and presses the save button.

Since selectonechoice stores only the index value, the value that is stored will point to the index value within the selectonechoice. for e.g if the user select’s choice ‘Third Option in the list'(underlying SubCategory as ‘THIRD_VALUE’) then the value for deliverTrigger will store as 2 as the index starts from 0.

So how to map this to the correct selection?.

Have an actionlistner for the save button and have the following code that computes the correct selection for the select one choice

What this method does:

  • Get the iterator binding corresponding to the iterator name passed through ‘iteratorName’, in our case this will be ‘DeliveryTriggerTypeIterator’
  • Get the Row corresponding to the index passed , this will be ‘deliveryTrigger’ which holds the index value of the selectonechoice
  • Get the ‘SubCategory’ value from the corresponding row and return it.

private String getListValueFromIndex(int index, String iteratorName) {
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry()

DCIteratorBinding dciterContainer =
(DCIteratorBinding)bindings.get(iteratorName);

Row ri = dciterContainer.getRowAtRangeIndex(index);
String val = (String)ri.getAttribute("SubCategory");
return val;
}

The value returned will have the underlying subCategory value(‘THIRD_VALUE’) corresponding to the value selected in the UI as ”Third Option in the list”. Then the value can be saved to the ‘Captrgtyp’ column of the DB for ‘DeliveryRuleView’

Edit Operation

During the Edit operation of the record, we have to fetch the corresponding value for the selected record and populate for ‘Captrgtyp’ attribute in the selectonechoice.

What this method does:

  • The currentRow and the ‘Captrgtyp’ is passed as attribName to the method
  • Get the iterator binding corresponding to the iterator name passed through ‘iteratorName’, in our case this will be ‘DeliveryTriggerTypeIterator’
  • Get the RowSetIterator from the iiterator binding
  • Iterate over the record and check if the value is equal to the value of the ‘SubCategory’ from the rowSetIterator
  • If it’s equlal then save the index value to a variable ‘i’
  • Finally return the value ‘i’ which corresponds to corresct value in the selectonechoice

private Integer getListValueIndex(Row row, String iteratorName, String attribName) {
int i = 0;
BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry()

DCIteratorBinding dciterContainer =
(DCIteratorBinding)bindings.get(iteratorName);

RowSetIterator rs = dciterContainer.getRowSetIterator();
Row ri = rs.first();
for(int index = 0;index < rs.getRowCount(); index ++){
String temp = (String)ri.getAttribute("SubCategory");
String ar = (String)row.getAttribute(attribName);
if(ar != null && temp.equals(ar.toString().trim())){
i = index;
}
ri = rs.next();
}

return i;
}

ADF JavaScript Partitioning


Most of the performance issue at the client side for the ADF application is related to large number of client side scripting files getting download which increases the download time and relatively degrading the performance of the application. Breaking up the JavaScript into small chunks will result in modularity but will increase the round trip.

To resolve this ADF Faces Framework comes up with a concept of partitioning of the huge JavaScript at the client side based on the components used in the pages. That means only limited number of scripts required for the page is loaded at the client which will increase the performance drastically.

ADF Faces groups the components JavaScript files into two groups namely partitions and features. These two groups are defined separately in two configuration files i.e. adf-js-features.xml and adf-js-partitions.xml.

The default files are location at

C:\oracle\Middleware\oracle_common\modules\oracle.adf.view_11.1.1\ adf-richclient-impl-11.jar\

If the application is loaded without using an explicit partitions then adf will use the configuration settings defined in the above specified files and does the partition.
By default, the partitioning is enabled for performance which can be disabled using the initial parameter in web.xml

<context-param>
<param-name> oracle.adf.view.rich.libraryPartitioning.ENABLED</param-name>
<param-value>false</param-value>
</context-param>

Or

<context-param>
 oracle.adfinternal.view.rich.libraryPartitioning.ENABLED
</param-name>
<param-value>false</param-value>
</context-param>

Or

<context-param>
<param-name>oracle.adf.view.rich.libraryPartitioning.DISABLED</param-name>
<param-value>true</param-value>
</context-param>

Or

<context-param>
<param-name>oracle.adfinternal.view.rich.libraryPartitioning.DISABLED</param-name>
<param-value>true</param-value>
</context-param>

Now we will see how to create these files
•    Create adf-js-partitions.xml in public_html/WEB-INF folder of your application
•    Create adf-js-features.xml in src/META-INF folder of your application
•    The skeleton of these files looks like

adf-js-partitions.xml

<?xml version="1.0" encoding="utf-8"?>
xmlns="http://xmlns.oracle.com/adf/faces/partition">
<partition>
<partition-name>dnd</partition-name>
<feature>AdfDropTarget</feature>
</partition>
</partitions>

<partitions> – root tag to define partitions, all partitions are defined inside this
<partition> – define the partition, any number of partition is allowed
<partition-name> – name of the partion. For example the javascript will be loaded as dnd-11.1.1.4.js
<feature> – hook reference to the particular feature that defines to load the javascript for the component

adf-js-features.xml

<?xml version="1.0" encoding="utf-8"?>
xmlns="http://xmlns.oracle.com/adf/faces/feature">
<feature>
<feature-name>AdfDropTarget</feature-name>
oracle/adf/view/js/dnd/AdfBasicDropTarget.js
<feature-dependency>AdfDragAndDrop</feature-dependency>
</feature>
<feature>
<feature-name>AdfDragAndDrop</feature-name>
oracle/adfinternal/view/js/laf/dhtml/rich/AdfDhtmlDnDContext.js
</feature>
</features>

<features> – root tag for the features, all features are defined inside this
<feature> – define the feature, any number of feature is allowed
<feature-name> – name of the feature that has to be referenced from the partition file
<feature-class> – javascript class for the component
<feature-dependency> – name of the feature which is extended by the javascript class

Sample:

When you don’t have these files in place then the loaded scripts are

When you replicate the default content in your configuration files then the partition will be

Let’s have the partitions which defines only two partition forcomponent. It’s better to have AdfBootStrap and AdfCore as part of the partitions as these are the basic components and JavaScript to render some of the key components. You are free to research on these components to come up with your own core and booting features. You can get the base AdfBootStrap and AdfCore partitions and features configurations from

The partition file with the configuration and hook looks like

And the corresponding features are defined as

And when you run your page in internet explored 8 with Developer Tools on (F12) you can see in the script section that our partition is available as a separate script

Troubleshooting:

If we refer to an invalid feature in the partition then we will get

References:

http://docs.oracle.com/cd/E24382_01/web.1112/e16181/af_arch.htm#CHDDEAJH
http://docs.oracle.com/cd/E12839_01/apirefs.1111/e12046/oracle/adf/view/js/component/rich/input/package-summary.html
http://docs.oracle.com/cd/E25054_01/web.1111/b31973/ap_config.htm#BABCJIDJ
http://adfcodebits.blogspot.com/2012/01/bit-34-using-javascript-partitioning.html

You can download the pdf file of this from here

snippets – to collapse a tree which is expanded



public void rowDisclosureListener(RowDisclosureEvent rowDisclosureEvent) {
System.out.println(":::: RowDisclosureListener Invoked");
int depth = tree.getDepth();
if (depth > 0) {
return;
}
RowKeySet discloseRowKeySet = tree.getDisclosedRowKeys();
RowKeySet addedRowKeySet = rowDisclosureEvent.getAddedSet();
Iterator addedRowKeySetIter = addedRowKeySet.iterator();
if (addedRowKeySetIter.hasNext()) {
discloseRowKeySet.clear();
Object firstKey = addedRowKeySetIter.next();
discloseRowKeySet.add(firstKey);
tree.setSelectedRowKeys(addedRowKeySet);
tree.setRowKey(firstKey);
AdfFacesContext adfFacesContext = null;
adfFacesContext = AdfFacesContext.getCurrentInstance();
adfFacesContext.addPartialTarget(tree.getParent());
}
}
}

Producer and Consumer Problem


Self explanatory Producer and Consumer problem

Producer.java

package com.producer;

import java.util.ArrayList;

import java.util.Random;

public class Producer implements Runnable{

//Engine Batch ID reference

ArrayList engineList;

//Engine No is stored here

String str;

//Assigning the BATCH ID reference

public Producer(ArrayList engineList) {

this.engineList = engineList;

}

/*

* Produce Engines

*/

public void produce(){

//Random number for engine number

str = ""+Math.abs(new Random(System.currentTimeMillis()).nextLong());

//Only one thread can enter the BATCH for any operation

synchronized (engineList) {

//Add the Engine to the batch

engineList.add(str);

//Notify all other threads that the new engine is added to the BATCH

engineList.notifyAll();

}

//Print Engine Number

System.out.println("Produced :: ENGINE CHASIS NO ["+ str + "]");

}

@Override

public void run() {

System.out.println("Production started");

//10 Engine is to be produced

for(int i=0;i<10;i++){

try {

//Produce the engine

this.produce();

//Sleep for 2 seconds for the next operation

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

Consumer.java


package com.producer;

import java.util.ArrayList;

public class Consumer extends Thread{

//Engine Batch ID reference

ArrayList<String> engineList;

//Engine No is stored here

String str;

//Assigning the BATCH ID reference

public Consumer(ArrayList<String> engineList) {

this.engineList = engineList;

}

/*

* Consume Engines from the BATCH

*/

public  void consume(int index) throws InterruptedException{

//Only one thread can enter the BATCH for any operation

synchronized (engineList) {

//if the Batch has no engines

while(engineList.size() == 0){

//then wait for some time until you get a new engine to consume

engineList.wait();

}

//If the engine is deployed to the BATCH

if(engineList.size() >= 0){

//The consume the engine from the respective position

str = engineList.get(index);

}

}

//Print Engine Number that it has been consumed

System.out.println("Consumed :: -------> ENGINE CHASIS NO ["+ str + "]" );

}

@Override

public void run() {

//10 Engine has to be consumed

for(int i=0;i<10;i++){

try {

//Produce the engine

this.consume(i);

//Sleep for 3 seconds for the next operation

Thread.sleep(3000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

Main.java


package com.producer;

import java.util.ArrayList;

public class Main {

public static void main(String[] args) throws InterruptedException {

//ArrayList to store the produced engines - BATCH

ArrayList<String> engine = new ArrayList<String>();

//Producer taking the engine BATCH ID Reference

Producer producer = new Producer(engine);

//Consumer taking the reference of the engine BATCH ID Reference

Consumer consumer = new Consumer(engine);

//Threads

Thread prodThread = new Thread(producer);

Thread conThread = new Thread(consumer);

//Starting the production

prodThread.start();

//Start Consumption

conThread.start();

}

}


Getting the value of Saved search – programatic way


QueryModel qm = (QueryModel)evaluateEL("#{bindings.SearchViewCriteriaQuery.queryModel}");
 for(int indexCount=0; indexCount
 {
 QueryDescriptor qd = qm.getUserQueries().get(indexCount);
 if(qd!=null){
 String queryDescriptorName = qd.getName();
 System.out.println(queryDesctiptorName);
 m = qd.getUIHints();
 isDefault = (Boolean) m.get(QueryDescriptor.UIHINT_DEFAULT);
 System.out.println(isDefault);
 isRunAutomatically = (Boolean) m.get(QueryDescriptor.UIHINT_AUTO_EXECUTE);
 System.out.println(isRunAutomatically);
 if (Boolean.TRUE.equals(isDefault) && Boolean.TRUE.equals(isRunAutomatically){
 break();
 }
 else{
 queryDescriptorName = null;
 }
 System.out.println(isDefault);
 }