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.commons.lang.StringEscapeUtils;
23  import org.apache.continuum.model.repository.LocalRepository;
24  import org.apache.continuum.repository.RepositoryServiceException;
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.web.exception.AuthorizationRequiredException;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import java.util.List;
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(),
136                                        AuditLogConstants.ADD_PROJECT_GROUP );
137         event.setCategory( AuditLogConstants.PROJECT );
138         event.setCurrentUser( getPrincipal() );
139         event.log();
140 
141         return SUCCESS;
142     }
143 
144     public String input()
145     {
146         try
147         {
148             checkAddProjectGroupAuthorization();
149 
150             return INPUT;
151         }
152         catch ( AuthorizationRequiredException authzE )
153         {
154             addActionError( authzE.getMessage() );
155             return REQUIRES_AUTHORIZATION;
156         }
157     }
158 
159     public String getDescription()
160     {
161         return description;
162     }
163 
164     public void setDescription( String description )
165     {
166         this.description = description;
167     }
168 
169     public String getGroupId()
170     {
171         return groupId;
172     }
173 
174     public void setGroupId( String groupId )
175     {
176         this.groupId = groupId;
177     }
178 
179     public String getName()
180     {
181         return name;
182     }
183 
184     public void setName( String name )
185     {
186         this.name = name;
187     }
188 
189     public int getRepositoryId()
190     {
191         return repositoryId;
192     }
193 
194     public void setRepositoryId( int repositoryId )
195     {
196         this.repositoryId = repositoryId;
197     }
198 
199     public List<LocalRepository> getRepositories()
200     {
201         return repositories;
202     }
203 
204     public void setRepositories( List<LocalRepository> repositories )
205     {
206         this.repositories = repositories;
207     }
208 }