/**
 * Test hello world.
 * Simple J2ME app that calls the OTN hello world web service.
 */

package oracle.wireless.me.sample1;

// This is the client stub for the web service.  It should be
// generated from the J2ME Proxy Server.
import IOTNHelloWorldJ2MEStub;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

import oracle.wireless.me.core.*;

/**
 * HelloWorld uses a client stub to call the OTN hello world web service.
 * Register that web service on the J2ME Proxy Server using this WSDL document:
 *
 * http://otn.oracle.com/tech/webservices/htdocs/live/hello.wsdl
 *
 */
public class HelloWorld extends MIDlet implements CommandListener
{
    private Display display;
    private Form form;
    private Command exitCommand;
    private Command helloCommand;
    private Command clearCCommand;
    private Command clearSCommand;

    private IOTNHelloWorldJ2MEStub helloWebService;

    /**
     * Constructor does nothing.
     */
    public HelloWorld()
    {
    }

    /**
     * startApp calls method to initialize midlet.
     */
    protected void startApp() throws MIDletStateChangeException
    {
	    if (display == null) {
        initMIDlet();
	    }
    }

    /**
     * pauseApp calls ServiceManager.close()
     */
    protected void pauseApp()
    {
      try {
        ServiceManager.getInstance().close();
      }
      catch (Exception e) {
      }
    }

    /**
     * destroyApp calls method that cleans up, which is also called
     * from the "Exit" menu command.
     */
    protected void destroyApp(boolean unconditional)
	      throws MIDletStateChangeException
    {
	    exitMIDlet();
    }

    /**
     * CommandListener implementation, calls appropriate method depending
     * on command selected.
     */
    public void commandAction(Command c, Displayable d)
    {
	    if (c == exitCommand) {
	      exitMIDlet();
	    }
	    else if (c == helloCommand) {
	      callHello();
	    }
    	else if (c == clearCCommand) {
 	      clearCache();
    	}
    	else if (c == clearSCommand) {
    	  clearScreen();
    	}
    }

    /**
     * Initialize the midlet: make an instance of the client stub,
     * create the menu commands, create simple form.
     */
    private void initMIDlet()
    {
      helloWebService = new IOTNHelloWorldJ2MEStub();

      // Get MIDlet display.
      display = Display.getDisplay(this);

      // Create menu commands.
      exitCommand = new Command("Exit", Command.EXIT, 1);
      helloCommand = new Command("hello", Command.SCREEN, 1);
      clearCCommand = new Command("Clear Cache", Command.SCREEN, 2);
      clearSCommand = new Command("Clear Screen", Command.SCREEN, 2);

      // Create a simple form to hold menu commands and display call returns.
      form = new SimpleForm();
      display.setCurrent( form );
    }

  /**
   * On exit from the midlet, clear cache and close ServiceManager.
   */
  private void exitMIDlet()
  {
    clearCache();
    try {
      // ALWAYS call close() on ServiceManager on exit.
      ServiceManager.getInstance().close();
    }
    catch (Exception e) {
    }

    notifyDestroyed();
  }


  /**
   * Call hello world method through the generated client stub.
   */
  private void callHello()
  {
    try {
      // Set cache timeout to 1 minute.
      String returnValue = helloWebService.getDefaultHelloWorld(1, false);

      // Display the result of the call.
      form.append( returnValue + "\n" );
    }
    catch (ServiceException e) {
      form.append("Error with code: " + e.getErrorCode() + ", message: " + e.getMessage() + "\n");
    }
  }


  /**
   * Clear ALL entries from the response cache.
   */
  void clearCache()
  {
    try {
      ServiceManager sm = ServiceManager.getInstance();
      sm.clearCache();
      form.append( "Cache cleared.\n" );
    }
    catch (ServiceException e) {
      form.append("Error with code: " + e.getErrorCode() + ", clearing cache, message: " + e.getMessage() + "\n");
    }
  }

  /**
   * Clear the screen.
   */
  void clearScreen()
  {
    for (int i = (form.size() - 1); i >= 0; i--) {
      form.delete(i);
    }
  }

  /**
   * Simple form to hold menu commands and web service call returns.
   */
  class SimpleForm extends Form
  {
    SimpleForm() {
	    super( "HelloWorld" );

      addCommand(exitCommand);
      addCommand(helloCommand);
	    addCommand(clearCCommand);
	    addCommand(clearSCommand);

	    setCommandListener(HelloWorld.this);
	  }
  }

}
