1 /**
2 * Copyright 2006
3 *
4 * Created on Tue Aug 29 09:10:56 CEST 2006
5 */
6
7 /*
8 {{WriteMO specification start}}
9 ManagedObject AlarmClock
10 Package net.ponder2.example
11
12 Use Attributes
13 state # Turns the alarm on and off using the values 'on' and 'off'
14
15 Use Operations
16 show # Shows the alarm window
17 hide # hides the alarm window
18
19
20 {{WriteMO specification end}}
21 */
22
23 package net.ponder2.example;
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 AlarmClock 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 AlarmClock();
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 //{{WriteMO create attributes end}}
81 initialOperation(true, xml, result);
82 initialOpCalled = true;
83 for (Iterator it = xml.iterator(); it.hasNext();) {
84 Element element = (Element)it.next();
85 if (element instanceof TaggedElement) {
86 attribute.clear();
87 TaggedElement command = (TaggedElement)element;
88 String operation = command.getName();
89 //{{WriteMO create operations start}}
90 //{{WriteMO create operations end}}
91 }
92 }
93 // Now pick up any normal use related commands that might be here
94 execute(xml, result);
95 }
96
97 /**
98 * optional method to set values within this object taken from XML attributes
99 *
100 * @param xml
101 * the XML element containing the attributes
102 * @param result
103 * the structure to contain the results
104 * @return true if this managed object accepts the values else false
105 */
106 @Override
107 public boolean executeSetup(TaggedElement command, Result result) {
108 /*
109 * Here we get attributes from the create or use constructs
110 * This method is optional
111 */
112 String att;
113 //{{WriteMO executeSetup attributes start}}
114 // Optional use attribute: state
115 // Turns the alarm on and off using the values 'on' and 'off'
116 att = command.getAttribute("state");
117 if (att != null) useAttribute.put("state", att);
118 //{{WriteMO executeSetup attributes end}}
119 if (!initialOpCalled)
120 initialOperation(false, command, result);
121 initialOpCalled = false;
122 return true; // if we are happy else return false for an error
123 }
124
125 /**
126 * executes a single command taken from the xml structure. For any one xml
127 * structure, this is called for each sub-command, one by one. executeSetup is
128 * guaranteed to have been called before this method for any one xml
129 * structure.
130 *
131 * @param xml
132 * the XML structure containing all the commands
133 * @param command
134 * the single command taken from the XML structure
135 * @param result
136 * the structure to contain the results
137 * @return true if this was a valid command else false
138 */
139 @Override
140 public boolean execute(TaggedElement xml, TaggedElement command, Result result) {
141 // Check the operations and execute them
142 String att;
143 attribute.clear();
144 // Get the element's tag name
145 String operation = command.getName();
146 //{{WriteMO execute operations start}}
147 if (operation.equals("show")) {
148 useOperationShow(xml, command, result);
149 }
150 else if (operation.equals("hide")) {
151 useOperationHide(xml, command, result);
152 }
153 //{{WriteMO execute operations end}}
154 return true;
155 }
156
157 //{{WriteMO user operations start}}
158
159 /**
160 * deals with the initial use or create command.
161 * Called once before operation methods are called.
162 *
163 * @param create
164 * true if <create>, false if <use>
165 * @param xml
166 * the XML element containing the attributes
167 * @param result
168 * the result structure for ordinary results and errors
169 */
170 private void initialOperation(boolean create, TaggedElement xml, Result result) {
171 // Attributes are in Map's
172 // useAttribute - the <use> attributes
173 // createAttribute - the <create> attributes
174
175 if (create) {
176 //{{WriteMO code start [initialOperation-create]}}
177 //TODO - Replace this line with your code
178 //{{WriteMO code end [initialOperation-create]}}
179 } else {
180 // Operation specific attributes for this operation are
181 // String state # Turns the alarm on and off using the values 'on' and 'off'
182 String attState = (String)useAttribute.get("state");
183 //{{WriteMO code start [initialOperation-use]}}
184 //TODO - Replace this line with your code
185 //{{WriteMO code end [initialOperation-use]}}
186 }
187 //{{WriteMO code start [initialOperation-common]}}
188 //TODO - Replace this line with your code
189 //{{WriteMO code end [initialOperation-common]}}
190 }
191
192 /**
193 * deals with the show command.
194 * Shows the alarm window
195 *
196 * @param xml
197 * the main create or use structure
198 * @param command
199 * the command that we are actually dealing with
200 * @param result
201 * the result structure for ordinary results and errors
202 */
203 private void useOperationShow(TaggedElement xml, TaggedElement command, Result result) {
204 // Attributes are in Map's
205 // attribute - this command's attributes
206 // useAttribute - the <use> attributes
207 // createAttribute - the <create> attributes
208
209 //{{WriteMO code start [useOperationShow]}}
210 //TODO - Replace this line with your code
211 //{{WriteMO code end [useOperationShow]}}
212 }
213
214 /**
215 * deals with the hide command.
216 * hides the alarm window
217 *
218 * @param xml
219 * the main create or use structure
220 * @param command
221 * the command that we are actually dealing with
222 * @param result
223 * the result structure for ordinary results and errors
224 */
225 private void useOperationHide(TaggedElement xml, TaggedElement command, Result result) {
226 // Attributes are in Map's
227 // attribute - this command's attributes
228 // useAttribute - the <use> attributes
229 // createAttribute - the <create> attributes
230
231 //{{WriteMO code start [useOperationHide]}}
232 //TODO - Replace this line with your code
233 //{{WriteMO code end [useOperationHide]}}
234 }
235 //{{WriteMO user operations end}}
236
237 // Add any new methods etc after this line
238 }
