El nexo entre tecnología y negocios
 
[ AHT00096 ] How to execute queries and business classes from a web service
Summary
It is desirable to be able to communicate from an external system with APIA as to establish an interaction, whether it is with the purpose of executing a business class or queries that are configured in APIA.

It is applied to
  • Design
  • Business classes


Procedure

The solution consists of creating a Java class in the external system, in which the necessary elements to establish the communication with APIA´s Web Service (WS) will be found. This class will contain the calling methods and the corresponding APIA´s API methods.

To be able to make the call to a business class (execute Class) and/or a query (execute Query), it is necessary that there is a definite java bussines class and/or a query, respectively, in APIA, which will be called upon when the calling to APIA´s WS is executed.

A java class will be executed from the external system to establish the communication with an APIA, and this class must implement "com.dogma.ws.gen.ApiaWSInterfaceService",for which it is necessary to implement the following methods:

 

public class ApiaWsTester extends org.apache.axis.client.Service implements com.dogma.ws.gen.ApiaWSInterfaceService {

 

  private static final long serialVersionUID = 1L;
  //--- Private attributes --------------------
  private String apiaWSAddress = null;
  private String apiaWsWsddServiceName = null;
    private HashSet ports = new HashSet();

  //--- Constructors --------------------------
  public ApiaWsTester(String wsAddress, String wsName) {
    this.apiaWSAddress = wsAddress;
    this.apiaWsWsddServiceName = wsName;
  }

    //--- Implemented methods -------------------
  public ApiaWSInterface getApiaWS() throws ServiceException {
    URL endpoint;
    try {
      endpoint = new java.net.URL(this.apiaWSAddress);
    } catch (MalformedURLException e) {
      throw new ServiceException(e);
    }
    return getApiaWS(endpoint);
  }

  public ApiaWSInterface getApiaWS(URL portAddress) throws ServiceException {
    try {
      ApiaWSSoapBindingStub stub = new ApiaWSSoapBindingStub(portAddress,this);
      stub.setPortName(this.apiaWsWsddServiceName);
      return stub;
    } catch (AxisFault e) {
      return null;
    }
  }
  public String getApiaWSAddress() {
    return this.apiaWSAddress;
  }

/  /--- Overriden methods ---------------------
  public Remote getPort(Class serviceEndpointInterface) throws ServiceException {
    try {
      if (ApiaWSInterface.class.isAssignableFrom(serviceEndpointInterface)) {
        ApiaWSSoapBindingStub stub = new ApiaWSSoapBindingStub(new URL(this.apiaWSAddress), this);
        stub.setPortName(this.apiaWsWsddServiceName);
        return stub;
      }
    } catch (Throwable t) {
      throw new ServiceException(t);
    }

    throw new ServiceException("There is no stub implementation for the interface: " + (serviceEndpointInterface == null ? "null": serviceEndpointInterface.getName()));
  }

  public Remote getPort(QName portName,Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
    if (portName == null) {
      return getPort(serviceEndpointInterface);
    }
    String inputPortName = portName.getLocalPart();
    if ("ApiaWS".equals(inputPortName)) {
      return getApiaWS();v    } else {
      Remote stub = getPort(serviceEndpointInterface);
      ((Stub) stub).setPortName(portName);
      return stub;
    }
  }

  public QName getServiceName() {
    return new javax.xml.namespace.QName("http://www.apiasolutions.com/WebServices","ApiaWSInterfaceService");
  }

  public Iterator getPorts() {
    if (this.ports == null) {
      this.ports = new java.util.HashSet();
      this.ports.add(new QName("http://www.apiasolutions.com/WebServices", "ApiaWS"));
    }
    return ports.iterator();
  }

