Monday, August 8, 2011

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.

No comments:

Post a Comment