1 package org.apache.maven.continuum.web.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
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
93
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 }