1 package org.apache.maven.continuum.web.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
40
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 }