View Javadoc

1   package org.apache.maven.continuum.web.action;
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 org.apache.continuum.buildmanager.BuildManagerException;
23  import org.apache.continuum.buildmanager.BuildsManager;
24  import org.apache.maven.continuum.ContinuumException;
25  import org.apache.maven.continuum.configuration.ConfigurationService;
26  import org.apache.maven.continuum.model.project.BuildResult;
27  import org.apache.maven.continuum.model.project.Project;
28  import org.apache.maven.continuum.project.ContinuumProjectState;
29  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
30  import org.apache.maven.continuum.web.model.GroupSummary;
31  import org.apache.maven.continuum.web.model.ProjectSummary;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.Comparator;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   * Used to render the list of projects in the project group page.
44   *
45   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
46   * @version $Id: SummaryAction.java 1372260 2012-08-13 04:29:09Z brett $
47   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="summary"
48   */
49  public class SummaryAction
50      extends ContinuumActionSupport
51  {
52      private static final Logger logger = LoggerFactory.getLogger( SummaryAction.class );
53  
54      private int projectGroupId;
55  
56      private String projectGroupName;
57  
58      private List<ProjectSummary> summary;
59  
60      private GroupSummary groupSummary = new GroupSummary();
61  
62      /**
63       * @plexus.requirement role-hint="parallel"
64       */
65      private BuildsManager parallelBuildsManager;
66  
67      public String execute()
68          throws ContinuumException
69      {
70          try
71          {
72              checkViewProjectGroupAuthorization( getProjectGroupName() );
73          }
74          catch ( AuthorizationRequiredException authzE )
75          {
76              addActionError( authzE.getMessage() );
77              return REQUIRES_AUTHORIZATION;
78          }
79  
80          Collection<Project> projectsInGroup;
81  
82          //TODO: Create a summary jpox request so code will be more simple and performance will be better
83          projectsInGroup = getContinuum().getProjectsInGroup( projectGroupId );
84  
85          Map<Integer, BuildResult> buildResults = getContinuum().getLatestBuildResults( projectGroupId );
86  
87          Map<Integer, BuildResult> buildResultsInSuccess = getContinuum().getBuildResultsInSuccess( projectGroupId );
88  
89          summary = new ArrayList<ProjectSummary>();
90  
91          groupSummary.setNumErrors( 0 );
92          groupSummary.setNumFailures( 0 );
93          groupSummary.setNumSuccesses( 0 );
94          groupSummary.setNumProjects( 0 );
95  
96          for ( Project project : projectsInGroup )
97          {
98              groupSummary.setNumProjects( groupSummary.getNumProjects() + 1 );
99  
100             ProjectSummary model = new ProjectSummary();
101 
102             model.setId( project.getId() );
103 
104             model.setName( project.getName() );
105 
106             model.setVersion( project.getVersion() );
107 
108             model.setProjectGroupId( project.getProjectGroup().getId() );
109 
110             model.setProjectGroupName( project.getProjectGroup().getName() );
111 
112             model.setProjectType( project.getExecutorId() );
113 
114             try
115             {
116                 if ( parallelBuildsManager.isInAnyBuildQueue( project.getId() ) ||
117                     parallelBuildsManager.isInPrepareBuildQueue( project.getId() ) )
118                 {
119                     model.setInBuildingQueue( true );
120                 }
121                 else if ( parallelBuildsManager.isInAnyCheckoutQueue( project.getId() ) )
122                 {
123                     model.setInCheckoutQueue( true );
124                 }
125                 else
126                 {
127                     model.setInBuildingQueue( false );
128                     model.setInCheckoutQueue( false );
129                 }
130             }
131             catch ( BuildManagerException e )
132             {
133                 throw new ContinuumException( e.getMessage(), e );
134             }
135 
136             model.setState( project.getState() );
137 
138             model.setBuildNumber( project.getBuildNumber() );
139 
140             if ( buildResultsInSuccess != null )
141             {
142                 BuildResult buildInSuccess = buildResultsInSuccess.get( project.getId() );
143 
144                 if ( buildInSuccess != null )
145                 {
146                     model.setBuildInSuccessId( buildInSuccess.getId() );
147                 }
148             }
149 
150             if ( buildResults != null )
151             {
152                 BuildResult latestBuild = buildResults.get( project.getId() );
153 
154                 if ( latestBuild != null )
155                 {
156                     model.setLatestBuildId( latestBuild.getId() );
157                     populateGroupSummary( latestBuild );
158                     model.setLastBuildDateTime( latestBuild.getEndTime() );
159                     model.setLastBuildDuration( latestBuild.getDurationTime() );
160                 }
161 
162                 ConfigurationService configuration = getContinuum().getConfiguration();
163 
164                 if ( configuration.isDistributedBuildEnabled() && project.getState() == ContinuumProjectState.BUILDING )
165                 {
166                     model.setLatestBuildId( 0 );
167                 }
168             }
169 
170             summary.add( model );
171         }
172 
173         Comparator<ProjectSummary> projectComparator = new Comparator<ProjectSummary>()
174         {
175             public int compare( ProjectSummary ps1, ProjectSummary ps2 )
176             {
177                 return ps1.getName().compareTo( ps2.getName() );
178             }
179         };
180 
181         Collections.sort( summary, projectComparator );
182 
183         return SUCCESS;
184     }
185 
186     private void populateGroupSummary( BuildResult latestBuild )
187     {
188         switch ( latestBuild.getState() )
189         {
190             case ContinuumProjectState.ERROR:
191                 groupSummary.setNumErrors( groupSummary.getNumErrors() + 1 );
192                 break;
193             case ContinuumProjectState.OK:
194                 groupSummary.setNumSuccesses( groupSummary.getNumSuccesses() + 1 );
195                 break;
196             case ContinuumProjectState.FAILED:
197                 groupSummary.setNumFailures( groupSummary.getNumFailures() + 1 );
198                 break;
199             default:
200                 if ( latestBuild.getState() == 5 || latestBuild.getState() > 10 )
201                 {
202                     logger.warn(
203                         "unknown buildState value " + latestBuild.getState() + " with build " + latestBuild.getId() );
204                 }
205         }
206     }
207 
208     public List<ProjectSummary> getProjects()
209     {
210         return summary;
211     }
212 
213     public int getProjectGroupId()
214     {
215         return projectGroupId;
216     }
217 
218     public void setProjectGroupId( int projectGroupId )
219     {
220         this.projectGroupId = projectGroupId;
221     }
222 
223     public String getProjectGroupName()
224     {
225         return projectGroupName;
226     }
227 
228     public void setProjectGroupName( String projectGroupName )
229     {
230         this.projectGroupName = projectGroupName;
231     }
232 
233     public GroupSummary getGroupSummary()
234     {
235         return groupSummary;
236     }
237 
238     public void setGroupSummary( GroupSummary groupSummary )
239     {
240         this.groupSummary = groupSummary;
241     }
242 
243     // test
244     public void setParallelBuildsManager( BuildsManager parallelBuildsManager )
245     {
246         this.parallelBuildsManager = parallelBuildsManager;
247     }
248 }