// TestingServiceNotes.java
//
// Created: January 2006
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

import org.biomoby.shared.parser.MobyPackage;
import org.biomoby.shared.parser.MobyJob;
import org.biomoby.shared.parser.ServiceException;
import org.biomoby.shared.datatypes.MobyObject;
import org.biomoby.shared.MobyException;

/**
 * A not-too-generally-useful command-line client for testing creation
 * and parsig of "service notes" (a piece of the BioMoby envelope
 * where error handling takes place). Once we have jUnit testing in
 * jMoby, this client should be transfer to a jUnit test. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: TestingServiceNotes.java,v 1.2 2006/01/09 16:20:53 senger Exp $
 */
public class TestingServiceNotes {

    /*************************************************************************
     *
     *  An entry point...
     *
     *************************************************************************/
    public static void main (String [] args) {
	try {

	    // create a package with one job and one Object input
	    MobyObject data = new MobyObject ("my.good.ns", "my.good.id");
	    MobyPackage moby = new MobyPackage();
	    moby.setAuthority ("net.samples.jmoby");
	    MobyJob job1 = new MobyJob ("job1");
	    job1.addData (data, "my_data_name");
	    moby.addJob (job1);
	    testIt ("no service notes there yet", moby);

	    // notes attached (as string)
	    moby.setServiceNotes ("This is a service note.");
	    testIt ("a text note attached", moby);

// 	    // notes change to CDATA
// 	    moby.setServiceNotesXML
// 		("This is a service note attached as CDATA. See here an XML: </serviceNotes>");
// 	    testIt ("a text note changed to CDATA", moby);

	    // add a general exception
	    ServiceException anError =
		new ServiceException (ServiceException.ERROR,
				      ServiceException.INPUTS_INVALID,
				      "This refers to all jobs and data");
	    moby.addException (anError);
	    testIt ("a general error attached", moby);

	    // add all exception types (using various constructors)
	    moby.addException
		(new ServiceException (ServiceException.ERROR,
				       ServiceException.INPUTS_INVALID,
				       job1.getId(),
				       (job1.getDataElements())[0].getName()));
	    moby.addException
		(new ServiceException (ServiceException.ERROR,
				       ServiceException.INPUTS_INVALID,
				       job1.getId(),
				       (job1.getDataElements())[0].getName(),
				       "This now has its own error message"));
	    moby.addException (ServiceException.warning ("This is just a warning"));
	    moby.addException (ServiceException.info ("This is just an info"));
	    testIt ("exceptions for each severity level added", moby);

	} catch (MobyException e) {
	    System.err.println ("===ERROR===");
	    System.err.println (e.getMessage());
	    System.err.println ("===========");


	} catch (Exception e) {
	    System.err.println ("===ERROR===");
	    e.printStackTrace();
	    System.err.println ("===========");
	}
    }

    static void testIt (String title, MobyPackage moby)
	throws MobyException {
	String xmlBefore = moby.toXML();
	String xmlAfter = MobyPackage.createFromXML (xmlBefore).toXML();

	System.out.println ("---[ " + title + " ]---\n");
	if (xmlBefore.equals (xmlAfter)) {
	    System.out.print (xmlAfter);
	} else {
	    System.out.println ("Problems:");
	    System.out.print ("\tBefore:\n" + xmlBefore);
	    System.out.print ("\tAfter:\n"  + xmlAfter);
	}
    }

}
