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.List;
23  
24  import org.apache.commons.lang.StringEscapeUtils;
25  import org.apache.continuum.web.util.AuditLog;
26  import org.apache.continuum.web.util.AuditLogConstants;
27  import org.apache.continuum.model.repository.LocalRepository;
28  import org.apache.continuum.repository.RepositoryServiceException;
29  import org.apache.maven.continuum.ContinuumException;
30  import org.apache.maven.continuum.model.project.ProjectGroup;
31  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * @author Henry Isidro <hisidro@exist.com>
37   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="addProjectGroup"
38   */
39  public class AddProjectGroupAction
40      extends ContinuumActionSupport
41  {
42      private static final Logger logger = LoggerFactory.getLogger( AddProjectGroupAction.class );
43  
44      private String name;
45  
46      private String groupId;
47  
48      private String description;
49  
50      private int repositoryId;
51  
52      private List<LocalRepository> repositories;
53  
54      public void prepare()
55          throws Exception
56      {
57          super.prepare();
58  
59          repositories = getContinuum().getRepositoryService().getAllLocalRepositories();
60      }
61  
62      public String execute()
63      {
64          try
65          {
66              checkAddProjectGroupAuthorization();
67          }
68          catch ( AuthorizationRequiredException authzE )
69          {
70              addActionError( authzE.getMessage() );
71              return REQUIRES_AUTHORIZATION;
72          }
73  
74          for ( ProjectGroup projectGroup : getContinuum().getAllProjectGroups() )
75          {
76              if ( name.equals( projectGroup.getName() ) )
77              {
78                  addActionError( getText( "projectGroup.error.name.already.exists" ) );
79                  break;
80              }
81          }
82  
83          try
84          {
85              if ( getContinuum().getProjectGroupByGroupId( groupId ) != null )
86              {
87                  addActionError( getText( "projectGroup.error.groupId.already.exists" ) );
88              }
89          }
90          catch ( ContinuumException e )
91          {
92              //since we want to add a new project group, we should be getting
93              //this exception
94          }
95  
96          if ( hasActionErrors() )
97          {
98              return INPUT;
99          }
100 
101         ProjectGroup projectGroup = new ProjectGroup();
102 
103         projectGroup.setName( name.trim() );
104 
105         projectGroup.setGroupId( groupId.trim() );
106 
107         projectGroup.setDescription( StringEscapeUtils.escapeXml( StringEscapeUtils.unescapeXml( description ) ) );
108 
109         try
110         {
111             if ( repositoryId > 0 )
112             {
113                 LocalRepository repository = getContinuum().getRepositoryService().getLocalRepository( repositoryId );
114                 projectGroup.setLocalRepository( repository );
115             }
116         }
117         catch ( RepositoryServiceException e )
118         {
119             logger.error( "Error adding project group" + e.getLocalizedMessage() );
120 
121             return ERROR;
122         }
123 
124         try
125         {
126             getContinuum().addProjectGroup( projectGroup );
127         }
128         catch ( ContinuumException e )
129         {
130             logger.error( "Error adding project group: " + e.getLocalizedMessage() );
131 
132             return ERROR;
133         }
134         
135         AuditLog event = new AuditLog( "Project Group id=" + projectGroup.getId(), AuditLogConstants.ADD_PROJECT_GROUP );
136         event.setCategory( AuditLogConstants.PROJECT );
137         event.setCurrentUser( getPrincipal() );
138         event.log();
139 
140         return SUCCESS;
141     }
142 
143     public String input()
144     {
145         try
146         {
147             checkAddProjectGroupAuthorization();
148 
149             return INPUT;
150         }
151         catch ( AuthorizationRequiredException authzE )
152         {
153             addActionError( authzE.getMessage() );
154             return REQUIRES_AUTHORIZATION;
155         }
156     }
157 
158     public String getDescription()
159     {
160         return description;
161     }
162 
163     public void setDescription( String description )
164     {
165         this.description = description;
166     }
167 
168     public String getGroupId()
169     {
170         return groupId;
171     }
172 
173     public void setGroupId( String groupId )
174     {
175         this.groupId = groupId;
176     }
177 
178     public String getName()
179     {
180         return name;
181     }
182 
183     public void setName( String name )
184     {
185         this.name = name;
186     }
187 
188     public int getRepositoryId()
189     {
190         return repositoryId;
191     }
192 
193     public void setRepositoryId( int repositoryId )
194     {
195         this.repositoryId = repositoryId;
196     }
197 
198     public List<LocalRepository> getRepositories()
199     {
200         return repositories;
201     }
202 
203     public void setRepositories( List<LocalRepository> repositories )
204     {
205         this.repositories = repositories;
206     }
207 }