gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.lib / org.gvsig.online.lib.impl / src / main / java / org / gvsig / online / lib / impl / workspace / tables / AbstractTable.java @ 9512
History | View | Annotate | Download (9.51 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.online.lib.impl.workspace.tables; |
24 |
|
25 |
import java.sql.Timestamp; |
26 |
import java.util.HashMap; |
27 |
import java.util.Map; |
28 |
import org.gvsig.fmap.dal.exception.DataException; |
29 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
30 |
import org.gvsig.fmap.dal.feature.Feature; |
31 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
32 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.fmap.dal.feature.FeatureType; |
35 |
import org.gvsig.tools.dispose.DisposeUtils; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
import org.gvsig.online.lib.api.workingcopy.OnlineWorkingcopy; |
39 |
|
40 |
/**
|
41 |
*
|
42 |
* @author gvSIG Team
|
43 |
*/
|
44 |
@SuppressWarnings(value = "UseSpecificCatch") |
45 |
public abstract class AbstractTable { |
46 |
|
47 |
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractTable.class); |
48 |
|
49 |
public static abstract class AbstractRow { |
50 |
protected final OnlineWorkingcopy workspace; |
51 |
// private EditableFeature feature;
|
52 |
private final String tableName; |
53 |
private final String codeName; |
54 |
private Map<String,Object> values; |
55 |
|
56 |
protected AbstractRow(OnlineWorkingcopy workspace, String tableName, String codeName) { |
57 |
this.tableName = tableName;
|
58 |
this.codeName = codeName;
|
59 |
this.workspace = workspace;
|
60 |
this.values = new HashMap<>(); |
61 |
// this.newCode();
|
62 |
} |
63 |
|
64 |
protected AbstractRow(OnlineWorkingcopy workspace, String tableName, String codeName, Feature feature) { |
65 |
this(workspace, tableName, codeName);
|
66 |
if (feature != null) { |
67 |
FeatureType ft = feature.getType(); |
68 |
for (FeatureAttributeDescriptor attrdesc : ft) {
|
69 |
String attrname = attrdesc.getName();
|
70 |
this.values.put(attrname, feature.get(attrname));
|
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
protected boolean isNull(String attrname) { |
76 |
return this.values.get(attrname) == null; |
77 |
} |
78 |
|
79 |
protected String getString(String attrname) { |
80 |
return (String) this.values.get(attrname); |
81 |
} |
82 |
|
83 |
protected Timestamp getTimestamp(String attrname) { |
84 |
return (Timestamp) this.values.getOrDefault(attrname, null); |
85 |
} |
86 |
|
87 |
protected int getInt(String attrname) { |
88 |
return this.getInt(attrname, -1); |
89 |
} |
90 |
|
91 |
protected int getInt(String attrname, int defaultValue) { |
92 |
Integer n = (Integer) this.values.get(attrname); |
93 |
if( n==null ) { |
94 |
return defaultValue;
|
95 |
} |
96 |
return n;
|
97 |
} |
98 |
|
99 |
protected long getLong(String attrname) { |
100 |
return this.getLong(attrname, -1); |
101 |
} |
102 |
|
103 |
protected long getLong(String attrname, long defaultValue) { |
104 |
Long n = (Long) this.values.get(attrname); |
105 |
if( n==null ) { |
106 |
return defaultValue;
|
107 |
} |
108 |
return n;
|
109 |
} |
110 |
|
111 |
protected boolean getBoolean(String attrname) { |
112 |
return this.getBoolean(attrname, false); |
113 |
} |
114 |
|
115 |
protected boolean getBoolean(String attrname, boolean defaultValue) { |
116 |
Boolean n = (Boolean) this.values.get(attrname); |
117 |
if( n==null ) { |
118 |
return defaultValue;
|
119 |
} |
120 |
return n;
|
121 |
} |
122 |
|
123 |
protected String getLabelOfValue(String attrname) { |
124 |
FeatureType ft = this.workspace.getFeatureType(this.tableName); |
125 |
FeatureAttributeDescriptor attrdesc = ft.getAttributeDescriptor(attrname); |
126 |
String label = attrdesc.getLabelOfValue(this.values.get(attrname)); |
127 |
return label;
|
128 |
} |
129 |
|
130 |
protected void set(String attrname, String value) { |
131 |
this.values.put(attrname, value);
|
132 |
} |
133 |
|
134 |
protected void set(String attrname, int value) { |
135 |
this.values.put(attrname, value);
|
136 |
} |
137 |
|
138 |
protected void set(String attrname, long value) { |
139 |
this.values.put(attrname, value);
|
140 |
} |
141 |
|
142 |
protected void set(String attrname, boolean value) { |
143 |
this.values.put(attrname, value);
|
144 |
} |
145 |
|
146 |
protected void set(String attrname, Timestamp value) { |
147 |
this.values.put(attrname, value);
|
148 |
} |
149 |
|
150 |
public void update() { |
151 |
FeatureStore store = null;
|
152 |
boolean pendingFinishEditing = false; |
153 |
try {
|
154 |
store = this.workspace.openFeatureStore(this.tableName,false); |
155 |
if( store.getMode() == FeatureStore.MODE_QUERY ) {
|
156 |
store.edit(FeatureStore.MODE_PASS_THROUGH); |
157 |
pendingFinishEditing = true;
|
158 |
} |
159 |
this.update(store);
|
160 |
if( pendingFinishEditing ) {
|
161 |
store.finishEditing(); |
162 |
} |
163 |
} catch (Exception ex) { |
164 |
if( store!=null && pendingFinishEditing ) { |
165 |
store.cancelEditingQuietly(); |
166 |
} |
167 |
throw new RuntimeException("Can't update feature on '"+this.tableName+"'.", ex); |
168 |
} finally {
|
169 |
DisposeUtils.disposeQuietly(store); |
170 |
} |
171 |
} |
172 |
|
173 |
public void insert() { |
174 |
FeatureStore store = null;
|
175 |
boolean pendingFinishEditing = false; |
176 |
try {
|
177 |
store = this.workspace.openFeatureStore(this.tableName, false); |
178 |
if( !store.isEditing() ) {
|
179 |
store.edit(FeatureStore.MODE_FULLEDIT); |
180 |
pendingFinishEditing = true;
|
181 |
} |
182 |
this.insert(store);
|
183 |
if( pendingFinishEditing ) {
|
184 |
store.finishEditing(); |
185 |
} |
186 |
} catch (Exception ex) { |
187 |
if( store!=null && pendingFinishEditing ) { |
188 |
store.cancelEditingQuietly(); |
189 |
} |
190 |
throw new RuntimeException("Can't update feature on '"+this.tableName+"'.", ex); |
191 |
} finally {
|
192 |
DisposeUtils.disposeQuietly(store); |
193 |
} |
194 |
} |
195 |
|
196 |
public EditableFeature createFeature(FeatureStore store) throws DataException { |
197 |
EditableFeature f = store.createNewFeature(); |
198 |
for (Map.Entry<String, Object> value : this.values.entrySet()) { |
199 |
if( f.hasValue(value.getKey())) {
|
200 |
f.set(value.getKey(), value.getValue()); |
201 |
} |
202 |
} |
203 |
return f;
|
204 |
} |
205 |
|
206 |
public void insert(FeatureStore store) { |
207 |
try {
|
208 |
store.insert(this.createFeature(store));
|
209 |
} catch (Exception ex) { |
210 |
throw new RuntimeException("Can't insert feature on '"+this.tableName+"'.", ex); |
211 |
} |
212 |
} |
213 |
|
214 |
public void update(FeatureStore store) { |
215 |
try {
|
216 |
EditableFeature feature = this.createFeature(store);
|
217 |
feature.setUpdatable(true);
|
218 |
store.update(feature); |
219 |
} catch (Exception ex) { |
220 |
throw new RuntimeException("Can't update feature on '"+this.tableName+"'.", ex); |
221 |
} |
222 |
} |
223 |
|
224 |
public void update(FeatureStore store, FeatureSet fSet) { |
225 |
try {
|
226 |
EditableFeature feature = this.createFeature(store);
|
227 |
feature.setUpdatable(true);
|
228 |
fSet.update(feature); |
229 |
} catch (Exception ex) { |
230 |
throw new RuntimeException("Can't update feature on '"+this.tableName+"'.", ex); |
231 |
} |
232 |
} |
233 |
|
234 |
public void delete(FeatureStore store) { |
235 |
try {
|
236 |
// EditableFeature feat = this.createFeature(store);
|
237 |
// feat.setUpdatable(true);
|
238 |
// store.delete(feat);
|
239 |
store.delete("\""+this.codeName+"\" = '"+this.getString(this.codeName)+"'"); |
240 |
} catch (Exception ex) { |
241 |
throw new RuntimeException("Can't delete feature on '"+this.tableName+"'.", ex); |
242 |
} |
243 |
} |
244 |
|
245 |
public String getCode() { |
246 |
return this.getString(this.codeName); |
247 |
} |
248 |
|
249 |
public void newCode() { |
250 |
this.set(this.codeName, workspace.createUniqueCode()); |
251 |
} |
252 |
|
253 |
public void setCode(String code) { |
254 |
this.set(this.codeName, code); |
255 |
} |
256 |
} |
257 |
|
258 |
private final String tableName; |
259 |
private final FeatureType featureType; |
260 |
|
261 |
public AbstractTable(String tableName, FeatureType featureType) { |
262 |
this.tableName = tableName;
|
263 |
this.featureType = featureType;
|
264 |
} |
265 |
|
266 |
public String getTableName() { |
267 |
return this.tableName; |
268 |
} |
269 |
|
270 |
public FeatureType getFeatureType() {
|
271 |
return this.featureType; |
272 |
} |
273 |
|
274 |
} |