1 package org.apache.maven.continuum.web.action.admin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.opensymphony.xwork2.Preparable;
23 import org.apache.continuum.buildmanager.BuildManagerException;
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.BuildQueue;
28 import org.apache.maven.continuum.security.ContinuumRoleConstants;
29 import org.apache.maven.continuum.web.action.ContinuumConfirmAction;
30 import org.codehaus.plexus.redback.rbac.Resource;
31 import org.codehaus.redback.integration.interceptor.SecureAction;
32 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
33 import org.codehaus.redback.integration.interceptor.SecureActionException;
34
35 import java.util.List;
36
37
38
39
40 public class BuildQueueAction
41 extends ContinuumConfirmAction
42 implements Preparable, SecureAction
43 {
44 private String name;
45
46 private int size;
47
48 private List<BuildQueue> buildQueueList;
49
50 private BuildQueue buildQueue;
51
52 private String message;
53
54 private boolean confirmed;
55
56 public void prepare()
57 throws ContinuumException
58 {
59 this.buildQueueList = getContinuum().getAllBuildQueues();
60 }
61
62 public String input()
63 {
64 return INPUT;
65 }
66
67 public String list()
68 throws Exception
69 {
70 try
71 {
72 this.buildQueueList = getContinuum().getAllBuildQueues();
73 }
74 catch ( ContinuumException e )
75 {
76 addActionError( "Cannot get build queues from the database : " + e.getMessage() );
77 return ERROR;
78 }
79 return SUCCESS;
80 }
81
82 public String save()
83 throws Exception
84 {
85 int allowedBuilds = getContinuum().getConfiguration().getNumberOfBuildsInParallel();
86 if ( allowedBuilds < ( this.buildQueueList.size() + 1 ) )
87 {
88 addActionError( "You are only allowed " + allowedBuilds + " number of builds in parallel." );
89 return ERROR;
90 }
91 else
92 {
93 try
94 {
95 if ( !isDuplicate( name ) )
96 {
97 BuildQueue buildQueue = new BuildQueue();
98 buildQueue.setName( name );
99 BuildQueue addedBuildQueue = getContinuum().addBuildQueue( buildQueue );
100
101 getContinuum().getBuildsManager().addOverallBuildQueue( addedBuildQueue );
102
103 AuditLog event = new AuditLog( "Build Queue id=" + addedBuildQueue.getId(),
104 AuditLogConstants.ADD_BUILD_QUEUE );
105 event.setCategory( AuditLogConstants.BUILD_QUEUE );
106 event.setCurrentUser( getPrincipal() );
107 event.log();
108 }
109 else
110 {
111 addActionError( "Build queue name already exists." );
112 return ERROR;
113 }
114 }
115 catch ( ContinuumException e )
116 {
117 addActionError( "Error adding build queue to database: " + e.getMessage() );
118 return ERROR;
119 }
120 catch ( BuildManagerException e )
121 {
122 addActionError( "Error creating overall build queue: " + e.getMessage() );
123 return ERROR;
124 }
125
126 return SUCCESS;
127 }
128 }
129
130 public String edit()
131 throws Exception
132 {
133 try
134 {
135 BuildQueue buildQueueToBeEdited = getContinuum().getBuildQueue( this.buildQueue.getId() );
136 }
137 catch ( ContinuumException e )
138 {
139 addActionError( "Error retrieving build queue from the database : " + e.getMessage() );
140 return ERROR;
141 }
142 return SUCCESS;
143 }
144
145 public String delete()
146 throws Exception
147 {
148 if ( confirmed )
149 {
150 BuildQueue buildQueueToBeDeleted = getContinuum().getBuildQueue( this.buildQueue.getId() );
151 getContinuum().getBuildsManager().removeOverallBuildQueue( buildQueueToBeDeleted.getId() );
152 getContinuum().removeBuildQueue( buildQueueToBeDeleted );
153
154 this.buildQueueList = getContinuum().getAllBuildQueues();
155
156 AuditLog event = new AuditLog( "Build Queue id=" + buildQueue.getId(),
157 AuditLogConstants.REMOVE_BUILD_QUEUE );
158 event.setCategory( AuditLogConstants.BUILD_QUEUE );
159 event.setCurrentUser( getPrincipal() );
160 event.log();
161 }
162 else
163 {
164 return CONFIRM;
165 }
166
167 return SUCCESS;
168 }
169
170 public SecureActionBundle getSecureActionBundle()
171 throws SecureActionException
172 {
173 SecureActionBundle bundle = new SecureActionBundle();
174 bundle.setRequiresAuthentication( true );
175 bundle.addRequiredAuthorization( ContinuumRoleConstants.CONTINUUM_MANAGE_PARALLEL_BUILDS, Resource.GLOBAL );
176
177 return bundle;
178 }
179
180 public String getName()
181 {
182 return name;
183 }
184
185 public void setName( String name )
186 {
187 this.name = name;
188 }
189
190 public List<BuildQueue> getBuildQueueList()
191 {
192 return buildQueueList;
193 }
194
195 public void setBuildQueueList( List<BuildQueue> buildQueueList )
196 {
197 this.buildQueueList = buildQueueList;
198 }
199
200 public int getSize()
201 {
202 return size;
203 }
204
205 public void setSize( int size )
206 {
207 this.size = size;
208 }
209
210 public BuildQueue getBuildQueue()
211 {
212 return buildQueue;
213 }
214
215 public void setBuildQueue( BuildQueue buildQueue )
216 {
217 this.buildQueue = buildQueue;
218 }
219
220 public String getMessage()
221 {
222 return message;
223 }
224
225 public void setMessage( String message )
226 {
227 this.message = message;
228 }
229
230 private boolean isDuplicate( String queueName )
231 throws ContinuumException
232 {
233 boolean isExisting = false;
234
235 List<BuildQueue> buildQueues = getContinuum().getAllBuildQueues();
236
237 for ( BuildQueue bq : buildQueues )
238 {
239 if ( queueName.equals( bq.getName() ) )
240 {
241 isExisting = true;
242 break;
243 }
244 }
245
246 return isExisting;
247 }
248
249 public boolean isConfirmed()
250 {
251 return confirmed;
252 }
253
254 public void setConfirmed( boolean confirmed )
255 {
256 this.confirmed = confirmed;
257 }
258 }