Skip to main content

ADF Webservice data control HACK: DONT TRY THIS AT HOME

I really wanted to be able to do things with my ADF service implementation based web service data control:

1) Change configuration of the wsdl location dynamically for each environment.
2) Make sure that I could add to the results of the web service with ADF. (createNewRow)

The first point: This is hard to do because the url is looked up from the bindings and wsdl. You could do this at build time and build an artifact for each environment but I didnt want to.

The second point: The result created by the ws data control was an XMLHandler$DataCollection wich did not implement the add method - I got a Not supported exception.

So here is the solution very ugly but hey well (this is a work in progress so shout if you want more code/ explanation):

1) Override the SOAPProvider in the DataControls.dcx

<DataControlConfigs xmlns="http://xmlns.oracle.com/adfm/configuration"
  version="11.1.2.60.17" id="DataControls"
  Package="za.co.transcode.adf.prj.model">
  <AdapterDataControl id="YourModelService" 
    FactoryClass="oracle.adf.model.adapter.DataControlFactoryImpl"
    ImplDef="oracle.adfinternal.model.adapter.webservice.WSDefinition"
    SupportsTransactions="false"
    SupportsSortCollection="true" SupportsResetState="true"
    SupportsRangesize="true"
    SupportsFindMode="true" SupportsUpdates="true"
    Definition="za.co.transcode.adf.prj.model.YourModelService"
    BeanClass="za.co.transcode.adf.prj.model.YourModelService"
    xmlns="http://xmlns.oracle.com/adfm/datacontrol">
    <Source>
      <definition xmlns="http://xmlns.oracle.com/adfm/adapter/webservice" 
      name="YourModelService" version="1.0"
      provider="za.co.transcode.adf.prj.model.YourSOAPProvider"
      wsdl="http://127.0.0.1:7101/Your-Model-context-root/YourAppModuleService?WSDL"
      UsePersistedStructure="false"> 



2) Implement your own SOAP Provider link this one.


package za.co.transcode.adf.prj.model;

import java.net.URL;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import oracle.adf.model.adapter.AdapterException;
import oracle.adf.model.connection.webservice.api.WebServiceConnection;
import oracle.adf.model.connection.webservice.impl.WebServiceConnectionImpl;

import oracle.adfinternal.model.adapter.webservice.WSOperation;
import oracle.adfinternal.model.adapter.webservice.provider.WSProvider;
import oracle.adfinternal.model.adapter.webservice.provider.soap.SOAPProvider;
import oracle.adfinternal.model.adapter.webservice.resource.WSDCResource;
import oracle.adfinternal.model.adapter.webservice.resource.WSDCResourceBundle;

import oracle.j2ee.ws.model.soap.SoapPortImpl;

import oracle.webservices.model.Operation;
import oracle.webservices.model.Port;
import oracle.webservices.model.soap.SoapPort;


public class YourSOAPProvider extends SOAPProvider {
  Map mOperations;

  public YourSOAPProvider() {

    super();
  }

