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.Collections; |
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.project.BuildQueue; |
32 | import org.apache.maven.continuum.store.ContinuumStoreException; |
33 | import org.slf4j.Logger; |
34 | import org.slf4j.LoggerFactory; |
35 | import org.springframework.stereotype.Repository; |
36 | |
37 | /** |
38 | * |
39 | * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a> |
40 | * @plexus.component role="org.apache.continuum.dao.BuildQueueDao" |
41 | */ |
42 | @Repository("buildQueueDao") |
43 | public class BuildQueueDaoImpl |
44 | extends AbstractDao |
45 | implements BuildQueueDao |
46 | { |
47 | private static Logger log = LoggerFactory.getLogger( BuildQueueDaoImpl.class ); |
48 | |
49 | public BuildQueue addBuildQueue( BuildQueue buildQueue ) |
50 | throws ContinuumStoreException |
51 | { |
52 | return (BuildQueue) addObject( buildQueue ); |
53 | } |
54 | |
55 | public List<BuildQueue> getAllBuildQueues() |
56 | throws ContinuumStoreException |
57 | { |
58 | PersistenceManager pm = getPersistenceManager(); |
59 | |
60 | Transaction tx = pm.currentTransaction(); |
61 | |
62 | try |
63 | { |
64 | tx.begin(); |
65 | |
66 | Extent extent = pm.getExtent( BuildQueue.class, true ); |
67 | |
68 | Query query = pm.newQuery( extent ); |
69 | |
70 | List result = (List) query.execute(); |
71 | |
72 | return result == null ? Collections.EMPTY_LIST : (List<BuildQueue>) pm.detachCopyAll( result ); |
73 | } |
74 | finally |
75 | { |
76 | tx.commit(); |
77 | |
78 | rollback( tx ); |
79 | } |
80 | } |
81 | |
82 | public BuildQueue getBuildQueue( int buildQueueId ) |
83 | throws ContinuumStoreException |
84 | { |
85 | return (BuildQueue) getObjectById( BuildQueue.class, buildQueueId ); |
86 | } |
87 | |
88 | public BuildQueue getBuildQueueByName( String name ) |
89 | throws ContinuumStoreException |
90 | { |
91 | PersistenceManager pm = getPersistenceManager(); |
92 | |
93 | Transaction tx = pm.currentTransaction(); |
94 | |
95 | try |
96 | { |
97 | tx.begin(); |
98 | |
99 | Extent extent = pm.getExtent( BuildQueue.class, true ); |
100 | |
101 | Query query = pm.newQuery( extent ); |
102 | |
103 | query.declareImports( "import java.lang.String" ); |
104 | |
105 | query.declareParameters( "String name" ); |
106 | |
107 | query.setFilter( "this.name == name" ); |
108 | |
109 | Collection result = (Collection) query.execute( name ); |
110 | |
111 | if ( result.size() == 0 ) |
112 | { |
113 | tx.commit(); |
114 | |
115 | return null; |
116 | } |
117 | |
118 | Object object = pm.detachCopy( result.iterator().next() ); |
119 | |
120 | tx.commit(); |
121 | |
122 | return (BuildQueue) object; |
123 | } |
124 | finally |
125 | { |
126 | rollback( tx ); |
127 | } |
128 | } |
129 | |
130 | public void removeBuildQueue( BuildQueue buildQueue ) |
131 | throws ContinuumStoreException |
132 | { |
133 | removeObject( buildQueue ); |
134 | } |
135 | |
136 | public BuildQueue storeBuildQueue( BuildQueue buildQueue ) |
137 | throws ContinuumStoreException |
138 | { |
139 | updateObject( buildQueue ); |
140 | |
141 | return buildQueue; |
142 | } |
143 | } |