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 @ 46125
History | View | Annotate | Download (6.84 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.HashSet; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import java.util.Set; |
31 |
import java.util.UUID; |
32 |
import org.gvsig.fmap.dal.DataServerExplorer; |
33 |
import org.gvsig.fmap.dal.DataStore; |
34 |
import org.gvsig.fmap.dal.SupportTransactions; |
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
37 |
import static org.gvsig.fmap.dal.feature.FeatureStore.MODE_QUERY; |
38 |
import org.gvsig.fmap.dal.spi.DataTransactionServices; |
39 |
import org.gvsig.tools.dispose.Disposable; |
40 |
import org.gvsig.tools.dispose.DisposeUtils; |
41 |
|
42 |
/**
|
43 |
*
|
44 |
* @author gvSIG Team
|
45 |
*/
|
46 |
@SuppressWarnings("UseSpecificCatch") |
47 |
public class DefaultTransaction implements DataTransactionServices { |
48 |
|
49 |
private final String code; |
50 |
private final Set<DataServerExplorer> explorers; |
51 |
private final Map<String,ConnectionService> connections; |
52 |
private Set<DataStore> stores; |
53 |
private boolean inProgress; |
54 |
private List<Disposable> disposables; |
55 |
|
56 |
public DefaultTransaction() {
|
57 |
this.code = UUID.randomUUID().toString().replace("-", ""); |
58 |
this.stores = new HashSet<>(); |
59 |
this.explorers = new HashSet<>(); |
60 |
this.disposables = new ArrayList<>(); |
61 |
this.inProgress = false; |
62 |
this.connections = new HashMap<>(); |
63 |
} |
64 |
|
65 |
@Override
|
66 |
public String getCode() { |
67 |
return this.code; |
68 |
} |
69 |
|
70 |
@Override
|
71 |
public void begin() throws DataException { |
72 |
if( this.inProgress ) { |
73 |
throw new IllegalStateException("Transaction already started."); |
74 |
} |
75 |
this.inProgress = true; |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public void commit() throws DataException { |
80 |
if( !this.isInProgress() ) { |
81 |
throw new IllegalStateException("Can't commit transaction without begin."); |
82 |
} |
83 |
for (DataStore store : stores) {
|
84 |
if( store instanceof FeatureStore ) { |
85 |
FeatureStore fstore = (FeatureStore) store; |
86 |
if( fstore.getMode() != MODE_QUERY) {
|
87 |
fstore.finishEditing(); |
88 |
} |
89 |
} |
90 |
} |
91 |
this.inProgress = false; |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public void rollback() throws DataException { |
96 |
if( !this.isInProgress() ) { |
97 |
throw new IllegalStateException("Can't rollback transaction without begin."); |
98 |
} |
99 |
|
100 |
for (DataStore store : stores) {
|
101 |
if( store instanceof FeatureStore ) { |
102 |
FeatureStore fstore = (FeatureStore) store; |
103 |
if( fstore.getMode() != MODE_QUERY) {
|
104 |
fstore.cancelEditing(); |
105 |
} |
106 |
} |
107 |
} |
108 |
this.inProgress = false; |
109 |
} |
110 |
|
111 |
@Override
|
112 |
public void rollbackQuietly() { |
113 |
try {
|
114 |
this.rollback();
|
115 |
} catch(Exception ex) { |
116 |
// Do nothing
|
117 |
} |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public void add(DataStore store) { |
122 |
add(store, true);
|
123 |
} |
124 |
|
125 |
@Override
|
126 |
public void add(DataStore store, boolean local) { |
127 |
if(this.stores.contains(store)){ |
128 |
return;
|
129 |
} |
130 |
if( store instanceof SupportTransactions ) { |
131 |
((SupportTransactions) store).setTransaction(this);
|
132 |
} |
133 |
if(!local){
|
134 |
DisposeUtils.bind(store); |
135 |
} |
136 |
this.stores.add(store);
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void add(DataServerExplorer explorer) { |
141 |
add(explorer, true);
|
142 |
} |
143 |
|
144 |
@Override
|
145 |
public void add(DataServerExplorer explorer, boolean local) { |
146 |
if(this.explorers.contains(explorer)){ |
147 |
return;
|
148 |
} |
149 |
if( explorer instanceof SupportTransactions ) { |
150 |
((SupportTransactions) explorer).setTransaction(this);
|
151 |
} |
152 |
if(!local){
|
153 |
DisposeUtils.bind(explorer); |
154 |
} |
155 |
this.explorers.add(explorer);
|
156 |
} |
157 |
|
158 |
@Override
|
159 |
public void add(Disposable resource) throws DataException { |
160 |
this.disposables.add(resource);
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public void remove(DataStore store) { |
165 |
if( this.inProgress ) { |
166 |
throw new IllegalStateException("Can't remove store from a in progress transaction."); |
167 |
} |
168 |
if( store instanceof SupportTransactions ) { |
169 |
((SupportTransactions) store).setTransaction(null);
|
170 |
} |
171 |
this.stores.remove(store);
|
172 |
DisposeUtils.dispose(store); |
173 |
} |
174 |
|
175 |
@Override
|
176 |
public boolean isInProgress() { |
177 |
return inProgress;
|
178 |
} |
179 |
|
180 |
@Override
|
181 |
public void dispose() { |
182 |
if( this.inProgress ) { |
183 |
this.rollbackQuietly();
|
184 |
} |
185 |
for (DataStore store : stores) {
|
186 |
if( store instanceof SupportTransactions ) { |
187 |
((SupportTransactions) store).setTransaction(null);
|
188 |
} |
189 |
DisposeUtils.disposeQuietly(store); |
190 |
|
191 |
} |
192 |
for (DataServerExplorer explorer : explorers) {
|
193 |
if( explorer instanceof SupportTransactions ) { |
194 |
((SupportTransactions) explorer).setTransaction(null);
|
195 |
} |
196 |
DisposeUtils.disposeQuietly(explorer); |
197 |
|
198 |
} |
199 |
for (Disposable resource : disposables) {
|
200 |
if( resource instanceof SupportTransactions ) { |
201 |
((SupportTransactions) resource).setTransaction(null);
|
202 |
} |
203 |
DisposeUtils.disposeQuietly(resource); |
204 |
|
205 |
} |
206 |
this.disposables = null; |
207 |
this.stores = null; |
208 |
} |
209 |
|
210 |
@Override
|
211 |
public void close() throws Exception { |
212 |
this.dispose();
|
213 |
} |
214 |
|
215 |
@Override
|
216 |
public void addConnection(ConnectionService connection) { |
217 |
if( this.connections.containsKey(connection.getId()) ) { |
218 |
return;
|
219 |
} |
220 |
this.connections.put(connection.getId(), connection);
|
221 |
} |
222 |
|
223 |
@Override
|
224 |
public ConnectionService getConnection(String id) { |
225 |
return this.connections.get(id); |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public void removeConnection(String id) { |
230 |
this.connections.remove(id);
|
231 |
} |
232 |
|
233 |
@Override
|
234 |
public boolean existsConnection(String id) { |
235 |
return this.connections.containsKey(id); |
236 |
} |
237 |
} |