1 | package org.apache.maven.continuum.web.action.notifier; |
2 | |
3 | /* |
4 | * Licensed to the Apache Software Foundation (ASF) under one |
5 | * or more contributor license agreements. See the NOTICE file |
6 | * distributed with this work for additional information |
7 | * regarding copyright ownership. The ASF licenses this file |
8 | * to you under the Apache License, Version 2.0 (the |
9 | * "License"); you may not use this file except in compliance |
10 | * with the License. You may obtain a copy of the License at |
11 | * |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | * |
14 | * Unless required by applicable law or agreed to in writing, |
15 | * software distributed under the License is distributed on an |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
17 | * KIND, either express or implied. See the License for the |
18 | * specific language governing permissions and limitations |
19 | * under the License. |
20 | */ |
21 | |
22 | import java.util.Map; |
23 | |
24 | import org.apache.maven.continuum.ContinuumException; |
25 | import org.apache.maven.continuum.model.project.ProjectNotifier; |
26 | import org.apache.maven.continuum.web.action.ContinuumActionSupport; |
27 | import org.apache.maven.continuum.web.exception.AuthorizationRequiredException; |
28 | |
29 | /** |
30 | * Common base class that consolidates the common properties used by extending |
31 | * <code>XXXEditAction</code> implementations and defines a contract expected of |
32 | * the extending clases. |
33 | * |
34 | * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a> |
35 | * @version $Id: AbstractNotifierEditActionSupport.java 766895 2009-04-20 22:02:13Z evenisse $ |
36 | * @since 1.1 |
37 | */ |
38 | public abstract class AbstractNotifierEditActionSupport |
39 | extends ContinuumActionSupport |
40 | { |
41 | /** |
42 | * Identifier for the {@link ProjectNotifier} instance being edited. |
43 | */ |
44 | private int notifierId; |
45 | |
46 | /** |
47 | * Type of {@link ProjectNotifier} tagged as a String value. |
48 | */ |
49 | private String notifierType; |
50 | |
51 | /** |
52 | * Detemines if the notifier should fire when build was successful.<p> |
53 | * <code>true</code> implies notifier executes on a successful build. |
54 | */ |
55 | private boolean sendOnSuccess; |
56 | |
57 | /** |
58 | * Detemines if the notifier should fire when build failed.<p> |
59 | * <code>true</code> implies notifier executes for a failed build. |
60 | */ |
61 | private boolean sendOnFailure; |
62 | |
63 | /** |
64 | * Detemines if the notifier should fire when build resulted in any error(s).<p> |
65 | * <code>true</code> implies notifier executes when any error(s) is/are detected |
66 | * for the build. |
67 | */ |
68 | private boolean sendOnError; |
69 | |
70 | /** |
71 | * Detemines if the notifier should fire when build resulted in any warning(s).<p> |
72 | * <code>true</code> implies notifier executes when any warning(s) is/are detected |
73 | * for the build. |
74 | */ |
75 | private boolean sendOnWarning; |
76 | |
77 | /** |
78 | * Detemines if the notifier should fire when prepare build resulted in any error(s).<p> |
79 | * <code>true</code> implies notifier executes when any error(s) is/are detected |
80 | * for the build. |
81 | */ |
82 | private boolean sendOnScmFailure; |
83 | |
84 | /** |
85 | * Detemines if the save operation returns to the project group notifier page or not.<p> |
86 | * <code>true</code> implies return to the project group notifier page. |
87 | */ |
88 | private boolean fromGroupPage = false; |
89 | |
90 | /** |
91 | * Obtain and return the {@link ProjectNotifier} instance for editing. |
92 | * |
93 | * @return {@link ProjectNotifier} instance. |
94 | * @throws ContinuumException if there was error retrieving the target {@link ProjectNotifier} instance. |
95 | */ |
96 | protected abstract ProjectNotifier getNotifier() |
97 | throws ContinuumException; |
98 | |
99 | /** |
100 | * Persists update to the {@link ProjectNotifier} instance being edited. |
101 | * |
102 | * @param notifier {@link ProjectNotifier} to save. |
103 | * @throws ContinuumException if there was an error saving the |
104 | * {@link ProjectNotifier} instance. |
105 | */ |
106 | protected abstract void saveNotifier( ProjectNotifier notifier ) |
107 | throws ContinuumException; |
108 | |
109 | /** |
110 | * Creates or updates {@link ProjectNotifier} instance. |
111 | * |
112 | * @return result as String. |
113 | * @throws ContinuumException |
114 | */ |
115 | public String save() |
116 | throws ContinuumException |
117 | { |
118 | try |
119 | { |
120 | checkAuthorization(); |
121 | } |
122 | catch ( AuthorizationRequiredException authzE ) |
123 | { |
124 | addActionError( authzE.getMessage() ); |
125 | return REQUIRES_AUTHORIZATION; |
126 | } |
127 | |
128 | ProjectNotifier notifier = getNotifier(); |
129 | |
130 | boolean isNew = ( notifier == null || getNotifierId() == 0 ); |
131 | |
132 | if ( isNew ) |
133 | { |
134 | notifier = new ProjectNotifier(); |
135 | } |
136 | |
137 | notifier.setType( getNotifierType() ); |
138 | |
139 | notifier.setSendOnSuccess( isSendOnSuccess() ); |
140 | |
141 | notifier.setSendOnFailure( isSendOnFailure() ); |
142 | |
143 | notifier.setSendOnError( isSendOnError() ); |
144 | |
145 | notifier.setSendOnWarning( isSendOnWarning() ); |
146 | |
147 | notifier.setSendOnScmFailure( isSendOnScmFailure() ); |
148 | |
149 | setNotifierConfiguration( notifier ); |
150 | |
151 | saveNotifier( notifier ); |
152 | |
153 | if ( fromGroupPage ) |
154 | { |
155 | return "to_group_page"; |
156 | } |
157 | |
158 | return SUCCESS; |
159 | } |
160 | |
161 | /** |
162 | * Obtains the {@link ProjectNotifier} instance for edit purposes. |
163 | * |
164 | * @return result as String. |
165 | * @throws ContinuumException |
166 | */ |
167 | public String edit() |
168 | throws ContinuumException |
169 | { |
170 | ProjectNotifier notifier = getNotifier(); |
171 | |
172 | if ( notifier == null ) |
173 | { |
174 | notifier = new ProjectNotifier(); |
175 | } |
176 | |
177 | try |
178 | { |
179 | checkAuthorization(); |
180 | } |
181 | catch ( AuthorizationRequiredException authzE ) |
182 | { |
183 | addActionError( authzE.getMessage() ); |
184 | return REQUIRES_AUTHORIZATION; |
185 | } |
186 | |
187 | // setup Action fields |
188 | setNotifierType( notifier.getType() ); |
189 | |
190 | setSendOnSuccess( notifier.isSendOnSuccess() ); |
191 | |
192 | setSendOnFailure( notifier.isSendOnFailure() ); |
193 | |
194 | setSendOnError( notifier.isSendOnError() ); |
195 | |
196 | setSendOnWarning( notifier.isSendOnWarning() ); |
197 | |
198 | setSendOnScmFailure( notifier.isSendOnScmFailure() ); |
199 | |
200 | initConfiguration( notifier.getConfiguration() ); |
201 | |
202 | return SUCCESS; |
203 | } |
204 | |
205 | public int getNotifierId() |
206 | { |
207 | return notifierId; |
208 | } |
209 | |
210 | /** |
211 | * @return the notifierType |
212 | */ |
213 | public String getNotifierType() |
214 | { |
215 | return notifierType; |
216 | } |
217 | |
218 | /** |
219 | * @param notifierType the notifierType to set |
220 | */ |
221 | public void setNotifierType( String notifierType ) |
222 | { |
223 | this.notifierType = notifierType; |
224 | } |
225 | |
226 | /** |
227 | * @return the sendOnSuccess |
228 | */ |
229 | public boolean isSendOnSuccess() |
230 | { |
231 | return sendOnSuccess; |
232 | } |
233 | |
234 | /** |
235 | * @param sendOnSuccess the sendOnSuccess to set |
236 | */ |
237 | public void setSendOnSuccess( boolean sendOnSuccess ) |
238 | { |
239 | this.sendOnSuccess = sendOnSuccess; |
240 | } |
241 | |
242 | /** |
243 | * @return the sendOnFailure |
244 | */ |
245 | public boolean isSendOnFailure() |
246 | { |
247 | return sendOnFailure; |
248 | } |
249 | |
250 | /** |
251 | * @param sendOnFailure the sendOnFailure to set |
252 | */ |
253 | public void setSendOnFailure( boolean sendOnFailure ) |
254 | { |
255 | this.sendOnFailure = sendOnFailure; |
256 | } |
257 | |
258 | /** |
259 | * @return the sendOnError |
260 | */ |
261 | public boolean isSendOnError() |
262 | { |
263 | return sendOnError; |
264 | } |
265 | |
266 | /** |
267 | * @param sendOnError the sendOnError to set |
268 | */ |
269 | public void setSendOnError( boolean sendOnError ) |
270 | { |
271 | this.sendOnError = sendOnError; |
272 | } |
273 | |
274 | /** |
275 | * @return the sendOnWarning |
276 | */ |
277 | public boolean isSendOnWarning() |
278 | { |
279 | return sendOnWarning; |
280 | } |
281 | |
282 | /** |
283 | * @param sendOnWarning the sendOnWarning to set |
284 | */ |
285 | public void setSendOnWarning( boolean sendOnWarning ) |
286 | { |
287 | this.sendOnWarning = sendOnWarning; |
288 | } |
289 | |
290 | public boolean isSendOnScmFailure() |
291 | { |
292 | return sendOnScmFailure; |
293 | } |
294 | |
295 | public void setSendOnScmFailure( boolean sendOnScmFailure ) |
296 | { |
297 | this.sendOnScmFailure = sendOnScmFailure; |
298 | } |
299 | |
300 | /** |
301 | * @param notifierId the notifierId to set |
302 | */ |
303 | public void setNotifierId( int notifierId ) |
304 | { |
305 | this.notifierId = notifierId; |
306 | } |
307 | |
308 | /** |
309 | * @return the fromGroupPage |
310 | */ |
311 | public boolean isFromGroupPage() |
312 | { |
313 | return fromGroupPage; |
314 | } |
315 | |
316 | /** |
317 | * @param fromGroupPage the fromGroupPage to set |
318 | */ |
319 | public void setFromGroupPage( boolean fromGroupPage ) |
320 | { |
321 | this.fromGroupPage = fromGroupPage; |
322 | } |
323 | |
324 | /** |
325 | * Initialises the configuration map that the {@link ProjectNotifier} |
326 | * instance is to be inited with. |
327 | * |
328 | * @param configuration map of configuration key-value pairs. |
329 | */ |
330 | protected abstract void initConfiguration( Map<String, String> configuration ); |
331 | |
332 | /** |
333 | * Sets the configuration for the specified {@link ProjectNotifier} |
334 | * instance. |
335 | * |
336 | * @param notifier The project notifier. |
337 | * @see #initConfiguration(Map) |
338 | */ |
339 | protected abstract void setNotifierConfiguration( ProjectNotifier notifier ); |
340 | |
341 | protected abstract void checkAuthorization() |
342 | throws AuthorizationRequiredException, ContinuumException; |
343 | } |