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

package org.jmoby.tutorial.service;

import net.jmoby.samples.TestServiceNotesSkel;
import org.biomoby.shared.MobyException;
import org.biomoby.shared.parser.MobyPackage;
import org.biomoby.shared.parser.MobyJob;
import org.biomoby.shared.parser.ServiceException;
import org.biomoby.shared.datatypes.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * A service that does nothing except filling various service notes
 * (with info messages and with exceptions). Just for tetsing that the
 * service notes are properly encoded, and to see what they can
 * contain and how. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: TestServiceNotesImpl.java,v 1.4 2008/03/03 14:11:08 senger Exp $
 */

public class TestServiceNotesImpl
    extends TestServiceNotesSkel {

    public void processIt (MobyJob request,
			   MobyJob response,
			   MobyPackage outputContext)
	throws MobyException {
	MobyDateTime input = get_input_date (request);
	if (input != null) {
	    String date = input.getValue();
	    outputContext.addException (ServiceException.info ("Input date was " + date),
					request);
	}

	StringBuilder buf = new StringBuilder();
	buf.append ("This is a general service note.\n");
	buf.append ("HTTP Headers:\n");
	buf.append ("-------------\n");
	buf.append (getHTTPHeaders().toString());
	buf.append ("\n");
	buf.append ("Length: ");
	buf.append (getRequestLength() + "\n");
	try {
	    buf.append ("Request URL: ");
	    buf.append (getServletRequest().getRequestURL().toString());
	    buf.append ("\n");
	} catch (NullPointerException e) {
	    // can happen if the servlet request is not available
	}
	outputContext.setServiceNotes (buf.toString());

	SimpleDateFormat sdf = new SimpleDateFormat (MobyDateTime.FULL_FORMAT);
	MobyDateTime output = new MobyDateTime ();
	output.setValue (sdf.format (new Date()));
  	set_output_date (response, output);
    }
}

