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