/**
 * Copyright 2006
 * 
 * Created on %date%
 */
 
/*
{{WriteMO specification start}}
{{WriteMO specification end}}
*/

package %package%;

import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;

import net.ponder2.ManagedObject;
import net.ponder2.ManagedObjectFactory;
import net.ponder2.Result;

import com.twicom.qdparser.Element;
import com.twicom.qdparser.TaggedElement;

public class %class% extends ManagedObject {

  public static class Factory implements ManagedObjectFactory {

    /**
     * creates and initialises a ManagedObject
     * 
     * @param command
     *          the initialisation parameters and commands
     * @param result
     *          the general result structure for errors and complex results
     * @return the new managed object
     */
    public ManagedObject create(TaggedElement command, Result result) {
      ManagedObject mo = new %class%();
      mo.initialise(command, result);
      return mo;
    }
  }

  //{{WriteMO:fields start}}
  private Map createAttribute = new HashMap();
  private Map useAttribute = new HashMap();
  private Map attribute = new HashMap();
  boolean initialOpCalled = false;
  //{{WriteMO:fields end}}

  /**
   * initialises this managed object. Called with the create construct after the
   * factory create is executed. Typically deals with specalised create things
   * and then calls the normal execute method to handle run-of-the-mill managed
   * object commands.
   * 
   * @param xml
   *          the initialisation parameters and commands
   * @param result
   *          the general result structure for errors and complex results
   */
  @Override
  public void initialise(TaggedElement xml, Result result) {
    // Pick up special create attributes and XML child create-specific
    // information here
    String att;
    //{{WriteMO create attributes start}}
    // Optional attribute
    att = xml.getAttribute("name1");
    if (att != null)
    	createAttribute.put("name1", att);
    // Required attribute
     att = xml.getAttribute("name2");
      if (att == null) 
        result.error("class: create requires attribute name2");
     createAttribute.put("name2", att);
    // Attribute with a default value
    att = xml.getAttribute("name3");
    if (att == null) 
       att="defaultValue";
     createAttribute.put("name3", att);
    //{{WriteMO create attributes end}}
    initialOperation(true, xml, result);
    initialOpCalled = true;
    for (Iterator it = xml.iterator(); it.hasNext();) {
      Element element = (Element)it.next();
      if (element instanceof TaggedElement) {
        attribute.clear();
        TaggedElement command = (TaggedElement)element;
        String operation = command.getName();
       //{{WriteMO create operations start}}
        if (operation.equals("createop1")) {

        }
        else if (operation.equals("createop2")) {

        }
       //{{WriteMO create operations end}}
     }
    }
    // Now pick up any normal use related commands that might be here
    execute(xml, result);
  }

  /**
   * optional method to set values within this object taken from XML attributes
   * 
   * @param xml
   *          the XML element containing the attributes
   * @param result
   *          the structure to contain the results
   * @return true if this managed object accepts the values else false
   */
  @Override
  public boolean executeSetup(TaggedElement command, Result result) {
     /*
     * Here we get attributes from the create or use constructs
     * This method is optional
     */
     String att;
     //{{WriteMO executeSetup attributes start}}
    String att = command.getAttribute("myattribute");
    if (att != null) {
      // Do something
      // Could set
    }
    //{{WriteMO executeSetup attributes end}}
    if (!initialOpCalled)
      initialOperation(false, command, result);
    initialOpCalled = false;
    return true; // if we are happy else return false for an error
  }
 
  /**
   * executes a single command taken from the xml structure. For any one xml
   * structure, this is called for each sub-command, one by one. executeSetup is
   * guaranteed to have been called before this method for any one xml
   * structure.
   * 
   * @param xml
   *          the XML structure containing all the commands
   * @param command
   *          the single command taken from the XML structure
   * @param result
   *          the structure to contain the results
   * @return true if this was a valid command else false
   */
  @Override
  public boolean execute(TaggedElement xml, TaggedElement command, Result result) {
    // Check the operations and execute them
    String att;
    attribute.clear();
    // Get the element's tag name
    String operation = command.getName();
    //{{WriteMO execute operations start}}
    if (operation.equals("op1")) {
      // Cycle through the sub-elements of the command
      // We would expect to get <op1op/> here
      for (Iterator it = command.iterator(); it.hasNext();) {
        Element e = (Element)it.next();
        // Have we another XML tag?
        if (e instanceof TaggedElement) {
          @SuppressWarnings("unused")
          TaggedElement subop = (TaggedElement)e;
          String subopname = subop.getName();
          // do something with subop, e.g. getAttribute
          if (subopname != null) {

          }
        }
      }
    }
    else if (operation.equals("op2")) {
      // Do something for op2
      String att = command.getAttribute("att1");
      if (att != null) {
        ;
      }
    }
    else
      // Indicate that we have not recognised the operation
      return false;
    // Indicate that we did recognise the operation, no further processing
    // needed
    //{{WriteMO execute operations end}}
    return true;
  }
  
  //{{WriteMO user operations start}}
  //{{WriteMO user operation op start}}
  private void opOperation(TaggedElement xml, TaggedElement command) {
    // Attributes are in Maps
    // attribute - this command's attributes
    // useAttribute - the <use> attributes
    // createAttribute - the <create> attributes
    //TODO - Operation code goes here
  }  
  //{{WriteMO user operation op end}}
  //{{WriteMO user operations end}}
  
  // Add any new methods etc after this line
}
