1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.apache.shale.examples.mailreader; |
21 | |
|
22 | |
import javax.faces.application.FacesMessage; |
23 | |
import org.apache.commons.logging.Log; |
24 | |
import org.apache.commons.logging.LogFactory; |
25 | |
import org.apache.struts.apps.mailreader.dao.User; |
26 | |
import org.apache.struts.apps.mailreader.dao.UserDatabase; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class Subscription extends BaseViewController { |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | private static final Log log = LogFactory.getLog(Subscription.class); |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | private boolean autoConnect = false; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public boolean isAutoConnect() { |
56 | 0 | return this.autoConnect; |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public void setAutoConnect(boolean autoConnect) { |
63 | 0 | this.autoConnect = autoConnect; |
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | 0 | private String host = null; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public String getHost() { |
76 | 0 | return this.host; |
77 | |
} |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void setHost(String host) { |
83 | 0 | this.host = host; |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 0 | private String password = null; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public String getPassword() { |
96 | 0 | return this.password; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public void setPassword(String password) { |
103 | 0 | this.password = password; |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | 0 | private String type = null; |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public String getType() { |
116 | 0 | return this.type; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void setType(String type) { |
123 | 0 | this.type = type; |
124 | 0 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | 0 | private String username = null; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public String getUsername() { |
136 | 0 | return this.username; |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public void setUsername(String username) { |
143 | 0 | this.username = username; |
144 | 0 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public String cancel() { |
154 | |
|
155 | 0 | if ("CREATE".equals(getState().getMode())) { |
156 | 0 | return "welcome"; |
157 | |
} else { |
158 | 0 | getState().setMode("EDIT"); |
159 | 0 | return "registration"; |
160 | |
} |
161 | |
|
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public String save() { |
169 | |
|
170 | 0 | State state = getState(); |
171 | 0 | UserDatabase database = getUserDatabase(); |
172 | 0 | String mode = state.getMode(); |
173 | 0 | User user = state.getUser(); |
174 | 0 | org.apache.struts.apps.mailreader.dao.Subscription subscription = |
175 | |
user.findSubscription(state.getHost()); |
176 | |
|
177 | 0 | if ("CREATE".equals(mode)) { |
178 | |
|
179 | |
|
180 | 0 | if (user.findSubscription(host) != null) { |
181 | |
|
182 | 0 | getFacesContext().addMessage("subscription:host", |
183 | |
new FacesMessage("That hostname is already defined")); |
184 | 0 | return null; |
185 | |
} |
186 | |
|
187 | |
|
188 | 0 | subscription = user.createSubscription(host); |
189 | 0 | Registration registration = (Registration)getBean("registration"); |
190 | 0 | user = getState().getUser(); |
191 | 0 | registration.setFromAddress(user.getFromAddress()); |
192 | 0 | registration.setFullName(user.getFullName()); |
193 | 0 | registration.setPassword(user.getPassword()); |
194 | 0 | registration.setPassword2(user.getPassword()); |
195 | 0 | registration.setReplyToAddress(user.getReplyToAddress()); |
196 | 0 | registration.setSubscriptions(user.getSubscriptions()); |
197 | 0 | registration.setUsername(user.getUsername()); |
198 | |
|
199 | 0 | } else if ("DELETE".equals(mode)) { |
200 | |
|
201 | 0 | user.removeSubscription(subscription); |
202 | |
try { |
203 | 0 | database.save(); |
204 | 0 | } catch (Exception e) { |
205 | 0 | getFacesContext().addMessage(null, |
206 | |
new FacesMessage(e.getMessage())); |
207 | 0 | log.error("Database save exception", e); |
208 | 0 | return null; |
209 | 0 | } |
210 | 0 | state.setMode("EDIT"); |
211 | 0 | return "registration"; |
212 | |
|
213 | |
} else { |
214 | |
|
215 | |
; |
216 | |
|
217 | |
} |
218 | |
|
219 | |
|
220 | 0 | subscription.setUsername(username); |
221 | 0 | subscription.setPassword(password); |
222 | 0 | subscription.setType(type); |
223 | 0 | subscription.setAutoConnect(autoConnect); |
224 | |
|
225 | |
|
226 | |
try { |
227 | 0 | database.save(); |
228 | 0 | } catch (Exception e) { |
229 | 0 | getFacesContext().addMessage(null, |
230 | |
new FacesMessage(e.getMessage())); |
231 | 0 | log.error("Database save exception", e); |
232 | 0 | return null; |
233 | 0 | } |
234 | |
|
235 | |
|
236 | 0 | state.setMode("EDIT"); |
237 | 0 | return "registration"; |
238 | |
|
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
public void prerender() { |
250 | |
|
251 | 0 | State state = getState(); |
252 | |
|
253 | |
|
254 | 0 | if (!"DELETE".equals(state.getMode()) && |
255 | |
!"EDIT".equals(state.getMode())) { |
256 | 0 | return; |
257 | |
} |
258 | |
|
259 | |
|
260 | 0 | User user = state.getUser(); |
261 | 0 | org.apache.struts.apps.mailreader.dao.Subscription subscription = |
262 | |
user.findSubscription(state.getHost()); |
263 | 0 | if (!isPostBack()) { |
264 | 0 | setHost(subscription.getHost()); |
265 | 0 | setUsername(subscription.getUsername()); |
266 | 0 | setPassword(subscription.getPassword()); |
267 | 0 | setType(subscription.getType()); |
268 | 0 | setAutoConnect(subscription.getAutoConnect()); |
269 | |
} |
270 | |
|
271 | 0 | } |
272 | |
|
273 | |
|
274 | |
} |