View Javadoc

1   package org.apache.maven.continuum.web.action;
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 java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.apache.commons.lang.StringEscapeUtils;
27  import org.apache.continuum.web.util.AuditLog;
28  import org.apache.continuum.web.util.AuditLogConstants;
29  import org.apache.maven.continuum.ContinuumException;
30  import org.apache.maven.continuum.builddefinition.BuildDefinitionServiceException;
31  import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
32  import org.apache.maven.continuum.model.project.Project;
33  import org.apache.maven.continuum.model.project.ProjectGroup;
34  import org.apache.maven.continuum.model.system.Profile;
35  import org.apache.maven.continuum.profile.ProfileException;
36  import org.apache.maven.continuum.profile.ProfileService;
37  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
38  import org.codehaus.plexus.util.StringUtils;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  /**
43   * @author Nick Gonzalez
44   * @version $Id: AddProjectAction.java 1101669 2011-05-10 22:46:21Z ctan $
45   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="addProject"
46   */
47  public class AddProjectAction
48      extends ContinuumActionSupport
49  {
50      private static final Logger logger = LoggerFactory.getLogger( AddProjectAction.class );
51  
52      private String projectName;
53  
54      private String projectDescription;
55  
56      private String projectVersion;
57  
58      private String projectScmUrl;
59  
60      private String projectScmUsername;
61  
62      private String projectScmPassword;
63  
64      private String projectScmTag;
65  
66      private String projectType;
67  
68      private Collection<ProjectGroup> projectGroups;
69  
70      private int selectedProjectGroup;
71  
72      private String projectGroupName;
73  
74      private boolean disableGroupSelection;
75  
76      private boolean projectScmUseCache;
77  
78      private List<Profile> profiles;
79  
80      /**
81       * @plexus.requirement role-hint="default"
82       */
83      private ProfileService profileService;
84  
85      private int projectGroupId;
86  
87      private int buildDefintionTemplateId;
88  
89      private List<BuildDefinitionTemplate> buildDefinitionTemplates;
90  
91      private boolean emptyProjectGroups;
92  
93      public String add()
94          throws ContinuumException, ProfileException, BuildDefinitionServiceException
95      {
96          initializeProjectGroupName();
97  
98          try
99          {
100             if ( StringUtils.isEmpty( getProjectGroupName() ) )
101             {
102                 checkAddProjectGroupAuthorization();
103             }
104             else
105             {
106                 checkAddProjectToGroupAuthorization( getProjectGroupName() );
107             }
108         }
109         catch ( AuthorizationRequiredException authzE )
110         {
111             addActionError( authzE.getMessage() );
112             return REQUIRES_AUTHORIZATION;
113         }
114 
115         if ( isEmptyProjectGroups() )
116         {
117             addActionError( getText( "addProject.projectGroup.required" ) );
118         }
119 
120         String projectNameTrim = projectName.trim();
121         String versionTrim = projectVersion.trim();
122         String scmTrim = projectScmUrl.trim();
123         //TODO: Instead of get all projects then test them, it would be better to check it directly in the DB
124         for ( Project project : getContinuum().getProjects() )
125         {
126             // CONTINUUM-1445
127             if ( StringUtils.equalsIgnoreCase( project.getName(), projectNameTrim ) &&
128                 StringUtils.equalsIgnoreCase( project.getVersion(), versionTrim ) &&
129                 StringUtils.equalsIgnoreCase( project.getScmUrl(), scmTrim ) )
130             {
131                 addActionError( getText( "projectName.already.exist.error" ) );
132                 break;
133             }
134         }
135 
136         if ( hasActionErrors() )
137         {
138             return INPUT;
139         }
140 
141         Project project = new Project();
142 
143         project.setName( projectNameTrim );
144 
145         if ( projectDescription != null )
146         {
147             project.setDescription( StringEscapeUtils.escapeXml( StringEscapeUtils.unescapeXml( projectDescription.trim() ) ) );
148         }
149 
150         project.setVersion( versionTrim );
151 
152         project.setScmUrl( scmTrim );
153 
154         project.setScmUsername( projectScmUsername );
155 
156         project.setScmPassword( projectScmPassword );
157 
158         project.setScmTag( projectScmTag );
159 
160         project.setScmUseCache( projectScmUseCache );
161 
162         project.setExecutorId( projectType );
163 
164         getContinuum().addProject( project, projectType, selectedProjectGroup, this.getBuildDefintionTemplateId() );
165 
166         if ( this.getSelectedProjectGroup() > 0 )
167         {
168             this.setProjectGroupId( this.getSelectedProjectGroup() );
169             return "projectGroupSummary";
170         }
171 
172         AuditLog event = new AuditLog( "Project id=" + project.getId(), AuditLogConstants.ADD_PROJECT );
173         event.setCategory( AuditLogConstants.PROJECT );
174         event.setCurrentUser( getPrincipal() );
175         event.log();
176 
177         return SUCCESS;
178     }
179 
180     public String input()
181         throws ContinuumException, ProfileException, BuildDefinitionServiceException
182     {
183         try
184         {
185             if ( StringUtils.isEmpty( getProjectGroupName() ) )
186             {
187                 checkAddProjectGroupAuthorization();
188             }
189             else
190             {
191                 checkAddProjectToGroupAuthorization( getProjectGroupName() );
192             }
193         }
194         catch ( AuthorizationRequiredException authzE )
195         {
196             addActionError( authzE.getMessage() );
197             return REQUIRES_AUTHORIZATION;
198         }
199 
200         projectGroups = new ArrayList<ProjectGroup>();
201 
202         Collection<ProjectGroup> allProjectGroups = getContinuum().getAllProjectGroups();
203 
204         for ( ProjectGroup pg : allProjectGroups )
205         {
206             if ( isAuthorizedToAddProjectToGroup( pg.getName() ) )
207             {
208                 projectGroups.add( pg );
209             }
210         }
211 
212         this.profiles = profileService.getAllProfiles();
213         buildDefinitionTemplates = getContinuum().getBuildDefinitionService().getAllBuildDefinitionTemplate();
214         return INPUT;
215     }
216 
217     private void initializeProjectGroupName()
218     {
219         if ( disableGroupSelection )
220         {
221             try
222             {
223                 projectGroupName = getContinuum().getProjectGroup( selectedProjectGroup ).getName();
224             }
225             catch ( ContinuumException e )
226             {
227                 e.printStackTrace();
228             }
229         }
230     }
231 
232     public String getProjectName()
233     {
234         return projectName;
235     }
236 
237     public void setProjectName( String projectName )
238     {
239         this.projectName = projectName;
240     }
241 
242     public String getProjectScmPassword()
243     {
244         return projectScmPassword;
245     }
246 
247     public void setProjectScmPassword( String projectScmPassword )
248     {
249         this.projectScmPassword = projectScmPassword;
250     }
251 
252     public String getProjectScmTag()
253     {
254         return projectScmTag;
255     }
256 
257     public void setProjectScmTag( String projectScmTag )
258     {
259         this.projectScmTag = projectScmTag;
260     }
261 
262     public String getProjectScmUrl()
263     {
264         return projectScmUrl;
265     }
266 
267     public void setProjectScmUrl( String projectScmUrl )
268     {
269         this.projectScmUrl = projectScmUrl;
270     }
271 
272     public String getProjectScmUsername()
273     {
274         return projectScmUsername;
275     }
276 
277     public void setProjectScmUsername( String projectScmUsername )
278     {
279         this.projectScmUsername = projectScmUsername;
280     }
281 
282     public String getProjectType()
283     {
284         return projectType;
285     }
286 
287     public void setProjectType( String projectType )
288     {
289         this.projectType = projectType;
290     }
291 
292     public String getProjectVersion()
293     {
294         return projectVersion;
295     }
296 
297     public void setProjectVersion( String projectVersion )
298     {
299         this.projectVersion = projectVersion;
300     }
301 
302     public Collection<ProjectGroup> getProjectGroups()
303     {
304         return projectGroups;
305     }
306 
307     public void setProjectGroups( Collection<ProjectGroup> projectGroups )
308     {
309         this.projectGroups = projectGroups;
310     }
311 
312     public int getSelectedProjectGroup()
313     {
314         return selectedProjectGroup;
315     }
316 
317     public void setSelectedProjectGroup( int selectedProjectGroup )
318     {
319         this.selectedProjectGroup = selectedProjectGroup;
320     }
321 
322     public boolean isDisableGroupSelection()
323     {
324         return disableGroupSelection;
325     }
326 
327     public void setDisableGroupSelection( boolean disableGroupSelection )
328     {
329         this.disableGroupSelection = disableGroupSelection;
330     }
331 
332     public String getProjectGroupName()
333     {
334         return projectGroupName;
335     }
336 
337     public void setProjectGroupName( String projectGroupName )
338     {
339         this.projectGroupName = projectGroupName;
340     }
341 
342     public boolean isProjectScmUseCache()
343     {
344         return projectScmUseCache;
345     }
346 
347     public void setProjectScmUseCache( boolean projectScmUseCache )
348     {
349         this.projectScmUseCache = projectScmUseCache;
350     }
351 
352     public List<Profile> getProfiles()
353     {
354         return profiles;
355     }
356 
357     public void setProfiles( List<Profile> profiles )
358     {
359         this.profiles = profiles;
360     }
361 
362     public int getProjectGroupId()
363     {
364         return projectGroupId;
365     }
366 
367     public void setProjectGroupId( int projectGroupId )
368     {
369         this.projectGroupId = projectGroupId;
370     }
371 
372     public int getBuildDefintionTemplateId()
373     {
374         return buildDefintionTemplateId;
375     }
376 
377     public void setBuildDefintionTemplateId( int buildDefintionTemplateId )
378     {
379         this.buildDefintionTemplateId = buildDefintionTemplateId;
380     }
381 
382     public List<BuildDefinitionTemplate> getBuildDefinitionTemplates()
383     {
384         return buildDefinitionTemplates;
385     }
386 
387     public void setBuildDefinitionTemplates( List<BuildDefinitionTemplate> buildDefinitionTemplates )
388     {
389         this.buildDefinitionTemplates = buildDefinitionTemplates;
390     }
391 
392     private boolean isAuthorizedToAddProjectToGroup( String projectGroupName )
393     {
394         try
395         {
396             checkAddProjectToGroupAuthorization( projectGroupName );
397             return true;
398         }
399         catch ( AuthorizationRequiredException authzE )
400         {
401             return false;
402         }
403     }
404 
405     public String getProjectDescription()
406     {
407         return projectDescription;
408     }
409 
410     public void setProjectDescription( String projectDescription )
411     {
412         this.projectDescription = projectDescription;
413     }
414 
415     public boolean isEmptyProjectGroups()
416     {
417         return emptyProjectGroups;
418     }
419 
420     public void setEmptyProjectGroups( boolean emptyProjectGroups )
421     {
422         this.emptyProjectGroups = emptyProjectGroups;
423     }
424 }