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