package biz.mbisoftware.fn.wstypes; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import org.w3c.dom.Node; import biz.mbisoftware.common.Constants; import biz.mbisoftware.common.XmlDocument; import biz.mbisoftware.common.XmlHelper; /** * class for service response. * * @author lach */ public class ServiceResponse implements Serializable { /** success value for error code. */ public static final Integer SUCCESS = new Integer( 0 ); /** warning value for error code. */ public static final Integer WARNING = new Integer( 1 ); /** error value for error code. */ public static final Integer ERROR = new Integer( -1 ); /** error code of processing. */ private Integer errorCode; /** Count of rows procession successful. */ private Integer numRowsSuccess; /** Count of rows procession successful with warnings. */ private Integer numRowsWarning; /** Count of rows procession failed. */ private Integer numRowsError; /** array of success messages. */ private String[] successMessages; /** array of warning messages. */ private String[] warnMessages; /** array of error messages. */ private String[] errorMessages; /** * @return Returns the errorMessages as array String. */ public String[] getErrorMessages() { return this.errorMessages; } /** * @return Returns the errorMessages as one String. */ public String getErrorMessagesAsString() { StringBuffer sb = new StringBuffer( Constants.DEFAULT_STRING_BUFFER_SIZE ); sb.append( '[' ); if ( this.errorMessages != null ) { for ( int i = 0; i < this.errorMessages.length; i++ ) { sb.append( this.errorMessages[i] ); if ( i + 1 < this.errorMessages.length ) { sb.append( ',' ); } } } sb.append( ']' ); return sb.toString(); } /** * @param errorMessages The errorMessages to set. */ public void setErrorMessages( final String[] errorMessages ) { this.errorMessages = errorMessages; this.numRowsError = new Integer( errorMessages.length ); } /** * @return Returns the numRowsError. */ public Integer getNumRowsError() { return this.numRowsError; } /** * @param numRowsError The numRowsError to set. */ public void setNumRowsError( final Integer numRowsError ) { this.numRowsError = numRowsError; } /** * @return Returns the numRowsSuccess. */ public Integer getNumRowsSuccess() { return this.numRowsSuccess; } /** * @param numRowsSuccess The numRowsSuccess to set. */ public void setNumRowsSuccess( final Integer numRowsSuccess ) { this.numRowsSuccess = numRowsSuccess; } /** * @return Returns the numRowsWarning. */ public Integer getNumRowsWarning() { return this.numRowsWarning; } /** * @param numRowsWarning The numRowsWarning to set. */ public void setNumRowsWarning( final Integer numRowsWarning ) { this.numRowsWarning = numRowsWarning; } /** * @return Returns the errorCode. */ public Integer getErrorCode() { return this.errorCode; } /** * @param errorCode The errorCode to set. */ public void setErrorCode( final Integer errorCode ) { this.errorCode = errorCode; } /** * @return Returns the successMessages. */ public String[] getSuccessMessages() { return this.successMessages; } /** * @param successMessages The successMessages to set. */ public void setSuccessMessages( final String[] successMessages ) { this.successMessages = successMessages; this.numRowsSuccess = new Integer( successMessages.length ); } /** * @return Returns the warnMessages. */ public String[] getWarnMessages() { return this.warnMessages; } /** * @param warnMessages The warnMessages to set. */ public void setWarnMessages( final String[] warnMessages ) { this.warnMessages = warnMessages; this.numRowsWarning = new Integer( warnMessages.length ); } /** * Increments the numRowsError value. */ public void incrNumRowsError() { Integer incr = new Integer( this.numRowsError.intValue() + 1 ); this.setNumRowsError( incr ); } // incrNumRowsError /** * Increments the numRowsSuccess value. */ public void incrNumRowsSuccess() { Integer incr = new Integer( this.numRowsSuccess.intValue() + 1 ); this.setNumRowsSuccess( incr ); } // incrNumRowsSuccess /** * Initializes the num of rows components. */ public void initNumRows() { this.setNumRowsError( new Integer( 0 ) ); this.setNumRowsSuccess( new Integer( 0 ) ); } // initNumRows /** * Increments the num of rows components. * * @param success true - numRowsSuccess * false - numRowsError */ public void incrNumRows( final boolean success ) { if ( success ) { this.incrNumRowsSuccess(); } else { this.incrNumRowsError(); } } // incrNumRows /** * Builds a vector from a message array and adds one message line. * * @param messages massage array * @param message message to add * @return the new vector of messages */ private ArrayList addMessages( final String[] messages, final String message ) { ArrayList v = new ArrayList(); if ( messages != null ) { for ( int i = 0; i < messages.length; i++ ) { v.add( messages[ i ] ); } } if ( message != null ) { v.add( message ); } return v; } // addMessages /** * Sets the warnMessages. * * @param vMessages vector of warn messages */ private void setWarnMessages( final ArrayList vMessages ) { if ( vMessages != null ) { String[] messages = (String[])vMessages.toArray( new String[ vMessages.size() ] ); this.setWarnMessages( messages ); } } // setWarnMessages /** * Adds a message to the warnMessages. * * @param message warn message to add */ public void addWarnMessages( final String message ) { ArrayList v = addMessages( this.warnMessages, message ); setWarnMessages( v ); } // addWarnMessages /** * Adds a message to the errorMessages. * * @param message error message to add */ public void addErrorMessages( final String message ) { ArrayList v = addMessages( this.errorMessages, message ); setErrorMessages( v ); } // addErrorMessages /** * Adds a message array to the errorMessages. * * @param messages error messages to add */ public void addErrorMessages( final String[] messages ) { ArrayList v = new ArrayList(); if ( this.errorMessages != null ) { for ( int i = 0; i < this.errorMessages.length; i++ ) { v.add( this.errorMessages[ i ] ); } } if ( messages != null ) { for ( int i = 0; i < messages.length; i++ ) { v.add( messages[ i ] ); } } setErrorMessages( v ); } // addErrorMessages /** * Sets the errorMessages. * * @param vMessages vector of error messages */ public void setErrorMessages( final ArrayList vMessages ) { if ( vMessages != null ) { String[] messages = (String[])vMessages.toArray( new String[ vMessages.size() ] ); this.setErrorMessages( messages ); } } // setErrorMessages /** * Sets the errorMessages. * * @param rootNode XML node */ public void setErrorMessages( final Node rootNode ) { Node messages = XmlHelper.getOptionalChild( rootNode, "MESSAGES" ); if ( messages != null ) { ArrayList msg = XmlHelper.getChildrenListByTagName( messages, "MSG" ); if ( msg.size() > 0 ) { String[] errMessages = new String[ msg.size() ]; Iterator iter = msg.iterator(); int i = 0; if ( iter != null ) { while ( iter.hasNext() && i < msg.size() ) { errMessages[ i ] = XmlHelper.getNodeContent( (Node)iter.next() ); i++; } } this.setErrorMessages( errMessages ); } } } // setErrorMessages /** * Gets the root node from the XML document specified by doc. * Fills the error and warn messages. * * @param doc XML document * @return the root node */ private Node getRootNodeFromDoc( final XmlDocument doc ) { Node rootNode = null; setErrorCode( ServiceResponse.ERROR ); if ( doc == null ) { addErrorMessages( "document not valid" ); } else { rootNode = doc.getDocumentElement(); if ( rootNode == null ) { addWarnMessages( "attribute errno not found" ); } else { String errNo = XmlHelper.getNodeAttribute( rootNode, "errno" ); if ( errNo == null ) { setErrorCode( Integer.valueOf( errNo ) ); } else { if ( "0".equals( errNo ) ) { setErrorCode( ServiceResponse.SUCCESS ); } else { setErrorCode( Integer.valueOf( errNo ) ); } } } } return rootNode; } // getRootNodeFromDoc /** * Gets the root node from the XML document represented by xmlResult. * Fills the error and warn messages. * * @param xmlResult XML result as byte stream * @return the root node */ public Node getRootNode( final byte[] xmlResult ) { Node rc = null; if ( !( xmlResult == null ) ) { XmlDocument doc = new XmlDocument( xmlResult ); rc = this.getRootNodeFromDoc( doc ); } return rc; } // getRootNode }