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 @ 45445
History | View | Annotate | Download (4.39 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.HashSet; |
27 |
import java.util.List; |
28 |
import java.util.Set; |
29 |
import java.util.UUID; |
30 |
import org.gvsig.fmap.dal.DataStore; |
31 |
import org.gvsig.fmap.dal.DataTransaction; |
32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.tools.dispose.Disposable; |
35 |
import org.gvsig.tools.dispose.DisposeUtils; |
36 |
|
37 |
/**
|
38 |
*
|
39 |
* @author gvSIG Team
|
40 |
*/
|
41 |
@SuppressWarnings("UseSpecificCatch") |
42 |
public class DefaultTransaction implements DataTransaction { |
43 |
|
44 |
private final String code; |
45 |
private Set<DataStore> stores; |
46 |
private boolean inProgress; |
47 |
private List<Disposable> disposables; |
48 |
|
49 |
public DefaultTransaction() {
|
50 |
this.code = UUID.randomUUID().toString().replace("-", ""); |
51 |
this.stores = new HashSet<>(); |
52 |
this.disposables = new ArrayList<>(); |
53 |
this.inProgress = false; |
54 |
} |
55 |
|
56 |
@Override
|
57 |
public String getCode() { |
58 |
return this.code; |
59 |
} |
60 |
|
61 |
@Override
|
62 |
public void begin() throws DataException { |
63 |
this.inProgress = true; |
64 |
} |
65 |
|
66 |
@Override
|
67 |
public void commit() throws DataException { |
68 |
if( !this.isInProgress() ) { |
69 |
return;
|
70 |
} |
71 |
for (DataStore store : stores) {
|
72 |
if( store instanceof FeatureStore ) { |
73 |
FeatureStore fstore = (FeatureStore) store; |
74 |
if( fstore.isAppending() || fstore.isEditing() ) {
|
75 |
fstore.finishEditing(); |
76 |
} |
77 |
} |
78 |
} |
79 |
this.inProgress = false; |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public void rollback() throws DataException { |
84 |
if( !this.isInProgress() ) { |
85 |
return;
|
86 |
} |
87 |
for (DataStore store : stores) {
|
88 |
if( store instanceof FeatureStore ) { |
89 |
FeatureStore fstore = (FeatureStore) store; |
90 |
if( fstore.isAppending() || fstore.isEditing() ) {
|
91 |
fstore.cancelEditing(); |
92 |
} |
93 |
} |
94 |
} |
95 |
this.inProgress = false; |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public void rollbackQuietly() { |
100 |
try {
|
101 |
this.rollback();
|
102 |
} catch(Exception ex) { |
103 |
// Do nothing
|
104 |
} |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public void add(DataStore store) { |
109 |
// if( !store.setTransaction(this) ) {
|
110 |
// throw new IllegalStateException("Can't add store to transaction.");
|
111 |
// }
|
112 |
DisposeUtils.bind(store); |
113 |
this.stores.add(store);
|
114 |
} |
115 |
|
116 |
@Override
|
117 |
public void add(Disposable resource) throws DataException { |
118 |
this.disposables.add(resource);
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
public void remove(DataStore store) { |
123 |
if( this.inProgress ) { |
124 |
throw new IllegalStateException("Can't remove store from a in progress transaction."); |
125 |
} |
126 |
// store.setTransaction(null);
|
127 |
this.stores.remove(store);
|
128 |
DisposeUtils.dispose(store); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public boolean isInProgress() { |
133 |
return inProgress;
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public void dispose() { |
138 |
if( this.inProgress ) { |
139 |
this.rollbackQuietly();
|
140 |
} |
141 |
for (DataStore store : stores) {
|
142 |
// store.setTransaction(null);
|
143 |
DisposeUtils.disposeQuietly(store); |
144 |
|
145 |
} |
146 |
for (Disposable resource : disposables) {
|
147 |
// store.setTransaction(null);
|
148 |
DisposeUtils.disposeQuietly(resource); |
149 |
|
150 |
} |
151 |
this.disposables = null; |
152 |
this.stores = null; |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void close() throws Exception { |
157 |
this.dispose();
|
158 |
} |
159 |
|
160 |
} |