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