/**
 * Test sample 1.
 * Calls registered web services using client stubs.
 */

package oracle.wireless.me.sample1;

import TestWebServiceJ2MEStub;
import PushServerJ2MEStub;

import java.util.Vector;

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

import oracle.wireless.me.core.*;

public class Sample1 extends MIDlet implements CommandListener
{
  private Display display;
  private Form form;
  private Command exitCommand;
  private Command helloCommand;
  private Command testVectorCommand;
  private Command pushVersionCommand;
  private Command clearCCommand;
  private Command clearSCommand;

  private TestWebServiceJ2MEStub testWebService;
  private PushServerJ2MEStub pushService;

  public Sample1()
  {
  }

  protected void startApp() throws MIDletStateChangeException
  {
    if (display == null) {
      initMIDlet();
    }
  }

  protected void pauseApp()
  {
    try {
      ServiceManager.getInstance().close();
    }
    catch (Exception e) {
    }
    //System.out.println("Sample1 paused");
  }


  protected void destroyApp(boolean unconditional)
      throws MIDletStateChangeException
  {
    exitMIDlet();
  }

  public void commandAction(Command c, Displayable d)
  {
    if (c == exitCommand) {
      exitMIDlet();
    }
    else if (c == helloCommand) {
      callHello();
    }
    else if (c == testVectorCommand) {
      callTestVector();
    }
    else if (c == pushVersionCommand) {
      callPushVersion();
    }
    else if (c == clearCCommand) {
      clearCache();
    }
    else if (c == clearSCommand) {
      clearScreen();
    }
  }

  private void initMIDlet()
  {
    testWebService = new TestWebServiceJ2MEStub();
    pushService = new PushServerJ2MEStub();

    // Display, etc.
    display = Display.getDisplay(this);

    exitCommand = new Command("Exit", Command.EXIT, 1);
    helloCommand = new Command("hello", Command.SCREEN, 1);
    testVectorCommand = new Command("testVector", Command.SCREEN, 2);
    pushVersionCommand = new Command("pushVersion", Command.SCREEN, 2);
    clearCCommand = new Command("Clear Cache", Command.SCREEN, 2);
    clearSCommand = new Command("Clear Screen", Command.SCREEN, 2);

    form = new SimpleForm();
    display.setCurrent( form );
  }

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

    notifyDestroyed();
  }


  // Call hello.
  private void callHello()
  {
    try {
      // Set cache timeout to 1 minute.
      String returnValue = testWebService.hello(1, false, "World");

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

  
  // Call testVector.
  private void callTestVector()
  {
    Vector param = new Vector();
    param.addElement( new String("Vector test") );
    param.addElement( new String("success") );

    try {
      Vector respVector = testWebService.testVector(1, false, new Integer(25), "Hello", param);
      // Display the result of the call.
      form.append( respVector.toString() + "\n" );
    }
    catch (ServiceException e) {
      form.append("Error with code: " + e.getErrorCode() + ", message: " + e.getMessage() + "\n");
    }
  }

  
  // Call get version method of PushServer.
  private void callPushVersion()
  {
    try {
      // Set cache timeout to 1 minute.
      String returnValue = pushService.getVersion(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);
    }
  }

  class SimpleForm extends Form
  {
    SimpleForm() {
      super( "Sample1" );

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

      setCommandListener(Sample1.this);
    }
  }


}
