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.store.ContinuumObjectNotFoundException; |
23 | import org.apache.maven.continuum.store.ContinuumStoreException; |
24 | import org.codehaus.plexus.jdo.PlexusJdoUtils; |
25 | import org.codehaus.plexus.jdo.PlexusObjectNotFoundException; |
26 | import org.codehaus.plexus.jdo.PlexusStoreException; |
27 | |
28 | import javax.annotation.Resource; |
29 | import javax.jdo.FetchPlan; |
30 | import javax.jdo.PersistenceManager; |
31 | import javax.jdo.PersistenceManagerFactory; |
32 | import javax.jdo.Transaction; |
33 | import java.util.List; |
34 | |
35 | /** |
36 | * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a> |
37 | * @version $Id: AbstractDao.java 742493 2009-02-09 13:45:14Z ctan $ |
38 | */ |
39 | public class AbstractDao |
40 | { |
41 | // ---------------------------------------------------------------------- |
42 | // Fetch Groups |
43 | // ---------------------------------------------------------------------- |
44 | |
45 | protected static final String PROJECT_WITH_BUILDS_FETCH_GROUP = "project-with-builds"; |
46 | |
47 | protected static final String PROJECT_WITH_CHECKOUT_RESULT_FETCH_GROUP = "project-with-checkout-result"; |
48 | |
49 | protected static final String BUILD_RESULT_WITH_DETAILS_FETCH_GROUP = "build-result-with-details"; |
50 | |
51 | protected static final String PROJECT_BUILD_DETAILS_FETCH_GROUP = "project-build-details"; |
52 | |
53 | protected static final String PROJECT_ALL_DETAILS_FETCH_GROUP = "project-all-details"; |
54 | |
55 | protected static final String PROJECT_DEPENDENCIES_FETCH_GROUP = "project-dependencies"; |
56 | |
57 | protected static final String PROJECTGROUP_PROJECTS_FETCH_GROUP = "projectgroup-projects"; |
58 | |
59 | protected static final String BUILD_TEMPLATE_BUILD_DEFINITIONS = "build-template-build-definitions"; |
60 | |
61 | /** |
62 | * @plexus.requirement |
63 | */ |
64 | @Resource |
65 | private StoreUtilities storeUtilities; |
66 | |
67 | protected Object addObject( Object object ) |
68 | { |
69 | return addObject( getPersistenceManager(), object ); |
70 | } |
71 | |
72 | private Object addObject( PersistenceManager pmf, Object object ) |
73 | { |
74 | return PlexusJdoUtils.addObject( pmf, object ); |
75 | } |
76 | |
77 | protected void removeObject( Object o ) |
78 | { |
79 | PlexusJdoUtils.removeObject( getPersistenceManager(), o ); |
80 | } |
81 | |
82 | protected void updateObject( Object object ) |
83 | throws ContinuumStoreException |
84 | { |
85 | updateObject( getPersistenceManager(), object ); |
86 | } |
87 | |
88 | private void updateObject( PersistenceManager pmf, Object object ) |
89 | throws ContinuumStoreException |
90 | { |
91 | try |
92 | { |
93 | PlexusJdoUtils.updateObject( pmf, object ); |
94 | } |
95 | catch ( PlexusStoreException e ) |
96 | { |
97 | throw new ContinuumStoreException( e.getMessage(), e ); |
98 | } |
99 | } |
100 | |
101 | protected Object getObjectById( Class clazz, int id ) |
102 | throws ContinuumStoreException |
103 | { |
104 | return getObjectById( clazz, id, null ); |
105 | } |
106 | |
107 | protected Object getObjectById( Class clazz, int id, String fetchGroup ) |
108 | throws ContinuumStoreException |
109 | { |
110 | try |
111 | { |
112 | return PlexusJdoUtils.getObjectById( getPersistenceManager(), clazz, id, fetchGroup ); |
113 | } |
114 | catch ( PlexusObjectNotFoundException e ) |
115 | { |
116 | throw new ContinuumObjectNotFoundException( e.getMessage() ); |
117 | } |
118 | catch ( PlexusStoreException e ) |
119 | { |
120 | throw new ContinuumStoreException( e.getMessage(), e ); |
121 | } |
122 | } |
123 | |
124 | protected Object getObjectFromQuery( Class clazz, String idField, String id, String fetchGroup ) |
125 | throws ContinuumStoreException |
126 | { |
127 | try |
128 | { |
129 | return PlexusJdoUtils.getObjectFromQuery( getPersistenceManager(), clazz, idField, id, fetchGroup ); |
130 | } |
131 | catch ( PlexusObjectNotFoundException e ) |
132 | { |
133 | throw new ContinuumObjectNotFoundException( e.getMessage() ); |
134 | } |
135 | catch ( PlexusStoreException e ) |
136 | { |
137 | throw new ContinuumStoreException( e.getMessage(), e ); |
138 | } |
139 | } |
140 | |
141 | protected List getAllObjectsDetached( Class clazz ) |
142 | { |
143 | return getAllObjectsDetached( clazz, null ); |
144 | } |
145 | |
146 | protected List getAllObjectsDetached( Class clazz, String fetchGroup ) |
147 | { |
148 | return getAllObjectsDetached( clazz, null, fetchGroup ); |
149 | } |
150 | |
151 | protected List getAllObjectsDetached( Class clazz, String ordering, String fetchGroup ) |
152 | { |
153 | return getAllObjectsDetached( getPersistenceManager(), clazz, ordering, fetchGroup ); |
154 | } |
155 | |
156 | protected List getAllObjectsDetached( PersistenceManager pmf, Class clazz, String ordering, String fetchGroup ) |
157 | { |
158 | return PlexusJdoUtils.getAllObjectsDetached( pmf, clazz, ordering, fetchGroup ); |
159 | } |
160 | |
161 | protected void rollback( Transaction tx ) |
162 | { |
163 | PlexusJdoUtils.rollbackIfActive( tx ); |
164 | } |
165 | |
166 | protected void attachAndDelete( Object object ) |
167 | { |
168 | PlexusJdoUtils.attachAndDelete( getPersistenceManager(), object ); |
169 | } |
170 | |
171 | protected PersistenceManager getPersistenceManager() |
172 | { |
173 | return getPersistenceManager( getContinuumPersistenceManagerFactory() ); |
174 | } |
175 | |
176 | private PersistenceManager getPersistenceManager( PersistenceManagerFactory pmf ) |
177 | { |
178 | PersistenceManager pm = pmf.getPersistenceManager(); |
179 | |
180 | pm.getFetchPlan().setMaxFetchDepth( -1 ); |
181 | pm.getFetchPlan().setDetachmentOptions( FetchPlan.DETACH_LOAD_FIELDS ); |
182 | |
183 | return pm; |
184 | } |
185 | |
186 | protected PersistenceManagerFactory getContinuumPersistenceManagerFactory() |
187 | { |
188 | return storeUtilities.getContinuumPersistenceManagerFactory(); |
189 | } |
190 | |
191 | protected Object makePersistent( PersistenceManager pm, Object object, boolean detach ) |
192 | { |
193 | return PlexusJdoUtils.makePersistent( pm, object, detach ); |
194 | } |
195 | |
196 | } |