View Javadoc

1   package org.apache.maven.continuum.web.action.admin;
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 com.opensymphony.xwork2.Preparable;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.LinkedList;
28  import java.util.List;
29  
30  import org.apache.commons.lang.StringEscapeUtils;
31  import org.apache.continuum.web.util.AuditLog;
32  import org.apache.continuum.web.util.AuditLogConstants;
33  import org.apache.maven.continuum.ContinuumException;
34  import org.apache.maven.continuum.builddefinition.BuildDefinitionServiceException;
35  import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
36  import org.apache.maven.continuum.model.project.BuildDefinition;
37  import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
38  import org.apache.maven.continuum.model.project.Schedule;
39  import org.apache.maven.continuum.model.system.Profile;
40  import org.apache.maven.continuum.security.ContinuumRoleConstants;
41  import org.apache.maven.continuum.web.action.AbstractBuildDefinitionAction;
42  import org.apache.maven.continuum.web.model.BuildDefinitionSummary;
43  import org.codehaus.plexus.redback.rbac.Resource;
44  import org.codehaus.redback.integration.interceptor.SecureAction;
45  import org.codehaus.redback.integration.interceptor.SecureActionBundle;
46  import org.codehaus.redback.integration.interceptor.SecureActionException;
47  
48  /**
49   * @author <a href="mailto:olamy@apache.org">olamy</a>
50   * @version $Id: BuildDefinitionTemplateAction.java 1101669 2011-05-10 22:46:21Z ctan $
51   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="buildDefinitionTemplates"
52   * @since 16 sept. 07
53   */
54  public class BuildDefinitionTemplateAction
55      extends AbstractBuildDefinitionAction
56      implements SecureAction, Preparable
57  {
58      private List<BuildDefinitionTemplate> templates;
59  
60      private BuildDefinitionTemplate buildDefinitionTemplate;
61  
62      private List<String> buildDefinitionTypes;
63  
64      private List<BuildDefinitionSummary> buildDefinitionSummaries;
65  
66      private BuildDefinition buildDefinition;
67  
68      private Collection<Schedule> schedules;
69  
70      private List<Profile> profiles;
71  
72      private List<String> selectedBuildDefinitionIds;
73  
74      private List<BuildDefinition> buildDefinitions;
75  
76      // -------------------------------------------------------
77      //  Webwork Methods
78      // ------------------------------------------------------- 
79  
80      @Override
81      public void prepare()
82          throws Exception
83      {
84          super.prepare();
85          buildDefinitionTypes = new LinkedList<String>();
86          buildDefinitionTypes.add( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
87          buildDefinitionTypes.add( ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR );
88          buildDefinitionTypes.add( ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR );
89          buildDefinitionTypes.add( ContinuumBuildExecutorConstants.SHELL_BUILD_EXECUTOR );
90          this.setSchedules( getContinuum().getSchedules() );
91          this.setProfiles( getContinuum().getProfileService().getAllProfiles() );
92          this.setBuildDefinitions( getContinuum().getBuildDefinitionService().getAllTemplates() );
93      }
94  
95      @Override
96      public String input()
97          throws Exception
98      {
99          return INPUT;
100     }
101 
102     public String summary()
103         throws Exception
104     {
105         this.templates = getContinuum().getBuildDefinitionService().getAllBuildDefinitionTemplate();
106         List<BuildDefinition> buildDefinitions = getContinuum().getBuildDefinitionService().getAllTemplates();
107         this.buildDefinitionSummaries = generateBuildDefinitionSummaries( buildDefinitions );
108         return SUCCESS;
109     }
110 
111     public String edit()
112         throws Exception
113     {
114         this.buildDefinitionTemplate = getContinuum().getBuildDefinitionService().getBuildDefinitionTemplate(
115             this.buildDefinitionTemplate.getId() );
116         this.setBuildDefinitions( getContinuum().getBuildDefinitionService().getAllTemplates() );
117         this.selectedBuildDefinitionIds = new ArrayList<String>();
118         if ( this.buildDefinitionTemplate.getBuildDefinitions() != null )
119         {
120             for ( BuildDefinition bd : (List<BuildDefinition>) buildDefinitionTemplate.getBuildDefinitions() )
121             {
122                 this.selectedBuildDefinitionIds.add( Integer.toString( bd.getId() ) );
123             }
124         }
125         List<BuildDefinition> nonUsedBuildDefinitions = new ArrayList<BuildDefinition>();
126         for ( BuildDefinition buildDefinition : getBuildDefinitions() )
127         {
128             if ( !getSelectedBuildDefinitionIds().contains( Integer.toString( buildDefinition.getId() ) ) )
129             {
130                 nonUsedBuildDefinitions.add( buildDefinition );
131             }
132         }
133         this.setBuildDefinitions( nonUsedBuildDefinitions );
134         return SUCCESS;
135     }
136 
137     public String save()
138         throws Exception
139     {
140         List<BuildDefinition> selectedBuildDefinitions = getBuildDefinitionsFromSelectedBuildDefinitions();
141         
142         BuildDefinitionTemplate result;
143         
144         AuditLog event = new AuditLog( buildDefinitionTemplate.getName(), AuditLogConstants.ADD_TEMPLATE );
145         event.setCategory( AuditLogConstants.TEMPLATE );
146         event.setCurrentUser( getPrincipal() );
147 
148         if ( this.buildDefinitionTemplate.getId() > 0 )
149         {
150             buildDefinitionTemplate.setBuildDefinitions( selectedBuildDefinitions );
151             result = this.getContinuum().getBuildDefinitionService().updateBuildDefinitionTemplate( buildDefinitionTemplate );
152             event.setAction( AuditLogConstants.MODIFY_TEMPLATE );
153         }
154         else
155         {
156             buildDefinitionTemplate.setBuildDefinitions( selectedBuildDefinitions );
157             this.buildDefinitionTemplate =
158                 this.getContinuum().getBuildDefinitionService().addBuildDefinitionTemplate( buildDefinitionTemplate );
159             result = this.buildDefinitionTemplate;
160         }
161         
162         if ( result == null )
163         {
164             addActionError( getText( "buildDefintionTemplate.name.exists" ) );
165             return INPUT;
166         }
167         else
168         {
169             event.log();
170         }
171 
172         return SUCCESS;
173     }
174 
175     public String delete()
176         throws BuildDefinitionServiceException
177     {
178         if ( confirmed )
179         {
180             buildDefinitionTemplate = getContinuum().getBuildDefinitionService().getBuildDefinitionTemplate(
181                 this.buildDefinitionTemplate.getId() );
182 
183             AuditLog event = new AuditLog( buildDefinitionTemplate.getName(), AuditLogConstants.REMOVE_TEMPLATE );
184             event.setCategory( AuditLogConstants.TEMPLATE );
185             event.setCurrentUser( getPrincipal() );
186             event.log();
187 
188             this.getContinuum().getBuildDefinitionService().removeBuildDefinitionTemplate( buildDefinitionTemplate );
189         }
190         else
191         {
192             return CONFIRM;
193         }
194         return SUCCESS;
195     }
196 
197     private List<BuildDefinition> getBuildDefinitionsFromSelectedBuildDefinitions()
198         throws ContinuumException
199     {
200         if ( this.selectedBuildDefinitionIds == null )
201         {
202             return Collections.EMPTY_LIST;
203         }
204         List<BuildDefinition> selectedBuildDefinitions = new ArrayList<BuildDefinition>();
205         for ( String selectedBuildDefinitionId : selectedBuildDefinitionIds )
206         {
207             BuildDefinition buildDefinition =
208                 getContinuum().getBuildDefinition( Integer.parseInt( selectedBuildDefinitionId ) );
209             selectedBuildDefinitions.add( buildDefinition );
210         }
211         return selectedBuildDefinitions;
212     }
213 
214     // -----------------------------------------------------
215     //  BuildDefinition
216     // -----------------------------------------------------
217 
218     public String inputBuildDefinition()
219     {
220         return INPUT;
221     }
222 
223     public String editBuildDefinition()
224         throws Exception
225     {
226         this.buildDefinition =
227             getContinuum().getBuildDefinitionService().getBuildDefinition( this.buildDefinition.getId() );
228         return SUCCESS;
229     }
230 
231     public String saveBuildDefinition()
232         throws Exception
233     {
234         Schedule schedule = null;
235 
236         // need to escape xml to prevent xss attacks
237         buildDefinition.setDescription( StringEscapeUtils.escapeXml( StringEscapeUtils.unescapeXml( buildDefinition.getDescription() ) ) );
238 
239         if ( buildDefinition.getProfile() != null )
240         {
241             Profile profile = getContinuum().getProfileService().getProfile( buildDefinition.getProfile().getId() );
242             if ( profile != null )
243             {
244                 buildDefinition.setProfile( profile );
245             }
246             else
247             {
248                 buildDefinition.setProfile( null );
249             }
250         }
251         if ( buildDefinition.getSchedule() != null )
252         {
253             if ( buildDefinition.getSchedule().getId() > 0 )
254             {
255                 schedule = getContinuum().getSchedule( buildDefinition.getSchedule().getId() );
256                 buildDefinition.setSchedule( schedule );
257             }
258         }
259 
260         if ( this.buildDefinition.getId() > 0 )
261         {
262             this.getContinuum().getBuildDefinitionService().updateBuildDefinition( buildDefinition );
263         }
264         else
265         {
266             this.buildDefinition =
267                 this.getContinuum().getBuildDefinitionService().addBuildDefinition( buildDefinition );
268         }
269         
270         if ( schedule != null )
271         {
272             getContinuum().activeBuildDefinitionSchedule( schedule );
273         }
274 
275         return SUCCESS;
276     }
277 
278     public String deleteBuildDefinition()
279         throws BuildDefinitionServiceException
280     {
281         if ( confirmed )
282         {
283             buildDefinition =
284                 getContinuum().getBuildDefinitionService().getBuildDefinition( this.buildDefinition.getId() );
285             this.getContinuum().getBuildDefinitionService().removeBuildDefinition( buildDefinition );
286         }
287         else
288         {
289             return CONFIRM;
290         }
291 
292         return SUCCESS;
293     }
294 
295     // -----------------------------------------------------
296     // security
297     // -----------------------------------------------------    
298 
299     public SecureActionBundle getSecureActionBundle()
300         throws SecureActionException
301     {
302         SecureActionBundle bundle = new SecureActionBundle();
303         bundle.setRequiresAuthentication( true );
304         bundle.addRequiredAuthorization( ContinuumRoleConstants.CONTINUUM_MANAGE_BUILD_TEMPLATES, Resource.GLOBAL );
305 
306         return bundle;
307     }
308 
309     // -------------------------------------------------------
310     // Webwork setter/getter
311     // -------------------------------------------------------    
312 
313     public BuildDefinitionTemplate getBuildDefinitionTemplate()
314     {
315         if ( buildDefinitionTemplate == null )
316         {
317             this.buildDefinitionTemplate = new BuildDefinitionTemplate();
318         }
319         return buildDefinitionTemplate;
320     }
321 
322     public void setBuildDefinitionTemplate( BuildDefinitionTemplate buildDefinitionTemplate )
323     {
324         this.buildDefinitionTemplate = buildDefinitionTemplate;
325     }
326 
327     public List<String> getBuildDefinitionTypes()
328     {
329         return buildDefinitionTypes;
330     }
331 
332     public void setBuildDefinitionTypes( List<String> buildDefinitionTypes )
333     {
334         this.buildDefinitionTypes = buildDefinitionTypes;
335     }
336 
337     public List<BuildDefinitionTemplate> getTemplates()
338     {
339         return templates;
340     }
341 
342     public void setTemplates( List<BuildDefinitionTemplate> templates )
343     {
344         this.templates = templates;
345     }
346 
347     public List<BuildDefinitionSummary> getBuildDefinitionSummaries()
348     {
349         return buildDefinitionSummaries;
350     }
351 
352     public void setBuildDefinitionSummaries( List<BuildDefinitionSummary> buildDefinitionSummaries )
353     {
354         this.buildDefinitionSummaries = buildDefinitionSummaries;
355     }
356 
357     public BuildDefinition getBuildDefinition()
358     {
359         if ( this.buildDefinition == null )
360         {
361             this.buildDefinition = new BuildDefinition();
362         }
363         return buildDefinition;
364     }
365 
366     public void setBuildDefinition( BuildDefinition buildDefinition )
367     {
368         this.buildDefinition = buildDefinition;
369     }
370 
371     public List<Profile> getProfiles()
372     {
373         return profiles;
374     }
375 
376     public void setProfiles( List<Profile> profiles )
377     {
378         this.profiles = profiles;
379     }
380 
381     public void setSchedules( Collection<Schedule> schedules )
382     {
383         this.schedules = schedules;
384     }
385 
386     public Collection<Schedule> getSchedules()
387     {
388         return schedules;
389     }
390 
391     public List<BuildDefinition> getBuildDefinitions()
392     {
393         return buildDefinitions;
394     }
395 
396     public void setBuildDefinitions( List<BuildDefinition> buildDefinitions )
397     {
398         this.buildDefinitions = buildDefinitions;
399     }
400 
401     public List<String> getSelectedBuildDefinitionIds()
402     {
403         return selectedBuildDefinitionIds == null ? Collections.EMPTY_LIST : selectedBuildDefinitionIds;
404     }
405 
406     public void setSelectedBuildDefinitionIds( List<String> selectedBuildDefinitionIds )
407     {
408         this.selectedBuildDefinitionIds = selectedBuildDefinitionIds;
409     }
410 
411 }