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