EMMA Coverage Report (generated Sun Sep 18 11:34:27 PHT 2011)
[all classes][org.apache.continuum.dao]

COVERAGE SUMMARY FOR SOURCE FILE [DaoUtilsImpl.java]

nameclass, %method, %block, %line, %
DaoUtilsImpl.java100% (1/1)57%  (4/7)24%  (103/429)28%  (30/106)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DaoUtilsImpl100% (1/1)57%  (4/7)24%  (103/429)28%  (30/106)
getAggregatedProjectIdsAndBuildDefinitionIdsBySchedule (int): Map 0%   (0/1)0%   (0/66)0%   (0/15)
getProjectGroupIdsAndBuildDefinitionsIdsBySchedule (int): Map 0%   (0/1)0%   (0/115)0%   (0/26)
getProjectIdsAndBuildDefinitionsIdsBySchedule (int): Map 0%   (0/1)0%   (0/127)0%   (0/28)
closePersistenceManagerFactory (PersistenceManagerFactory, int): void 100% (1/1)33%  (9/27)42%  (5/12)
DaoUtilsImpl (): void 100% (1/1)100% (3/3)100% (1/1)
closeStore (): void 100% (1/1)100% (6/6)100% (2/2)
eraseDatabase (): void 100% (1/1)100% (85/85)100% (22/22)

