Saturday, 21 July 2012

iBatis Code FLow

JAR :  2.3.4

package mybatis;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class IbatisRead
{
  public static void main(String[] args)
   throws IOException,SQLException{
   Reader rd = Resources.getResourceAsReader("mybatis\\SqlMapConfig.xml");
   SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);

  
    /* This would insert one record in Employee table. */
   System.out.println("Going to insert record.....");
   A_HARRY em1 = new A_HARRY("Farah", "Ali", 5000);

   smc.insert("ServiceRequestLog.insert", em1);

   System.out.println("Record Inserted Successfully ");

   System.out.println("Records Read Successfully ");

  
   /* This would read all records from the Employee table. */
   System.out.println("Going to read records.....");
   List <A_HARRY> ems = (List<A_HARRY>) smc.queryForList("ServiceRequestLog.getAll", null);
   A_HARRY em = null;
   for (A_HARRY e : ems)
   {
      System.out.print("  " + e.getId());
      System.out.print("  " + e.getFirstName());
      System.out.print("  " + e.getLastName());
      System.out.print("  " + e.getSalary());
      em = e;
      System.out.println("");
      System.out.println("");
   }   
  
   A_HARRY rec = new A_HARRY();
   rec.setId(1);
   rec.setFirstName("Roma");
   smc.update("ServiceRequestLog.update", rec );
   System.out.println("Record updated Successfully ");

   System.out.println("Going to read records.....");
   ems = (List<A_HARRY>) smc.queryForList("ServiceRequestLog.getAll", null);
   A_HARRY em2 = null;
   for (A_HARRY e : ems)
   {
      System.out.print("  " + e.getId());
      System.out.print("  " + e.getFirstName());
      System.out.print("  " + e.getLastName());
      System.out.print("  " + e.getSalary());
      em2 = e;
      System.out.println("");
      System.out.println("");
   }   
  
  
  }



______________________


package mybatis;

public class A_HARRY {

    private int id;
    private String first_name;
    private String last_name;
    private int salary;

    public A_HARRY() {
    }

    public A_HARRY(String fname, String lname, int salary) {
        this.first_name = fname;
        this.last_name = lname;
        this.salary = salary;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return first_name;
    }

    public void setFirstName(String fname) {
        this.first_name = fname;
    }

    public String getLastName() {
        return last_name;
    }

    public void setlastName(String lname) {
        this.last_name = lname;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }
}



_______________________________


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
     <settings useStatementNamespaces="true"/>
     <transactionManager type="JDBC">
        <dataSource type="SIMPLE">
           
          <property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
          <property name="JDBC.ConnectionURL"
               value="jdbc:oracle:thin:@192.168.5.13:1521:veqa"/>
          <property name="JDBC.Username" value="een"/>
          <property name="JDBC.Password" value="en123"/>
        </dataSource>
      </transactionManager>
     <sqlMap resource="mybatis\ServiceRequestLog.xml"/>
</sqlMapConfig>





