1 /**
   2  * Copyright 2006
   3  * 
   4  * Created on Sun Aug 20 17:47:08 CEST 2006
   5  */
   6  
   7 /*
   8 {{WriteMO specification start}}
   9 ManagedObject Backup
  10 
  11 Create Attributes
  12 backupdirectory String /tmp # The directory where all the information is to be stored
  13 
  14 Use Operations
  15 save             # Saves some XML information into the given filename
  16 
  17 save Attributes
  18 filename String required # where to save the file
  19 
  20 {{WriteMO specification end}}
  21 */
  22 
  23 package net.ponder2.managedobject;
  24 
  25 import java.util.Iterator;
  26 import java.util.HashMap;
  27 import java.util.Map;
  28 
  29 import net.ponder2.ManagedObject;
  30 import net.ponder2.ManagedObjectFactory;
  31 import net.ponder2.Result;
  32 
  33 import com.twicom.qdparser.Element;
  34 import com.twicom.qdparser.TaggedElement;
  35 
  36 public class Backup extends ManagedObject {
  37 
  38   public static class Factory implements ManagedObjectFactory {
  39 
  40     /**
  41      * creates and initialises a ManagedObject
  42      * 
  43      * @param command
  44      *          the initialisation parameters and commands
  45      * @param result
  46      *          the general result structure for errors and complex results
  47      * @return the new managed object
  48      */
  49     public ManagedObject create(TaggedElement command, Result result) {
  50       ManagedObject mo = new Backup();
  51       mo.initialise(command, result);
  52       return mo;
  53     }
  54   }
  55 
  56   //{{WriteMO:fields start}}
  57   private Map createAttribute = new HashMap();
  58   private Map useAttribute = new HashMap();
  59   private Map attribute = new HashMap();
  60   boolean initialOpCalled = false;
  61   //{{WriteMO:fields end}}
  62 
  63   /**
  64    * initialises this managed object. Called with the create construct after the
  65    * factory create is executed. Typically deals with specalised create things
  66    * and then calls the normal execute method to handle run-of-the-mill managed
  67    * object commands.
  68    * 
  69    * @param xml
  70    *          the initialisation parameters and commands
  71    * @param result
  72    *          the general result structure for errors and complex results
  73    */
  74   @Override
  75   public void initialise(TaggedElement xml, Result result) {
  76     // Pick up special create attributes and XML child create-specific
  77     // information here
  78     String att;
  79     //{{WriteMO create attributes start}}
  80     // create attribute backupdirectory with default value: /tmp
  81     // The directory where all the information is to be stored
  82     att = xml.getAttribute("backupdirectory");
  83     if (att == null) att="/tmp";
  84     createAttribute.put("backupdirectory", att);
  85     //{{WriteMO create attributes end}}
  86     initialOperation(true, xml, result);
  87     initialOpCalled = true;
  88     for (Iterator it = xml.iterator(); it.hasNext();) {
  89       Element element = (Element)it.next();
  90       if (element instanceof TaggedElement) {
  91         attribute.clear();
  92         TaggedElement command = (TaggedElement)element;
  93         String operation = command.getName();
  94        //{{WriteMO create operations start}}
  95        //{{WriteMO create operations end}}
  96      }
  97     }
  98     // Now pick up any normal use related commands that might be here
  99     execute(xml, result);
 100   }
 101 
 102   /**
 103    * optional method to set values within this object taken from XML attributes
 104    * 
 105    * @param xml
 106    *          the XML element containing the attributes
 107    * @param result
 108    *          the structure to contain the results
 109    * @return true if this managed object accepts the values else false
 110    */
 111   @Override
 112   public boolean executeSetup(TaggedElement command, Result result) {
 113      /*
 114      * Here we get attributes from the create or use constructs
 115      * This method is optional
 116      */
 117      String att;
 118      //{{WriteMO executeSetup attributes start}}
 119     //{{WriteMO executeSetup attributes end}}
 120     if (!initialOpCalled)
 121       initialOperation(true, command, result);
 122     initialOpCalled = false;
 123     return true; // if we are happy else return false for an error
 124   }
 125  
 126   /**
 127    * executes a single command taken from the xml structure. For any one xml
 128    * structure, this is called for each sub-command, one by one. executeSetup is
 129    * guaranteed to have been called before this method for any one xml
 130    * structure.
 131    * 
 132    * @param xml
 133    *          the XML structure containing all the commands
 134    * @param command
 135    *          the single command taken from the XML structure
 136    * @param result
 137    *          the structure to contain the results
 138    * @return true if this was a valid command else false
 139    */
 140   @Override
 141   public boolean execute(TaggedElement xml, TaggedElement command, Result result) {
 142     // Check the operations and execute them
 143     String att;
 144     attribute.clear();
 145     // Get the element's tag name
 146     String operation = command.getName();
 147     //{{WriteMO execute operations start}}
 148     if (operation.equals("save")) {
 149       // Required save attribute: filename
 150       // where to save the file
 151       att = command.getAttribute("filename");
 152       if (att == null) result.error("Backup: save requires attribute 'filename'");
 153       attribute.put("filename", att);
 154       useOperationSave(xml, command, result);
 155     }
 156     //{{WriteMO execute operations end}}
 157     return true;
 158   }
 159   
 160   //{{WriteMO user operations start}}
 161 
 162   /**
 163   *  deals with the initial use or create command.
 164   *  Called once before operation methods are called.
 165   * 
 166   * @param create
 167   *          true if <create>, false if <use>
 168   * @param xml
 169   *          the XML element containing the attributes
 170   * @param result
 171   *          the result structure for ordinary results and errors
 172   */
 173   private void initialOperation(boolean create, TaggedElement xml, Result result) {
 174     // Attributes are in Map's
 175     // useAttribute - the <use> attributes
 176     // createAttribute - the <create> attributes
 177 
 178     if (create) {
 179       // Operation specific attributes for this operation are
 180       // String backupdirectory default: /tmp # The directory where all the information is to be stored
 181       String attBackupdirectory = (String)attribute.get("backupdirectory");
 182       //{{WriteMO code start [initialOperation-create]}}
 183       //TODO - Replace this line with your code
 184       //{{WriteMO code end [initialOperation-create]}}
 185     } else {
 186       //{{WriteMO code start [initialOperation-use]}}
 187       //TODO - Replace this line with your code
 188       //{{WriteMO code end [initialOperation-use]}}
 189     }
 190     //{{WriteMO code start [initialOperation-common]}}
 191     //TODO - Replace this line with your code
 192     //{{WriteMO code end [initialOperation-common]}}
 193   }
 194 
 195   /**
 196   *  deals with the save command.
 197   * Saves some XML information into the given filename
 198   * 
 199   * @param xml
 200   *          the main create or use structure
 201   * @param command
 202   *          the command that we are actually dealing with
 203   * @param result
 204   *          the result structure for ordinary results and errors
 205   */
 206   private void useOperationSave(TaggedElement xml, TaggedElement command, Result result) {
 207     // Attributes are in Map's
 208     // attribute - this command's attributes
 209     // useAttribute - the <use> attributes
 210     // createAttribute - the <create> attributes
 211 
 212     // Operation specific attributes for this operation are
 213     // String filename required # where to save the file
 214     String attFilename = (String)attribute.get("filename");
 215     //{{WriteMO code start [useOperationSave]}}
 216     //TODO - Replace this line with your code
 217     //{{WriteMO code end [useOperationSave]}}
 218   }
 219   //{{WriteMO user operations end}}
 220   
 221   // Add any new methods etc after this line
 222 }


CategoryPonder2Project

Ponder2JavaBackup (last edited 2008-01-03 17:40:14 by localhost)