1 package org.apache.maven.continuum.core.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.List;
23
24 import org.apache.continuum.dao.BuildDefinitionDao;
25 import org.apache.continuum.dao.ScheduleDao;
26 import org.apache.maven.continuum.ContinuumException;
27 import org.apache.maven.continuum.configuration.ConfigurationService;
28 import org.apache.maven.continuum.model.project.BuildDefinition;
29 import org.apache.maven.continuum.model.project.Project;
30 import org.apache.maven.continuum.model.project.ProjectGroup;
31 import org.apache.maven.continuum.model.project.Schedule;
32 import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
33 import org.apache.maven.continuum.store.ContinuumStoreException;
34
35
36
37
38
39
40
41 public abstract class AbstractBuildDefinitionContinuumAction
42 extends AbstractContinuumAction
43 {
44
45
46
47 private BuildDefinitionDao buildDefinitionDao;
48
49
50
51
52 private ScheduleDao scheduleDao;
53
54 protected void resolveDefaultBuildDefinitionsForProject( BuildDefinition buildDefinition, Project project )
55 throws ContinuumException
56 {
57 try
58 {
59
60 if ( buildDefinition.isDefaultForProject() )
61 {
62 BuildDefinition storedDefinition =
63 buildDefinitionDao.getDefaultBuildDefinitionForProject( project.getId() );
64
65 if ( storedDefinition != null )
66 {
67 storedDefinition.setDefaultForProject( false );
68
69 buildDefinitionDao.storeBuildDefinition( storedDefinition );
70 }
71 }
72 }
73 catch ( ContinuumObjectNotFoundException nfe )
74 {
75 getLogger().debug( getClass().getName() +
76 ": safely ignoring the resetting of old build definition becuase it didn't exist" );
77 }
78 catch ( ContinuumStoreException cse )
79 {
80 throw new ContinuumException( "error updating old default build definition", cse );
81 }
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96 protected void resolveDefaultBuildDefinitionsForProjectGroup( BuildDefinition buildDefinition,
97 ProjectGroup projectGroup )
98 throws ContinuumException
99 {
100 try
101 {
102 List<BuildDefinition> storedDefinitions =
103 buildDefinitionDao.getDefaultBuildDefinitionsForProjectGroup( projectGroup.getId() );
104
105 for ( BuildDefinition storedDefinition : storedDefinitions )
106 {
107
108 if ( buildDefinition.isDefaultForProject() )
109 {
110 if ( storedDefinition != null && storedDefinition.getId() != buildDefinition.getId() )
111 {
112 if ( buildDefinition.getType() != null &&
113 buildDefinition.getType().equals( storedDefinition.getType() ) )
114 {
115
116 BuildDefinition def = buildDefinitionDao.getBuildDefinition( storedDefinition.getId() );
117 def.setDefaultForProject( false );
118
119 buildDefinitionDao.storeBuildDefinition( def );
120 }
121 }
122 }
123 else
124 {
125
126 if ( buildDefinition.getId() == storedDefinition.getId() )
127 {
128 getLogger().info(
129 "processing this build definition would result in no default build definition for project group" );
130 throw new ContinuumException(
131 "processing this build definition would result in no default build definition for project group" );
132 }
133 }
134 }
135 }
136 catch ( ContinuumStoreException cse )
137 {
138 getLogger().info( "error updating old default build definition", cse );
139 throw new ContinuumException( "error updating old default build definition", cse );
140 }
141 }
142
143
144
145
146
147
148
149
150
151 protected void updateBuildDefinitionInList( List<BuildDefinition> buildDefinitions,
152 BuildDefinition buildDefinition )
153 throws ContinuumException
154 {
155 try
156 {
157 BuildDefinition storedDefinition = null;
158
159 for ( BuildDefinition bd : buildDefinitions )
160 {
161 if ( bd.getId() == buildDefinition.getId() )
162 {
163 storedDefinition = bd;
164 }
165 }
166
167 if ( storedDefinition != null )
168 {
169 storedDefinition.setGoals( buildDefinition.getGoals() );
170 storedDefinition.setArguments( buildDefinition.getArguments() );
171 storedDefinition.setBuildFile( buildDefinition.getBuildFile() );
172 storedDefinition.setBuildFresh( buildDefinition.isBuildFresh() );
173
174
175 storedDefinition.setDefaultForProject( buildDefinition.isDefaultForProject() );
176
177 Schedule schedule;
178 if ( buildDefinition.getSchedule() == null )
179 {
180 try
181 {
182 schedule = scheduleDao.getScheduleByName( ConfigurationService.DEFAULT_SCHEDULE_NAME );
183 }
184 catch ( ContinuumStoreException e )
185 {
186 throw new ContinuumException( "Can't get default schedule.", e );
187 }
188 }
189 else
190 {
191 schedule = scheduleDao.getSchedule( buildDefinition.getSchedule().getId() );
192 }
193
194 storedDefinition.setSchedule( schedule );
195
196 storedDefinition.setProfile( buildDefinition.getProfile() );
197
198 storedDefinition.setDescription( buildDefinition.getDescription() );
199
200 storedDefinition.setType( buildDefinition.getType() );
201
202 storedDefinition.setAlwaysBuild( buildDefinition.isAlwaysBuild() );
203
204 buildDefinitionDao.storeBuildDefinition( storedDefinition );
205 }
206 else
207 {
208 throw new ContinuumException( "failed update, build definition didn't exist in project group" );
209 }
210 }
211 catch ( ContinuumStoreException cse )
212 {
213 throw new ContinuumException( "error in accessing or storing build definition" );
214 }
215 }
216 }