View Javadoc

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