Sunday, December 9, 2012

Active Data Service With Active Image andActive Output text

Hi

In my last post i have explained about active data with bar graph component that was little bit tricky but for implementing active data service with active image and  active output text are easy to implements.

Implementation of Active Data Service with Active Image :

Step 1:Extends BaseActiveDataModel class and override method.Code is following

package com.blog.example.image.model;

import java.util.ArrayList;
import java.util.Collection;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import java.util.concurrent.atomic.AtomicInteger;

import oracle.adf.view.rich.activedata.ActiveModelContext;
import oracle.adf.view.rich.activedata.BaseActiveDataModel;
import oracle.adf.view.rich.event.ActiveDataEntry;
import oracle.adf.view.rich.event.ActiveDataUpdateEvent;

import oracle.adfinternal.view.faces.activedata.ActiveDataEventUtil;

public class ImageActiveModelClass extends BaseActiveDataModel
{
    private final AtomicInteger changeCounter = new AtomicInteger();
    private static final Timer timer = new Timer();
    private final AtomicInteger counter = new AtomicInteger(0);
    List<String> list = new ArrayList<String>();
    private String image = "/image/graphPage.png";
    static int j = 0;

    public ImageActiveModelClass() {
        super();
        list.add("/image/inde1x.jpg");
        list.add("/image/index.jpg");
        list.add("/image/index2.jpg");
        list.add("/image/index3.jpg");
        list.add("/image/index4.jpg");
        list.add("/image/index7.jpg");
        list.add("/image/index8.jpg");
        list.add("/image/index9.jpg");
        list.add("/image/index10.jpg");
        list.add("/image/index11.jpg");
        ActiveModelContext context =
            ActiveModelContext.getActiveModelContext();
        Object[] keyPath = new String[0];
        context.addActiveModelInfo(this, keyPath, "image");
        timer.schedule(new UpdateTask(), 2000, 2000);
    }

    protected void startActiveData(Collection<Object> collection, int i) {
    }

    protected void stopActiveData(Collection<Object> collection) {
    }

    @Override
    public int getCurrentChangeCount() {
        return changeCounter.get();
    }


    public void setImage(String image) {
        this.image = image;
    }

    public String getImage() {
        return image;
    }

    protected class UpdateTask extends TimerTask {
        public void run() {
            counter.incrementAndGet();
            ActiveDataUpdateEvent event =
                ActiveDataEventUtil.buildActiveDataUpdateEvent(ActiveDataEntry.ChangeType.UPDATE,
                                                               counter.get(),
                                                               new String[0],
                                                               null,
                                                               new String[] { "image" },
                                                               new Object[] { list.get(j) });
            fireActiveDataUpdate(event);
            j = j + 1;
            if (j == 10) {
                j = 0;
            }
        }
    }

}

Step2: For register active image with ads i have written following code

        ActiveModelContext context =
            ActiveModelContext.getActiveModelContext();
        Object[] keyPath = new String[0];
        context.addActiveModelInfo(this, keyPath, "image");

Step3:Register ImageActiveModelClass class in adfc-config.xml 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="__2">
    <managed-bean-name id="__4">imageActive</managed-bean-name>
    <managed-bean-class id="__3">com.blog.example.image.model.ImageActiveModelClass</managed-bean-class>
    <managed-bean-scope id="__1">session</managed-bean-scope>
  </managed-bean>
  <managed-bean id="__6">
    <managed-bean-name id="__8">outPutActive</managed-bean-name>
    <managed-bean-class id="__5">com.blog.example.output.model.OutPutActiveDataModel</managed-bean-class>
    <managed-bean-scope id="__7">session</managed-bean-scope>
  </managed-bean>
</adfc-config>

Step 4: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">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1">
      <af:form id="f1">
        <af:activeImage source="#{imageActive.image}" id="ai1"/>
        <af:activeOutputText id="aot1" value="#{outPutActive.outPut}"/>
      </af:form>
    </af:document>
  </f:view>
</jsp:root>


Same step i have implemented for active out put text also.you can find code at following link.

Link is

https://docs.google.com/open?id=0B8cP4jZuxLlXN2pQYzN6Ql91Vlk


Thank,
Prateek


Saturday, December 8, 2012

Active Data Serivce With Data Visualizations Graph Component (DVT)

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

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;
    }
}

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;
    }


}

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;
    }
}


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);
}

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>

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>

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