svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / impl / DefaultTransaction.java @ 46160
History | View | Annotate | Download (9.42 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.fmap.dal.impl; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.HashMap; |
27 |
import java.util.List; |
28 |
import java.util.Map; |
29 |
import java.util.UUID; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.gvsig.fmap.dal.DataServerExplorer; |
32 |
import org.gvsig.fmap.dal.DataStore; |
33 |
import org.gvsig.fmap.dal.SupportTransactions; |
34 |
import org.gvsig.fmap.dal.exception.DataException; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import static org.gvsig.fmap.dal.feature.FeatureStore.MODE_QUERY; |
37 |
import org.gvsig.fmap.dal.spi.DataTransactionServices; |
38 |
import org.gvsig.tools.dispose.Disposable; |
39 |
import org.gvsig.tools.dispose.DisposeUtils; |
40 |
|
41 |
/**
|
42 |
*
|
43 |
* @author gvSIG Team
|
44 |
*/
|
45 |
@SuppressWarnings("UseSpecificCatch") |
46 |
public class DefaultTransaction implements DataTransactionServices { |
47 |
|
48 |
private final String code; |
49 |
private final Map<String,DataServerExplorer> explorers; |
50 |
private final Map<String,ConnectionService> connections; |
51 |
private Map<String,DataStore> stores; |
52 |
private boolean inProgress; |
53 |
private List<Disposable> disposables; |
54 |
|
55 |
public DefaultTransaction() {
|
56 |
this.code = UUID.randomUUID().toString().replace("-", ""); |
57 |
this.stores = new HashMap<>(); |
58 |
this.explorers = new HashMap<>(); |
59 |
this.disposables = new ArrayList<>(); |
60 |
this.inProgress = false; |
61 |
this.connections = new HashMap<>(); |
62 |
} |
63 |
|
64 |
@Override
|
65 |
public String getCode() { |
66 |
return this.code; |
67 |
} |
68 |
|
69 |
@Override
|
70 |
public void begin() throws DataException { |
71 |
if( this.inProgress ) { |
72 |
throw new IllegalStateException("Transaction already started."); |
73 |
} |
74 |
this.inProgress = true; |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public void commit() throws DataException { |
79 |
if( !this.isInProgress() ) { |
80 |
throw new IllegalStateException("Can't commit transaction without begin."); |
81 |
} |
82 |
for (DataStore store : stores.values()) {
|
83 |
if( store instanceof FeatureStore ) { |
84 |
FeatureStore fstore = (FeatureStore) store; |
85 |
if( fstore.getMode() != MODE_QUERY) {
|
86 |
fstore.finishEditing(); |
87 |
} |
88 |
} |
89 |
} |
90 |
this.inProgress = false; |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public void rollback() throws DataException { |
95 |
if( !this.isInProgress() ) { |
96 |
throw new IllegalStateException("Can't rollback transaction without begin."); |
97 |
} |
98 |
|
99 |
for (DataStore store : stores.values()) {
|
100 |
if( store instanceof FeatureStore ) { |
101 |
FeatureStore fstore = (FeatureStore) store; |
102 |
if( fstore.getMode() != MODE_QUERY) {
|
103 |
fstore.cancelEditing(); |
104 |
} |
105 |
} |
106 |
} |
107 |
this.inProgress = false; |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public void rollbackQuietly() { |
112 |
try {
|
113 |
this.rollback();
|
114 |
} catch(Exception ex) { |
115 |
// Do nothing
|
116 |
} |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public void add(DataStore store) { |
121 |
add(store, null, true); |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public void add(DataStore store, String id) { |
126 |
this.add(store, id, true); |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public void add(DataStore store, boolean local) { |
131 |
this.add(store, null, local); |
132 |
} |
133 |
|
134 |
@Override
|
135 |
public void add(DataStore store, String id, boolean local) { |
136 |
String theId = id;
|
137 |
if( StringUtils.isBlank(id) ) {
|
138 |
theId = store.hashCode() + "@"+ store.getFullName();
|
139 |
} else {
|
140 |
DataStore theStore = this.stores.get(theId);
|
141 |
if(theStore!=null ){ |
142 |
if( theStore==store ) {
|
143 |
return;
|
144 |
} |
145 |
throw new IllegalArgumentException("The id '"+id+"' is already used."); |
146 |
} |
147 |
} |
148 |
if( store instanceof SupportTransactions ) { |
149 |
((SupportTransactions) store).setTransaction(this);
|
150 |
} |
151 |
if(!local){
|
152 |
DisposeUtils.bind(store); |
153 |
} |
154 |
this.stores.put(theId,store);
|
155 |
} |
156 |
|
157 |
@Override
|
158 |
public FeatureStore getFeatureStore(String id) { |
159 |
return (FeatureStore) this.stores.get(id); |
160 |
} |
161 |
|
162 |
@Override
|
163 |
public void add(DataServerExplorer explorer) { |
164 |
this.add(explorer, null, true); |
165 |
} |
166 |
|
167 |
@Override
|
168 |
public void add(DataServerExplorer explorer, String id) { |
169 |
this.add(explorer, id, true); |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public void add(DataServerExplorer explorer, boolean local) { |
174 |
this.add(explorer, null, local); |
175 |
} |
176 |
|
177 |
@Override
|
178 |
public void add(DataServerExplorer explorer, String id, boolean local) { |
179 |
String theId = id;
|
180 |
if( StringUtils.isBlank(id) ) {
|
181 |
theId = String.valueOf(explorer.hashCode());
|
182 |
} else {
|
183 |
DataServerExplorer theExplorer = this.explorers.get(theId);
|
184 |
if(theExplorer!=null ){ |
185 |
if( theExplorer==explorer ) {
|
186 |
return;
|
187 |
} |
188 |
throw new IllegalArgumentException("The id '"+id+"' is already used."); |
189 |
} |
190 |
} |
191 |
|
192 |
if( explorer instanceof SupportTransactions ) { |
193 |
((SupportTransactions) explorer).setTransaction(this);
|
194 |
} |
195 |
if(!local){
|
196 |
DisposeUtils.bind(explorer); |
197 |
} |
198 |
this.explorers.put(theId,explorer);
|
199 |
} |
200 |
|
201 |
@Override
|
202 |
public DataServerExplorer getServerExplorer(String id) { |
203 |
return this.explorers.get(id); |
204 |
} |
205 |
|
206 |
@Override
|
207 |
public void add(Disposable resource) throws DataException { |
208 |
this.disposables.add(resource);
|
209 |
} |
210 |
|
211 |
@Override
|
212 |
public void remove(DataStore store) { |
213 |
if( this.inProgress ) { |
214 |
throw new IllegalStateException("Can't remove store from a in progress transaction."); |
215 |
} |
216 |
String id = null; |
217 |
for (Map.Entry<String, DataStore> entry : this.stores.entrySet()) { |
218 |
if( store == entry.getValue() ) {
|
219 |
id = entry.getKey(); |
220 |
break;
|
221 |
} |
222 |
} |
223 |
if( id==null ) { |
224 |
return;
|
225 |
} |
226 |
if( store instanceof SupportTransactions ) { |
227 |
((SupportTransactions) store).setTransaction(null);
|
228 |
} |
229 |
this.stores.remove(id);
|
230 |
DisposeUtils.dispose(store); |
231 |
} |
232 |
|
233 |
@Override
|
234 |
public void remove(DataServerExplorer serverExplorer) { |
235 |
if( this.inProgress ) { |
236 |
throw new IllegalStateException("Can't remove server explorer from a in progress transaction."); |
237 |
} |
238 |
String id = null; |
239 |
for (Map.Entry<String, DataServerExplorer> entry : this.explorers.entrySet()) { |
240 |
if( serverExplorer == entry.getValue() ) {
|
241 |
id = entry.getKey(); |
242 |
break;
|
243 |
} |
244 |
} |
245 |
if( id==null ) { |
246 |
return;
|
247 |
} |
248 |
if( serverExplorer instanceof SupportTransactions ) { |
249 |
((SupportTransactions) serverExplorer).setTransaction(null);
|
250 |
} |
251 |
this.explorers.remove(id);
|
252 |
DisposeUtils.dispose(serverExplorer); |
253 |
} |
254 |
|
255 |
@Override
|
256 |
public boolean isInProgress() { |
257 |
return inProgress;
|
258 |
} |
259 |
|
260 |
@Override
|
261 |
public void dispose() { |
262 |
if( this.inProgress ) { |
263 |
this.rollbackQuietly();
|
264 |
} |
265 |
for (DataStore store : stores.values()) {
|
266 |
if( store instanceof SupportTransactions ) { |
267 |
((SupportTransactions) store).setTransaction(null);
|
268 |
} |
269 |
DisposeUtils.disposeQuietly(store); |
270 |
|
271 |
} |
272 |
for (DataServerExplorer explorer : explorers.values()) {
|
273 |
if( explorer instanceof SupportTransactions ) { |
274 |
((SupportTransactions) explorer).setTransaction(null);
|
275 |
} |
276 |
DisposeUtils.disposeQuietly(explorer); |
277 |
|
278 |
} |
279 |
for (Disposable resource : disposables) {
|
280 |
if( resource instanceof SupportTransactions ) { |
281 |
((SupportTransactions) resource).setTransaction(null);
|
282 |
} |
283 |
DisposeUtils.disposeQuietly(resource); |
284 |
|
285 |
} |
286 |
this.disposables = null; |
287 |
this.stores = null; |
288 |
} |
289 |
|
290 |
@Override
|
291 |
public void close() throws Exception { |
292 |
this.dispose();
|
293 |
} |
294 |
|
295 |
@Override
|
296 |
public void addConnection(ConnectionService connection) { |
297 |
if( this.connections.containsKey(connection.getId()) ) { |
298 |
return;
|
299 |
} |
300 |
this.connections.put(connection.getId(), connection);
|
301 |
} |
302 |
|
303 |
@Override
|
304 |
public ConnectionService getConnection(String id) { |
305 |
return this.connections.get(id); |
306 |
} |
307 |
|
308 |
@Override
|
309 |
public void removeConnection(String id) { |
310 |
this.connections.remove(id);
|
311 |
} |
312 |
|
313 |
@Override
|
314 |
public boolean existsConnection(String id) { |
315 |
return this.connections.containsKey(id); |
316 |
} |
317 |
} |