Source code: com/sample/addressbook/controller/contact/ContactModifyController.java
1 /*
2 * ContactModifyController.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.sample.addressbook.controller.contact;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14
15 import javax.servlet.*;
16 import javax.servlet.http.*;
17
18 import com.aendvari.satyr.servlet.*;
19 import com.aendvari.satyr.servlet.gateway.tethys.*;
20
21 import com.aendvari.griffin.util.XmlUtil;
22 import com.aendvari.griffin.bean.transform.BeanTransformer;
23
24 import com.aendvari.common.model.*;
25 import com.aendvari.common.notices.Notices;
26
27 import com.aendvari.tethys.context.*;
28 import com.aendvari.tethys.context.model.*;
29
30 import com.aendvari.tethys.tag.context.*;
31 import com.aendvari.tethys.tag.model.*;
32
33 import com.aendvari.cerberus.component.descriptor.ComponentDescriptor;
34
35 import com.aendvari.cerberus.component.assembly.AssembledComponent;
36 import com.aendvari.cerberus.component.assembly.AssemblyContext;
37
38 import com.aendvari.hermes.broker.*;
39 import com.aendvari.hermes.broker.http.*;
40
41 import com.sample.addressbook.Names;
42 import com.sample.addressbook.dumbdb.*;
43
44 import com.sample.addressbook.model.contact.data.Contact;
45
46 import com.sample.addressbook.controller.contact.validation.ContactValidation;
47
48 import com.sample.addressbook.controller.components.EmailAddressesController;
49
50
51 /**
52 * <p>Prepares for displaying/submitting a contact for editing.</p>
53 *
54 * @author Scott Milne
55 *
56 */
57
58 public class ContactModifyController implements AssembledComponent
59 {
60 protected ComponentDescriptor descriptor;
61 protected String model;
62 protected String errorMessages;
63 protected String emailAddressesFormModel;
64 protected String associatesFormModel;
65
66
67 /* Constructors. */
68
69
70 /**
71 * Constructs a <code>ContactModifyController</code> instance.
72 *
73 */
74
75 public ContactModifyController()
76 {
77 }
78
79 /**
80 * Creates the component based on the provided descriptor.
81 *
82 * The {@link AssemblyContext} object is transient and should not be retained
83 * by the component.
84 *
85 * The {@link ComponentDescriptor} may be retained by the component.
86 *
87 * @param context The {@link AssemblyContext} for this component.
88 * @param descriptor The {@link ComponentDescriptor} for this component.
89 *
90 */
91
92 public void createComponent(AssemblyContext context, ComponentDescriptor descriptor)
93 {
94 MessageBrokerConnection connection = context.getMessageBroker().createConnection();
95
96 // subscribe to the topic associated with the "prepare" message
97 connection.subscribe(descriptor.getMessage("prepare").getTopic(), new ContactPrepareListener());
98
99 // subscribe to the topic associated with the "copyform" message
100 connection.subscribe(descriptor.getMessage("copyform").getTopic(), new ContactCopyFormListener());
101
102 // subscribe to the topic associated with the "submit" message
103 connection.subscribe(descriptor.getMessage("submit").getTopic(), new ContactSubmitListener());
104
105 // subscribe to the topic associated with the "close" message
106 connection.subscribe(descriptor.getMessage("close").getTopic(), new ContactCloseListener());
107
108 // store descriptor
109 this.descriptor = descriptor;
110
111 // retrieve information
112 model = descriptor.getAttribute("model").getValue();
113 errorMessages = descriptor.getAttribute("errorMessages").getValue();
114 emailAddressesFormModel = descriptor.getAttribute("emailAddressesFormModel").getValue();
115 associatesFormModel = descriptor.getAttribute("associatesFormModel").getValue();
116 }
117
118 class ContactPrepareListener implements MessageListener
119 {
120 public void onMessage(Message message)
121 {
122 // retrieve web objects
123 HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
124 HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
125 HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
126
127 // get view model
128 ModelTree modelTree = ServletModelUtil.getModelTree(session);
129
130 // get the contact node
131 ModelNode contactNode = modelTree.getNode(modelTree.getRootNode(), model);
132
133 // get the id from the form data
134 String contactId = message.getProperties().getString("itemId");
135
136 // get the email addresses form node
137 ModelNode emfNode = modelTree.getNode(modelTree.getRootNode(), emailAddressesFormModel);
138
139 // get the associations form node
140 ModelNode assocNode = modelTree.getNode(modelTree.getRootNode(), associatesFormModel);
141
142 // get the db instance
143 DumbDB db = DumbDB.getDumbDB(request, Names.Constants.DumbDB);
144
145 // get the contact from the db
146 Contact contactData = (Contact)db.getObjectById(contactId);
147
148 // just in case
149 if (contactData != null)
150 {
151 // load the node with "data" (from the bean)
152 try
153 {
154 BeanTransformer.beanToModel( contactData, contactNode, true );
155 }
156 catch( Exception exception )
157 {
158 exception.printStackTrace();
159 }
160
161
162 //
163 // Emails
164 //
165
166 // now extract the contact email addresses into the address form
167 EmailAddressesController.EmailAddressesForm addresses =
168 new EmailAddressesController.EmailAddressesForm();
169
170 // set the emails into the addresses form
171 addresses.setEmailList(contactData.getBasic().getEmail());
172
173 // load the node with "data" (from the bean)
174 try
175 {
176 BeanTransformer.beanToModel( addresses, emfNode, true );
177 }
178 catch( Exception exception )
179 {
180 exception.printStackTrace();
181 }
182
183
184 //
185 // Associations
186 //
187
188 // now extract the contact email addresses into the address form
189 ContactAssociatesController.AssociatesForm associatesForm =
190 new ContactAssociatesController.AssociatesForm();
191
192 // set the emails into the addresses form
193 associatesForm.setAssociateList(contactData.getAssociates());
194
195 // load the node with "data" (from the bean)
196 try
197 {
198 BeanTransformer.beanToModel( associatesForm, assocNode, true );
199 }
200 catch( Exception exception )
201 {
202 exception.printStackTrace();
203 }
204
205
206 //
207 // Completion
208 //
209
210 // ok we're done preparing, so send the message for display
211 MessageBrokerConnection connection = message.getContext().createConnection();
212
213 // get the "display" topic"
214 String display = descriptor.getMessage("display").getTopic();
215
216 // create the success message and send it
217 Message displayMessage = connection.createMessage();
218 MessageTopic topic = connection.getTopic(display);
219 connection.publish(topic, displayMessage);
220 }
221 }
222 }
223
224 class ContactSubmitListener implements MessageListener
225 {
226 public void onMessage(Message message)
227 {
228 // retrieve web objects
229 HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
230 HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
231 HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
232
233 // get view model
234 ModelTree modelTree = ServletModelUtil.getModelTree(session);
235
236 // get the node that is holding the form data
237 ModelNode messageNode = modelTree.getNode(modelTree.getRootNode(), model);
238
239 // retrieve node to update
240 ModelUtil.osmToModel(message.getProperties(), null, messageNode, false);
241
242 // validation the form data
243 Notices validationErrorMessages = ContactValidation.validate( messageNode );
244
245 // were any messages produced
246 if (validationErrorMessages != null && validationErrorMessages.getNotices("*",true).size() > 0)
247 {
248 // ok we're done preparing, so send the message for display
249 MessageBrokerConnection connection = message.getContext().createConnection();
250
251 // create the success message and send it
252 Message responseMessage = connection.createMessage();
253
254 // set the error message list into the message
255 responseMessage.getProperties().setObject(
256 errorMessages,
257 validationErrorMessages
258 );
259
260 // get the "display" topic"
261 String display = descriptor.getMessage("display").getTopic();
262
263 // create the topic and public the message
264 MessageTopic topic = connection.getTopic(display);
265 connection.publish(topic, responseMessage);
266 }
267 // otherwise, continue with the modify
268 else
269 {
270 // ok, we're done, send the message to go back to the main page
271 MessageBrokerConnection connection = message.getContext().createConnection();
272
273 // get the "next" topic
274 String next = descriptor.getMessage("next").getTopic();
275
276 // create the success message and send it
277 Message displayMessage = connection.createMessage();
278 MessageTopic topic = connection.getTopic(next);
279 connection.publish(topic, displayMessage);
280 }
281 }
282 }
283
284 class ContactCopyFormListener implements MessageListener
285 {
286 public void onMessage(Message message)
287 {
288 // retrieve web objects
289 HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
290 HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
291 HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
292
293 // get view model
294 ModelTree modelTree = ServletModelUtil.getModelTree(session);
295
296 // get the node that is holding the form data
297 ModelNode messageNode = modelTree.getNode(modelTree.getRootNode(), model);
298
299 // update the DOM with the form data
300 ModelUtil.osmToModel(message.getProperties(), "MainForm", messageNode, false);
301 }
302 }
303
304 class ContactCloseListener implements MessageListener
305 {
306 public void onMessage(Message message)
307 {
308 // retrieve web objects
309 HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
310 HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
311 HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
312
313 // get view model
314 ModelTree modelTree = ServletModelUtil.getModelTree(session);
315 ModelNode modelNode = modelTree.getNode(modelTree.getRootNode(), model);
316
317 // update the DOM with the form data
318 ModelUtil.osmToModel(message.getProperties(), null, modelNode, false);
319
320 // get the id node
321 ModelNode idNode = modelTree.getNode(modelNode, "id/text()");
322
323 // get the email addresses form node
324 ModelNode emfNode = modelTree.getNode(modelTree.getRootNode(), emailAddressesFormModel);
325
326 // get the associations form node
327 ModelNode assocNode = modelTree.getNode(modelTree.getRootNode(), associatesFormModel);
328
329 // get the id of the contact to use
330 String contactId = idNode.getNodeValue();
331
332 // validation the form data
333 Notices validationErrorMessages = ContactValidation.validate( modelNode );
334
335 // were any messages produced
336 if (validationErrorMessages != null && validationErrorMessages.getNotices("*",true).size() > 0)
337 {
338 // ok we're done preparing, so send the message for display
339 MessageBrokerConnection connection = message.getContext().createConnection();
340
341 // create the success message and send it
342 Message responseMessage = connection.createMessage();
343
344 // set the error message list into the message
345 responseMessage.getProperties().setObject(
346 errorMessages,
347 validationErrorMessages
348 );
349
350 // get the "display" topic"
351 String display = descriptor.getMessage("display").getTopic();
352
353 // create the topic and public the message
354 MessageTopic topic = connection.getTopic(display);
355 connection.publish(topic, responseMessage);
356 }
357 // otherwise, continue with the modify
358 else
359 {
360 // get the db instance
361 DumbDB db = DumbDB.getDumbDB(request, Names.Constants.DumbDB);
362
363 // get the current contact from the db
364 Contact contactBean = (Contact)db.getObjectById(contactId);
365
366 // just in case
367 if (contactBean != null)
368 {
369 // update contact bean with DOM data
370 try
371 {
372 contactBean =
373 (Contact)BeanTransformer.updateBeanFromModel( contactBean, modelNode );
374 }
375 catch(Exception ex)
376 {
377 ex.printStackTrace();
378 }
379
380
381 //
382 // Email Addresses
383 //
384
385 // update the email addresses bean with the DOM data
386 EmailAddressesController.EmailAddressesForm emailAddressesForm =
387 new EmailAddressesController.EmailAddressesForm();
388
389 try
390 {
391 emailAddressesForm =
392 (EmailAddressesController.EmailAddressesForm)
393 BeanTransformer.updateBeanFromModel( emailAddressesForm, emfNode );
394 }
395 catch(Exception ex)
396 {
397 ex.printStackTrace();
398 }
399
400 // update the contact bean emails list with the form data
401 contactBean.getBasic().setEmail(emailAddressesForm.getEmailList());
402
403
404 //
405 // Associations
406 //
407
408 // update the associations list with the DOM data
409 ContactAssociatesController.AssociatesForm assocForm =
410 new ContactAssociatesController.AssociatesForm();
411
412 try
413 {
414 assocForm =
415 (ContactAssociatesController.AssociatesForm)
416 BeanTransformer.updateBeanFromModel( assocForm, assocNode );
417 }
418 catch(Exception ex)
419 {
420 ex.printStackTrace();
421 }
422
423 // update the contact bean, setting the associations
424 contactBean.setAssociates(assocForm.getAssociateList());
425
426
427 //
428 // save to db
429 //
430
431 // send the contact to the db
432 db.updateObjectById(contactId, contactBean);
433
434 // ok we're done preparing, so send the message for display
435 MessageBrokerConnection connection = message.getContext().createConnection();
436
437 // get the "next" topic
438 String next = descriptor.getMessage("next").getTopic();
439
440 // create the success message and send it
441 Message displayMessage = connection.createMessage();
442 MessageTopic topic = connection.getTopic(next);
443 connection.publish(topic, displayMessage);
444 }
445 }
446 }
447 }
448 }
449