Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sample/addressbook/controller/contact/ContactCreateController.java


1   /*
2    * ContactCreateController.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.Iterator;
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.util.*;
25  import com.aendvari.common.model.*;
26  import com.aendvari.common.notices.Notices;
27  
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.model.contact.data.Contact;
42  
43  import com.sample.addressbook.controller.contact.validation.ContactValidation;
44  
45  import com.sample.addressbook.controller.components.EmailAddressesController;
46  
47  import com.sample.addressbook.Names;
48  import com.sample.addressbook.dumbdb.DumbDB;
49  
50  
51  /**
52   * <p>Prepares for displaying/submitting the form for creating a contact.</p>
53   *
54   * @author  Scott Milne
55   *
56   */
57  
58  public class ContactCreateController 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>ContactCreateController</code> instance.
72     *
73     */
74  
75    public ContactCreateController()
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       ModelNode modelNode = modelTree.getNode(modelTree.getRootNode(), model);
130 
131       // get the email addresses form node
132       ModelNode emfNode = modelTree.getNode(modelTree.getRootNode(), emailAddressesFormModel);
133 
134       // get the associations form node
135       ModelNode assocNode = modelTree.getNode(modelTree.getRootNode(), associatesFormModel);
136 
137       // clear <form> DOM
138       ModelUtil.removeNamedChildren(emfNode, "emailList");
139       ModelUtil.removeNamedChildren(assocNode, "associateList");
140       ModelUtil.clearNodes(modelNode);
141 
142       // ok we're done preparing, so send the message for display
143       MessageBrokerConnection connection = message.getContext().createConnection();
144 
145       // get the "display" topic"
146       String display = descriptor.getMessage("display").getTopic();
147 
148       // create the success message and send it
149       Message displayMessage = connection.createMessage();
150       MessageTopic topic = connection.getTopic(display);
151       connection.publish(topic, displayMessage);
152     }
153   }
154 
155   class ContactCopyFormListener implements MessageListener
156   {
157     public void onMessage(Message message)
158     {
159       // retrieve web objects
160       HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
161       HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
162       HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
163 
164       // get view model
165       ModelTree modelTree = ServletModelUtil.getModelTree(session);
166 
167       // get the node that is holding the form data
168       ModelNode messageNode = modelTree.getNode(modelTree.getRootNode(), model);
169 
170       // update the DOM with the form data
171       ModelUtil.osmToModel(message.getProperties(), "MainForm", messageNode, false);
172     }
173   }
174 
175   class ContactSubmitListener implements MessageListener
176   {
177     public void onMessage(Message message)
178     {
179       // retrieve web objects
180       HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
181       HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
182       HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
183 
184       // get view model
185       ModelTree modelTree = ServletModelUtil.getModelTree(session);
186       ModelNode messageNode = modelTree.getNode(modelTree.getRootNode(), model);
187 
188       // retrieve node to update
189       ModelUtil.osmToModel(message.getProperties(), null, messageNode, false);
190 
191       // validation the form data
192       Notices validationErrorMessages = ContactValidation.validate( messageNode );
193 
194       // were any messages produced
195       if (validationErrorMessages != null && validationErrorMessages.getNotices("*",true).size() > 0)
196       {
197         // ok we're done preparing, so send the message for display
198         MessageBrokerConnection connection = message.getContext().createConnection();
199 
200         // create the success message and send it
201         Message responseMessage = connection.createMessage();
202 
203         // set the error message list into the message
204         responseMessage.getProperties().setObject(
205           errorMessages,
206           validationErrorMessages
207         );
208 
209         // get the "display" topic"
210         String display = descriptor.getMessage("display").getTopic();
211 
212         // create the topic and public the message
213         MessageTopic topic = connection.getTopic(display);
214         connection.publish(topic, responseMessage);
215       }
216       // otherwise, continue with the modify
217       else
218       {
219         // ok, we're done, send the message to go back to the main page
220         MessageBrokerConnection connection = message.getContext().createConnection();
221 
222         // get the "next" topic
223         String next = descriptor.getMessage("next").getTopic();
224 
225         // create the success message and send it
226         Message displayMessage = connection.createMessage();
227         MessageTopic topic = connection.getTopic(next);
228         connection.publish(topic, displayMessage);
229       }
230     }
231   }
232 
233   class ContactCloseListener implements MessageListener
234   {
235     public void onMessage(Message message)
236     {
237       // retrieve web objects
238       HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
239       HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
240       HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
241 
242       // get view model
243       ModelTree modelTree = ServletModelUtil.getModelTree(session);
244       ModelNode modelNode = modelTree.getNode(modelTree.getRootNode(), model);
245 
246       // get the email addresses form node
247       ModelNode emfNode = modelTree.getNode(modelTree.getRootNode(), emailAddressesFormModel);
248 
249       // get the associations form node
250       ModelNode assocNode = modelTree.getNode(modelTree.getRootNode(), associatesFormModel);
251 
252       // retrieve node to update
253       ModelUtil.osmToModel(message.getProperties(), null, modelNode, false);
254 
255       // validation the form data
256       Notices validationErrorMessages = ContactValidation.validate( modelNode );
257 
258       // were any messages produced
259       if (validationErrorMessages != null && validationErrorMessages.getNotices("*",true).size() > 0)
260       {
261         // ok we're done preparing, so send the message for display
262         MessageBrokerConnection connection = message.getContext().createConnection();
263 
264         // create the success message and send it
265         Message responseMessage = connection.createMessage();
266 
267         // set the error message list into the message
268         responseMessage.getProperties().setObject(
269           errorMessages,
270           validationErrorMessages
271         );
272 
273         // get the "display" topic"
274         String display = descriptor.getMessage("display").getTopic();
275 
276         // create the topic and public the message
277         MessageTopic topic = connection.getTopic(display);
278         connection.publish(topic, responseMessage);
279       }
280       // otherwise, continue with the modify
281       else
282       {
283         // create a new contact instance and fill with the form data
284         Contact contactBean = new Contact();
285         try
286         {
287           contactBean =
288             (Contact)BeanTransformer.updateBeanFromModel( contactBean, modelNode );
289         }
290         catch(Exception ex)
291         {
292           ex.printStackTrace();
293         }
294 
295 
296         //
297         // Email Addresses
298         //
299 
300         // update the email addresses bean with the DOM data
301         EmailAddressesController.EmailAddressesForm emailAddressesForm =
302           new EmailAddressesController.EmailAddressesForm();
303 
304         try
305         {
306           emailAddressesForm =
307             (EmailAddressesController.EmailAddressesForm)
308               BeanTransformer.updateBeanFromModel( emailAddressesForm, emfNode );
309         }
310         catch(Exception ex)
311         {
312           ex.printStackTrace();
313         }
314 
315         // update the contact bean emails list with the form data
316         contactBean.getBasic().setEmail(emailAddressesForm.getEmailList());
317 
318 
319         //
320         // Associations
321         //
322 
323         // update the associations list with the DOM data
324         ContactAssociatesController.AssociatesForm assocForm =
325           new ContactAssociatesController.AssociatesForm();
326 
327         try
328         {
329           assocForm =
330             (ContactAssociatesController.AssociatesForm)
331               BeanTransformer.updateBeanFromModel( assocForm, assocNode );
332         }
333         catch(Exception ex)
334         {
335           ex.printStackTrace();
336         }
337 
338         // update the contact bean, setting the associations
339         contactBean.setAssociates(assocForm.getAssociateList());
340 
341 
342         //
343         // save to DB
344         //
345 
346         // get the db instance
347         DumbDB db = DumbDB.getDumbDB(request, Names.Constants.DumbDB);
348 
349         // create the unique id
350         String id = Integer.toString(db.newIncrement());
351 
352         // set it in the contact
353         contactBean.setId(id);
354 
355         // send the contact to the db
356         db.updateObjectById(id, contactBean);
357 
358         // ok we're done preparing, so send the message for display
359         MessageBrokerConnection connection = message.getContext().createConnection();
360 
361         // get the "next" topic"
362         String main = descriptor.getMessage("next").getTopic();
363 
364         // create the success message and send it
365         Message displayMessage = connection.createMessage();
366         MessageTopic topic = connection.getTopic(main);
367         connection.publish(topic, displayMessage);
368       }
369     }
370   }
371 }
372