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.Map;
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.dataservices.DSException;
import com.sybase.sup.sap3.SAPResultChecker;
import com.sybase.vader.utils.logging.SybLogger;

public class DefaultSAPResultCheck implements SAPResultChecker
{
    private static Set<String>              nonErrorMessages;
    static
    {
        nonErrorMessages = new HashSet<String>();
        nonErrorMessages.add("No data found");
        nonErrorMessages.add("Data was not found for the document");
    }
    
    public Map.Entry<Boolean, String> checkReturn(JCoFunction f)
    {
        JCoRecord returnStructure = null;
        JCoParameterList jpl = f.getExportParameterList();
        String msg = null;
        boolean success = true;
        if ( jpl != null )
        {
            try
            {
                returnStructure = jpl.getStructure("RETURN");
                if ( returnStructure != null )
                {
                    String type = returnStructure.getString("TYPE");
                    // generally TYPE is S for success, I for informational,
                    // or empty
                    if ( !(type.equals("") || type.equals("S") || type.equals("I")) )
                    {
                        String message = returnStructure.getString("MESSAGE");
                        /*UWPLogger.LogWarning*/ 
                        SybLogger.warn("SapUtils.execute: TYPE: <<" + type + ">>, MESSAGE: <<" + message + ">>, NUMBER: <<" + returnStructure.getInt("NUMBER") + ">>");
                        if ( !type.equals("W") && !nonErrorMessages.contains(message) )
                        {
                            success = false;
                            msg =  "TYPE: <<" + type + ">>, MESSAGE: <<" + message + ">>";
                            throw new DSException(DSException.INTERNAL_SERVER_ERROR, returnStructure.getInt("NUMBER"), msg);
                        }
                        else
                        {
                            msg =  "TYPE: <<" + type + ">>, MESSAGE: <<" + message + ">>";
                        }
                    }
                    else
                    {
                        if (SybLogger.isDebugEnabled()) 
                        {
                            String message = returnStructure.getString("MESSAGE");
                            SybLogger.debug("SapUtils.execute: TYPE: <<" + type + ">>, MESSAGE: <<" + message + ">>");
                        }
                    }

                }
            }
            catch (DSException dse) {
            	throw dse;
            }
            catch (Exception e)
            {
				/*
                if (UWPLogger.isTrace())
                    UWPLogger.LogTrace
				*/
				SybLogger.debug("SapUtils::execute: 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
                {
                    StringBuilder retMessage = new StringBuilder();
                    JCoTable returnTable = jpl.getTable("RETURN");
                    for (int i = 0; i < returnTable.getNumRows(); i++)
                    {
                        returnTable.setRow(i);
                        String type = returnTable.getString("TYPE");
                        // generally TYPE is S for success, I for
                        // informational, or empty
                        if ( !(type.equals("") || type.equals("S") || type.equals("I")) )
                        {
                            String message = returnTable.getString("MESSAGE");
                            /*UWPLogger.LogWarning*/ SybLogger.warn("SapUtils.execute[" + i + "]: TYPE: <<" + type + ">>, MESSAGE: <<"
                                    + message + ">>, NUMBER: <<" + returnTable.getInt("NUMBER") + ">>");
                            if ( !type.equals("W") && !nonErrorMessages.contains(message) )
                            {
                                success = false;
                                retMessage
                                        .append("[" + i + "]TYPE: <<" + type + ">>, MESSAGE: <<" + message + ">>");
                                throw new DSException(DSException.INTERNAL_SERVER_ERROR, returnTable.getInt("NUMBER"), retMessage.toString());

                            }
                            else
                            {
                                retMessage
                                        .append("[" + i + "]TYPE: <<" + type + ">>, MESSAGE: <<" + message + ">>");
                            }
                        }
                        else
                        {
                            if (SybLogger.isDebugEnabled()) 
                            {
                                String message = returnTable.getString("MESSAGE");
                                SybLogger.debug("SapUtils.execute[" + i + "]: TYPE: <<" + type + ">>, MESSAGE: <<" + message + ">>");
                            }
                        }

                    }
                    if( retMessage.length() > 0 )
                    {
                        msg = retMessage.toString();
                    }
                }
                catch (DSException dse) {
                	throw dse;
                }
                catch (Exception e)
                {
                    /*UWPLogger.LogWarning*/ SybLogger.warn("SapUtils::execute: error in execution while retrieving RETURN table: ", e);
                    success = false;
                    msg = e.getMessage();
                    if (msg == null || msg.isEmpty())
                    {
                        // Revert to old behavior if no message is supplied.
                        msg = e.toString();
                    }
                }
            }
        }
        
        return new CheckReturnMapEntry<Boolean, String>(success, msg);
    }
    
    class CheckReturnMapEntry<Boolean, String> extends java.util.AbstractMap.SimpleImmutableEntry<Boolean, String> {

        public CheckReturnMapEntry(Boolean success, String msg) {
            super(success, msg);
        }
    }

}