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 java.util.ArrayList; |
23 | import java.util.Arrays; |
24 | import java.util.Collection; |
25 | import java.util.Collections; |
26 | import java.util.Iterator; |
27 | import java.util.List; |
28 | |
29 | import javax.annotation.Resource; |
30 | import javax.jdo.Extent; |
31 | import javax.jdo.PersistenceManager; |
32 | import javax.jdo.Query; |
33 | import javax.jdo.Transaction; |
34 | |
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.jdo.PlexusJdoUtils; |
41 | import org.springframework.stereotype.Repository; |
42 | |
43 | /** |
44 | * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a> |
45 | * @version $Id: ProjectGroupDaoImpl.java 765340 2009-04-15 20:22:00Z evenisse $ |
46 | * @plexus.component role="org.apache.continuum.dao.ProjectGroupDao" |
47 | */ |
48 | @Repository("projectGroupDao") |
49 | public class ProjectGroupDaoImpl |
50 | extends AbstractDao |
51 | implements ProjectGroupDao |
52 | { |
53 | /** |
54 | * @plexus.requirement role=org.apache.continuum.dao.ProjectDao" |
55 | */ |
56 | @Resource |
57 | private ProjectDao projectDao; |
58 | |
59 | public ProjectGroup addProjectGroup( ProjectGroup group ) |
60 | { |
61 | return (ProjectGroup) addObject( group ); |
62 | } |
63 | |
64 | public void removeProjectGroup( ProjectGroup projectGroup ) |
65 | { |
66 | ProjectGroup pg = null; |
67 | try |
68 | { |
69 | pg = getProjectGroupWithBuildDetailsByProjectGroupId( projectGroup.getId() ); |
70 | } |
71 | catch ( Exception e ) |
72 | { |
73 | // Do nothing |
74 | } |
75 | |
76 | if ( pg != null ) |
77 | { |
78 | // TODO: why do we need to do this? if not - build results are not |
79 | // removed and a integrity constraint is violated. I assume its |
80 | // because of the fetch groups |
81 | for ( Project p : (List<Project>) pg.getProjects() ) |
82 | { |
83 | projectDao.removeProject( p ); |
84 | } |
85 | |
86 | List<BuildDefinition> buildDefs = new ArrayList<BuildDefinition>(); |
87 | Iterator<BuildDefinition> it = pg.getBuildDefinitions().listIterator(); |
88 | boolean template = false; |
89 | while ( it.hasNext() ) |
90 | { |
91 | BuildDefinition bd = it.next(); |
92 | if ( bd.isTemplate() ) |
93 | { |
94 | template = true; |
95 | } |
96 | else |
97 | { |
98 | buildDefs.add( bd ); |
99 | } |
100 | } |
101 | if ( template ) |
102 | { |
103 | try |
104 | { |
105 | pg.setBuildDefinitions( buildDefs ); |
106 | updateProjectGroup( pg ); |
107 | } |
108 | catch ( ContinuumStoreException e ) |
109 | { |
110 | // Do nothing |
111 | } |
112 | } |
113 | |
114 | removeObject( pg ); |
115 | } |
116 | } |
117 | |
118 | public ProjectGroup getProjectGroup( int projectGroupId ) |
119 | throws ContinuumStoreException |
120 | { |
121 | return (ProjectGroup) getObjectById( ProjectGroup.class, projectGroupId ); |
122 | } |
123 | |
124 | public ProjectGroup getProjectGroupByGroupId( String groupId ) |
125 | throws ContinuumStoreException |
126 | { |
127 | return (ProjectGroup) getObjectFromQuery( ProjectGroup.class, "groupId", groupId, null ); |
128 | } |
129 | |
130 | public ProjectGroup getProjectGroupByGroupIdWithBuildDetails( String groupId ) |
131 | throws ContinuumStoreException |
132 | { |
133 | return (ProjectGroup) getObjectFromQuery( ProjectGroup.class, "groupId", groupId, |
134 | PROJECT_BUILD_DETAILS_FETCH_GROUP ); |
135 | } |
136 | |
137 | public ProjectGroup getProjectGroupByGroupIdWithProjects( String groupId ) |
138 | throws ContinuumStoreException |
139 | { |
140 | return (ProjectGroup) getObjectFromQuery( ProjectGroup.class, "groupId", groupId, |
141 | PROJECTGROUP_PROJECTS_FETCH_GROUP ); |
142 | } |
143 | |
144 | public ProjectGroup getProjectGroupByProjectId( int projectId ) |
145 | throws ContinuumObjectNotFoundException |
146 | { |
147 | try |
148 | { |
149 | return projectDao.getProject( projectId ).getProjectGroup(); |
150 | } |
151 | catch ( ContinuumStoreException e ) |
152 | { |
153 | throw new ContinuumObjectNotFoundException( |
154 | "unable to find project group containing project with id: " + projectId ); |
155 | |
156 | } |
157 | } |
158 | |
159 | public ProjectGroup getProjectGroupByProject( Project project ) |
160 | throws ContinuumObjectNotFoundException |
161 | { |
162 | return getProjectGroupByProjectId( project.getId() ); |
163 | } |
164 | |
165 | public void updateProjectGroup( ProjectGroup group ) |
166 | throws ContinuumStoreException |
167 | { |
168 | updateObject( group ); |
169 | } |
170 | |
171 | public ProjectGroup getProjectGroupWithProjects( int projectGroupId ) |
172 | throws ContinuumStoreException |
173 | { |
174 | return (ProjectGroup) getObjectById( ProjectGroup.class, projectGroupId, PROJECTGROUP_PROJECTS_FETCH_GROUP ); |
175 | } |
176 | |
177 | public Collection<ProjectGroup> getAllProjectGroupsWithProjects() |
178 | { |
179 | return getAllObjectsDetached( ProjectGroup.class, "name ascending", PROJECTGROUP_PROJECTS_FETCH_GROUP ); |
180 | } |
181 | |
182 | public List<ProjectGroup> getAllProjectGroupsWithBuildDetails() |
183 | { |
184 | return getAllObjectsDetached( ProjectGroup.class, "name ascending", PROJECT_BUILD_DETAILS_FETCH_GROUP ); |
185 | } |
186 | |
187 | public List<ProjectGroup> getAllProjectGroups() |
188 | { |
189 | return getAllObjectsDetached( ProjectGroup.class, "name ascending", null ); |
190 | } |
191 | |
192 | public List<ProjectGroup> getAllProjectGroupsWithTheLot() |
193 | { |
194 | List fetchGroups = Arrays.asList( |
195 | new String[]{PROJECT_WITH_BUILDS_FETCH_GROUP, PROJECTGROUP_PROJECTS_FETCH_GROUP, |
196 | BUILD_RESULT_WITH_DETAILS_FETCH_GROUP, PROJECT_WITH_CHECKOUT_RESULT_FETCH_GROUP, |
197 | PROJECT_ALL_DETAILS_FETCH_GROUP, PROJECT_BUILD_DETAILS_FETCH_GROUP} ); |
198 | return PlexusJdoUtils.getAllObjectsDetached( getPersistenceManager(), ProjectGroup.class, "name ascending", |
199 | fetchGroups ); |
200 | } |
201 | |
202 | public ProjectGroup getProjectGroupWithBuildDetailsByProjectGroupId( int projectGroupId ) |
203 | throws ContinuumStoreException |
204 | { |
205 | return (ProjectGroup) getObjectById( ProjectGroup.class, projectGroupId, PROJECT_BUILD_DETAILS_FETCH_GROUP ); |
206 | } |
207 | |
208 | public List<ProjectGroup> getProjectGroupByRepository( int repositoryId ) |
209 | { |
210 | PersistenceManager pm = getPersistenceManager(); |
211 | |
212 | Transaction tx = pm.currentTransaction(); |
213 | |
214 | try |
215 | { |
216 | tx.begin(); |
217 | |
218 | Extent extent = pm.getExtent( ProjectGroup.class, true ); |
219 | |
220 | Query query = pm.newQuery( extent ); |
221 | |
222 | query.declareParameters( "int repositoryId" ); |
223 | |
224 | query.setFilter( "this.localRepository.id == repositoryId" ); |
225 | |
226 | List result = (List) query.execute( repositoryId ); |
227 | |
228 | return result == null ? Collections.EMPTY_LIST : (List) pm.detachCopyAll( result ); |
229 | } |
230 | finally |
231 | { |
232 | tx.commit(); |
233 | |
234 | rollback( tx ); |
235 | } |
236 | } |
237 | |
238 | } |