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