1package 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 
22import java.util.ArrayList;
23import java.util.HashMap;
24import java.util.Iterator;
25import java.util.List;
26import java.util.Map;
27 
28import javax.annotation.Resource;
29import javax.jdo.Extent;
30import javax.jdo.JDOUserException;
31import javax.jdo.PersistenceManager;
32import javax.jdo.PersistenceManagerFactory;
33import javax.jdo.Query;
34import javax.jdo.Transaction;
35 
36import org.apache.continuum.model.project.ProjectScmRoot;
37import org.apache.continuum.model.release.ContinuumReleaseResult;
38import org.apache.continuum.model.repository.DirectoryPurgeConfiguration;
39import org.apache.continuum.model.repository.LocalRepository;
40import org.apache.continuum.model.repository.RepositoryPurgeConfiguration;
41import org.apache.maven.continuum.model.project.BuildDefinition;
42import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
43import org.apache.maven.continuum.model.project.BuildQueue;
44import org.apache.maven.continuum.model.project.BuildResult;
45import org.apache.maven.continuum.model.project.Project;
46import org.apache.maven.continuum.model.project.ProjectDependency;
47import org.apache.maven.continuum.model.project.ProjectDeveloper;
48import org.apache.maven.continuum.model.project.ProjectGroup;
49import org.apache.maven.continuum.model.project.ProjectNotifier;
50import org.apache.maven.continuum.model.project.Schedule;
51import org.apache.maven.continuum.model.scm.ChangeFile;
52import org.apache.maven.continuum.model.scm.ChangeSet;
53import org.apache.maven.continuum.model.scm.ScmResult;
54import org.apache.maven.continuum.model.system.Installation;
55import org.apache.maven.continuum.model.system.Profile;
56import org.apache.maven.continuum.model.system.SystemConfiguration;
57import org.apache.maven.continuum.store.ContinuumStoreException;
58import org.codehaus.plexus.jdo.PlexusJdoUtils;
59import org.springframework.stereotype.Repository;
60 
61/**
62 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
63 * @version $Id: DaoUtilsImpl.java 790386 2009-07-01 21:27:25Z evenisse $
64 * @plexus.component role="org.apache.continuum.dao.DaoUtils"
65 */
66@Repository("daoUtils")
67public class DaoUtilsImpl
68    extends AbstractDao
69    implements DaoUtils
70{
71    /**
72     * @plexus.requirement role="org.apache.continuum.dao.ProjectDao"
73     */
74    @Resource
75    private ProjectDao projectDao;
76 
77    public void closeStore()
78    {
79        closePersistenceManagerFactory( getContinuumPersistenceManagerFactory(), 1 );
80    }
81 
82    public void eraseDatabase()
83    {
84        PlexusJdoUtils.removeAll( getPersistenceManager(), BuildDefinitionTemplate.class );
85        PlexusJdoUtils.removeAll( getPersistenceManager(), BuildResult.class );
86        PlexusJdoUtils.removeAll( getPersistenceManager(), ContinuumReleaseResult.class );
87        PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectScmRoot.class );
88        PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectGroup.class );
89        PlexusJdoUtils.removeAll( getPersistenceManager(), Project.class );
90        PlexusJdoUtils.removeAll( getPersistenceManager(), BuildDefinition.class );
91        PlexusJdoUtils.removeAll( getPersistenceManager(), RepositoryPurgeConfiguration.class );
92        PlexusJdoUtils.removeAll( getPersistenceManager(), LocalRepository.class );
93        PlexusJdoUtils.removeAll( getPersistenceManager(), DirectoryPurgeConfiguration.class );
94        PlexusJdoUtils.removeAll( getPersistenceManager(), Schedule.class );
95        PlexusJdoUtils.removeAll( getPersistenceManager(), BuildQueue.class );
96        PlexusJdoUtils.removeAll( getPersistenceManager(), Profile.class );
97        PlexusJdoUtils.removeAll( getPersistenceManager(), Installation.class );
98        PlexusJdoUtils.removeAll( getPersistenceManager(), ScmResult.class );
99        PlexusJdoUtils.removeAll( getPersistenceManager(), SystemConfiguration.class );
100        PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectNotifier.class );
101        PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectDeveloper.class );
102        PlexusJdoUtils.removeAll( getPersistenceManager(), ProjectDependency.class );
103        PlexusJdoUtils.removeAll( getPersistenceManager(), ChangeSet.class );
104        PlexusJdoUtils.removeAll( getPersistenceManager(), ChangeFile.class );
105    }
106 
107    /**
108     * Close the PersistenceManagerFactory.
109     *
110     * @param numTry The number of try. The maximum try is 5.
111     */
112    private void closePersistenceManagerFactory( PersistenceManagerFactory pmf, int numTry )
113    {
114        if ( pmf != null )
115        {
116            if ( !pmf.isClosed() )
117            {
118                try
119                {
120                    pmf.close();
121                }
122                catch ( JDOUserException e )
123                {
124                    if ( numTry < 5 )
125                    {
126                        try
127                        {
128                            Thread.currentThread().wait( 1000 );
129                        }
130                        catch ( InterruptedException ie )
131                        {
132                            // nothing to do
133                        }
134 
135                        closePersistenceManagerFactory( pmf, numTry + 1 );
136                    }
137                    else
138                    {
139                        throw e;
140                    }
141                }
142            }
143        }
144    }
145 
146    /**
147     * get the combined list of projectId and build definitions, including the
148     * ones inherited by their project group
149     *
150     * @param scheduleId
151     * @return
152     * @throws org.apache.maven.continuum.store.ContinuumStoreException
153     *
154     * @todo Move to a better place
155     */
156    public Map<Integer, Object> getAggregatedProjectIdsAndBuildDefinitionIdsBySchedule( int scheduleId )
157        throws ContinuumStoreException
158    {
159        Map<Integer, Object> projectSource = getProjectIdsAndBuildDefinitionsIdsBySchedule( scheduleId );
160        Map<Integer, Object> projectGroupSource = getProjectGroupIdsAndBuildDefinitionsIdsBySchedule( scheduleId );
161 
162        Map<Integer, Object> aggregate = new HashMap<Integer, Object>();
163 
164        // start out by checking if we have projects with this scheduleId
165        if ( projectSource != null )
166        {
167            aggregate.putAll( projectSource );
168        }
169 
170        // iterate through the project groups and make sure we are not walking
171        // over projects that
172        // might define their own build definitions
173        if ( projectGroupSource != null )
174        {
175            for ( Integer projectGroupId : projectGroupSource.keySet() )
176            {
177                List<Project> projectsInGroup = projectDao.getProjectsInGroup( projectGroupId );
178 
179                for ( Project p : projectsInGroup )
180                {
181                    Integer projectId = p.getId();
182                    if ( !aggregate.keySet().contains( projectId ) )
183                    {
184                        aggregate.put( projectId, projectGroupSource.get( projectGroupId ) );
185                    }
186                }
187            }
188        }
189        return aggregate;
190    }
191 
192    /**
193     * @param scheduleId
194     * @return
195     * @throws ContinuumStoreException
196     * @todo Move to a better place
197     */
198    public Map<Integer, Object> getProjectIdsAndBuildDefinitionsIdsBySchedule( int scheduleId )
199        throws ContinuumStoreException
200    {
201        PersistenceManager pm = getPersistenceManager();
202 
203        Transaction tx = pm.currentTransaction();
204 
205        try
206        {
207            tx.begin();
208 
209            Extent extent = pm.getExtent( Project.class, true );
210 
211            Query query = pm.newQuery( extent );
212 
213            query.declareParameters( "int scheduleId" );
214 
215            query.declareImports( "import org.apache.maven.continuum.model.project.BuildDefinition" );
216 
217            query.declareVariables( "BuildDefinition buildDef" );
218 
219            query.setFilter( "buildDefinitions.contains(buildDef) && buildDef.schedule.id == scheduleId" );
220 
221            query.setResult( "this.id, buildDef.id" );
222 
223            List result = (List) query.execute( scheduleId );
224 
225            Map projects = new HashMap();
226 
227            if ( result != null && !result.isEmpty() )
228            {
229                for ( Iterator i = result.iterator(); i.hasNext(); )
230                {
231                    Object[] obj = (Object[]) i.next();
232 
233                    List buildDefinitions;
234 
235                    if ( projects.get( obj[0] ) != null )
236                    {
237                        buildDefinitions = (List) projects.get( obj[0] );
238                    }
239                    else
240                    {
241                        buildDefinitions = new ArrayList();
242 
243                        projects.put( obj[0], buildDefinitions );
244                    }
245 
246                    buildDefinitions.add( obj[1] );
247                }
248 
249                return projects;
250            }
251            if ( !projects.isEmpty() )
252            {
253                return projects;
254            }
255        }
256        finally
257        {
258            tx.commit();
259 
260            rollback( tx );
261        }
262 
263        return null;
264    }
265 
266    /**
267     * @param scheduleId
268     * @return
269     * @throws ContinuumStoreException
270     * @todo Move to a better place
271     */
272    public Map<Integer, Object> getProjectGroupIdsAndBuildDefinitionsIdsBySchedule( int scheduleId )
273        throws ContinuumStoreException
274    {
275        PersistenceManager pm = getPersistenceManager();
276 
277        Transaction tx = pm.currentTransaction();
278 
279        try
280        {
281            tx.begin();
282 
283            Extent extent = pm.getExtent( ProjectGroup.class, true );
284 
285            Query query = pm.newQuery( extent );
286 
287            query.declareParameters( "int scheduleId" );
288 
289            query.declareImports( "import org.apache.maven.continuum.model.project.BuildDefinition" );
290 
291            query.declareVariables( "BuildDefinition buildDef" );
292 
293            query.setFilter( "buildDefinitions.contains(buildDef) && buildDef.schedule.id == scheduleId" );
294 
295            query.setResult( "this.id, buildDef.id" );
296 
297            List result = (List) query.execute( scheduleId );
298 
299            Map projectGroups = new HashMap();
300 
301            if ( result != null && !result.isEmpty() )
302            {
303                for ( Iterator i = result.iterator(); i.hasNext(); )
304                {
305                    Object[] obj = (Object[]) i.next();
306 
307                    List buildDefinitions;
308 
309                    if ( projectGroups.get( obj[0] ) != null )
310                    {
311                        buildDefinitions = (List) projectGroups.get( obj[0] );
312                    }
313                    else
314                    {
315                        buildDefinitions = new ArrayList();
316 
317                        projectGroups.put( obj[0], buildDefinitions );
318                    }
319 
320                    buildDefinitions.add( obj[1] );
321                }
322 
323                return projectGroups;
324            }
325        }
326        finally
327        {
328            tx.commit();
329 
330            rollback( tx );
331        }
332        return null;
333    }
334}

[all classes][org.apache.continuum.dao]
EMMA 2.0.5312 (C) Vladimir Roubtsov