Step by Step Guide to Validate EDI ANSI X12 Document using Java Mapping

Java class ValidateX12EDIDocument 

/*
 * Created on Jun 26, 2009
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package validateEDIFile;
/**
 * @author Kubra.Fatima
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
public class ValidateX12EDIDocument implements StreamTransformation
 { 	
   public void setParameter(Map arg0)
   {
   }
   public void execute(InputStream inputStream, OutputStream outputStream)
   throws StreamTransformationException
   {
		 String data="";
		 List segmentArray = new ArrayList();
		// create a DOM parser
		 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		 DocumentBuilder builder = null;
		 try
		 {
			builder = factory.newDocumentBuilder();
		 } 
		 catch (ParserConfigurationException e)
		 {
		 e.printStackTrace();
		 }
	   	 // parse input to create document tree
		 Document doc = null;
		 try
		 {
		 doc = builder.parse(inputStream);
		 }
		 catch (SAXException e1)
		 {
		 e1.printStackTrace();
		 }
		 catch (IOException e1)
		 {
		 e1.printStackTrace();
		 }
		 NodeList EDIString =doc.getElementsByTagName("Data");
		 data=EDIString.item(0).getFirstChild().getNodeValue();
                           // method call to do all the validations
		 doAllValidations(data);
	 
		// create transformer for producing XML output   
	    TransformerFactory tFactory = TransformerFactory.newInstance();
	    Transformer transformer = null;
		try
		{
		transformer = tFactory.newTransformer();
		}
		catch (TransformerConfigurationException e2)
		{
		e2.printStackTrace();
		}
	    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	    // create XML same as source message type
	    Document target =builder.newDocument();
	    Element rootNodeT1 = target.createElement("ns0:MT_Source");
		rootNodeT1.setAttribute(
						 "xmlns:ns0",
						 "http://yash.com/yh1309/EDIFile_2_EDIFile");
		target.appendChild(rootNodeT1);				 
		Element Record = target.createElement("Record");	
		rootNodeT1.appendChild(Record);
		Element Data= target.createElement("Data");
		Text textData=target.createTextNode(data);	
		Data.appendChild(textData);
		Record.appendChild(Data);
	    DOMSource source = new DOMSource(target);
	    StreamResult result = new StreamResult(outputStream);   
	    try
	    {
		transformer.transform(source, result);
		}
		catch (TransformerException e3) 
		{
		e3.printStackTrace();
		}    
	}
 private void doAllValidations(String input) throws StreamTransformationException
 {                    
 	   List segmentArray = new ArrayList();
	   int count=0;
	   StringTokenizer st = new StringTokenizer(input,"~");
	    while (st.hasMoreTokens())
		{
			segmentArray.add(st.nextToken());
		}
		
	   	validateSegmentExistence(segmentArray);
		validateNoOfSegSTSE(segmentArray);
		validateIntCtrlNo(segmentArray);
		validateGrpCtrlNo(segmentArray);
		validateTrnSetCtrlNo(segmentArray);									
	 
  }
private void validateSegmentExistence(List segmentArray)
{
	String flag="";
	int getIndex=0,startIndexGS=2,startIndexST=3, k;
	if(segmentArray.get(0).toString().startsWith("ISA") &&segmentArray.get(segmentArray.size()-1).toString().startsWith("IEA"))
	{
		for (int j=1; j < segmentArray.size()-2; j++)
		{ 
		   if(segmentArray.get(j).toString().startsWith("ISA")||segmentArray.get(j).toString().startsWith("IEA"))
		  throw new RuntimeException("ISA_IEA_DATA_SEGMENT_MISMATCH");
		  else flag="ISAIEATestSuccess";
			
		}
   }
   else 
   throw new RuntimeException("ISA_IEA_DATA_SEGMENT_MISMATCH");
  	
	if(flag.equals("ISAIEATestSuccess"))
	{
		 
		if(segmentArray.get(1).toString().startsWith("GS") &&segmentArray.get(segmentArray.size()-2).toString().startsWith("GE"))
		{
			for (int j=2; j < segmentArray.size(); j++)
			{
				if(segmentArray.get(j).toString().startsWith("GE"))
				getIndex=j;
				if(getIndex!=0)
				{
					for(k=startIndexGS;k<getIndex;k++)
					{ 
						if(segmentArray.get(k).toString().startsWith("GS"))
						throw new RuntimeException("GS_GE_DATA_SEGMENT_MISMATCH");
						else  flag="GSGETestSuccess";
					}
				   j=getIndex+1;
				   startIndexGS=getIndex+2;
				   getIndex=0; 				
				}
			}
		
		}
		else throw new RuntimeException("GS_GE_DATA_SEGMENT_MISMATCH");
	}
	if(flag.equals("GSGETestSuccess"))
	{	
				if(segmentArray.get(2).toString().startsWith("ST") &&segmentArray.get(segmentArray.size()-3).toString().startsWith("SE"))
				{
					for (int j=3; j < segmentArray.size(); j++)
					{
						if(segmentArray.get(j).toString().startsWith("SE"))
						getIndex=j;
						if(getIndex!=0)
						{
							for(k=startIndexST;k<getIndex;k++)
							{ 
								if(segmentArray.get(k).toString().startsWith("ST"))
								throw new RuntimeException("ST_SE_DATA_SEGMENT_MISMATCH");
								else  flag="STSETestSuccess";
							}
						   j=getIndex+1;
						   if(segmentArray.get(getIndex+2).toString().startsWith("GS"))
						   {
							startIndexST=getIndex+4;
						   }
						   else
						   startIndexST=getIndex+2;
						   getIndex=0; 				
						}
					}
		
				}
				else throw new RuntimeException("ST_SE_DATA_SEGMENT_MISMATCH");
	}
}
private void validateNoOfSegSTSE(List segmentArray) 
{
	int indexST=0,indexSE=0;
	String noOfSegment="";
	for (int j=2; j < segmentArray.size(); j++)
	{
		if(segmentArray.get(j).toString().startsWith("ST"))
		{
		indexST=j;
		}
		else if(segmentArray.get(j).toString().startsWith("SE"))
		{
			String[]SE=segmentArray.get(j).toString().split("\\*");
			noOfSegment=SE[1];
			indexSE=j;
		}
		if(indexST!=0 &&indexSE!=0)
		{
			Integer countSegment=new Integer(indexSE-indexST+1);
			if((!noOfSegment.equals(countSegment.toString())))
			throw new RuntimeException("MISMATCH_TOTAL_NUMBER_OF_SEGMENT _SPECIFIED_IN_SE_DATA_SEGMENT");
			indexST= 0;
			indexSE = 0;
		}
	}	
}
private void validateIntCtrlNo(List segmentArray) 
{
	List IEADEArray = new ArrayList();
	List ISADEArray = new ArrayList();
	StringTokenizer st;
	String intCtrlNoISA ="",intCtrlNoIEA="";
	String[] ISA=segmentArray.get(0).toString().split("\\*");
	intCtrlNoISA=ISA[13];
	if(ISA[5].trim().equals(""))
	throw new RuntimeException("ISA05_DATA_ELEMENT_IS_EMPTY");
	else if(ISA[6].trim().equals(""))
	throw new RuntimeException("ISA06_DATA_ELEMENT_IS_EMPTY");
	if(ISA[7].trim().equals(""))
	throw new RuntimeException("ISA07_DATA_ELEMENT_IS_EMPTY");
	if(ISA[8].trim().equals(""))
	throw new RuntimeException("ISA08_DATA_ELEMENT_IS_EMPTY");
	String[]IEA=segmentArray.get(segmentArray.size()-1).toString().split("\\*");
    intCtrlNoIEA=IEA[2];
    if(!(intCtrlNoISA.equals(intCtrlNoIEA)))
    throw new RuntimeException("ISA_IEA_INTERCHANGE_CONTROL_NO_NOT_VALID");
}
private void validateTrnSetCtrlNo(List segmentArray) 
{
	String trnSetCtrlNoST="",trnSetCtrlNoSE="" ;
		for (int j=2; j < segmentArray.size(); j++)
		{
		  if(segmentArray.get(j).toString().startsWith("ST"))
		  {
			String[]ST=segmentArray.get(j).toString().split("\\*");
			trnSetCtrlNoST=ST[2];
		  }
		 if(segmentArray.get(j).toString().startsWith("SE"))
		  {
			String[]SE=segmentArray.get(j).toString().split("\\*");
			trnSetCtrlNoSE=SE[2];
		  }
		 if(!trnSetCtrlNoST.equals("")&&!trnSetCtrlNoSE.equals(""))
		  {
			if(trnSetCtrlNoST.equals(trnSetCtrlNoSE))
			{
				trnSetCtrlNoST="";
				trnSetCtrlNoSE="";
			}
			else throw new RuntimeException("ST_SE_TRANSACTION_SET_CONTROL_NO_NOT_VALID");
		    
		 }
	  }	
}
private void validateGrpCtrlNo(List segmentArray)
 {
	String grpCtrlNoGS="",grpCtrlNoGE="";
	for (int j=1; j < segmentArray.size(); j++)
	{
	  if(segmentArray.get(j).toString().startsWith("GS"))
	  {
		String[]GS=segmentArray.get(j).toString().split("\\*");
		grpCtrlNoGS=GS[6];
	  }
	  if(segmentArray.get(j).toString().startsWith("GE"))
	  {
		String[]GE=segmentArray.get(j).toString().split("\\*");
		grpCtrlNoGE=GE[2];
	  }
	  if(!grpCtrlNoGS.equals("")&&!grpCtrlNoGE.equals(""))
	  {
		if(grpCtrlNoGS.equals(grpCtrlNoGE))
		{
			grpCtrlNoGS="";
			grpCtrlNoGE="";
		}
		else throw new RuntimeException("GS_GE_GROUP_CONTROL_NO_NOT_VALID");
		    
	 }
  }
 }	
}
 

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image