Hi ,
In this post i would like to show how dvt component will work with active data service.Since there is no example available on internet therefore might it will very useful for reader.
Use Case: Bar Graph integration with Active Data Service.
The Importance Step for implementing Active Data Service are following.
1-Need to provide configuration for Active Data Service adf-config.xml
2-Create the Model for Active Data Service.
In Graph context the model will have following data structure.
1-Serial Name.
2-Column Name.
3-Data Point.
3-Extend ActiveDataModelDecorator class to register Graph Model into Active Data Serive
4-Extend BaseActiveDataModel class to fire change event and use for creating resource / removing resource when Active data Service will stop and start.
5-Build the Active Data Event which send to client to update the UI screen.
6-A class which will trigger change event.
Implementation Step
Step 1:First i have created GraphData class which represent Graph Data for bar graph.Code is following
Step2:After that i have create a class which has List of Graph Data object.Later i have created the data control for same class and i have drag and drop list from data control into page as bar graph.
Create the data control for this class and then drag and drop graphData List as bar graph.
Step 3:Till here it is just same as what we used to do for other component.Next step to create the data model for graph component which we need to associate with pages.
Step4:For creating Model class for bar graph we do require to extends ActiveDataModelDecorator class which has following two abstract methods which we need to override .
1-public abstract oracle.adf.view.faces.bi.model.DataModel getModel() { }
2-public abstract oracle.adf.view.rich.model.ActiveDataModel getActiveDataModel() { }
In getModel() need to return model which i have created above.
And getActiveDataModel need to return instance of the Active Data model.
My class name is GraphActiveCollectionModelDecorator and code is following
Model is ready and need to use in jspx page.
Step5:Above getActiveDataModel () method need to return our active data model reference hence i have create one class where i extends BaseActiveDataModel class and code is following
Step 6:Need to register Model class in adfc-confg.xml file with session scope.
Step7:Jspx code is following
Image is following
In GraphActiveCollectionModelDecorator class i have written code for insert/update/delete build event which you can use for inserting ,updating and deleting the data value from graph.
Link is following :
1-https://docs.google.com/open?id=0B8cP4jZuxLlXN1VFQXlKUlZZbDg
PS:In Example i am just adding new data in bar graph but in Testing Class which is responsible for calling adding/updating/deleting method where you can change with updating and deleting method calling.In Updating and Deleting case it do require to pass correct row key.For any bar the row key will column number and zero for series since here i have only one series hence i am passing 1,0 as key value.
Thanks
Prateek
In this post i would like to show how dvt component will work with active data service.Since there is no example available on internet therefore might it will very useful for reader.
Use Case: Bar Graph integration with Active Data Service.
The Importance Step for implementing Active Data Service are following.
1-Need to provide configuration for Active Data Service adf-config.xml
2-Create the Model for Active Data Service.
In Graph context the model will have following data structure.
1-Serial Name.
2-Column Name.
3-Data Point.
3-Extend ActiveDataModelDecorator class to register Graph Model into Active Data Serive
4-Extend BaseActiveDataModel class to fire change event and use for creating resource / removing resource when Active data Service will stop and start.
5-Build the Active Data Event which send to client to update the UI screen.
6-A class which will trigger change event.
Implementation Step
Step 1:First i have created GraphData class which represent Graph Data for bar graph.Code is following
package com.blog.dvt.adf.graph.data;
public class GraphData {
public GraphData() {
super();
}
private int id;
private String columnName;
private double dataPoint;
public GraphData(int id, String columnName, double dataPoint) {
super();
this.id = id;
this.columnName = columnName;
this.dataPoint = dataPoint;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getColumnName() {
return columnName;
}
public void setDataPoint(double dataPoint) {
this.dataPoint = dataPoint;
}
public double getDataPoint() {
return dataPoint;
}
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (!(object instanceof GraphData)) {
return false;
}
final GraphData other = (GraphData)object;
if (id != other.id) {
return false;
}
if (!(columnName == null ? other.columnName == null :
columnName.equals(other.columnName))) {
return false;
}
if (Double.compare(dataPoint, other.dataPoint) != 0) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int PRIME = 37;
int result = 1;
result = PRIME * result + id;
result =
PRIME * result + ((columnName == null) ? 0 : columnName.hashCode());
long temp = Double.doubleToLongBits(dataPoint);
result = PRIME * result + (int)(temp ^ (temp >>> 32));
return result;
}
}
public class GraphData {
public GraphData() {
super();
}
private int id;
private String columnName;
private double dataPoint;
public GraphData(int id, String columnName, double dataPoint) {
super();
this.id = id;
this.columnName = columnName;
this.dataPoint = dataPoint;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getColumnName() {
return columnName;
}
public void setDataPoint(double dataPoint) {
this.dataPoint = dataPoint;
}
public double getDataPoint() {
return dataPoint;
}
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (!(object instanceof GraphData)) {
return false;
}
final GraphData other = (GraphData)object;
if (id != other.id) {
return false;
}
if (!(columnName == null ? other.columnName == null :
columnName.equals(other.columnName))) {
return false;
}
if (Double.compare(dataPoint, other.dataPoint) != 0) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int PRIME = 37;
int result = 1;
result = PRIME * result + id;
result =
PRIME * result + ((columnName == null) ? 0 : columnName.hashCode());
long temp = Double.doubleToLongBits(dataPoint);
result = PRIME * result + (int)(temp ^ (temp >>> 32));
return result;
}
}
Step2:After that i have create a class which has List of Graph Data object.Later i have created the data control for same class and i have drag and drop list from data control into page as bar graph.
package com.blog.dvt.adf.graph.model;
import com.blog.dvt.adf.graph.data.GraphData;
import com.blog.dvt.adf.graphinterface.GraphInterface;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
public class GraphModel {
public GraphModel() {
super();
}
private List<GraphData> graphData;
public void setGraphData(List<GraphData> grpahData) {
this.graphData = grpahData;
}
public List<GraphData> getGraphData() {
if (graphData == null) {
graphData = getDataForBarGraph();
}
return graphData;
}
private static List<GraphData> getDataForBarGraph() {
List<GraphData> loadDataList = new ArrayList<GraphData>();
GraphData g1 = new GraphData(0, "employee1", 10.0);
GraphData g2 = new GraphData(1, "employee2", 20.0);
GraphData g3 = new GraphData(2, "employee3", 30.0);
GraphData g4 = new GraphData(3, "employee4", 40.0);
GraphData g5 = new GraphData(4, "employee5", 50.0);
loadDataList.add(g1);
loadDataList.add(g2);
loadDataList.add(g3);
loadDataList.add(g4);
loadDataList.add(g5);
return loadDataList;
}
}
import com.blog.dvt.adf.graph.data.GraphData;
import com.blog.dvt.adf.graphinterface.GraphInterface;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
public class GraphModel {
public GraphModel() {
super();
}
private List<GraphData> graphData;
public void setGraphData(List<GraphData> grpahData) {
this.graphData = grpahData;
}
public List<GraphData> getGraphData() {
if (graphData == null) {
graphData = getDataForBarGraph();
}
return graphData;
}
private static List<GraphData> getDataForBarGraph() {
List<GraphData> loadDataList = new ArrayList<GraphData>();
GraphData g1 = new GraphData(0, "employee1", 10.0);
GraphData g2 = new GraphData(1, "employee2", 20.0);
GraphData g3 = new GraphData(2, "employee3", 30.0);
GraphData g4 = new GraphData(3, "employee4", 40.0);
GraphData g5 = new GraphData(4, "employee5", 50.0);
loadDataList.add(g1);
loadDataList.add(g2);
loadDataList.add(g3);
loadDataList.add(g4);
loadDataList.add(g5);
return loadDataList;
}
}
Create the data control for this class and then drag and drop graphData List as bar graph.
Step 3:Till here it is just same as what we used to do for other component.Next step to create the data model for graph component which we need to associate with pages.
Step4:For creating Model class for bar graph we do require to extends ActiveDataModelDecorator class which has following two abstract methods which we need to override .
1-public abstract oracle.adf.view.faces.bi.model.DataModel getModel() { }
2-public abstract oracle.adf.view.rich.model.ActiveDataModel getActiveDataModel() { }
In getModel() need to return model which i have created above.
And getActiveDataModel need to return instance of the Active Data model.
My class name is GraphActiveCollectionModelDecorator and code is following
package com.blog.dvt.adf.decorator;
import com.blog.dvt.adf.activemodel.GraphActiveModelClass;
import com.blog.dvt.adf.activemodel.testing.TestingClass;
import com.blog.dvt.adf.graph.data.GraphData;
import com.blog.dvt.adf.graph.model.GraphModel;
import com.blog.dvt.adf.graphinterface.GraphInterface;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.faces.bi.model.ActiveDataModelDecorator;
import oracle.adf.view.faces.bi.model.DataModel;
import oracle.adf.view.rich.event.ActiveDataEntry;
import oracle.adf.view.rich.event.ActiveDataUpdateEvent;
import oracle.adf.view.rich.model.ActiveDataModel;
import oracle.adfinternal.view.faces.activedata.ActiveDataEventUtil;
import oracle.adfinternal.view.faces.dvt.model.binding.graph.FacesGraphBinding;
import oracle.dss.util.DataSource;
public class GraphActiveCollectionModelDecorator extends ActiveDataModelDecorator implements GraphInterface {
private GraphActiveModelClass _activeDataModel =
new GraphActiveModelClass();
private DataModel graphData = null;
private oracle.dss.dataView.LocalXMLDataSource ds = null;
int i = 5;
int id = 5;
int dataPoint = 40;
public GraphActiveCollectionModelDecorator() {
super();
}
public ActiveDataModel getActiveDataModel() {
return _activeDataModel;
}
public DataModel getModel() {
if (graphData == null) {
DCBindingContainer dcBindings =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
FacesGraphBinding treeData =
(FacesGraphBinding)dcBindings.getControlBinding("graphData");
graphData = treeData.getDataModel();
DCBindingContainer bc =
(DCBindingContainer)FacesContext.getCurrentInstance().getApplication().evaluateExpressionGet(FacesContext.getCurrentInstance(),
"#{bindings}",
Object.class);
TestingClass cc = (TestingClass)getBean();
cc.setListener(this);
}
return graphData;
}
public void addingData() {
GraphActiveModelClass asm = _activeDataModel;
asm.graphDataChange();
ActiveDataUpdateEvent event =
ActiveDataEventUtil.buildActiveDataUpdateEvent(ActiveDataEntry.ChangeType.INSERT_AFTER,
asm.getCurrentChangeCount(),
new Object[] { i },
new Object[] { null },
new String[] { "id",
"columnName",
"dataPoint" },
new Object[] { id,
"testing",
dataPoint }); // the payload for the above attribute
asm.notifyDataChange(event);
id = id + 1;
i = i + 1;
dataPoint = dataPoint + 10;
}
public void updatingData() {
GraphActiveModelClass asm = _activeDataModel;
asm.graphDataChange();
ActiveDataUpdateEvent event =
ActiveDataEventUtil.buildActiveDataUpdateEvent(ActiveDataEntry.ChangeType.UPDATE,
asm.getCurrentChangeCount(),
new Object[] { 1,
0 },
new Object[] { null },
new String[] { "dataPoint" },
new Object[] { 40 }); // the payload for the above attribute
asm.notifyDataChange(event);
}
public void deletiangData() {
GraphActiveModelClass asm = _activeDataModel;
asm.graphDataChange();
ActiveDataUpdateEvent event =
ActiveDataEventUtil.buildActiveDataUpdateEvent(ActiveDataEntry.ChangeType.REMOVE,
asm.getCurrentChangeCount(),
new Object[] { 1,
0 },
new Object[] { null },
new String[] { "id",
"columnName",
"dataPoint" },
new Object[] { 0,
"testing",
10.0 }); // the payload for the above attribute
asm.notifyDataChange(event);
}
public Object getBean() {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
ValueBinding binding = app.createValueBinding("#{testingClass}");
Object value = binding.getValue(context);
return value;
}
}
import com.blog.dvt.adf.activemodel.GraphActiveModelClass;
import com.blog.dvt.adf.activemodel.testing.TestingClass;
import com.blog.dvt.adf.graph.data.GraphData;
import com.blog.dvt.adf.graph.model.GraphModel;
import com.blog.dvt.adf.graphinterface.GraphInterface;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.view.faces.bi.model.ActiveDataModelDecorator;
import oracle.adf.view.faces.bi.model.DataModel;
import oracle.adf.view.rich.event.ActiveDataEntry;
import oracle.adf.view.rich.event.ActiveDataUpdateEvent;
import oracle.adf.view.rich.model.ActiveDataModel;
import oracle.adfinternal.view.faces.activedata.ActiveDataEventUtil;
import oracle.adfinternal.view.faces.dvt.model.binding.graph.FacesGraphBinding;
import oracle.dss.util.DataSource;
public class GraphActiveCollectionModelDecorator extends ActiveDataModelDecorator implements GraphInterface {
private GraphActiveModelClass _activeDataModel =
new GraphActiveModelClass();
private DataModel graphData = null;
private oracle.dss.dataView.LocalXMLDataSource ds = null;
int i = 5;
int id = 5;
int dataPoint = 40;
public GraphActiveCollectionModelDecorator() {
super();
}
public ActiveDataModel getActiveDataModel() {
return _activeDataModel;
}
public DataModel getModel() {
if (graphData == null) {
DCBindingContainer dcBindings =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
FacesGraphBinding treeData =
(FacesGraphBinding)dcBindings.getControlBinding("graphData");
graphData = treeData.getDataModel();
DCBindingContainer bc =
(DCBindingContainer)FacesContext.getCurrentInstance().getApplication().evaluateExpressionGet(FacesContext.getCurrentInstance(),
"#{bindings}",
Object.class);
TestingClass cc = (TestingClass)getBean();
cc.setListener(this);
}
return graphData;
}
public void addingData() {
GraphActiveModelClass asm = _activeDataModel;
asm.graphDataChange();
ActiveDataUpdateEvent event =
ActiveDataEventUtil.buildActiveDataUpdateEvent(ActiveDataEntry.ChangeType.INSERT_AFTER,
asm.getCurrentChangeCount(),
new Object[] { i },
new Object[] { null },
new String[] { "id",
"columnName",
"dataPoint" },
new Object[] { id,
"testing",
dataPoint }); // the payload for the above attribute
asm.notifyDataChange(event);
id = id + 1;
i = i + 1;
dataPoint = dataPoint + 10;
}
public void updatingData() {
GraphActiveModelClass asm = _activeDataModel;
asm.graphDataChange();
ActiveDataUpdateEvent event =
ActiveDataEventUtil.buildActiveDataUpdateEvent(ActiveDataEntry.ChangeType.UPDATE,
asm.getCurrentChangeCount(),
new Object[] { 1,
0 },
new Object[] { null },
new String[] { "dataPoint" },
new Object[] { 40 }); // the payload for the above attribute
asm.notifyDataChange(event);
}
public void deletiangData() {
GraphActiveModelClass asm = _activeDataModel;
asm.graphDataChange();
ActiveDataUpdateEvent event =
ActiveDataEventUtil.buildActiveDataUpdateEvent(ActiveDataEntry.ChangeType.REMOVE,
asm.getCurrentChangeCount(),
new Object[] { 1,
0 },
new Object[] { null },
new String[] { "id",
"columnName",
"dataPoint" },
new Object[] { 0,
"testing",
10.0 }); // the payload for the above attribute
asm.notifyDataChange(event);
}
public Object getBean() {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
ValueBinding binding = app.createValueBinding("#{testingClass}");
Object value = binding.getValue(context);
return value;
}
}
Model is ready and need to use in jspx page.
Step5:Above getActiveDataModel () method need to return our active data model reference hence i have create one class where i extends BaseActiveDataModel class and code is following
package com.blog.dvt.adf.activemodel;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import oracle.adf.view.rich.activedata.BaseActiveDataModel;
import oracle.adf.view.rich.event.ActiveDataUpdateEvent;
public class GraphActiveModelClass extends BaseActiveDataModel {
protected void startActiveData(Collection<Object> rowKeys,
int startChangeCount) {
_listenerCount.incrementAndGet();
}
@Override
protected void stopActiveData(Collection<Object> rowKeys) {
}
@Override
public int getCurrentChangeCount() {
return changeCounter.get();
}
public void graphDataChange() {
changeCounter.incrementAndGet();
}
public void notifyDataChange(ActiveDataUpdateEvent event) {
fireActiveDataUpdate(event);
}
private final AtomicInteger changeCounter = new AtomicInteger();
private final AtomicInteger _listenerCount = new AtomicInteger(0);
}
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import oracle.adf.view.rich.activedata.BaseActiveDataModel;
import oracle.adf.view.rich.event.ActiveDataUpdateEvent;
public class GraphActiveModelClass extends BaseActiveDataModel {
protected void startActiveData(Collection<Object> rowKeys,
int startChangeCount) {
_listenerCount.incrementAndGet();
}
@Override
protected void stopActiveData(Collection<Object> rowKeys) {
}
@Override
public int getCurrentChangeCount() {
return changeCounter.get();
}
public void graphDataChange() {
changeCounter.incrementAndGet();
}
public void notifyDataChange(ActiveDataUpdateEvent event) {
fireActiveDataUpdate(event);
}
private final AtomicInteger changeCounter = new AtomicInteger();
private final AtomicInteger _listenerCount = new AtomicInteger(0);
}
Step 6:Need to register Model class in adfc-confg.xml file with session scope.
<?xml version="1.0" encoding="windows-1252" ?>
<adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2">
<managed-bean id="__1">
<managed-bean-name id="__4">graphActive</managed-bean-name>
<managed-bean-class id="__2">com.blog.dvt.adf.decorator.GraphActiveCollectionModelDecorator</managed-bean-class>
<managed-bean-scope id="__3">session</managed-bean-scope>
</managed-bean>
<managed-bean id="__8">
<managed-bean-name id="__7">testingClass</managed-bean-name>
<managed-bean-class id="__5">com.blog.dvt.adf.activemodel.testing.TestingClass</managed-bean-class>
<managed-bean-scope id="__6">session</managed-bean-scope>
<managed-property id="__13">
<property-name id="__12">listener</property-name>
<value id="__14">#{graphActive}</value>
</managed-property>
</managed-bean>
</adfc-config>
<adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2">
<managed-bean id="__1">
<managed-bean-name id="__4">graphActive</managed-bean-name>
<managed-bean-class id="__2">com.blog.dvt.adf.decorator.GraphActiveCollectionModelDecorator</managed-bean-class>
<managed-bean-scope id="__3">session</managed-bean-scope>
</managed-bean>
<managed-bean id="__8">
<managed-bean-name id="__7">testingClass</managed-bean-name>
<managed-bean-class id="__5">com.blog.dvt.adf.activemodel.testing.TestingClass</managed-bean-class>
<managed-bean-scope id="__6">session</managed-bean-scope>
<managed-property id="__13">
<property-name id="__12">listener</property-name>
<value id="__14">#{graphActive}</value>
</managed-property>
</managed-bean>
</adfc-config>
Step7:Jspx code is following
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
xmlns:dvt="http://xmlns.oracle.com/dss/adf/faces">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<f:view>
<af:document id="d1">
<af:messages id="m1"/>
<af:form id="f1">
<dvt:barGraph id="barGraph1" value="#{graphActive}"
subType="BAR_VERT_CLUST" shortDesc="testing">
<dvt:background>
<dvt:specialEffects/>
</dvt:background>
<dvt:graphPlotArea/>
<dvt:seriesSet>
<dvt:series/>
</dvt:seriesSet>
<dvt:o1Axis/>
<dvt:y1Axis/>
<dvt:legendArea automaticPlacement="AP_NEVER"/>
</dvt:barGraph>
</af:form>
</af:document>
</f:view>
</jsp:root>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:af="http://xmlns.oracle.com/adf/faces/rich"
xmlns:dvt="http://xmlns.oracle.com/dss/adf/faces">
<jsp:directive.page contentType="text/html;charset=UTF-8"/>
<f:view>
<af:document id="d1">
<af:messages id="m1"/>
<af:form id="f1">
<dvt:barGraph id="barGraph1" value="#{graphActive}"
subType="BAR_VERT_CLUST" shortDesc="testing">
<dvt:background>
<dvt:specialEffects/>
</dvt:background>
<dvt:graphPlotArea/>
<dvt:seriesSet>
<dvt:series/>
</dvt:seriesSet>
<dvt:o1Axis/>
<dvt:y1Axis/>
<dvt:legendArea automaticPlacement="AP_NEVER"/>
</dvt:barGraph>
</af:form>
</af:document>
</f:view>
</jsp:root>
Image is following
In GraphActiveCollectionModelDecorator class i have written code for insert/update/delete build event which you can use for inserting ,updating and deleting the data value from graph.
Link is following :
1-https://docs.google.com/open?id=0B8cP4jZuxLlXN1VFQXlKUlZZbDg
PS:In Example i am just adding new data in bar graph but in Testing Class which is responsible for calling adding/updating/deleting method where you can change with updating and deleting method calling.In Updating and Deleting case it do require to pass correct row key.For any bar the row key will column number and zero for series since here i have only one series hence i am passing 1,0 as key value.
Thanks
Prateek
Hi
ReplyDeleteCan you please explain me what does this code mean ?
(DCBindingContainer)FacesContext.getCurrentInstance().getApplication().evaluateExpressionGet(FacesContext.getCurrentInstance(),
"#{bindings}",
Object.class);
What does Object.class do ?
This comment has been removed by the author.
ReplyDelete