1
2
3
4 package org.apache.maven.continuum.web.action.component;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import org.apache.continuum.web.util.GenerateRecipentNotifier;
26 import org.apache.maven.continuum.ContinuumException;
27 import org.apache.maven.continuum.model.project.Project;
28 import org.apache.maven.continuum.model.project.ProjectGroup;
29 import org.apache.maven.continuum.model.project.ProjectNotifier;
30 import org.apache.maven.continuum.web.action.ContinuumActionSupport;
31 import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
32 import org.apache.maven.continuum.web.model.NotifierSummary;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.List;
39
40
41
42
43
44
45
46
47
48 public class NotifierSummaryAction
49 extends ContinuumActionSupport
50 {
51 private static final Logger logger = LoggerFactory.getLogger( NotifierSummaryAction.class );
52
53
54
55
56
57 private int projectGroupId;
58
59
60
61
62
63 private int projectId;
64
65
66
67
68 private ProjectGroup projectGroup;
69
70 private List<NotifierSummary> projectGroupNotifierSummaries = new ArrayList<NotifierSummary>();
71
72 private List<NotifierSummary> projectNotifierSummaries = new ArrayList<NotifierSummary>();
73
74 private String projectGroupName = "";
75
76
77
78
79
80
81 public String summarizeForProject()
82 {
83 logger.debug( "Obtaining summary for Project Id: " + projectId );
84
85 try
86 {
87 checkViewProjectGroupAuthorization( getProjectGroupName() );
88
89 projectNotifierSummaries = summarizeForProject( projectId );
90 }
91 catch ( ContinuumException e )
92 {
93 logger.error( "Unable to prepare Notifier summaries for Project Id: " + projectId, e );
94 return ERROR;
95 }
96 catch ( AuthorizationRequiredException authzE )
97 {
98 addActionError( authzE.getMessage() );
99 return REQUIRES_AUTHORIZATION;
100 }
101
102 return SUCCESS;
103 }
104
105
106
107
108
109
110
111 private List<NotifierSummary> summarizeForProject( int projectId )
112 throws ContinuumException
113 {
114 return gatherProjectNotifierSummaries( projectId );
115 }
116
117
118
119
120
121
122 public String summarizeForProjectGroup()
123 {
124 logger.debug( "Obtaining summary for ProjectGroup Id:" + projectGroupId );
125
126 try
127 {
128 checkViewProjectGroupAuthorization( getProjectGroupName() );
129
130 projectGroupNotifierSummaries = gatherGroupNotifierSummaries();
131
132 Collection<Project> projects = getContinuum().getProjectsInGroup( projectGroupId );
133 if ( projects != null )
134 {
135 for ( Project project : projects )
136 {
137 projectNotifierSummaries.addAll( summarizeForProject( project.getId() ) );
138 }
139 }
140 }
141 catch ( ContinuumException e )
142 {
143 logger.error( "Unable to prepare Notifier summaries for ProjectGroup Id: " + projectGroupId, e );
144 return ERROR;
145 }
146 catch ( AuthorizationRequiredException authzE )
147 {
148 addActionError( authzE.getMessage() );
149 return REQUIRES_AUTHORIZATION;
150 }
151
152 return SUCCESS;
153 }
154
155
156
157
158
159
160
161
162
163 private List<NotifierSummary> gatherProjectNotifierSummaries( int projectId )
164 throws ContinuumException
165 {
166 List<NotifierSummary> summaryList = new ArrayList<NotifierSummary>();
167 Project project = getContinuum().getProjectWithAllDetails( projectId );
168
169 for ( ProjectNotifier pn : (List<ProjectNotifier>) project.getNotifiers() )
170 {
171 NotifierSummary ns = generateProjectNotifierSummary( pn, project );
172 summaryList.add( ns );
173 }
174
175 return summaryList;
176 }
177
178
179
180
181
182
183
184 private List<NotifierSummary> gatherGroupNotifierSummaries()
185 throws ContinuumException
186 {
187 List<NotifierSummary> summaryList = new ArrayList<NotifierSummary>();
188 projectGroup = getContinuum().getProjectGroupWithBuildDetails( projectGroupId );
189
190 for ( ProjectNotifier pn : (List<ProjectNotifier>) projectGroup.getNotifiers() )
191 {
192 NotifierSummary ns = generateGroupNotifierSummary( pn );
193 summaryList.add( ns );
194 }
195
196 return summaryList;
197 }
198
199
200
201
202
203
204
205 private NotifierSummary generateProjectNotifierSummary( ProjectNotifier notifier, Project project )
206 {
207 return generateNotifierSummary( notifier, projectGroupId, project );
208 }
209
210
211
212
213
214
215
216 private NotifierSummary generateGroupNotifierSummary( ProjectNotifier notifier )
217 {
218 return generateNotifierSummary( notifier, projectGroupId, null );
219 }
220
221
222
223
224
225
226
227 private NotifierSummary generateNotifierSummary( ProjectNotifier notifier, int projectGroupId, Project project )
228 {
229 NotifierSummary ns = new NotifierSummary();
230 ns.setId( notifier.getId() );
231 ns.setType( notifier.getType() );
232 ns.setProjectGroupId( projectGroupId );
233 if ( project != null )
234 {
235 ns.setProjectId( project.getId() );
236 ns.setProjectName( project.getName() );
237 }
238
239 if ( notifier.isFromProject() )
240 {
241 ns.setFromProject( true );
242 }
243 else
244 {
245 ns.setFromProject( false );
246 }
247
248 String recipient = GenerateRecipentNotifier.generate( notifier );
249
250 ns.setRecipient( recipient );
251
252
253 StringBuffer sb = new StringBuffer();
254 if ( notifier.isSendOnError() )
255 {
256 sb.append( "Error" );
257 }
258 if ( notifier.isSendOnFailure() )
259 {
260 if ( sb.length() > 0 )
261 {
262 sb.append( '/' );
263 }
264 sb.append( "Failure" );
265 }
266 if ( notifier.isSendOnSuccess() )
267 {
268 if ( sb.length() > 0 )
269 {
270 sb.append( '/' );
271 }
272 sb.append( "Success" );
273 }
274 if ( notifier.isSendOnWarning() )
275 {
276 if ( sb.length() > 0 )
277 {
278 sb.append( '/' );
279 }
280 sb.append( "Warning" );
281 }
282 if ( notifier.isSendOnScmFailure() )
283 {
284 if ( sb.length() > 0 )
285 {
286 sb.append( '/' );
287 }
288 sb.append( "SCM Failure" );
289 }
290 ns.setEvents( sb.toString() );
291
292 ns.setEnabled( notifier.isEnabled() );
293 return ns;
294 }
295
296
297
298
299
300
301 public int getProjectGroupId()
302 {
303 return projectGroupId;
304 }
305
306
307
308
309 public void setProjectGroupId( int projectGroupId )
310 {
311 this.projectGroupId = projectGroupId;
312 }
313
314
315
316
317 public int getProjectId()
318 {
319 return projectId;
320 }
321
322
323
324
325 public void setProjectId( int projectId )
326 {
327 this.projectId = projectId;
328 }
329
330
331
332
333 public ProjectGroup getProjectGroup()
334 {
335 return projectGroup;
336 }
337
338
339
340
341 public void setProjectGroup( ProjectGroup projectGroup )
342 {
343 this.projectGroup = projectGroup;
344 }
345
346
347
348
349 public List<NotifierSummary> getProjectGroupNotifierSummaries()
350 {
351 return projectGroupNotifierSummaries;
352 }
353
354
355
356
357 public void setProjectGroupNotifierSummaries( List<NotifierSummary> projectGroupNotifierSummaries )
358 {
359 this.projectGroupNotifierSummaries = projectGroupNotifierSummaries;
360 }
361
362
363
364
365 public List<NotifierSummary> getProjectNotifierSummaries()
366 {
367 return projectNotifierSummaries;
368 }
369
370
371
372
373 public void setProjectNotifierSummaries( List<NotifierSummary> projectNotifierSummaries )
374 {
375 this.projectNotifierSummaries = projectNotifierSummaries;
376 }
377
378 public String getProjectGroupName()
379 throws ContinuumException
380 {
381 if ( projectGroupName == null || "".equals( projectGroupName ) )
382 {
383 if ( projectGroupId != 0 )
384 {
385 projectGroupName = getContinuum().getProjectGroup( projectGroupId ).getName();
386 }
387 else
388 {
389 projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName();
390 }
391 }
392
393 return projectGroupName;
394 }
395 }