1 package org.apache.continuum.dao;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.continuum.model.system.Installation;
23 import org.apache.maven.continuum.model.system.Profile;
24 import org.apache.maven.continuum.store.ContinuumStoreException;
25 import org.codehaus.plexus.util.StringUtils;
26 import org.springframework.stereotype.Repository;
27
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31 import javax.jdo.Extent;
32 import javax.jdo.PersistenceManager;
33 import javax.jdo.Query;
34 import javax.jdo.Transaction;
35
36
37
38
39
40
41 @Repository( "installationDao" )
42 public class InstallationDaoImpl
43 extends AbstractDao
44 implements InstallationDao
45 {
46 public Installation addInstallation( Installation installation )
47 {
48 return (Installation) addObject( installation );
49 }
50
51 public List<Installation> getAllInstallations()
52 {
53 return getAllObjectsDetached( Installation.class, "name ascending", null );
54 }
55
56 public void removeInstallation( Installation installation )
57 throws ContinuumStoreException
58 {
59
60
61
62 PersistenceManager pm = getPersistenceManager();
63
64 Transaction tx = pm.currentTransaction();
65
66 try
67 {
68
69 tx.begin();
70
71
72
73 Extent extent = pm.getExtent( Profile.class, true );
74
75 Query query = pm.newQuery( extent );
76
77 query.declareImports( "import java.lang.String" );
78
79 query.declareParameters( "String name" );
80
81 query.setFilter( "this.jdk.name == name" );
82
83 Collection<Profile> result = (Collection) query.execute( installation.getName() );
84
85 if ( result.size() != 0 )
86 {
87 for ( Profile profile : result )
88 {
89 profile.setJdk( null );
90 pm.makePersistent( profile );
91 }
92 }
93
94
95 query = pm.newQuery( extent );
96
97 query.declareImports( "import java.lang.String" );
98
99 query.declareParameters( "String name" );
100
101 query.setFilter( "this.builder.name == name" );
102
103 result = (Collection) query.execute( installation.getName() );
104
105 if ( result.size() != 0 )
106 {
107 for ( Profile profile : result )
108 {
109 profile.setBuilder( null );
110 pm.makePersistent( profile );
111 }
112 }
113
114
115 query = pm.newQuery( extent );
116
117 query.declareImports( "import java.lang.String" );
118 query.declareImports( "import " + Installation.class.getName() );
119
120 query.declareParameters( "Installation installation" );
121
122 query.setFilter( "environmentVariables.contains(installation)" );
123
124
125
126
127 result = (Collection) query.execute( installation );
128
129 if ( result.size() != 0 )
130 {
131 for ( Profile profile : result )
132 {
133 List<Installation> newEnvironmentVariables = new ArrayList<Installation>();
134 for ( Installation current : (Iterable<Installation>) profile.getEnvironmentVariables() )
135 {
136 if ( !StringUtils.equals( current.getName(), installation.getName() ) )
137 {
138 newEnvironmentVariables.add( current );
139 }
140 }
141 profile.setEnvironmentVariables( newEnvironmentVariables );
142 pm.makePersistent( profile );
143 }
144 }
145
146 pm.deletePersistent( installation );
147
148 tx.commit();
149
150 }
151 finally
152 {
153 rollback( tx );
154 }
155 }
156
157 public void updateInstallation( Installation installation )
158 throws ContinuumStoreException
159 {
160 updateObject( installation );
161 }
162
163 public Installation getInstallation( int installationId )
164 throws ContinuumStoreException
165 {
166 PersistenceManager pm = getPersistenceManager();
167
168 Transaction tx = pm.currentTransaction();
169
170 try
171 {
172 tx.begin();
173
174 Extent extent = pm.getExtent( Installation.class, true );
175
176 Query query = pm.newQuery( extent );
177
178 query.declareParameters( "int installationId" );
179
180 query.setFilter( "this.installationId == installationId" );
181
182 Collection result = (Collection) query.execute( installationId );
183
184 if ( result.size() == 0 )
185 {
186 tx.commit();
187
188 return null;
189 }
190
191 Object object = pm.detachCopy( result.iterator().next() );
192
193 tx.commit();
194
195 return (Installation) object;
196 }
197 finally
198 {
199 rollback( tx );
200 }
201 }
202
203 public Installation getInstallation( String installationName )
204 throws ContinuumStoreException
205 {
206 PersistenceManager pm = getPersistenceManager();
207
208 Transaction tx = pm.currentTransaction();
209
210 try
211 {
212 tx.begin();
213
214 Extent extent = pm.getExtent( Installation.class, true );
215
216 Query query = pm.newQuery( extent );
217
218 query.declareImports( "import java.lang.String" );
219
220 query.declareParameters( "String name" );
221
222 query.setFilter( "this.name == name" );
223
224 Collection result = (Collection) query.execute( installationName );
225
226 if ( result.size() == 0 )
227 {
228 tx.commit();
229
230 return null;
231 }
232
233 Object object = pm.detachCopy( result.iterator().next() );
234
235 tx.commit();
236
237 return (Installation) object;
238 }
239 finally
240 {
241 rollback( tx );
242 }
243 }
244 }