1 package org.apache.continuum.dao;
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.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.annotation.Resource;
29 import javax.jdo.Extent;
30 import javax.jdo.PersistenceManager;
31 import javax.jdo.Query;
32 import javax.jdo.Transaction;
33
34 import org.apache.maven.continuum.execution.ContinuumBuildExecutorConstants;
35 import org.apache.maven.continuum.model.project.BuildDefinition;
36 import org.apache.maven.continuum.model.project.Project;
37 import org.apache.maven.continuum.model.project.ProjectGroup;
38 import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
39 import org.apache.maven.continuum.store.ContinuumStoreException;
40 import org.codehaus.plexus.util.StringUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.stereotype.Repository;
44
45
46
47
48
49
50 @Repository("buildDefinitionDao")
51 public class BuildDefinitionDaoImpl
52 extends AbstractDao
53 implements BuildDefinitionDao
54 {
55 private static final Logger log = LoggerFactory.getLogger( BuildDefinitionDaoImpl.class );
56
57
58
59
60 @Resource
61 private ProjectDao projectDao;
62
63
64
65
66 @Resource
67 private ProjectGroupDao projectGroupDao;
68
69 public BuildDefinition getBuildDefinition( int buildDefinitionId )
70 throws ContinuumStoreException
71 {
72 return (BuildDefinition) getObjectById( BuildDefinition.class, buildDefinitionId );
73 }
74
75 public void removeBuildDefinition( BuildDefinition buildDefinition )
76 throws ContinuumStoreException
77 {
78 removeObject( buildDefinition );
79 }
80
81 public BuildDefinition storeBuildDefinition( BuildDefinition buildDefinition )
82 throws ContinuumStoreException
83 {
84 updateObject( buildDefinition );
85
86 return buildDefinition;
87 }
88
89
90 public BuildDefinition addBuildDefinition( BuildDefinition buildDefinition )
91 throws ContinuumStoreException
92 {
93 return (BuildDefinition) addObject( buildDefinition );
94 }
95
96 public List<BuildDefinition> getAllBuildDefinitions()
97 throws ContinuumStoreException
98 {
99 PersistenceManager pm = getPersistenceManager();
100
101 Transaction tx = pm.currentTransaction();
102
103 try
104 {
105 tx.begin();
106
107 Extent extent = pm.getExtent( BuildDefinition.class, true );
108
109 Query query = pm.newQuery( extent );
110
111 List result = (List) query.execute();
112
113 return result == null ? Collections.EMPTY_LIST : (List<BuildDefinition>) pm.detachCopyAll( result );
114 }
115 finally
116 {
117 tx.commit();
118
119 rollback( tx );
120 }
121 }
122
123 public Map<Integer, Integer> getDefaultBuildDefinitions()
124 {
125 PersistenceManager pm = getPersistenceManager();
126
127 Transaction tx = pm.currentTransaction();
128
129 try
130 {
131 tx.begin();
132
133 Extent extent = pm.getExtent( Project.class, true );
134
135 Query query = pm.newQuery( extent );
136
137 query.declareImports( "import org.apache.maven.continuum.model.project.BuildDefinition" );
138
139 query.setFilter( "this.buildDefinitions.contains(buildDef) && buildDef.defaultForProject == true" );
140
141 query.declareVariables( "BuildDefinition buildDef" );
142
143 query.setResult( "this.id, buildDef.id" );
144
145 List result = (List) query.execute();
146
147
148
149 Map<Integer, Integer> builds = new HashMap<Integer, Integer>();
150
151 if ( result != null && !result.isEmpty() )
152 {
153 for ( Object aResult : result )
154 {
155 Object[] obj = (Object[]) aResult;
156
157 builds.put( (Integer) obj[0], (Integer) obj[1] );
158 }
159
160 return builds;
161 }
162 }
163 finally
164 {
165 tx.commit();
166
167 rollback( tx );
168 }
169
170 return null;
171 }
172
173 public List<BuildDefinition> getDefaultBuildDefinitionsForProjectGroup( int projectGroupId )
174 throws ContinuumStoreException
175 {
176 PersistenceManager pm = getPersistenceManager();
177
178 Transaction tx = pm.currentTransaction();
179
180 try
181 {
182 tx.begin();
183
184 Extent extent = pm.getExtent( ProjectGroup.class, true );
185
186 Query query = pm.newQuery( extent );
187
188 query.declareImports( "import " + BuildDefinition.class.getName() );
189
190 query.declareParameters( "int projectGroupId" );
191
192 query.setFilter(
193 "this.id == projectGroupId && this.buildDefinitions.contains(buildDef) && buildDef.defaultForProject == true" );
194
195 query.declareVariables( "BuildDefinition buildDef" );
196
197 query.setResult( "buildDef" );
198
199 List<BuildDefinition> result = (List<BuildDefinition>) query.execute( projectGroupId );
200
201 result = (List<BuildDefinition>) pm.detachCopyAll( result );
202
203 tx.commit();
204
205 if ( result != null )
206 {
207 return result;
208 }
209 }
210 finally
211 {
212 rollback( tx );
213 }
214
215 return new ArrayList<BuildDefinition>();
216 }
217
218 public List<BuildDefinition> getDefaultBuildDefinitionsForProjectGroup( ProjectGroup projectGroup )
219 throws ContinuumStoreException
220 {
221 return getDefaultBuildDefinitionsForProjectGroup( projectGroup.getId() );
222 }
223
224 public BuildDefinition getDefaultBuildDefinitionForProject( int projectId )
225 throws ContinuumStoreException
226 {
227 PersistenceManager pm = getPersistenceManager();
228
229 Transaction tx = pm.currentTransaction();
230
231 try
232 {
233 tx.begin();
234
235 Extent extent = pm.getExtent( BuildDefinition.class, true );
236
237 Query query = pm.newQuery( extent );
238
239 query.declareImports( "import " + Project.class.getName() );
240
241 query.declareParameters( "int projectId" );
242
243 query.setFilter(
244 "project.id == projectId && project.buildDefinitions.contains(this) && this.defaultForProject == true" );
245
246 query.declareVariables( "Project project" );
247
248 query.setResult( "this" );
249
250 List<BuildDefinition> result = (List<BuildDefinition>) query.execute( projectId );
251
252 result = (List<BuildDefinition>) pm.detachCopyAll( result );
253
254 tx.commit();
255
256 if ( result != null && !result.isEmpty() )
257 {
258 return result.get( 0 );
259 }
260 }
261 finally
262 {
263 rollback( tx );
264 }
265
266 throw new ContinuumObjectNotFoundException( "no default build definition declared for project " + projectId );
267 }
268
269 public BuildDefinition getDefaultBuildDefinitionForProject( Project project )
270 throws ContinuumStoreException
271 {
272 return getDefaultBuildDefinitionForProject( project.getId() );
273 }
274
275 public BuildDefinition getDefaultBuildDefinition( int projectId )
276 throws ContinuumStoreException
277 {
278
279 BuildDefinition bd = null;
280
281 try
282 {
283 bd = getDefaultBuildDefinitionForProject( projectId );
284 }
285 catch ( ContinuumObjectNotFoundException cne )
286 {
287
288 log.debug( "no default build definition on project, trying project group" );
289 }
290
291
292 if ( bd == null )
293 {
294 ProjectGroup projectGroup = projectGroupDao.getProjectGroupByProjectId( projectId );
295
296 Project p = projectDao.getProject( projectId );
297
298 List<BuildDefinition> bds = getDefaultBuildDefinitionsForProjectGroup( projectGroup.getId() );
299
300 for ( BuildDefinition bdef : bds )
301 {
302 if ( p.getExecutorId().equals( bdef.getType() ) || ( StringUtils.isEmpty( bdef.getType() ) &&
303 ContinuumBuildExecutorConstants.MAVEN_TWO_BUILD_EXECUTOR.equals( p.getExecutorId() ) ) )
304 {
305 return bdef;
306 }
307 }
308 }
309
310 return bd;
311 }
312
313 public List<BuildDefinition> getAllTemplates()
314 throws ContinuumStoreException
315 {
316 PersistenceManager pm = getPersistenceManager();
317
318 Transaction tx = pm.currentTransaction();
319
320 try
321 {
322 tx.begin();
323
324 Extent extent = pm.getExtent( BuildDefinition.class, true );
325
326 Query query = pm.newQuery( extent );
327 query.setFilter( "this.template == true" );
328 List result = (List) query.execute();
329 return result == null ? Collections.EMPTY_LIST : (List) pm.detachCopyAll( result );
330 }
331 finally
332 {
333 tx.commit();
334
335 rollback( tx );
336 }
337 }
338
339 public List<BuildDefinition> getBuildDefinitionsBySchedule( int scheduleId )
340 {
341 PersistenceManager pm = getPersistenceManager();
342
343 Transaction tx = pm.currentTransaction();
344
345 try
346 {
347 tx.begin();
348
349 Extent extent = pm.getExtent( BuildDefinition.class, true );
350
351 Query query = pm.newQuery( extent );
352
353 query.declareParameters( "int scheduleId" );
354
355 query.setFilter( "this.schedule.id == scheduleId" );
356
357 List result = (List) query.execute( scheduleId );
358
359 return result == null ? Collections.EMPTY_LIST : (List) pm.detachCopyAll( result );
360 }
361 finally
362 {
363 tx.commit();
364
365 rollback( tx );
366 }
367 }
368
369 }