Default SAP Result Checker Code

Default Result Checkers are built-in result checkers that are applied automatically on MBO operations by Unwired Server. They can be replaced by implementing and deploying a Custom Result Checker. This is the default result checker used to check results in SAP® data sources.

package com.sybase.sap3;

import java.util.HashSet;
import java.util.Set;

import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoRecord;
import com.sap.conn.jco.JCoTable;
import com.sybase.vader.utils.logging.SybLogger;
import com.sybase.sup.sap3.SAPOperationHandler;
import com.sybase.dataservices.OHLog;
import com.sybase.dataservices.OHException;

public class DefaultSAPOperationHandler extends SAPOperationHandler {

    private static Set<String>              nonErrorMessages;
    static
    {
        nonErrorMessages = new HashSet<String>();
        nonErrorMessages.add("No data found");
        nonErrorMessages.add("Data was not found for the document");
        nonErrorMessages.add("No customer was found with these selection criteria");
    }
    
	public void resultCheck(JCoFunction f) {
		JCoRecord returnStructure = null;
		JCoParameterList jpl = f.getExportParameterList();
        String errorMsg = null;
        int errorNumber = 0;
        String errorDebugMsg = null;
        boolean success = true;
        if ( jpl != null )
        {
            try
            {
                returnStructure = jpl.getStructure("RETURN");
                if ( returnStructure != null )
                {
            		SybLogger.debug("JCoRecord = '" + returnStructure.toXML() + '"');
                    String type = returnStructure.getString("TYPE");
                    String message = returnStructure.getString("MESSAGE");
                    // generally TYPE is S for success, I for informational,
                    // or empty
                    if ( type.equals("") || type.equals("S") || type.equals("I") )                    {
                		SybLogger.debug("Success");
                    	//sendDebugMsg(returnStructure);
                    	OHLog.info(OHLog.EIS_SUCCESS, returnStructure.getInt("NUMBER"), message.isEmpty()?"Success":message);
                    }
                    else
                    {
                        SybLogger.debug("TYPE: <<" + type + ">>, MESSAGE: <<" + message + ">>");
                        if ( type.equals("W") || nonErrorMessages.contains(message) )
                        {
                    		SybLogger.debug("Success");
                        	//sendDebugMsg(returnStructure);
                        	OHLog.warn(OHLog.EIS_SUCCESS, returnStructure.getInt("NUMBER"), message.isEmpty()?"Success":message);
                        }
                        else
                        {
                    		SybLogger.debug("Error");
                        	sendDebugMsg(returnStructure, OHLog.INTERNAL_SERVER_ERROR);
                        	SybLogger.debug("Throwing OHException. NUMBER="+ returnStructure.getInt("NUMBER"));
                        	throw new OHException(OHLog.INTERNAL_SERVER_ERROR, returnStructure.getInt("NUMBER"), returnStructure.getString("MESSAGE"));
                        }
                    }

                }
            }
            catch (OHException ohe)
            {
            	throw ohe;
            }
            catch (Exception e)
            {
				SybLogger.debug("Unable to retrieve RETURN structure - Will try to retrieve RETURN table next.", e);
            }
        }
        // if there is no RETURN structure, look for RETURN table
        if ( returnStructure == null )
        {
            jpl = f.getTableParameterList();
            if ( jpl != null )
            {
                try
                {
                    JCoTable returnTable = jpl.getTable("RETURN");
            		SybLogger.debug("JCoTable = '" + returnTable.toXML() + '"');
                    for (int i = 0; i < returnTable.getNumRows(); i++)
                    {
                        returnTable.setRow(i);
                        String type = returnTable.getString("TYPE");
                        String message = returnTable.getString("MESSAGE");
                        // generally TYPE is S for success, I for
                        // informational, or empty
                        if ( type.equals("") || type.equals("S") || type.equals("I") )
                        {
                    		SybLogger.debug("Success");
                        	//sendDebugMsg(returnTable);
                        	OHLog.warn(OHLog.EIS_SUCCESS, returnTable.getInt("NUMBER"), message.isEmpty()?"Success":message);
                        }
                        else
                        {
                            SybLogger.debug("TYPE: <<" + type + ">>, MESSAGE: <<" + message + ">>");
                        	// throw an exception on error, but need to discover if other rows exist first
                            if ( type.equals("W") || nonErrorMessages.contains(message) )
                            {
                        		SybLogger.debug("Success");
                            	//sendDebugMsg(returnTable);
                            	OHLog.warn(OHLog.EIS_SUCCESS, returnTable.getInt("NUMBER"), message.isEmpty()?"Success":message);
                            }
                            else
                            {
                            	// If we previously discovered an error we can log this one and throw the other, later
                            	if(!success)
                            	{
                            		SybLogger.debug("Error");
                                	sendDebugMsg(returnTable, OHLog.INTERNAL_SERVER_ERROR);
                                	OHLog.error(OHLog.INTERNAL_SERVER_ERROR, returnTable.getInt("NUMBER"), returnTable.getString("MESSAGE"));
                            	}
                            	else
                            	{
                            		SybLogger.debug("Error");
                                    success = false;
                                    errorMsg = message;
                                    errorNumber = returnTable.getInt("NUMBER");
                                	errorDebugMsg = makeDebugMsg(returnTable);
                            	}
                            }
                        }

                    }
                }
                catch (Exception e)
                {
                    success = false;
                    errorMsg = e.getMessage();
                    errorNumber = 0;
                    if (errorMsg == null || errorMsg.isEmpty())
                    {
                    	errorMsg = e.toString();
                    }
                }
            }
        }
        if(!success)
        {
        	if(errorDebugMsg != null)
        	{
            	OHLog.debug(OHLog.INTERNAL_SERVER_ERROR, errorNumber, errorDebugMsg);
        	}
        	throw new OHException(OHLog.INTERNAL_SERVER_ERROR, errorNumber, errorMsg);
        }
	}
	
	// JCoTables are JCoRecords, so this works
	private String makeDebugMsg (JCoRecord r) {
		String s = "";
		// Some fields may not be present, which will throw an exception we want to ignore
		try{s+="ID='" + r.getString("ID") + "'";} 
		catch(Exception e){}
		
		try{s+=",LOG_NO='" + r.getString("LOG_NO") + "'";} 
		catch(Exception e){}
		
		try{s+=",LOG_MSG_NO=" + r.getInt("LOG_MSG_NO");} 
		catch(Exception e){}
		
		try{s+=",PARAMETER='" + r.getString("PARAMETER") + "'";} catch(Exception e){}
		try{s+=",ROW=" + r.getInt("ROW");} 
		catch(Exception e){}
		
		try{s+=",FIELD='" + r.getString("FIELD") + "'";} 
		catch(Exception e){}
		
		try{s+=",SYSTEM='" + r.getString("SYSTEM") + "'";} 
		catch(Exception e){}
		return s;
	}

	private void sendDebugMsg (JCoRecord r, int supCode) {
		String s = makeDebugMsg(r);
    	OHLog.debug(supCode, r.getInt("NUMBER"), s);
	}
	private void sendDebugMsg (JCoRecord r) {
		sendDebugMsg(r, OHLog.EIS_SUCCESS);
	}
}