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.Collection;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.jdo.Extent;
28 import javax.jdo.PersistenceManager;
29 import javax.jdo.Query;
30 import javax.jdo.Transaction;
31
32 import org.apache.continuum.model.project.ProjectGroupSummary;
33 import org.apache.continuum.model.project.ProjectSummaryResult;
34 import org.apache.maven.continuum.model.project.Project;
35 import org.apache.maven.continuum.model.project.ProjectGroup;
36 import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
37 import org.apache.maven.continuum.store.ContinuumStoreException;
38 import org.springframework.stereotype.Repository;
39
40
41
42
43
44
45 @Repository("projectDao")
46 public class ProjectDaoImpl
47 extends AbstractDao
48 implements ProjectDao
49 {
50 public void removeProject( Project project )
51 {
52 removeObject( project );
53 }
54
55 public void updateProject( Project project )
56 throws ContinuumStoreException
57 {
58 updateObject( project );
59 }
60
61 public Project getProject( int projectId )
62 throws ContinuumStoreException
63 {
64 return (Project) getObjectById( Project.class, projectId );
65 }
66
67 public Project getProject( String groupId, String artifactId, String version )
68 throws ContinuumStoreException
69 {
70 PersistenceManager pm = getPersistenceManager();
71
72 Transaction tx = pm.currentTransaction();
73
74 try
75 {
76 tx.begin();
77
78 Extent extent = pm.getExtent( Project.class, true );
79
80 Query query = pm.newQuery( extent );
81
82 query.declareImports( "import java.lang.String" );
83
84 query.declareParameters( "String groupId, String artifactId, String version" );
85
86 query.setFilter( "this.groupId == groupId && this.artifactId == artifactId && this.version == version" );
87
88 Object[] params = new Object[3];
89 params[0] = groupId;
90 params[1] = artifactId;
91 params[2] = version;
92
93 Collection result = (Collection) query.executeWithArray( params );
94
95 if ( result.size() == 0 )
96 {
97 tx.commit();
98
99 return null;
100 }
101
102 Object object = pm.detachCopy( result.iterator().next() );
103
104 tx.commit();
105
106 return (Project) object;
107 }
108 finally
109 {
110 rollback( tx );
111 }
112 }
113
114 public Project getProjectByName( String name )
115 throws ContinuumStoreException
116 {
117 PersistenceManager pm = getPersistenceManager();
118
119 Transaction tx = pm.currentTransaction();
120
121 try
122 {
123 tx.begin();
124
125 Extent extent = pm.getExtent( Project.class, true );
126
127 Query query = pm.newQuery( extent );
128
129 query.declareImports( "import java.lang.String" );
130
131 query.declareParameters( "String name" );
132
133 query.setFilter( "this.name == name" );
134
135 Collection result = (Collection) query.execute( name );
136
137 if ( result.size() == 0 )
138 {
139 tx.commit();
140
141 return null;
142 }
143
144 Object object = pm.detachCopy( result.iterator().next() );
145
146 tx.commit();
147
148 return (Project) object;
149 }
150 finally
151 {
152 rollback( tx );
153 }
154 }
155
156 public List<Project> getProjectsWithDependenciesByGroupId( int projectGroupId )
157 {
158 PersistenceManager pm = getPersistenceManager();
159
160 Transaction tx = pm.currentTransaction();
161
162 try
163 {
164 tx.begin();
165
166 Extent extent = pm.getExtent( Project.class, true );
167
168 Query query = pm.newQuery( extent, "projectGroup.id == " + projectGroupId );
169
170 pm.getFetchPlan().addGroup( PROJECT_DEPENDENCIES_FETCH_GROUP );
171 List<Project> result = (List<Project>) query.execute();
172
173 result = (List<Project>) pm.detachCopyAll( result );
174
175 tx.commit();
176
177 return result;
178 }
179 finally
180 {
181 rollback( tx );
182 }
183 }
184
185 public Project getProjectWithBuilds( int projectId )
186 throws ContinuumStoreException
187 {
188 return (Project) getObjectById( Project.class, projectId, PROJECT_WITH_BUILDS_FETCH_GROUP );
189 }
190
191 public Project getProjectWithBuildDetails( int projectId )
192 throws ContinuumStoreException
193 {
194 return (Project) getObjectById( Project.class, projectId, PROJECT_BUILD_DETAILS_FETCH_GROUP );
195 }
196
197 public Project getProjectWithCheckoutResult( int projectId )
198 throws ContinuumStoreException
199 {
200 return (Project) getObjectById( Project.class, projectId, PROJECT_WITH_CHECKOUT_RESULT_FETCH_GROUP );
201 }
202
203 public List<Project> getProjectsInGroup( int projectGroupId )
204 throws ContinuumStoreException
205 {
206 PersistenceManager pm = getPersistenceManager();
207
208 Transaction tx = pm.currentTransaction();
209
210 try
211 {
212 tx.begin();
213
214 Extent extent = pm.getExtent( Project.class, true );
215
216 Query query = pm.newQuery( extent, "projectGroup.id == " + projectGroupId );
217
218 query.setOrdering( "name ascending" );
219
220 List<Project> result = (List<Project>) query.execute();
221
222 result = (List<Project>) pm.detachCopyAll( result );
223
224 tx.commit();
225
226 return result;
227 }
228 finally
229 {
230 rollback( tx );
231 }
232 }
233
234 public List<Project> getProjectsInGroupWithDependencies( int projectGroupId )
235 throws ContinuumStoreException
236 {
237 PersistenceManager pm = getPersistenceManager();
238
239 Transaction tx = pm.currentTransaction();
240
241 try
242 {
243 tx.begin();
244
245 Extent extent = pm.getExtent( Project.class, true );
246
247 Query query = pm.newQuery( extent, "projectGroup.id == " + projectGroupId );
248
249 query.setOrdering( "name ascending" );
250
251 pm.getFetchPlan().addGroup( PROJECT_DEPENDENCIES_FETCH_GROUP );
252
253 pm.getFetchPlan().addGroup( PROJECTGROUP_PROJECTS_FETCH_GROUP );
254
255 List<Project> result = (List<Project>) query.execute();
256
257 result = (List<Project>) pm.detachCopyAll( result );
258
259 tx.commit();
260
261 return result;
262 }
263 finally
264 {
265 rollback( tx );
266 }
267 }
268
269 public Project getProjectWithAllDetails( int projectId )
270 throws ContinuumStoreException
271 {
272 return (Project) getObjectById( Project.class, projectId, PROJECT_ALL_DETAILS_FETCH_GROUP );
273 }
274
275 public List<Project> getAllProjectsByName()
276 {
277 return getAllObjectsDetached( Project.class, "name ascending", null );
278 }
279
280
281 public List<Project> getAllProjectsByNameWithDependencies()
282 {
283 return getAllObjectsDetached( Project.class, "name ascending", PROJECT_DEPENDENCIES_FETCH_GROUP );
284 }
285
286 public List<Project> getAllProjectsByNameWithBuildDetails()
287 {
288 return getAllObjectsDetached( Project.class, "name ascending", PROJECT_BUILD_DETAILS_FETCH_GROUP );
289 }
290
291 public ProjectGroup getProjectGroupByProjectId( int projectId )
292 throws ContinuumObjectNotFoundException
293 {
294 try
295 {
296 return getProject( projectId ).getProjectGroup();
297 }
298 catch ( ContinuumStoreException e )
299 {
300 throw new ContinuumObjectNotFoundException(
301 "unable to find project group containing project with id: " + projectId );
302
303 }
304 }
305
306 public Project getProjectWithDependencies( int projectId )
307 throws ContinuumStoreException
308 {
309 return (Project) getObjectById( Project.class, projectId, PROJECT_DEPENDENCIES_FETCH_GROUP );
310 }
311
312 public Map<Integer, ProjectGroupSummary> getProjectsSummary()
313 {
314 PersistenceManager pm = getPersistenceManager();
315
316 Transaction tx = pm.currentTransaction();
317
318 try
319 {
320 tx.begin();
321
322 Extent extent = pm.getExtent( Project.class );
323
324 Query query = pm.newQuery( extent );
325
326 query.setResult( "projectGroup.id as projectGroupId, state as projectState, count(state) as size" );
327
328 query.setResultClass( ProjectSummaryResult.class );
329
330 query.setGrouping( "projectGroup.id, state" );
331
332 List<ProjectSummaryResult> results = (List<ProjectSummaryResult>) query.execute();
333
334 Map<Integer, ProjectGroupSummary> summaries = processProjectGroupSummary( results );
335
336 tx.commit();
337
338 return summaries;
339 }
340 finally
341 {
342 rollback( tx );
343 }
344 }
345
346 private Map<Integer, ProjectGroupSummary> processProjectGroupSummary( List<ProjectSummaryResult> results )
347 {
348 Map<Integer, ProjectGroupSummary> map = new HashMap<Integer, ProjectGroupSummary>();
349
350 for ( ProjectSummaryResult result : results )
351 {
352 ProjectGroupSummary summary;
353 int projectGroupId = result.getProjectGroupId();
354 int size = new Long( result.getSize() ).intValue();
355 int state = result.getProjectState();
356
357 if ( map.containsKey( projectGroupId ) )
358 {
359 summary = map.get( projectGroupId );
360 }
361 else
362 {
363 summary = new ProjectGroupSummary( projectGroupId );
364 }
365
366 summary.addProjects( size );
367
368 if ( state == 2 )
369 {
370 summary.addNumberOfSuccesses( size );
371 }
372 else if ( state == 3 )
373 {
374 summary.addNumberOfFailures( size );
375 }
376 else if ( state == 4 )
377 {
378 summary.addNumberOfErrors( size );
379 }
380
381 map.put( projectGroupId, summary );
382 }
383 return map;
384 }
385 }