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