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 org.apache.continuum.dao.BuildDefinitionDao;
23 import org.apache.continuum.dao.ScheduleDao;
24 import org.apache.maven.continuum.ContinuumException;
25 import org.apache.maven.continuum.configuration.ConfigurationService;
26 import org.apache.maven.continuum.model.project.BuildDefinition;
27 import org.apache.maven.continuum.model.project.Project;
28 import org.apache.maven.continuum.model.project.ProjectGroup;
29 import org.apache.maven.continuum.model.project.Schedule;
30 import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
31 import org.apache.maven.continuum.store.ContinuumStoreException;
32
33 import java.util.List;
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 = buildDefinitionDao.getDefaultBuildDefinitionForProject(
63 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 = buildDefinitionDao.getDefaultBuildDefinitionsForProjectGroup(
103 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 && buildDefinition.getType().equals(
113 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 storedDefinition.setUpdatePolicy( buildDefinition.getUpdatePolicy() );
174
175
176 storedDefinition.setDefaultForProject( buildDefinition.isDefaultForProject() );
177
178 Schedule schedule;
179 if ( buildDefinition.getSchedule() == null )
180 {
181 try
182 {
183 schedule = scheduleDao.getScheduleByName( ConfigurationService.DEFAULT_SCHEDULE_NAME );
184 }
185 catch ( ContinuumStoreException e )
186 {
187 throw new ContinuumException( "Can't get default schedule.", e );
188 }
189 }
190 else
191 {
192 schedule = scheduleDao.getSchedule( buildDefinition.getSchedule().getId() );
193 }
194
195 storedDefinition.setSchedule( schedule );
196
197 storedDefinition.setProfile( buildDefinition.getProfile() );
198
199 storedDefinition.setDescription( buildDefinition.getDescription() );
200
201 storedDefinition.setType( buildDefinition.getType() );
202
203 storedDefinition.setAlwaysBuild( buildDefinition.isAlwaysBuild() );
204
205 buildDefinitionDao.storeBuildDefinition( storedDefinition );
206 }
207 else
208 {
209 throw new ContinuumException( "failed update, build definition didn't exist in project group" );
210 }
211 }
212 catch ( ContinuumStoreException cse )
213 {
214 throw new ContinuumException( "error in accessing or storing build definition" );
215 }
216 }
217 }