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

Quick Search    Search Deep

Source code: com/sample/tutorial/update/controller/update/UpdateController.java


1   /*
2    * UpdateController.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.tutorial.update.controller.update;
11  
12  import com.aendvari.cerberus.component.assembly.*;
13  import com.aendvari.cerberus.component.descriptor.*;
14  
15  import com.aendvari.common.model.*;
16  import com.aendvari.common.notices.*;
17  
18  import com.aendvari.hermes.broker.*;
19  
20  import com.aendvari.tethys.tag.*;
21  import com.aendvari.tethys.tag.notices.*;
22  
23  import com.aendvari.griffin.bean.transform.BeanTransformer;
24  
25  import com.sample.tutorial.update.database.*;
26  
27  /**
28   * Update controller.
29   *
30   * @author  Trevor Milne
31   *
32   */
33  
34  public class UpdateController implements AssembledComponent
35  {
36    protected ComponentDescriptor descriptor;
37    protected String model;
38  
39    /**
40     * Creates the component based on the provided descriptor.
41     *
42     * @param    context            The {@link AssemblyContext} for this component.
43     * @param    descriptor          The {@link ComponentDescriptor} for this component.
44     *
45     */
46  
47    public void createComponent(AssemblyContext context, ComponentDescriptor descriptor)
48    {
49      // create a connection to the message broker
50      MessageBrokerConnection connection = context.getMessageBroker().createConnection();
51  
52      // subscribe to the topic associated with the "open" message
53      connection.subscribe(descriptor.getMessage("open").getTopic(), new OpenListener());
54  
55      // subscribe to the topic associated with the "update" message
56      connection.subscribe(descriptor.getMessage("update").getTopic(), new UpdateListener());
57  
58      // store descriptor
59      this.descriptor = descriptor;
60  
61      // retrieve information
62      model = descriptor.getAttribute("model").getValue();
63    }
64  
65    protected class OpenListener implements MessageListener
66    {
67      /**
68       * Receives a message from the {@link MessageBroker}.
69       *
70       * @param    message            The {@link Message} received.
71       *
72       */
73  
74      public void onMessage(Message message)
75      {
76        // get model
77        ModelTree modelTree = (ModelTree)message.getContext().getProperties().getObject("model");
78        ModelNode modelNode = modelTree.getNode(modelTree.getRootNode(), model);
79  
80        // get contact data
81        Contact contact = Database.getInstance().getContact(1);
82  
83        // copy data into application model
84        try
85        {
86          BeanTransformer.beanToModel(contact, modelNode, true);
87        }
88        catch (Exception exception)
89        {
90          exception.printStackTrace();
91          return;
92        }
93  
94        // create connection to broker
95        MessageBrokerConnection connection = message.getContext().createConnection();
96  
97        // display contact information
98        MessageTopic topic = connection.getTopic(
99          descriptor.getMessage("show").getTopic());
100 
101       connection.publish(topic, connection.createMessage());
102     }
103   }
104 
105   protected class UpdateListener implements MessageListener
106   {
107     /**
108      * Receives a message from the {@link MessageBroker}.
109      *
110      * @param    message            The {@link Message} received.
111      *
112      */
113 
114     public void onMessage(Message message)
115     {
116       // get model
117       ModelTree modelTree = (ModelTree)message.getContext().getProperties().getObject("model");
118       ModelNode modelNode = modelTree.getNode(modelTree.getRootNode(), model);
119 
120       // update model with new values in message
121       ModelUtil.osmToModel(message.getProperties(), model, modelNode, false);
122 
123       // get contact data
124       Contact contact = Database.getInstance().getContact(1);
125 
126       // update contact data
127       try
128       {
129         BeanTransformer.updateBeanFromModel(contact, modelNode);
130       }
131       catch (Exception exception)
132       {
133         exception.printStackTrace();
134 
135         return;
136       }
137 
138       // update contact data
139       Database.getInstance().setContact(contact);
140 
141       // create connection to broker
142       MessageBrokerConnection connection = message.getContext().createConnection();
143 
144       // successful
145       MessageTopic topic = connection.getTopic(
146         descriptor.getMessage("success").getTopic());
147 
148       connection.publish(topic, connection.createMessage());
149     }
150   }
151 }
152