1 package org.apache.maven.continuum.web.action.admin;
2
3 import java.util.ArrayList;
4 import java.util.LinkedHashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.ResourceBundle;
8
9 import org.apache.commons.lang.StringUtils;
10 import org.apache.maven.continuum.installation.AlreadyExistsInstallationException;
11 import org.apache.maven.continuum.installation.InstallationService;
12 import org.apache.maven.continuum.model.system.Installation;
13 import org.apache.maven.continuum.profile.AlreadyExistsProfileException;
14 import org.apache.maven.continuum.security.ContinuumRoleConstants;
15 import org.apache.maven.continuum.web.action.ContinuumConfirmAction;
16 import org.codehaus.plexus.redback.rbac.Resource;
17 import org.codehaus.redback.integration.interceptor.SecureAction;
18 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
19 import org.codehaus.redback.integration.interceptor.SecureActionException;
20
21 import com.opensymphony.xwork2.Preparable;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class InstallationAction
48 extends ContinuumConfirmAction
49 implements Preparable, SecureAction
50 {
51
52
53
54
55 private InstallationService installationService;
56
57 private List<Installation> installations;
58
59 private Installation installation;
60
61 private Map<String, String> typesLabels;
62
63 private List<String> types;
64
65 private boolean varNameUpdatable = false;
66
67 private boolean automaticProfile;
68
69 private boolean varNameDisplayable = false;
70
71 private boolean displayTypes = true;
72
73 private String installationType;
74
75 private Map<String, String> installationTypes;
76
77 private static final String TOOL_TYPE_KEY = "tool";
78
79 private boolean automaticProfileDisplayable = true;
80
81 private boolean confirmed;
82
83
84
85
86
87 public String list()
88 throws Exception
89 {
90 this.installations = installationService.getAllInstallations();
91 return SUCCESS;
92 }
93
94 public String edit()
95 throws Exception
96 {
97 this.installation = installationService.getInstallation( installation.getInstallationId() );
98
99 if ( this.installation != null )
100 {
101 this.configureUiFlags();
102 }
103 this.automaticProfileDisplayable = false;
104 return SUCCESS;
105 }
106
107 public String input()
108 throws Exception
109 {
110 if ( InstallationService.ENVVAR_TYPE.equalsIgnoreCase( this.getInstallationType() ) )
111 {
112 this.installation = new Installation();
113 this.installation.setType( InstallationService.ENVVAR_TYPE );
114 this.setDisplayTypes( false );
115 this.setVarNameUpdatable( true );
116 this.setVarNameDisplayable( true );
117 }
118 else
119 {
120 this.setVarNameUpdatable( false );
121 this.setVarNameDisplayable( false );
122 }
123 return INPUT;
124 }
125
126 public String save()
127 throws Exception
128 {
129 if ( InstallationService.ENVVAR_TYPE.equalsIgnoreCase( this.getInstallationType() ) )
130 {
131 this.installation.setType( InstallationService.ENVVAR_TYPE );
132 if ( StringUtils.isEmpty( installation.getVarName() ) )
133 {
134 addFieldError( "installation.varName", getResourceBundle().getString( "installation.varName.required" ) );
135 return INPUT;
136 }
137
138 }
139 if ( installation.getInstallationId() == 0 )
140 {
141 try
142 {
143 installationService.add( installation, this.automaticProfile );
144 }
145 catch ( AlreadyExistsInstallationException e )
146 {
147 this.addActionError( getResourceBundle().getString( "installation.name.duplicate" ) );
148 return INPUT;
149 }
150 catch ( AlreadyExistsProfileException e )
151 {
152 this.addActionError( getResourceBundle().getString( "profile.name.already.exists" ) );
153 return INPUT;
154 }
155 }
156 else
157 {
158 this.configureUiFlags();
159 try{
160 installationService.update( installation );
161 }
162 catch ( AlreadyExistsInstallationException e )
163 {
164 this.addActionError( getResourceBundle().getString( "installation.name.duplicate" ) );
165 return INPUT;
166 }
167 }
168 return SUCCESS;
169 }
170
171 public String delete()
172 throws Exception
173 {
174 if ( confirmed )
175 {
176 Installation installationToDelete = installationService.getInstallation( installation.getInstallationId() );
177 installationService.delete( installationToDelete );
178 this.installations = installationService.getAllInstallations();
179 }
180 else
181 {
182 return CONFIRM;
183 }
184 return SUCCESS;
185 }
186
187 public String listTypes()
188 {
189 this.installationTypes = new LinkedHashMap<String, String>();
190 ResourceBundle resourceBundle = getResourceBundle();
191 this.installationTypes.put( TOOL_TYPE_KEY, resourceBundle.getString( "installationTypeChoice.tool.label" ) );
192 this.installationTypes.put( InstallationService.ENVVAR_TYPE, resourceBundle.getString( "installationTypeChoice.envar.label" ) );
193
194 return SUCCESS;
195 }
196
197
198
199
200
201 public SecureActionBundle getSecureActionBundle()
202 throws SecureActionException
203 {
204 SecureActionBundle bundle = new SecureActionBundle();
205 bundle.setRequiresAuthentication( true );
206 bundle.addRequiredAuthorization( ContinuumRoleConstants.CONTINUUM_MANAGE_INSTALLATIONS, Resource.GLOBAL );
207
208 return bundle;
209 }
210
211
212
213
214 private void configureUiFlags()
215 {
216
217 if ( !InstallationService.ENVVAR_TYPE.equals( this.installation.getType() ) )
218 {
219 this.setDisplayTypes( true );
220 this.setVarNameUpdatable( false );
221 }
222 else
223 {
224 this.setDisplayTypes( false );
225 this.setVarNameUpdatable( true );
226 this.setVarNameDisplayable( true );
227 }
228 this.setInstallationType( this.getInstallation().getType() );
229 }
230
231
232
233
234
235
236 public List<Installation> getInstallations()
237 {
238 return installations;
239 }
240
241 public void setInstallations( List<Installation> installations )
242 {
243 this.installations = installations;
244 }
245
246 public Installation getInstallation()
247 {
248 return installation;
249 }
250
251 public void setInstallation( Installation installation )
252 {
253 this.installation = installation;
254 }
255
256 public Map<String, String> getTypesLabels()
257 {
258 if ( this.typesLabels == null )
259 {
260 this.typesLabels = new LinkedHashMap<String, String>();
261 ResourceBundle resourceBundle = getResourceBundle();
262 this.typesLabels.put( InstallationService.JDK_TYPE, resourceBundle
263 .getString( "installation.jdk.type.label" ) );
264 this.typesLabels.put( InstallationService.MAVEN2_TYPE, resourceBundle
265 .getString( "installation.maven2.type.label" ) );
266 this.typesLabels.put( InstallationService.MAVEN1_TYPE, resourceBundle
267 .getString( "installation.maven1.type.label" ) );
268 this.typesLabels.put( InstallationService.ANT_TYPE, resourceBundle
269 .getString( "installation.ant.type.label" ) );
270
271
272
273 }
274 return typesLabels;
275 }
276
277 public void setTypesLabels( Map<String, String> typesLabels )
278 {
279 this.typesLabels = typesLabels;
280 }
281
282 public boolean isVarNameUpdatable()
283 {
284 return varNameUpdatable;
285 }
286
287 public void setVarNameUpdatable( boolean varNameUpdatable )
288 {
289 this.varNameUpdatable = varNameUpdatable;
290 }
291
292 public List<String> getTypes()
293 {
294 if ( this.types == null )
295 {
296 this.types = new ArrayList<String>(5);
297 this.types.add( InstallationService.JDK_TYPE );
298 this.types.add( InstallationService.MAVEN2_TYPE );
299 this.types.add( InstallationService.MAVEN1_TYPE );
300 this.types.add( InstallationService.ANT_TYPE );
301
302
303
304 }
305 return types;
306 }
307
308 public void setTypes( List<String> types )
309 {
310 this.types = types;
311 }
312
313 public boolean isAutomaticProfile()
314 {
315 return automaticProfile;
316 }
317
318 public void setAutomaticProfile( boolean automaticProfile )
319 {
320 this.automaticProfile = automaticProfile;
321 }
322
323 public Map<String, String> getInstallationTypes()
324 {
325 return installationTypes;
326 }
327
328 public void setInstallationTypes( Map<String, String> installationTypes )
329 {
330 this.installationTypes = installationTypes;
331 }
332
333 public boolean isVarNameDisplayable()
334 {
335 return varNameDisplayable;
336 }
337
338 public void setVarNameDisplayable( boolean varNameDisplayable )
339 {
340 this.varNameDisplayable = varNameDisplayable;
341 }
342
343 public boolean isDisplayTypes()
344 {
345 return displayTypes;
346 }
347
348 public void setDisplayTypes( boolean displayTypes )
349 {
350 this.displayTypes = displayTypes;
351 }
352
353 public String getInstallationType()
354 {
355 return installationType;
356 }
357
358 public void setInstallationType( String installationType )
359 {
360 this.installationType = installationType;
361 }
362
363 public boolean isAutomaticProfileDisplayable()
364 {
365 return automaticProfileDisplayable;
366 }
367
368 public void setAutomaticProfileDisplayable( boolean automaticProfileDisplayable )
369 {
370 this.automaticProfileDisplayable = automaticProfileDisplayable;
371 }
372
373 public boolean isConfirmed()
374 {
375 return confirmed;
376 }
377
378 public void setConfirmed( boolean confirmed )
379 {
380 this.confirmed = confirmed;
381 }
382 }