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 java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.maven.continuum.ContinuumException;
29 import org.apache.maven.continuum.builddefinition.BuildDefinitionService;
30 import org.apache.maven.continuum.builddefinition.BuildDefinitionServiceException;
31 import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
32 import org.apache.maven.continuum.model.project.BuildDefinition;
33 import org.apache.maven.continuum.model.project.Project;
34 import org.apache.maven.continuum.model.project.Schedule;
35 import org.apache.maven.continuum.model.system.Profile;
36 import org.apache.maven.continuum.profile.ProfileException;
37 import org.apache.maven.continuum.store.ContinuumStoreException;
38 import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
39 import org.apache.maven.continuum.web.exception.ContinuumActionException;
40 import org.apache.commons.lang.StringEscapeUtils;
41 import org.apache.continuum.web.util.AuditLog;
42 import org.apache.continuum.web.util.AuditLogConstants;
43 import org.codehaus.plexus.util.StringUtils;
44
45
46
47
48
49
50
51
52 public class BuildDefinitionAction
53 extends ContinuumConfirmAction
54 {
55 private int buildDefinitionId;
56
57 private int projectId;
58
59 private int projectGroupId;
60
61 private int scheduleId;
62
63 private boolean defaultBuildDefinition;
64
65 private boolean confirmed = false;
66
67 private String executor;
68
69 private String goals;
70
71 private String arguments;
72
73 private String buildFile;
74
75 private boolean buildFresh;
76
77 private Map<Integer, String> schedules;
78
79 private List<Profile> profiles;
80
81 private boolean groupBuildDefinition = false;
82
83 private boolean groupBuildView = false;
84
85 private String projectGroupName = "";
86
87 private int profileId;
88
89 private String description;
90
91 private List<String> buildDefinitionTypes;
92
93 private String buildDefinitionType;
94
95 private boolean alwaysBuild;
96
97
98
99
100 private BuildDefinitionService buildDefinitionService;
101
102 @Override
103 public void prepare()
104 throws Exception
105 {
106 super.prepare();
107
108 if ( schedules == null )
109 {
110 schedules = new HashMap<Integer, String>();
111
112 Collection<Schedule> allSchedules = getContinuum().getSchedules();
113
114 for ( Schedule schedule : allSchedules )
115 {
116 schedules.put( schedule.getId(), schedule.getName() );
117 }
118 }
119
120
121 if ( profiles == null )
122 {
123 profiles = this.getContinuum().getProfileService().getAllProfiles();
124 }
125
126 buildDefinitionTypes = new ArrayList<String>();
127 buildDefinitionTypes.add( ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR );
128 buildDefinitionTypes.add( ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR );
129 buildDefinitionTypes.add( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR );
130 buildDefinitionTypes.add( ContinuumBuildExecutorConstants.SHELL_BUILD_EXECUTOR );
131 }
132
133
134
135
136
137
138 @Override
139 public String input()
140 throws ContinuumException, ContinuumStoreException, BuildDefinitionServiceException
141 {
142 try
143 {
144 if ( executor == null )
145 {
146 if ( projectId != 0 )
147 {
148 executor = getContinuum().getProject( projectId ).getExecutorId();
149 }
150 else
151 {
152 List<Project> projects = getContinuum().getProjectGroupWithProjects( projectGroupId ).getProjects();
153
154 if ( projects.size() > 0 )
155 {
156 Project project = projects.get( 0 );
157 executor = project.getExecutorId();
158 }
159 }
160 }
161
162 if ( buildDefinitionId != 0 )
163 {
164 if ( projectId != 0 )
165 {
166 checkModifyProjectBuildDefinitionAuthorization( getProjectGroupName() );
167 }
168 else
169 {
170 checkModifyGroupBuildDefinitionAuthorization( getProjectGroupName() );
171 }
172
173 BuildDefinition buildDefinition = getContinuum().getBuildDefinition( buildDefinitionId );
174 goals = buildDefinition.getGoals();
175 arguments = buildDefinition.getArguments();
176 buildFile = buildDefinition.getBuildFile();
177 buildFresh = buildDefinition.isBuildFresh();
178 scheduleId = buildDefinition.getSchedule().getId();
179 defaultBuildDefinition = buildDefinition.isDefaultForProject();
180 Profile profile = buildDefinition.getProfile();
181 if ( profile != null )
182 {
183 profileId = profile.getId();
184 }
185 description = buildDefinition.getDescription();
186 buildDefinitionType = buildDefinition.getType();
187 alwaysBuild = buildDefinition.isAlwaysBuild();
188 }
189 else
190 {
191 String preDefinedBuildFile = "";
192
193 if ( projectId != 0 )
194 {
195 checkAddProjectBuildDefinitionAuthorization( getProjectGroupName() );
196 BuildDefinition bd = getContinuum().getDefaultBuildDefinition( projectId );
197 if ( bd != null )
198 {
199 preDefinedBuildFile = bd.getBuildFile();
200 }
201 }
202 else
203 {
204 checkAddGroupBuildDefinitionAuthorization( getProjectGroupName() );
205 List<BuildDefinition> bds = getContinuum().getBuildDefinitionsForProjectGroup( projectGroupId );
206 if ( bds != null && !bds.isEmpty() )
207 {
208 preDefinedBuildFile = bds.get( 0 ).getBuildFile();
209 }
210 }
211
212 if ( StringUtils.isEmpty( preDefinedBuildFile ) )
213 {
214 if ( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR.equals( executor ) )
215 {
216 buildFile =
217 ( (BuildDefinition) buildDefinitionService.getDefaultMavenTwoBuildDefinitionTemplate().getBuildDefinitions().get(
218 0 ) ).getBuildFile();
219 buildDefinitionType = ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR;
220 }
221 else if ( ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR.equals( executor ) )
222 {
223 buildFile =
224 ( (BuildDefinition) buildDefinitionService.getDefaultMavenOneBuildDefinitionTemplate().getBuildDefinitions().get(
225 0 ) ).getBuildFile();
226 buildDefinitionType = ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR;
227 }
228 else if ( ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR.equals( executor ) )
229 {
230 buildFile =
231 ( (BuildDefinition) buildDefinitionService.getDefaultAntBuildDefinitionTemplate().getBuildDefinitions().get(
232 0 ) ).getBuildFile();
233 buildDefinitionType = ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR;
234 }
235 else
236 {
237 buildDefinitionType = ContinuumBuildExecutorConstants.SHELL_BUILD_EXECUTOR;
238 }
239 }
240 else
241 {
242 buildFile = preDefinedBuildFile;
243 }
244 }
245
246
247 if ( StringUtils.isEmpty( buildDefinitionType ) )
248 {
249 if ( ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR.equals( executor ) )
250 {
251 buildDefinitionType = ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR;
252 }
253 else if ( ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR.equals( executor ) )
254 {
255 buildDefinitionType = ContinuumBuildExecutorConstants.MAVEN_ONE_BUILD_EXECUTOR;
256 }
257 else if ( ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR.equals( executor ) )
258 {
259 buildDefinitionType = ContinuumBuildExecutorConstants.ANT_BUILD_EXECUTOR;
260 }
261 else
262 {
263 buildDefinitionType = ContinuumBuildExecutorConstants.SHELL_BUILD_EXECUTOR;
264 }
265 }
266
267 }
268 catch ( AuthorizationRequiredException authzE )
269 {
270 return REQUIRES_AUTHORIZATION;
271 }
272
273 return SUCCESS;
274 }
275
276 public String saveBuildDefinition()
277 throws ContinuumException, ProfileException
278 {
279 if ( projectId != 0 && !groupBuildDefinition )
280 {
281 return saveToProject();
282 }
283 else
284 {
285 return saveToGroup();
286 }
287 }
288
289 public String saveToProject()
290 throws ContinuumException, ProfileException
291 {
292
293 try
294 {
295 if ( buildDefinitionId == 0 )
296 {
297 checkAddProjectBuildDefinitionAuthorization( getProjectGroupName() );
298
299 getContinuum().addBuildDefinitionToProject( projectId, getBuildDefinitionFromInput() );
300 }
301 else
302 {
303 checkModifyProjectBuildDefinitionAuthorization( getProjectGroupName() );
304
305 getContinuum().updateBuildDefinitionForProject( projectId, getBuildDefinitionFromInput() );
306 }
307 }
308 catch ( ContinuumActionException cae )
309 {
310 addActionError( cae.getMessage() );
311 return INPUT;
312 }
313 catch ( AuthorizationRequiredException authzE )
314 {
315 addActionError( authzE.getMessage() );
316 return REQUIRES_AUTHORIZATION;
317 }
318
319 String resource = "Project id=" + projectId + ":" + goals + " " + arguments;
320 AuditLog event = new AuditLog( resource, AuditLogConstants.ADD_GOAL );
321 event.setCategory( AuditLogConstants.BUILD_DEFINITION );
322 event.setCurrentUser( getPrincipal() );
323 event.log();
324
325 if ( groupBuildView )
326 {
327 return "success_group";
328 }
329
330 return SUCCESS;
331 }
332
333 public String saveToGroup()
334 throws ContinuumException, ProfileException
335 {
336 try
337 {
338 BuildDefinition newBuildDef = getBuildDefinitionFromInput();
339
340 if ( getContinuum().getBuildDefinitionsForProjectGroup( projectGroupId ).size() == 0 )
341 {
342 newBuildDef.setDefaultForProject( true );
343 }
344
345 if ( buildDefinitionId == 0 )
346 {
347 checkAddGroupBuildDefinitionAuthorization( getProjectGroupName() );
348
349 getContinuum().addBuildDefinitionToProjectGroup( projectGroupId, newBuildDef );
350 }
351 else
352 {
353 checkModifyGroupBuildDefinitionAuthorization( getProjectGroupName() );
354
355 getContinuum().updateBuildDefinitionForProjectGroup( projectGroupId, newBuildDef );
356 }
357 }
358 catch ( ContinuumActionException cae )
359 {
360 addActionError( cae.getMessage() );
361 return INPUT;
362 }
363 catch ( AuthorizationRequiredException authzE )
364 {
365 addActionError( authzE.getMessage() );
366 return REQUIRES_AUTHORIZATION;
367 }
368
369 if ( projectId != 0 )
370 {
371 String resource = "Project id=" + projectId + ":" + goals + " " + arguments;
372 AuditLog event = new AuditLog( resource, AuditLogConstants.ADD_GOAL );
373 event.setCategory( AuditLogConstants.BUILD_DEFINITION );
374 event.setCurrentUser( getPrincipal() );
375 event.log();
376
377 return SUCCESS;
378 }
379 else
380 {
381 String resource = "Project Group id=" + projectGroupId + ":" + goals + " " + arguments;
382 AuditLog event = new AuditLog( resource, AuditLogConstants.ADD_GOAL );
383 event.setCategory( AuditLogConstants.BUILD_DEFINITION );
384 event.setCurrentUser( getPrincipal() );
385 event.log();
386 return "success_group";
387 }
388 }
389
390 public String removeFromProject()
391 throws ContinuumException
392 {
393 try
394 {
395 checkRemoveProjectBuildDefinitionAuthorization( getProjectGroupName() );
396
397 if ( confirmed )
398 {
399 getContinuum().removeBuildDefinitionFromProject( projectId, buildDefinitionId );
400
401 String resource = "Project id=" + projectId + ":" + goals + " " + arguments;
402 AuditLog event = new AuditLog( resource, AuditLogConstants.REMOVE_GOAL );
403 event.setCategory( AuditLogConstants.BUILD_DEFINITION );
404 event.setCurrentUser( getPrincipal() );
405 event.log();
406
407 return SUCCESS;
408 }
409 else
410 {
411 BuildDefinition buildDefinition = getContinuum().getBuildDefinition( buildDefinitionId );
412 this.description = buildDefinition.getDescription();
413 this.goals = buildDefinition.getGoals();
414 return CONFIRM;
415 }
416 }
417 catch ( AuthorizationRequiredException authzE )
418 {
419 addActionError( authzE.getMessage() );
420 return REQUIRES_AUTHORIZATION;
421 }
422 }
423
424 public String removeFromProjectGroup()
425 throws ContinuumException
426 {
427 try
428 {
429 checkRemoveGroupBuildDefinitionAuthorization( getProjectGroupName() );
430
431 if ( confirmed )
432 {
433 getContinuum().removeBuildDefinitionFromProjectGroup( projectGroupId, buildDefinitionId );
434
435 String resource = "Project Group id=" + projectGroupId + ":" + goals + " " + arguments;
436 AuditLog event = new AuditLog( resource, AuditLogConstants.REMOVE_GOAL );
437 event.setCategory( AuditLogConstants.BUILD_DEFINITION );
438 event.setCurrentUser( getPrincipal() );
439 event.log();
440
441 return SUCCESS;
442 }
443 else
444 {
445 BuildDefinition buildDefinition = getContinuum().getBuildDefinition( buildDefinitionId );
446 this.description = buildDefinition.getDescription();
447 this.goals = buildDefinition.getGoals();
448 return CONFIRM;
449 }
450 }
451 catch ( AuthorizationRequiredException authzE )
452 {
453 addActionError( authzE.getMessage() );
454 return REQUIRES_AUTHORIZATION;
455 }
456 }
457
458 private BuildDefinition getBuildDefinitionFromInput()
459 throws ContinuumActionException, ProfileException
460 {
461
462 Schedule schedule;
463
464 try
465 {
466 schedule = getContinuum().getSchedule( scheduleId );
467 }
468 catch ( ContinuumException e )
469 {
470 addActionError( getText( "unable to get schedule" ) );
471 throw new ContinuumActionException( "unable to get schedule" );
472 }
473
474 BuildDefinition buildDefinition = new BuildDefinition();
475
476 if ( buildDefinitionId != 0 )
477 {
478 buildDefinition.setId( buildDefinitionId );
479 }
480 buildDefinition.setGoals( goals );
481 buildDefinition.setArguments( arguments );
482 buildDefinition.setBuildFile( buildFile );
483 buildDefinition.setBuildFresh( buildFresh );
484 buildDefinition.setDefaultForProject( defaultBuildDefinition );
485 buildDefinition.setSchedule( schedule );
486 if ( profileId != -1 )
487 {
488 Profile profile = getContinuum().getProfileService().getProfile( profileId );
489 if ( profile != null )
490 {
491 buildDefinition.setProfile( profile );
492 }
493 }
494 buildDefinition.setDescription( StringEscapeUtils.escapeXml( StringEscapeUtils.unescapeXml( description ) ) );
495 buildDefinition.setType( buildDefinitionType );
496 buildDefinition.setAlwaysBuild( alwaysBuild );
497 return buildDefinition;
498 }
499
500 public int getBuildDefinitionId()
501 {
502 return buildDefinitionId;
503 }
504
505 public void setBuildDefinitionId( final int buildDefinitionId )
506 {
507 this.buildDefinitionId = buildDefinitionId;
508 }
509
510 public int getProjectId()
511 {
512 return projectId;
513 }
514
515 public void setProjectId( final int projectId )
516 {
517 this.projectId = projectId;
518 }
519
520 public int getProjectGroupId()
521 {
522 return projectGroupId;
523 }
524
525 public void setProjectGroupId( final int projectGroupId )
526 {
527 this.projectGroupId = projectGroupId;
528 }
529
530 public int getScheduleId()
531 {
532 return scheduleId;
533 }
534
535 public void setScheduleId( final int scheduleId )
536 {
537 this.scheduleId = scheduleId;
538 }
539
540 public boolean isDefaultBuildDefinition()
541 {
542 return defaultBuildDefinition;
543 }
544
545 public void setDefaultBuildDefinition( final boolean defaultBuildDefinition )
546 {
547 this.defaultBuildDefinition = defaultBuildDefinition;
548 }
549
550 @Override
551 public boolean isConfirmed()
552 {
553 return confirmed;
554 }
555
556 @Override
557 public void setConfirmed( final boolean confirmed )
558 {
559 this.confirmed = confirmed;
560 }
561
562 public String getExecutor()
563 {
564 return executor;
565 }
566
567 public void setExecutor( final String executor )
568 {
569 this.executor = executor;
570 }
571
572 public String getGoals()
573 {
574 return goals;
575 }
576
577 public void setGoals( final String goals )
578 {
579 this.goals = goals;
580 }
581
582 public String getArguments()
583 {
584 return arguments;
585 }
586
587 public void setArguments( final String arguments )
588 {
589 this.arguments = arguments;
590 }
591
592 public String getBuildFile()
593 {
594 return buildFile;
595 }
596
597 public void setBuildFile( final String buildFile )
598 {
599 this.buildFile = buildFile;
600 }
601
602 public boolean isBuildFresh()
603 {
604 return buildFresh;
605 }
606
607 public void setBuildFresh( final boolean buildFresh )
608 {
609 this.buildFresh = buildFresh;
610 }
611
612 public Map<Integer, String> getSchedules()
613 {
614 return schedules;
615 }
616
617 public void setSchedules( final Map<Integer, String> schedules )
618 {
619 this.schedules = schedules;
620 }
621
622 public List<Profile> getProfiles()
623 {
624 return profiles;
625 }
626
627 public void setProfiles( final List<Profile> profiles )
628 {
629 this.profiles = profiles;
630 }
631
632 public boolean isGroupBuildDefinition()
633 {
634 return groupBuildDefinition;
635 }
636
637 public void setGroupBuildDefinition( final boolean groupBuildDefinition )
638 {
639 this.groupBuildDefinition = groupBuildDefinition;
640 }
641
642 public String getProjectGroupName()
643 throws ContinuumException
644 {
645 if ( projectGroupName == null || "".equals( projectGroupName ) )
646 {
647 if ( projectGroupId != 0 )
648 {
649 projectGroupName = getContinuum().getProjectGroup( projectGroupId ).getName();
650 }
651 else
652 {
653 projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName();
654 }
655 }
656
657 return projectGroupName;
658 }
659
660 public int getProfileId()
661 {
662 return profileId;
663 }
664
665 public void setProfileId( final int profileId )
666 {
667 this.profileId = profileId;
668 }
669
670 public String getDescription()
671 {
672 return description;
673 }
674
675 public void setDescription( final String description )
676 {
677 this.description = description;
678 }
679
680 public String getBuildDefinitionType()
681 {
682 return buildDefinitionType;
683 }
684
685 public void setBuildDefinitionType( final String buildDefinitionType )
686 {
687 this.buildDefinitionType = buildDefinitionType;
688 }
689
690 public List<String> getBuildDefinitionTypes()
691 {
692 return buildDefinitionTypes;
693 }
694
695 public boolean isAlwaysBuild()
696 {
697 return alwaysBuild;
698 }
699
700 public void setAlwaysBuild( final boolean alwaysBuild )
701 {
702 this.alwaysBuild = alwaysBuild;
703 }
704
705 public boolean isGroupBuildView()
706 {
707 return groupBuildView;
708 }
709
710 public void setGroupBuildView( final boolean groupBuildView )
711 {
712 this.groupBuildView = groupBuildView;
713 }
714
715 }