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 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
40
41
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 }