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 org.apache.continuum.buildagent.NoBuildAgentException;
23  import org.apache.continuum.buildagent.NoBuildAgentInGroupException;
24  import org.apache.continuum.utils.build.BuildTrigger;
25  import org.apache.continuum.web.util.AuditLog;
26  import org.apache.continuum.web.util.AuditLogConstants;
27  import org.apache.maven.continuum.ContinuumException;
28  import org.apache.maven.continuum.model.project.ProjectGroup;
29  import org.apache.maven.continuum.project.ContinuumProjectState;
30  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
35   * @version $Id: BuildProjectAction.java 1372260 2012-08-13 04:29:09Z brett $
36   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="buildProject"
37   */
38  public class BuildProjectAction
39      extends ContinuumActionSupport
40  {
41      private int projectId;
42  
43      private int buildDefinitionId;
44  
45      private int projectGroupId;
46  
47      private boolean fromGroupPage = false;
48  
49      private boolean fromProjectPage = false;
50  
51      private String projectGroupName = "";
52  
53      public String execute()
54          throws ContinuumException
55      {
56          try
57          {
58              checkBuildProjectInGroupAuthorization( getProjectGroupName() );
59          }
60          catch ( AuthorizationRequiredException e )
61          {
62              return REQUIRES_AUTHORIZATION;
63          }
64  
65          BuildTrigger buildTrigger = new BuildTrigger( ContinuumProjectState.TRIGGER_FORCED, getPrincipal() );
66  
67          try
68          {
69              if ( projectId > 0 )
70              {
71                  if ( buildDefinitionId > 0 )
72                  {
73                      getContinuum().buildProjectWithBuildDefinition( projectId, buildDefinitionId, buildTrigger );
74                  }
75                  else
76                  {
77                      getContinuum().buildProject( projectId, buildTrigger.getTriggeredBy() );
78                  }
79              }
80              else
81              {
82                  if ( buildDefinitionId > 0 )
83                  {
84                      getContinuum().buildProjectGroupWithBuildDefinition( projectGroupId, buildDefinitionId,
85                                                                           buildTrigger );
86                  }
87                  else
88                  {
89                      //TODO: Check if this code is called, I don't think
90                      //If it is, it should used the projectId
91                      getContinuum().buildProjects( buildTrigger.getTriggeredBy() );
92                  }
93              }
94          }
95          catch ( NoBuildAgentException e )
96          {
97              addActionError( getText( "projectGroup.build.error.noBuildAgent" ) );
98          }
99          catch ( NoBuildAgentInGroupException e )
100         {
101             addActionError( getText( "projectGroup.build.error.noBuildAgentInGroup" ) );
102         }
103 
104         AuditLog event = new AuditLog( AuditLogConstants.FORCE_BUILD );
105         event.setCurrentUser( getPrincipal() );
106 
107         if ( projectId > 0 )
108         {
109             event.setResource( "Project id=" + projectId );
110             event.setCategory( AuditLogConstants.PROJECT );
111             event.log();
112 
113             if ( fromGroupPage || hasActionErrors() )
114             {
115                 return "to_group_page";
116             }
117             if ( fromProjectPage )
118             {
119                 return "to_project_page";
120             }
121         }
122         else
123         {
124             event.setResource( "Project Group id=" + projectGroupId );
125             event.setCategory( AuditLogConstants.PROJECT_GROUP );
126             event.log();
127             if ( fromGroupPage )
128             {
129                 return "to_group_page";
130             }
131         }
132 
133         return SUCCESS;
134     }
135 
136     public void setProjectId( int projectId )
137     {
138         this.projectId = projectId;
139     }
140 
141     public int getProjectId()
142     {
143         return projectId;
144     }
145 
146     public void setBuildDefinitionId( int buildDefinitionId )
147     {
148         this.buildDefinitionId = buildDefinitionId;
149     }
150 
151     public int getBuildDefinition()
152     {
153         return buildDefinitionId;
154     }
155 
156     public int getProjectGroupId()
157     {
158         return projectGroupId;
159     }
160 
161     public void setProjectGroupId( int projectGroupId )
162     {
163         this.projectGroupId = projectGroupId;
164     }
165 
166     public boolean isFromGroupPage()
167     {
168         return fromGroupPage;
169     }
170 
171     public void setFromGroupPage( boolean fromGroupPage )
172     {
173         this.fromGroupPage = fromGroupPage;
174     }
175 
176     public boolean isFromProjectPage()
177     {
178         return fromProjectPage;
179     }
180 
181     public void setFromProjectPage( boolean fromProjectPage )
182     {
183         this.fromProjectPage = fromProjectPage;
184     }
185 
186     public String getProjectGroupName()
187         throws ContinuumException
188     {
189         if ( StringUtils.isEmpty( projectGroupName ) )
190         {
191             if ( projectGroupId != 0 )
192             {
193                 projectGroupName = getContinuum().getProjectGroup( projectGroupId ).getName();
194             }
195             else
196             {
197                 ProjectGroup projectGroup = getContinuum().getProjectGroupByProjectId( projectId );
198 
199                 projectGroupName = projectGroup.getName();
200                 projectGroupId = projectGroup.getId();
201             }
202         }
203 
204         return projectGroupName;
205     }
206 }