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/ContactAssociatesController.java


1   /*
2    * ContactAssociatesController.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  import java.util.TreeSet;
15  import java.util.Iterator;
16  
17  import javax.servlet.*;
18  import javax.servlet.http.*;
19  
20  import com.aendvari.satyr.servlet.*;
21  import com.aendvari.satyr.servlet.gateway.tethys.*;
22  
23  import com.aendvari.griffin.util.XmlUtil;
24  import com.aendvari.griffin.bean.transform.BeanTransformer;
25  
26  import com.aendvari.common.model.*;
27  import com.aendvari.common.notices.Notices;
28  
29  import com.aendvari.tethys.context.*;
30  import com.aendvari.tethys.context.model.*;
31  
32  import com.aendvari.tethys.tag.context.*;
33  import com.aendvari.tethys.tag.model.*;
34  
35  import com.aendvari.cerberus.component.descriptor.ComponentDescriptor;
36  
37  import com.aendvari.cerberus.component.assembly.AssembledComponent;
38  import com.aendvari.cerberus.component.assembly.AssemblyContext;
39  
40  import com.aendvari.hermes.broker.*;
41  import com.aendvari.hermes.broker.http.*;
42  
43  import com.sample.addressbook.Names;
44  import com.sample.addressbook.dumbdb.*;
45  
46  import com.sample.addressbook.model.contact.data.Contact;
47  
48  import com.sample.addressbook.controller.contact.validation.ContactValidation;
49  
50  import com.sample.addressbook.controller.components.EmailAddressesController;
51  
52  
53  /**
54   * <p>Prepares for displaying/modifying a contact's associations for editing.</p>
55   *
56   * @author  Scott Milne
57   *
58   */
59  
60  public class ContactAssociatesController implements AssembledComponent
61  {
62    protected ComponentDescriptor descriptor;
63    protected String model;
64    protected String errorMessages;
65  
66  
67    /* Constructors. */
68  
69  
70    /**
71     * Constructs a <code>ContactAssociatesController</code> instance.
72     *
73     */
74  
75    public ContactAssociatesController()
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 "select" message
97      connection.subscribe(descriptor.getMessage("select").getTopic(), new ContactSelectListener());
98  
99      // subscribe to the topic associated with the "delete" message
100     connection.subscribe(descriptor.getMessage("delete").getTopic(), new ContactDeleteListener());
101 
102     // subscribe to the topic associated with the "submit" message
103     connection.subscribe(descriptor.getMessage("submit").getTopic(), new ContactSubmitListener());
104 
105     // store descriptor
106     this.descriptor = descriptor;
107 
108     // retrieve information
109     model = descriptor.getAttribute("model").getValue();
110     errorMessages = descriptor.getAttribute("errorMessages").getValue();
111   }
112 
113   private AssociatesForm getAssociatesForm( Message message, ModelTree modelTree )
114   {
115     // get the new email node
116     ModelNode modelNode = modelTree.getNode(modelTree.getRootNode(), model);
117 
118     // retrieve node to update
119     ModelUtil.osmToModel(message.getProperties(), null, modelNode, false);
120 
121     // update the bean with the node data
122     AssociatesForm associatesForm = new AssociatesForm();
123     try
124     {
125       associatesForm = (AssociatesForm)BeanTransformer.updateBeanFromModel(
126         associatesForm, modelNode );
127     }
128     catch(Exception ex)
129     {
130       ex.printStackTrace();
131     }
132 
133     return associatesForm;
134   }
135 
136   public static class AssociatesForm
137   {
138     private String selectedId;
139     private TreeSet associateList;
140 
141     public AssociatesForm()
142     {
143       associateList = new TreeSet();
144       selectedId = "";
145     }
146 
147     public String getSelectedId() { return selectedId; }
148     public void setSelectedId( String param) { selectedId = param; }
149 
150     public TreeSet getAssociateList() { return associateList; }
151     public void setAssociateList(TreeSet param) { associateList = param; }
152 
153     public void addAssociate(Contact contact) { associateList.add(contact); }
154     public void removeAssociateById(String id)
155     {
156       TreeSet list = new TreeSet(associateList);
157 
158       Iterator keysIterator = list.iterator();
159 
160       while (keysIterator.hasNext())
161       {
162         Contact contact = (Contact)keysIterator.next();
163 
164         if (contact.getId().compareToIgnoreCase(id) == 0)
165         {
166           associateList.remove(contact);
167         }
168       }
169     }
170   }
171 
172   class ContactSelectListener implements MessageListener
173   {
174     public void onMessage(Message message)
175     {
176       // retrieve web objects
177       HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
178       HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
179       HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
180 
181       // get view model
182       ModelTree modelTree = ServletModelUtil.getModelTree(session);
183 
184       // get the email addresses form node
185       ModelNode assocNode = modelTree.getNode(modelTree.getRootNode(), model);
186 
187       // retrieve node to update
188       ModelUtil.osmToModel(message.getProperties(), null, assocNode, false);
189 
190       // get the id from the form data
191       String contactId = message.getProperties().getString("itemId");
192 
193       // get the db instance
194       DumbDB db = DumbDB.getDumbDB(request, Names.Constants.DumbDB);
195 
196       // get the contact from the db
197       Contact contactData = (Contact)db.getObjectById(contactId);
198 
199       // just in case
200       if (contactData != null)
201       {
202         // get the form data, and add the new association to the list
203         AssociatesForm assocForm = getAssociatesForm(message, modelTree);
204         assocForm.addAssociate(contactData);
205 
206         try
207         {
208           BeanTransformer.beanToModel( assocForm, assocNode, true );
209         }
210         catch( Exception exception )
211         {
212           exception.printStackTrace();
213         }
214 
215         // ok we're done preparing, so send the message for display
216         MessageBrokerConnection connection = message.getContext().createConnection();
217 
218         // get the "display" topic"
219         String display = descriptor.getMessage("display").getTopic();
220 
221         // create the success message and send it
222         Message displayMessage = connection.createMessage();
223         MessageTopic topic = connection.getTopic(display);
224         connection.publish(topic, displayMessage);
225       }
226     }
227   }
228 
229   class ContactDeleteListener implements MessageListener
230   {
231     public void onMessage(Message message)
232     {
233       // retrieve web objects
234       HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
235       HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
236       HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
237 
238       // get view model
239       ModelTree modelTree = ServletModelUtil.getModelTree(session);
240 
241       // get the email addresses form node
242       ModelNode assocNode = modelTree.getNode(modelTree.getRootNode(), model);
243 
244       // get the form data
245       AssociatesForm form = getAssociatesForm(message, modelTree);
246 
247       // get the form data, and remove the association to the list
248       AssociatesForm assocForm = getAssociatesForm(message, modelTree);
249       assocForm.removeAssociateById(form.getSelectedId());
250 
251       // update the model tree
252       try
253       {
254         BeanTransformer.beanToModel( assocForm, assocNode, true );
255       }
256       catch( Exception exception )
257       {
258         exception.printStackTrace();
259       }
260 
261       // ok we're done preparing, so send the message for display
262       MessageBrokerConnection connection = message.getContext().createConnection();
263 
264       // get the "display" topic"
265       String display = descriptor.getMessage("display").getTopic();
266 
267       // create the success message and send it
268       Message displayMessage = connection.createMessage();
269       MessageTopic topic = connection.getTopic(display);
270       connection.publish(topic, displayMessage);
271     }
272   }
273 
274   class ContactSubmitListener implements MessageListener
275   {
276     public void onMessage(Message message)
277     {
278       // ok we're done, so send the message for next
279       MessageBrokerConnection connection = message.getContext().createConnection();
280 
281       // get the "next" topic
282       String next = descriptor.getMessage("next").getTopic();
283 
284       // create the success message and send it
285       Message displayMessage = connection.createMessage();
286       MessageTopic topic = connection.getTopic(next);
287       connection.publish(topic, displayMessage);
288     }
289   }
290 }
291