Source code: com/sample/addressbook/view/components/EmailAddressesView.java
1 /*
2 * EmailAddressesView.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.view.components;
11
12 import java.util.Iterator;
13 import java.util.Collection;
14
15 import javax.servlet.*;
16 import javax.servlet.http.*;
17
18 import com.aendvari.common.util.*;
19 import com.aendvari.common.model.*;
20 import com.aendvari.common.notices.Notices;
21 import com.aendvari.common.notices.NoticeTranslator;
22 import com.aendvari.common.notices.ResourceNoticeTranslator;
23
24 import com.aendvari.tethys.context.*;
25 import com.aendvari.tethys.context.model.*;
26 import com.aendvari.tethys.context.message.*;
27
28 import com.aendvari.tethys.tag.context.*;
29 import com.aendvari.tethys.tag.model.*;
30 import com.aendvari.tethys.tag.message.*;
31 import com.aendvari.tethys.tag.notices.*;
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.aendvari.satyr.servlet.*;
42
43 import com.sample.addressbook.Names;
44
45
46 /**
47 * <p>Displays a modify/create emails form for editing.</p>
48 *
49 * @author Scott Milne
50 *
51 */
52
53 public class EmailAddressesView implements AssembledComponent
54 {
55 protected ComponentDescriptor descriptor;
56 protected String model;
57
58
59 /* Constructors. */
60
61
62 /**
63 * Constructs a <code>EmailAddressesView</code> instance.
64 *
65 */
66
67 public EmailAddressesView()
68 {
69 }
70
71 /**
72 * Creates the component based on the provided descriptor.
73 *
74 * The {@link AssemblyContext} object is transient and should not be retained
75 * by the component.
76 *
77 * The {@link ComponentDescriptor} may be retained by the component.
78 *
79 * @param context The {@link AssemblyContext} for this component.
80 * @param descriptor The {@link ComponentDescriptor} for this component.
81 *
82 */
83
84 public void createComponent(AssemblyContext context, ComponentDescriptor descriptor)
85 {
86 MessageBrokerConnection connection = context.getMessageBroker().createConnection();
87
88 // subscribe to the topic associated with the "display" message
89 connection.subscribe(descriptor.getMessage("display").getTopic(), new EmailAddressesListener());
90
91 // store descriptor
92 this.descriptor = descriptor;
93
94 // retrieve information
95 model = descriptor.getAttribute("model").getValue();
96 }
97
98 class EmailAddressesListener implements MessageListener
99 {
100 public void onMessage(Message message)
101 {
102 // retrieve web objects
103 HttpServletRequest request = HttpMessageBrokerContext.getRequest(message.getContext());
104 HttpServletResponse response = HttpMessageBrokerContext.getResponse(message.getContext());
105 HttpSession session = HttpMessageBrokerContext.getSession(message.getContext());
106
107 // obtain model tree
108 ModelTree modelTree = ServletModelUtil.getModelTree(session);
109
110 // set the message tag data for the page
111 MessageTagData messageTagData = MessageTagData.getData(request);
112
113 // get the model tag data
114 ModelTagData modelTagData = ModelTagData.getData(request);
115
116 // get the node
117 ModelNode modelNode = modelTree.getNode(modelTree.getRootNode(), model);
118
119 // check for error messages
120 checkForErrorMessages(message, request);
121
122 // get the page messages and create a mapping for them
123 String add = descriptor.getMessage("add").getTopic();
124 MessageMapping addMapping = new MessageMapping("add", add);
125 messageTagData.getMessageMappingMap().addContext(addMapping);
126
127 String modify = descriptor.getMessage("modify").getTopic();
128 MessageMapping modifyMapping = new MessageMapping("modify", modify);
129 messageTagData.getMessageMappingMap().addContext(modifyMapping);
130
131 String delete = descriptor.getMessage("delete").getTopic();
132 MessageMapping deleteMapping = new MessageMapping("delete", delete);
133 messageTagData.getMessageMappingMap().addContext(deleteMapping);
134
135 if (descriptor.getMessage("copyform") != null)
136 {
137 String copyform = descriptor.getMessage("copyform").getTopic();
138 MessageMapping copyformMapping = new MessageMapping("copyform", copyform);
139 messageTagData.getMessageMappingMap().addContext(copyformMapping);
140 }
141
142 // define a context for the addresses component
143 ModelContext tagContext = new ModelContext("emailaddresses", modelNode);
144 modelTagData.getModelContextMap().setContext(tagContext);
145 }
146
147 private void checkForErrorMessages(Message message, HttpServletRequest request)
148 {
149 // get the "errorMessage" topic
150 String errorMessagesPath = descriptor.getAttribute("errorMessages").getValue();
151
152 // get the error messages list
153 Notices errorMessages =
154 (Notices)message.getProperties().getObject(errorMessagesPath);
155
156 if (errorMessages != null && errorMessages.getNotices("*",true).size() > 0)
157 {
158 // get the "resourceNoticeTranslator" topic
159 String resourceNoticeTranslator = descriptor.getAttribute("resourceNoticeTranslator").getValue();
160
161 // create a message translator. This is used to extract and parse notice text from a .properties file
162 NoticeTranslator translator = new ResourceNoticeTranslator(resourceNoticeTranslator);
163
164 // get the tag data for notices
165 NoticesTagData tagData = NoticesTagData.getData(request);
166
167 // set a new mapping into the tag data
168 NoticesMap map = new NoticesMap(errorMessages, translator);
169 tagData.setNoticesMap(map);
170 }
171 }
172 }
173 }
174