  //--- Private methods -----------------------
  private ApiaWSSoapBindingStub connect() throws ServiceException {
    ApiaWSSoapBindingStub binding = null;
    binding = (ApiaWSSoapBindingStub) this.getApiaWS();
    return binding;
  }


The called upon method to establish the calling from the external system to the APIA´s business class:

 

 

//--- Public methods ------------------------
/**
* Parameters:
*   envName - Enviroment Name
*   className - Name of class of business
*   user - User authenticated in WS
*   pwd - Password of user in WS
*/
public boolean executeClass(String envName, String className, String user, String pwd) throws RemoteException , ServiceException {
  ApiaWSSoapBindingStub binding = this.connect();
  binding.setTimeout(60000);

 

    boolean result = false;

//--- Create parameters
  Parameter paramName = new Parameter();
  paramName.setName("USER_NAME");
  paramName.set_value("userTest");

    Parameter paramAddress = new Parameter();
  paramName.setName("USER_ADDRESS");
  paramName.set_value("userTest");

      //--- Set parameters
  Parameters params = new Parameters();
  params.setParameter(new Parameter[] { paramName, paramAddress });

    //--- Create user authenticated in WS
  UserData userData = new UserData();
  userData.setUsrLogin(user);
  userData.setPassword(CryptUtils.makePasswordDigest(user, pwd));

  //--- Execute call to WS
  ClassResult resultWS = binding.executeClass(envName, className, params, userData);
   
    //--- Successful execution
    if (resultWS.getResult().getResultCode() == 0){
      result = true;
    }

    return result;
  }:

 


Java Business class executed when the APIA´s WS is called upon from the external system:

 

 

//--- I collect the data sent by parameter in ws
String userName = this.getParameter("USER_NAME").getValueAsString();
String userAddress = this.getParameter("USER_ADDRESS").getValueAsString();

 

//--- Position with the received data the values of the corresponding attributes
this.getCurrentEntity().getAttribute("ATT_NANE_USER").setValue(userName);
this.getCurrentEntity().getAttribute("ATT_NANE_ADDRESS").setValue(userAddress);

The called upon method to establish the call from the external system to APIA´s query (notice that this method uses the code´s first section described to establish the communication between the external system and APIA), in case the designed query in APIA has fields (filters) the numbers for them should be placed in parameters, taking into consideration that the name assigned to the parameter must correspond to the name of the field in the Query designed by APIA:

//--- Public methods ------------------------
/**
* Parameters:
*   envName - Enviroment Name
*   queryName - Name of query
*   user - User authenticated in WS
*   pwd - Password of user in WS
*/
public void executeQuery(String envName, String queryName, String user, String pwd) throws RemoteException , ServiceException {
  ApiaWSSoapBindingStub binding = this.connect();
  binding.setTimeout(60000);

//--- Create parameters
  Parameter paramName = new Parameter();
  paramName.setName("USER_NAME");
  paramName.set_value("userTest");

  Parameter paramAddress = new Parameter();
  paramName.setName("USER_ADDRESS");
  paramName.set_value("userTest");

  //--- Set parameters
  Parameters params = new Parameters();
  params.setParameter(new Parameter[] { paramName, paramAddress });

  //--- Create user authenticated in WS
  UserData userData = new UserData();
  userData.setUsrLogin(user);
  userData.setPassword(CryptUtils.makePasswordDigest(user, pwd));

  //--- Create user authenticated in WS
  UserData userData = new UserData();
  userData.setUsrLogin(user);
  userData.setPassword(CryptUtils.makePasswordDigest(user, pwd));

  //--- Execute call to WS
  QueryResult resultWS = binding.executeQuery(envName, queryName, params, userData);

  //--- Successful execution
  if (resultWS != null){
    //--- I collect the returned data of the query
    Row[] rowsQuery = resultWS.getQueryData().getRow();
    for(int i = 0; i < rowsQuery.length; i++){
      System.out.println("--- QUERY RESULT ---");
      System.out.println("- " + rowsQuery[i].getCol(1) + " -- " + rowsQuery[i].getCol(2) + " -");
    }
  }
}

 

Results of the execution of this class:

 


--- QUERY RESULT ---
- User1 -- AddressUser1 -
- User2 -- AddressUser2 -


Related elements
[ AHT00030 ] Synchronize two processes through web services
[ ATI00043 ] Error codes returned by the Web Services
Related from
[ AHT00029 ] Sending of files as text
Suggested items
[ AFC00498 ] Utilización de consultas del usuario en los monitores de negocio
[ ATI00184 ] Requirements to publish WebServices from Apia
[ AFC00280 ] BI global parameters
[ AHF00066 ] Possibility to perform user's queries with case insensitive filters
[ ANF00166 ] Cubes' design
[ AFC00522 ] Automatic adjustment of queries filter size
[ AFC00598 ] Regenerate user queries
[ AHF00113 ] Possibility of performing user's queries where a business class specifies the SQL sentence to execute
[ ADF01554 ] Design - Apia user's handbook 2.4
[ AFC00599 ] "See entity" action modification