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.Collection;
23  import java.util.List;
24  
25  import javax.jdo.Extent;
26  import javax.jdo.PersistenceManager;
27  import javax.jdo.Query;
28  import javax.jdo.Transaction;
29  
30  import org.apache.continuum.model.project.ProjectScmRoot;
31  import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
32  import org.apache.maven.continuum.store.ContinuumStoreException;
33  import org.springframework.stereotype.Repository;
34  
35  /**
36   * @author <a href="mailto:ctan@apache.org">Maria Catherine Tan</a>
37   * @version $Id: $
38   *  @plexus.component role="org.apache.continuum.dao.ProjectScmRootDao"
39   */
40  @Repository("projectScmRootDao")
41  public class ProjectScmRootDaoImpl
42      extends AbstractDao
43      implements ProjectScmRootDao
44  {
45      public ProjectScmRoot addProjectScmRoot( ProjectScmRoot projectScmRoot )
46          throws ContinuumStoreException
47      {
48          return (ProjectScmRoot) addObject( projectScmRoot );
49      }
50  
51      public List<ProjectScmRoot> getAllProjectScmRoots()
52      {
53          return getAllObjectsDetached( ProjectScmRoot.class );
54      }
55      
56      public List<ProjectScmRoot> getProjectScmRootByProjectGroup( int projectGroupId )
57      {
58          PersistenceManager pm = getPersistenceManager();
59  
60          Transaction tx = pm.currentTransaction();
61  
62          try
63          {
64              tx.begin();
65  
66              Extent extent = pm.getExtent( ProjectScmRoot.class, true );
67  
68              Query query = pm.newQuery( extent, "projectGroup.id == " + projectGroupId );
69  
70              List result = (List) query.execute();
71  
72              result = (List) pm.detachCopyAll( result );
73  
74              tx.commit();
75  
76              return result;
77          }
78          finally
79          {
80              rollback( tx );
81          }
82      }
83  
84      public void removeProjectScmRoot( ProjectScmRoot projectScmRoot )
85          throws ContinuumStoreException
86      {
87          removeObject( projectScmRoot );
88      }
89  
90      public void updateProjectScmRoot( ProjectScmRoot projectScmRoot )
91          throws ContinuumStoreException
92      {
93          updateObject( projectScmRoot );
94      }
95  
96      public ProjectScmRoot getProjectScmRootByProjectGroupAndScmRootAddress( int projectGroupId, String scmRootAddress )
97          throws ContinuumStoreException
98      {
99          PersistenceManager pm = getPersistenceManager();
100 
101         Transaction tx = pm.currentTransaction();
102 
103         try
104         {
105             tx.begin();
106 
107             Extent extent = pm.getExtent( ProjectScmRoot.class, true );
108 
109             Query query = pm.newQuery( extent );
110 
111             query.declareImports( "import java.lang.String" );
112 
113             query.declareParameters( "int projectGroupId, String scmRootAddress" );
114 
115             query.setFilter( "this.projectGroup.id == projectGroupId && this.scmRootAddress == scmRootAddress" );
116 
117             Object[] params = new Object[2];
118             params[0] = projectGroupId;
119             params[1] = scmRootAddress;
120             
121             Collection result = (Collection) query.executeWithArray( params );
122 
123             if ( result.size() == 0 )
124             {
125                 tx.commit();
126 
127                 return null;
128             }
129 
130             Object object = pm.detachCopy( result.iterator().next() );
131 
132             tx.commit();
133 
134             return (ProjectScmRoot) object;
135         }
136         finally
137         {
138             rollback( tx );
139         }
140     }
141     
142     public ProjectScmRoot getProjectScmRoot( int projectScmRootId )
143         throws ContinuumObjectNotFoundException, ContinuumStoreException
144     {
145         return (ProjectScmRoot) getObjectById( ProjectScmRoot.class, projectScmRootId );
146     }
147 }