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.buildagent.NoBuildAgentException;
23  import org.apache.continuum.buildagent.NoBuildAgentInGroupException;
24  import org.apache.continuum.web.util.AuditLog;
25  import org.apache.continuum.web.util.AuditLogConstants;
26  import org.apache.maven.continuum.ContinuumException;
27  import org.apache.maven.continuum.model.project.BuildDefinition;
28  import org.apache.maven.continuum.model.project.Project;
29  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
30  import org.codehaus.plexus.util.StringUtils;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  /**
38   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
39   * @version $Id: ProjectsListAction.java 1372260 2012-08-13 04:29:09Z brett $
40   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="projects"
41   */
42  public class ProjectsListAction
43      extends ContinuumActionSupport
44  {
45      private static final Logger logger = LoggerFactory.getLogger( ProjectsListAction.class );
46  
47      private List<String> selectedProjects;
48  
49      private List<String> selectedProjectsNames;
50  
51      private String projectGroupName = "";
52  
53      private int projectGroupId;
54  
55      private String methodToCall;
56  
57      private int buildDefinitionId;
58  
59      public String execute()
60          throws Exception
61      {
62          if ( StringUtils.isEmpty( methodToCall ) )
63          {
64              return SUCCESS;
65          }
66  
67          if ( "build".equals( methodToCall ) )
68          {
69              return build();
70          }
71          else if ( "remove".equals( methodToCall ) )
72          {
73              return remove();
74          }
75          else if ( "confirmRemove".equals( methodToCall ) )
76          {
77              return confirmRemove();
78          }
79  
80          return SUCCESS;
81      }
82  
83      private String remove()
84          throws ContinuumException
85      {
86          try
87          {
88              checkModifyProjectGroupAuthorization( getProjectGroupName() );
89          }
90          catch ( AuthorizationRequiredException e )
91          {
92              return REQUIRES_AUTHORIZATION;
93          }
94  
95          if ( selectedProjects != null && !selectedProjects.isEmpty() )
96          {
97              for ( String selectedProject : selectedProjects )
98              {
99                  int projectId = Integer.parseInt( selectedProject );
100 
101                 try
102                 {
103                     AuditLog event = new AuditLog( "Project id=" + projectId, AuditLogConstants.REMOVE_PROJECT );
104                     event.setCategory( AuditLogConstants.PROJECT );
105                     event.setCurrentUser( getPrincipal() );
106                     event.log();
107 
108                     getContinuum().removeProject( projectId );
109                 }
110                 catch ( ContinuumException e )
111                 {
112                     logger.error( "Error removing Project with id=" + projectId );
113                     addActionError( getText( "deleteProject.error", "Unable to delete project", new Integer(
114                         projectId ).toString() ) );
115                 }
116             }
117         }
118 
119         return SUCCESS;
120     }
121 
122     public String confirmRemove()
123         throws ContinuumException
124     {
125         if ( selectedProjects != null && !selectedProjects.isEmpty() )
126         {
127             this.selectedProjectsNames = new ArrayList<String>();
128             for ( String selectedProject : selectedProjects )
129             {
130                 int projectId = Integer.parseInt( selectedProject );
131                 selectedProjectsNames.add( getContinuum().getProject( projectId ).getName() );
132             }
133         }
134         return "confirmRemove";
135     }
136 
137     private String build()
138         throws ContinuumException
139     {
140         try
141         {
142             checkModifyProjectGroupAuthorization( getProjectGroupName() );
143         }
144         catch ( AuthorizationRequiredException e )
145         {
146             return REQUIRES_AUTHORIZATION;
147         }
148 
149         if ( selectedProjects != null && !selectedProjects.isEmpty() )
150         {
151             ArrayList<Project> projectsList = new ArrayList<Project>();
152             for ( String pId : selectedProjects )
153             {
154                 int projectId = Integer.parseInt( pId );
155                 Project p = getContinuum().getProjectWithAllDetails( projectId );
156                 projectsList.add( p );
157 
158                 AuditLog event = new AuditLog( "Project id=" + projectId, AuditLogConstants.FORCE_BUILD );
159                 event.setCategory( AuditLogConstants.PROJECT );
160                 event.setCurrentUser( getPrincipal() );
161                 event.log();
162             }
163 
164             List<Project> sortedProjects = getContinuum().getProjectsInBuildOrder( projectsList );
165 
166             try
167             {
168                 if ( this.getBuildDefinitionId() <= 0 )
169                 {
170                     List<BuildDefinition> groupDefaultBDs = getContinuum().getDefaultBuildDefinitionsForProjectGroup(
171                         projectGroupId );
172                     getContinuum().buildProjectsWithBuildDefinition( sortedProjects, groupDefaultBDs );
173                 }
174                 else
175                 {
176                     getContinuum().buildProjectsWithBuildDefinition( sortedProjects, buildDefinitionId );
177                 }
178             }
179             catch ( NoBuildAgentException e )
180             {
181                 addActionError( getText( "projectGroup.build.error.noBuildAgent" ) );
182             }
183             catch ( NoBuildAgentInGroupException e )
184             {
185                 addActionError( getText( "projectGroup.build.error.noBuildAgentInGroup" ) );
186             }
187         }
188 
189         return SUCCESS;
190     }
191 
192     public String getProjectGroupName()
193         throws ContinuumException
194     {
195         if ( StringUtils.isEmpty( projectGroupName ) )
196         {
197             projectGroupName = getContinuum().getProjectGroup( projectGroupId ).getName();
198         }
199 
200         return projectGroupName;
201     }
202 
203     public List<String> getSelectedProjects()
204     {
205         return selectedProjects;
206     }
207 
208     public void setSelectedProjects( List<String> selectedProjects )
209     {
210         this.selectedProjects = selectedProjects;
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 void setMethodToCall( String methodToCall )
224     {
225         this.methodToCall = methodToCall;
226     }
227 
228     public int getBuildDefinitionId()
229     {
230         return buildDefinitionId;
231     }
232 
233     public void setBuildDefinitionId( int buildDefinitionId )
234     {
235         this.buildDefinitionId = buildDefinitionId;
236     }
237 
238     public List<String> getSelectedProjectsNames()
239     {
240         return selectedProjectsNames;
241     }
242 
243     public void setSelectedProjectsNames( List<String> selectedProjectsNames )
244     {
245         this.selectedProjectsNames = selectedProjectsNames;
246     }
247 }