  public void init(Map map) {

    super.init(map);
    mOperations = (Map) map.get(WSProvider.PROVIDER_OPERATIONS);
    WebServiceConnection conn = (WebServiceConnection) map.get(WSProvider.PROVIDER_CONNECTION);
    try {
//@TODO look this up from an external propeties file
      ((WebServiceConnectionImpl) conn).setWsdlUrl(
new URL(
"http://10.10.10.10:7001/Your-Model-context-root/YourAppModuleService?WSDL"));
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
  private WSOperation findServiceOperation(String operationName) throws AdapterException {
    Iterator portIterator = mOperations.keySet().iterator();
    while(portIterator.hasNext()) {
      String portName = (String) portIterator.next();
      List operations = (List) mOperations.get(portName);
      Iterator operationIter = operations.iterator();
      while(operationIter.hasNext()) {
        WSOperation operation = (WSOperation) operationIter.next();
        if(operation.getName().equalsIgnoreCase(operationName))
          return operation;
      }
    }

    // Fatal Error if the operation to invoke could not be found
    throw new AdapterException(WSDCResourceBundle.class, WSDCResource.ERR_NOSUCH_OPERATION,
        new Object[] { operationName });
  }

  public void setUrl(Port port, String url) {
    SoapPort soap = getSoapExt(port);
    if(soap == null) {
      soap = new SoapPortImpl();
      port.addExtension(SoapPort.EXTENSION_TYPE, soap);
    }
    soap.setAddressUrl(url);
  }

  protected static SoapPort getSoapExt(Port port) {
    return (SoapPort) port.getExtension(SoapPort.EXTENSION_TYPE);
  }

  public Object execute(String string, Map map) throws AdapterException {
    Operation oper = findServiceOperation(string).getOperation();
//@TODO look this up from an external propeties file
    setUrl(oper.getPort(), "http://10.10.10.10:7001/Your-Model-context-root/YourAppModuleService");
    Object resp = super.execute(string, map);
    if(resp != null && resp instanceof Collection && ((Collection) resp).size() > 0) {
      Object resp1 = (Object) ((Collection) resp).iterator().next();
      if(resp1 != null && resp1 instanceof Map && ((Map) resp1).size() > 0) {
        Object key = ((Map) resp1).keySet().iterator().next();
        Object resp2 = ((Map) resp1).get(key);
        if(resp2 != null && resp2 instanceof Collection && ((Collection) resp2).size() > 0) {
          Collection replacedCollection = replaceCollection((Collection) resp2);
          ((Map) resp1).put(key, replacedCollection);
          Object resp3 = ((Collection) resp2).iterator().next();
        }
      }
    }
    return resp;
  }

  private Collection replaceCollection(Collection objects) {
    return new ArrayList(objects);
  }
}



NB: remember we may have to do this all again when a new release happens so it is better to follow the tried and tested solutions to these problems.

1) http://forums.oracle.com/forums/thread.jspa?messageID=9393754&tstart=0
2) Use two iterators (based on create for the form and the findresults for the other) instead of writing to the results. Or create two screens a search and a create screen.

Comments

Popular posts from this blog

ADF sort of generic screen for tables with the same structure

We have a couple (about a hundred) of tables with the same structure (Code, Description, Create Date, Update Date). So I wanted to do something simple so that I did not have to create all these screens 1) EO   I created the EO based on one of the tables I had that had the above columns. I then Added a transient attribute called table name to my EO based on a groovy expression. (the expression needs to change as I am reading web tier stuff from the model layer but I will fix this later) I then generated a java class for my EO. And added the following overriden method to my newly created java class. protected StringBuffer buildDMLStatement(int i, AttributeDefImpl[] attributeDefImpl,   AttributeDefImpl[] attributeDefImpl2, AttributeDefImpl[] attributeDefImpl3, boolean b) {   StringBuffer statement = super.buildDMLStatement(   i, attributeDefImpl, attributeDefImpl2, attributeDefImpl3, b); return new StringBuffer(StringUtils.replace(statement.to...

Util code

public static MethodExpression getMethodExpression( String expr, Class returnType, Class[] argTypes){ FacesContext fc = FacesContext.getCurrentInstance(); ELContext elctx = fc.getELContext(); ExpressionFactory elFactory = fc.getApplication().getExpressionFactory(); return elFactory.createMethodExpression( elctx, expr, returnType, argTypes); } public static javax.faces.el.MethodBinding getMethodBinding( String expr, Class[] argTypes){ FacesContext fc = FacesContext.getCurrentInstance(); ELContext elctx = fc.getELContext(); return fc.getApplication().createMethodBinding(expr, argTypes); } SetPropertyListener listener = new SetPropertyListener( ActionEvent.class.getName()); listener.setFrom(link.getRoute()); listener.setValueExpression("to", JSFUtils.getValueExpression("#{pageFlowScope.route}", String.class)); action.addActionListener(listener); AdfFacesContext.getCurrentInstance().getPageFlowScope() .put("route", lin...

MANIFEST.MF merge JDeveloper for an executable jar

Goto your project > properties. Then click on deployment in the menu. Edit or add a jar deployment profile. Fill in the details under jar options (select Include manifest and give it a main class name) Also remember that the merge functionality only works with a BLANK line at the end of the merge file. REALLY this caught me. My merge file contents: Class-Path: commons-codec-1.3.jar [...empty line here CRLF...]