View Javadoc

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.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   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
38   * @version $Id: InstallationDaoImpl.java 1372260 2012-08-13 04:29:09Z brett $
39   * @plexus.component role="org.apache.continuum.dao.InstallationDao"
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          // first delete link beetwen profile and this installation
60          // then removing this
61          //attachAndDelete( installation );
62          PersistenceManager pm = getPersistenceManager();
63  
64          Transaction tx = pm.currentTransaction();
65  
66          try
67          {
68              // this must be done in the same transaction
69              tx.begin();
70  
71              // first removing linked jdk
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              // removing linked builder
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             // removing linked env Var
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             //query = pm
125             //    .newQuery( "SELECT FROM profile WHERE environmentVariables.contains(installation) && installation.name == name" );
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 }