View Javadoc

1   /**
2    *
3    */
4   package org.apache.maven.continuum.web.action.component;
5   
6   /*
7    * Licensed to the Apache Software Foundation (ASF) under one
8    * or more contributor license agreements.  See the NOTICE file
9    * distributed with this work for additional information
10   * regarding copyright ownership.  The ASF licenses this file
11   * to you under the Apache License, Version 2.0 (the
12   * "License"); you may not use this file except in compliance
13   * with the License.  You may obtain a copy of the License at
14   *
15   *   http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing,
18   * software distributed under the License is distributed on an
19   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20   * KIND, either express or implied.  See the License for the
21   * specific language governing permissions and limitations
22   * under the License.
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   * Component Action that prepares and provides Project Group Notifier and
42   * Project Notifier summaries.
43   *
44   * @author <a href='mailto:rahul.thakur.xdev@gmail.com'>Rahul Thakur</a>
45   * @version $Id: NotifierSummaryAction.java 1372260 2012-08-13 04:29:09Z brett $
46   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="notifierSummary"
47   */
48  public class NotifierSummaryAction
49      extends ContinuumActionSupport
50  {
51      private static final Logger logger = LoggerFactory.getLogger( NotifierSummaryAction.class );
52  
53      /**
54       * Identifier for the {@link ProjectGroup} for which the Notifier summary
55       * needs to be prepared for.
56       */
57      private int projectGroupId;
58  
59      /**
60       * Identifier for the {@link Project} for which the Notifier summary needs
61       * to be prepared for.
62       */
63      private int projectId;
64  
65      /**
66       * {@link ProjectGroup} instance to obtain the Notifier summary for.
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       * Prepare Notifier summary for a {@link Project}.
78       *
79       * @return
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      * Prepare Notifier summary for a {@link Project}.
107      *
108      * @param projectId The project id.
109      * @return
110      */
111     private List<NotifierSummary> summarizeForProject( int projectId )
112         throws ContinuumException
113     {
114         return gatherProjectNotifierSummaries( projectId );
115     }
116 
117     /**
118      * Prepare Notifier summary for a {@link ProjectGroup}.
119      *
120      * @return
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      * Prepares and returns a list of Notifier summaries for the specified Project Id.
157      *
158      * @param projectId The project id.
159      * @return List of {@link NotifierSummary} instance for the specified project.
160      * @throws ContinuumException if there was an error obtaining
161      *                            and preparing Notifier Summary list for the project
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      * Prepares and returns {@link ProjectGroup} summaries for the specified project group Id.
180      *
181      * @return
182      * @throws ContinuumException if there was an error fetching the {@link ProjectGroup} for specified Id.
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      * Prepares a {@link NotifierSummary} from a {@link ProjectNotifier} instance.
201      *
202      * @param notifier
203      * @return
204      */
205     private NotifierSummary generateProjectNotifierSummary( ProjectNotifier notifier, Project project )
206     {
207         return generateNotifierSummary( notifier, projectGroupId, project );
208     }
209 
210     /**
211      * Prepares a {@link NotifierSummary} from a {@link ProjectNotifier} instance.
212      *
213      * @param notifier
214      * @return
215      */
216     private NotifierSummary generateGroupNotifierSummary( ProjectNotifier notifier )
217     {
218         return generateNotifierSummary( notifier, projectGroupId, null );
219     }
220 
221     /**
222      * Prepares a {@link NotifierSummary} from a {@link ProjectNotifier} instance.
223      *
224      * @param notifier
225      * @return
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         // XXX: Hack - just for testing :)
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     // property accessors
297 
298     /**
299      * @return the projectGroupId
300      */
301     public int getProjectGroupId()
302     {
303         return projectGroupId;
304     }
305 
306     /**
307      * @param projectGroupId the projectGroupId to set
308      */
309     public void setProjectGroupId( int projectGroupId )
310     {
311         this.projectGroupId = projectGroupId;
312     }
313 
314     /**
315      * @return the projectId
316      */
317     public int getProjectId()
318     {
319         return projectId;
320     }
321 
322     /**
323      * @param projectId the projectId to set
324      */
325     public void setProjectId( int projectId )
326     {
327         this.projectId = projectId;
328     }
329 
330     /**
331      * @return the projectGroup
332      */
333     public ProjectGroup getProjectGroup()
334     {
335         return projectGroup;
336     }
337 
338     /**
339      * @param projectGroup the projectGroup to set
340      */
341     public void setProjectGroup( ProjectGroup projectGroup )
342     {
343         this.projectGroup = projectGroup;
344     }
345 
346     /**
347      * @return the projectGroupNotifierSummaries
348      */
349     public List<NotifierSummary> getProjectGroupNotifierSummaries()
350     {
351         return projectGroupNotifierSummaries;
352     }
353 
354     /**
355      * @param projectGroupNotifierSummaries the projectGroupNotifierSummaries to set
356      */
357     public void setProjectGroupNotifierSummaries( List<NotifierSummary> projectGroupNotifierSummaries )
358     {
359         this.projectGroupNotifierSummaries = projectGroupNotifierSummaries;
360     }
361 
362     /**
363      * @return the projectNotifierSummaries
364      */
365     public List<NotifierSummary> getProjectNotifierSummaries()
366     {
367         return projectNotifierSummaries;
368     }
369 
370     /**
371      * @param projectNotifierSummaries the projectNotifierSummaries to set
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 }