______________________________________




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="ServiceRequestLog">
   
    <select id="getAll" resultClass="mybatis.A_HARRY">
        SELECT * FROM A_HARRY
    </select>
   
    <insert id="insert" parameterClass="mybatis.A_HARRY">
        <selectKey keyProperty="id" resultClass="int">
            SELECT EXAMPLE_ID_SEQ.NEXTVAL as val from dual
        </selectKey>
        insert into A_HARRY(id,first_name, last_name, salary)
        values (#id#,#first_name#, #last_name#, #salary#)
    </insert>
   
    <update id="update" parameterClass="mybatis.A_HARRY">
        UPDATE A_HARRY
        SET    first_name = #first_name#
        WHERE  id = #id#
    </update>
   
   
   
</sqlMap>

Tuesday, 17 July 2012

How to use Soap Handler Class to get Web Service Response?


import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.xml.sax.InputSource;

import pack.DefineStaticVariables;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import org.w3c.dom.Document;

public class WebServiceResponseHandler implements SOAPHandler<SOAPMessageContext>
{
    public static String getSOAPResponse;
    public static String outputSoapMsg;
    DBCustom objDBCustom =null;
   
    public WebServiceResponseHandler()
    {
        objDBCustom = DefineStaticVariables.objDBCustom;
    }
   
    public void init(Map mapCollection)
    {
        objDBCustom.logDebugInfo("In init function..");
    }

    public boolean handleMessage(SOAPMessageContext messageContext)     {
        objDBCustom.logDebugInfo("In handleMessage() function..");
        getRequestResponse(messageContext);
        return true;
    }

    public boolean handleFault(SOAPMessageContext messageContext) {
        objDBCustom.logDebugInfo("In handleFault() function..");
        getRequestResponse(messageContext);
        return true;
    }

   
    public void close(MessageContext messageContext) {
        objDBCustom.logDebugInfo("In close() function..");
    }
   
    public void destroy() {
        objDBCustom.logDebugInfo("In destroy() function..");
    }
    public void getRequestResponse(SOAPMessageContext messageContext)
    {       
        objDBCustom.logDebugInfo("In getRequestResponse() function..");
        SOAPMessage objSOAPMessage = null;
        ByteArrayOutputStream objByteArrayOS =null;
        Boolean outboundProperty;
        Document objDocument = null;
        InputSource objInputSource =null;
        DocumentBuilderFactory documentBuilderFactory = null;
        DocumentBuilder documentBuilder =null;
        OutputFormat outFormat = null;
        Writer streamWriter = null;
        XMLSerializer serializeXML = null;
       
         try
        {
           
            objSOAPMessage = messageContext.getMessage();
           
            objByteArrayOS = new ByteArrayOutputStream();
            objSOAPMessage.writeTo(objByteArrayOS);
            outboundProperty = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
           
            if (!outboundProperty.booleanValue())
            {
                documentBuilderFactory = DocumentBuilderFactory.newInstance();
                documentBuilder = documentBuilderFactory.newDocumentBuilder();
                objInputSource = new InputSource(new StringReader(objByteArrayOS.toString()));
                objDocument = documentBuilder.parse(objInputSource);
                outFormat = new OutputFormat(objDocument);
                outFormat.setIndenting(true);
                outFormat.setIndent(2);
               
                streamWriter = new StringWriter();
                serializeXML = new XMLSerializer(streamWriter, outFormat);
                serializeXML.serialize(objDocument);
                getSOAPResponse = streamWriter.toString();
                objDBCustom.logDebugInfo("Web Service Response : " + getSOAPResponse);
               
                if(!getSOAPResponse.equals(""))
                {
                           myDBCustom.gResponseData.append(getSOAPResponse);
                }
                else
                     objDBCustom.logDebugInfo("Unable to get response Data");
            }
            else
            {
                 objDBCustom.logDebugInfo("In getRequestResponse() function: Web Service request sent");
            }
       }   
       catch (Exception errorException)
       {
            objDBCustom.logErrorInfo("Exception in logging handler: " + errorException.toString());           
       }
    }
}


Interface:  javax.xml.ws.handler.soap.SOAPMessageContext;
The interface SOAPMessageContext provides access to the SOAP message for either RPC request or response.

Implementation Class: com.sun.xml.internal.ws.handler.SOAPMessageContextImpl



In XML FILE:

<handler-chains xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee">
    <handler-chain>
        <handler>
            <handler-name>WebServiceResponseHandler</handler-name>
            <handler-class>MasterProcess.WebServiceResponseHandler</handler-class>
        </handler>
    </handler-chain>
</handler-chains>

In Main Service File
@HandlerChain(file = "../Conf/ChainHandle.xml")
public class WSObjectContext extends Service
{
}

@Handler Chain, Associates the Web Service with an externally defined handler chain.