svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / TableInfo.java @ 46177
History | View | Annotate | Download (17.5 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.geodb; |
7 |
|
8 |
import java.util.ArrayList; |
9 |
import java.util.Arrays; |
10 |
import java.util.List; |
11 |
import java.util.Map; |
12 |
import javax.swing.ComboBoxModel; |
13 |
import javax.swing.DefaultComboBoxModel; |
14 |
import javax.swing.DefaultListModel; |
15 |
import javax.swing.DefaultListSelectionModel; |
16 |
import javax.swing.ListModel; |
17 |
import javax.swing.ListSelectionModel; |
18 |
import org.apache.commons.lang3.StringUtils; |
19 |
import org.cresques.cts.IProjection; |
20 |
import org.gvsig.expressionevaluator.Expression; |
21 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
22 |
import org.gvsig.fmap.dal.DALLocator; |
23 |
import org.gvsig.fmap.dal.DataManager; |
24 |
import org.gvsig.fmap.dal.DataServerExplorer; |
25 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
26 |
import org.gvsig.fmap.dal.DataTypes; |
27 |
import org.gvsig.fmap.dal.exception.DataException; |
28 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
29 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
30 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
31 |
import org.gvsig.fmap.dal.feature.FeatureType; |
32 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
33 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.fmap.geom.type.GeometryType; |
36 |
import org.gvsig.tools.ToolsLocator; |
37 |
import org.gvsig.tools.dispose.DisposeUtils; |
38 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
39 |
import org.gvsig.tools.util.ContainerUtils; |
40 |
import org.gvsig.tools.util.LabeledValue; |
41 |
import org.gvsig.tools.util.LabeledValueImpl; |
42 |
import org.gvsig.tools.util.PropertiesSupport; |
43 |
import org.gvsig.tools.util.PropertiesSupportHelper; |
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
|
47 |
/**
|
48 |
*
|
49 |
* @author fdiaz
|
50 |
*/
|
51 |
public class TableInfo extends LabeledValueImpl<JDBCStoreParameters> implements PropertiesSupport { |
52 |
|
53 |
protected static final Logger LOGGER = LoggerFactory.getLogger(TableInfo.class); |
54 |
|
55 |
private final ListSelectionModel columnChecksModel; |
56 |
protected FeatureType featureType;
|
57 |
protected int geomFieldSelected = -1; |
58 |
protected int idFieldSelected = -1; |
59 |
protected ComboBoxModel<String> idFieldComboModel; |
60 |
protected ComboBoxModel<String> geomFieldComboModel; |
61 |
protected List<FeatureAttributeDescriptor> attributeDescriptors; |
62 |
protected ListModel<LabeledValue<FeatureAttributeDescriptor>> columnsListModel; |
63 |
protected Expression filter; |
64 |
protected IProjection projection;
|
65 |
protected boolean selected; |
66 |
protected String documentName; |
67 |
protected boolean isView; |
68 |
protected Boolean readOnly; |
69 |
protected boolean requireGeometry; |
70 |
|
71 |
protected PropertiesSupportHelper propertiesHelper;
|
72 |
protected final String id; |
73 |
|
74 |
public TableInfo(String id, JDBCStoreParameters parameters, boolean requireGeometry, boolean isView) { |
75 |
super(getLabelForTable(parameters), parameters.getCopy());
|
76 |
this.id = id;
|
77 |
this.columnChecksModel = new DefaultListSelectionModel(); |
78 |
this.selected = false; |
79 |
this.documentName = parameters.getTable();
|
80 |
this.projection = parameters.getCRS();
|
81 |
this.isView = isView;
|
82 |
this.readOnly = null; |
83 |
this.requireGeometry = requireGeometry;
|
84 |
this.propertiesHelper = new PropertiesSupportHelper(); |
85 |
} |
86 |
|
87 |
private static String getLabelForTable(JDBCStoreParameters parameters) { |
88 |
String schema = parameters.getSchema();
|
89 |
String tableName = parameters.getTable();
|
90 |
if (StringUtils.isBlank(schema)) {
|
91 |
return tableName;
|
92 |
} |
93 |
return schema + "." + tableName; |
94 |
} |
95 |
|
96 |
public void fetch(JDBCStoreParameters parameters) { |
97 |
this.value = parameters.getCopy();
|
98 |
this.projection = parameters.getCRS();
|
99 |
this.setGeomFieldSelected(parameters.getDefaultGeometryField());
|
100 |
if(StringUtils.isBlank(parameters.getBaseFilter())){
|
101 |
this.filter = null; |
102 |
} else {
|
103 |
this.filter = ExpressionUtils.createExpression(parameters.getBaseFilter());
|
104 |
} |
105 |
if(StringUtils.isBlank(parameters.getPkFieldsString()) || StringUtils.contains(parameters.getPkFieldsString(), ',')){ |
106 |
this.setIdFieldSelected(-1); |
107 |
} else {
|
108 |
this.setIdFieldSelected(parameters.getPkFieldsString());
|
109 |
} |
110 |
|
111 |
String[] fieldsArray = parameters.getFields(); |
112 |
if(fieldsArray == null){ |
113 |
this.columnChecksModel.setSelectionInterval(0, this.getColumnsListModel().getSize()-1); |
114 |
} else {
|
115 |
ListModel<LabeledValue<FeatureAttributeDescriptor>> model = this.getColumnsListModel(); |
116 |
List<String> fields = Arrays.asList(fieldsArray); |
117 |
this.columnChecksModel.clearSelection();
|
118 |
for (int i = 0; i < model.getSize(); i++) { |
119 |
FeatureAttributeDescriptor attr = model.getElementAt(i).getValue(); |
120 |
if(ContainerUtils.contains(fields, attr.getName(), ContainerUtils.EQUALS_IGNORECASE_COMPARATOR)) {
|
121 |
this.columnChecksModel.addSelectionInterval(i, i);
|
122 |
} |
123 |
} |
124 |
} |
125 |
} |
126 |
|
127 |
public String getId() { |
128 |
return id;
|
129 |
} |
130 |
|
131 |
public String getDocumentName() { |
132 |
return this.documentName; |
133 |
} |
134 |
|
135 |
public void setDocumentName(String name) { |
136 |
this.documentName = name;
|
137 |
} |
138 |
|
139 |
public boolean isSelected() { |
140 |
return selected;
|
141 |
} |
142 |
|
143 |
public void setSelected(boolean selected) { |
144 |
this.fetchFromStore();
|
145 |
this.selected = selected;
|
146 |
} |
147 |
|
148 |
public ListSelectionModel getColumnChecksModel() { |
149 |
return this.columnChecksModel; |
150 |
} |
151 |
|
152 |
public JDBCStoreParameters getParameters() {
|
153 |
JDBCStoreParameters p = this.getValue();
|
154 |
StringBuilder fields = new StringBuilder(); |
155 |
List<FeatureAttributeDescriptor> attributes = this.getAttributeDescriptors(); |
156 |
boolean allSelected = true; |
157 |
for (int i = 0; i < attributes.size(); i++) { |
158 |
if (this.columnChecksModel.isSelectedIndex(i)) { |
159 |
if (fields.length() > 0) { |
160 |
fields.append(",");
|
161 |
} |
162 |
fields.append(attributes.get(i).getName()); |
163 |
} else {
|
164 |
allSelected = false;
|
165 |
} |
166 |
} |
167 |
if (!allSelected) {
|
168 |
p.setFields(fields.toString()); |
169 |
} |
170 |
if(this.idFieldSelected >= 0){ |
171 |
p.setPkFields(this.getFieldId());
|
172 |
} |
173 |
p.setCRS(this.getProjection());
|
174 |
p.setDefaultGeometryField(this.getGeomField());
|
175 |
if (!ExpressionUtils.isEmpty(this.filter)) { |
176 |
p.setBaseFilter(this.filter.getPhrase());
|
177 |
} else {
|
178 |
p.setBaseFilter(null);
|
179 |
} |
180 |
return p;
|
181 |
} |
182 |
|
183 |
public void setProjection(IProjection projection) { |
184 |
this.projection = projection;
|
185 |
} |
186 |
|
187 |
public IProjection getProjection() {
|
188 |
return projection;
|
189 |
} |
190 |
|
191 |
public String getFieldId() { |
192 |
if (this.idFieldSelected < 0) { |
193 |
return null; |
194 |
} |
195 |
return this.getIdFieldComboModel().getElementAt(this.idFieldSelected); |
196 |
} |
197 |
|
198 |
public String getGeomField() { |
199 |
if (this.geomFieldSelected < 0) { |
200 |
return null; |
201 |
} |
202 |
return this.geomFieldComboModel.getElementAt(this.geomFieldSelected); |
203 |
} |
204 |
|
205 |
public FeatureType getFeatureType() {
|
206 |
if (this.featureType == null) { |
207 |
fetchFromStore(); |
208 |
} |
209 |
return this.featureType; |
210 |
} |
211 |
|
212 |
protected void fetchFromStore() { |
213 |
DataManager dataManager = DALLocator.getDataManager(); |
214 |
if (this.featureType != null) { |
215 |
return;
|
216 |
} |
217 |
FeatureStore store = null;
|
218 |
try {
|
219 |
JDBCStoreParameters params = this.getValue();
|
220 |
store = (FeatureStore) dataManager.openStore(params.getDataStoreName(), params); |
221 |
this.featureType = store.getDefaultFeatureType();
|
222 |
FeatureAttributeDescriptor attr = this.featureType.getDefaultGeometryAttribute();
|
223 |
if (attr != null) { |
224 |
GeometryType geomType = attr.getGeomType(); |
225 |
if (geomType != null) { |
226 |
if (this.getGeometryType() == Geometry.TYPES.UNKNOWN) { |
227 |
this.setGeometryType(geomType.getType());
|
228 |
} |
229 |
if (this.getGeometrySubtype() == Geometry.SUBTYPES.UNKNOWN) { |
230 |
this.setGeometrySubtype(geomType.getSubType());
|
231 |
} |
232 |
} |
233 |
if (this.getProjection() == null) { |
234 |
this.setProjection(attr.getSRS());
|
235 |
} |
236 |
} |
237 |
if (this.readOnly == null) { |
238 |
this.readOnly = !store.allowWrite();
|
239 |
} |
240 |
|
241 |
// this force calculate some required values of tableinfo
|
242 |
this.getGeomFieldComboModel();
|
243 |
this.getIdFieldComboModel();
|
244 |
|
245 |
} catch (Exception ex) { |
246 |
LOGGER.trace("Can't get feature type.", ex); // To allow set break points |
247 |
} finally {
|
248 |
DisposeUtils.disposeQuietly(store); |
249 |
} |
250 |
|
251 |
} |
252 |
|
253 |
public ComboBoxModel getGeomFieldComboModel() { |
254 |
if (this.geomFieldComboModel == null) { |
255 |
DefaultComboBoxModel<String> geomModel = new DefaultComboBoxModel<>(); |
256 |
geomModel.addElement(" ");
|
257 |
int geomIndex = -1; |
258 |
int n = 1; |
259 |
for (FeatureAttributeDescriptor attr : this.getAttributeDescriptors()) { |
260 |
if (geomIndex < 0 && attr.getType() == DataTypes.GEOMETRY) { |
261 |
geomIndex = n; |
262 |
} |
263 |
int dataType = attr.getType();
|
264 |
if (dataType == DataTypes.GEOMETRY || dataType == DataTypes.BYTEARRAY || dataType == DataTypes.STRING) {
|
265 |
geomModel.addElement(attr.getName()); |
266 |
n++; |
267 |
} |
268 |
} |
269 |
if (geomIndex < 0) { |
270 |
geomIndex = 0;
|
271 |
} |
272 |
this.geomFieldComboModel = geomModel;
|
273 |
this.geomFieldSelected = geomIndex;
|
274 |
} |
275 |
return this.geomFieldComboModel; |
276 |
} |
277 |
|
278 |
public int getGeomFieldSelected() { |
279 |
return this.geomFieldSelected; |
280 |
} |
281 |
|
282 |
public ComboBoxModel<String> getIdFieldComboModel() { |
283 |
if (this.idFieldComboModel == null) { |
284 |
StringBuilder pkName = new StringBuilder(); |
285 |
for (FeatureAttributeDescriptor attr : this.getAttributeDescriptors()) { |
286 |
if (attr.isPrimaryKey()) {
|
287 |
if (!StringUtils.isBlank(pkName)) {
|
288 |
pkName.append(",");
|
289 |
} |
290 |
pkName.append(attr.getName()); |
291 |
} |
292 |
} |
293 |
DefaultComboBoxModel<String> idsModel = new DefaultComboBoxModel<>(); |
294 |
idsModel.addElement(" ");
|
295 |
int idsIndex = -1; |
296 |
int n = 1; |
297 |
if (!StringUtils.isBlank(pkName) && StringUtils.contains(pkName, ",")) { |
298 |
idsModel.addElement(pkName.toString()); |
299 |
idsIndex = n++; |
300 |
} |
301 |
for (FeatureAttributeDescriptor attr : getAttributeDescriptors()) {
|
302 |
if (idsIndex < 0 && attr.isPrimaryKey()) { |
303 |
idsIndex = n; |
304 |
} |
305 |
idsModel.addElement(attr.getName()); |
306 |
n++; |
307 |
} |
308 |
if (idsIndex < 0) { |
309 |
idsIndex = 0;
|
310 |
} |
311 |
this.idFieldComboModel = idsModel;
|
312 |
this.idFieldSelected = idsIndex;
|
313 |
} |
314 |
return this.idFieldComboModel; |
315 |
} |
316 |
|
317 |
public List<FeatureAttributeDescriptor> getAttributeDescriptors() { |
318 |
if (this.attributeDescriptors == null) { |
319 |
List<FeatureAttributeDescriptor> attrs = new ArrayList<>(); |
320 |
for (FeatureAttributeDescriptor attr : this.getFeatureType()) { |
321 |
attrs.add(attr); |
322 |
} |
323 |
attrs.sort((FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) -> o1.getName().compareTo(o2.getName())); |
324 |
this.columnChecksModel.setSelectionInterval(0, attrs.size()); |
325 |
this.attributeDescriptors = attrs;
|
326 |
} |
327 |
return this.attributeDescriptors; |
328 |
} |
329 |
|
330 |
public ListModel<LabeledValue<FeatureAttributeDescriptor>> getColumnsListModel() { |
331 |
if (this.columnsListModel == null) { |
332 |
DefaultListModel<LabeledValue<FeatureAttributeDescriptor>> model = new DefaultListModel<>(); |
333 |
for (FeatureAttributeDescriptor attr : this.getAttributeDescriptors()) { |
334 |
model.addElement(new LabeledValueImpl<>(attr.getName() + " [" + attr.getDataTypeName() + "]", attr)); |
335 |
} |
336 |
this.columnsListModel = model;
|
337 |
} |
338 |
return this.columnsListModel; |
339 |
} |
340 |
|
341 |
public FeatureAttributeDescriptor getAttributeDescriptor(String attrName) { |
342 |
return this.getFeatureType().getAttributeDescriptor(attrName); |
343 |
} |
344 |
|
345 |
public int getIdFieldSelected() { |
346 |
return this.idFieldSelected; |
347 |
} |
348 |
|
349 |
public Expression getFilter() { |
350 |
return this.filter; |
351 |
} |
352 |
|
353 |
public void setFilter(Expression filter) { |
354 |
this.filter = filter;
|
355 |
} |
356 |
|
357 |
void setIdFieldSelected(int selectedIndex) { |
358 |
this.idFieldSelected = selectedIndex;
|
359 |
} |
360 |
|
361 |
void setIdFieldSelected(String selected) { |
362 |
ComboBoxModel<String> model = getIdFieldComboModel(); |
363 |
for (int i = 0; i < model.getSize(); i++) { |
364 |
if(StringUtils.equalsIgnoreCase(selected, model.getElementAt(i))){
|
365 |
this.idFieldSelected = i;
|
366 |
return;
|
367 |
} |
368 |
} |
369 |
this.idFieldSelected = -1; |
370 |
} |
371 |
|
372 |
|
373 |
void setGeomFieldSelected(int selectedIndex) { |
374 |
this.geomFieldSelected = selectedIndex;
|
375 |
} |
376 |
|
377 |
void setGeomFieldSelected(String selected) { |
378 |
ComboBoxModel<String> model = getGeomFieldComboModel(); |
379 |
for (int i = 0; i < model.getSize(); i++) { |
380 |
if(StringUtils.equalsIgnoreCase(selected, model.getElementAt(i))){
|
381 |
this.geomFieldSelected = i;
|
382 |
return;
|
383 |
} |
384 |
} |
385 |
this.geomFieldSelected = -1; |
386 |
} |
387 |
|
388 |
public boolean requireGeometry() { |
389 |
return requireGeometry;
|
390 |
} |
391 |
|
392 |
public boolean hasValidValues() { |
393 |
if (this.getGeomFieldSelected() < 0 && requireGeometry()) { |
394 |
return false; |
395 |
} |
396 |
if (this.requireGeometry()) { |
397 |
FeatureType featType = this.getFeatureType();
|
398 |
if (StringUtils.isBlank(this.getGeomField())) { |
399 |
return false; |
400 |
} |
401 |
if (this.getGeometryType() == Geometry.TYPES.NULL || this.getGeometryType() == Geometry.TYPES.UNKNOWN) { |
402 |
return false; |
403 |
} |
404 |
if (this.getGeometrySubtype() == Geometry.SUBTYPES.UNKNOWN) { |
405 |
return false; |
406 |
} |
407 |
} |
408 |
|
409 |
JDBCStoreParameters p = this.getParameters();
|
410 |
try {
|
411 |
p.validate(); |
412 |
return true; |
413 |
} catch (ValidateDataParametersException ex) {
|
414 |
return false; |
415 |
} |
416 |
} |
417 |
|
418 |
public boolean isView() { |
419 |
return this.isView; |
420 |
} |
421 |
|
422 |
public boolean isReadOnly() { |
423 |
if (this.readOnly == null) { |
424 |
fetchFromStore(); |
425 |
} |
426 |
return this.readOnly; |
427 |
} |
428 |
|
429 |
@Override
|
430 |
public Object getProperty(String name) { |
431 |
return this.propertiesHelper.getProperty(name); |
432 |
} |
433 |
|
434 |
@Override
|
435 |
public void setProperty(String name, Object value) { |
436 |
this.propertiesHelper.setProperty(name, value);
|
437 |
} |
438 |
|
439 |
@Override
|
440 |
public Map<String, Object> getProperties() { |
441 |
return this.propertiesHelper.getProperties(); |
442 |
} |
443 |
|
444 |
protected JDBCServerExplorer getExplorer() throws DataException, ValidateDataParametersException { |
445 |
DataManager manager = DALLocator.getDataManager(); |
446 |
JDBCStoreParameters storeParameters = getParameters(); |
447 |
DataServerExplorerParameters explorerParameters = manager.createServerExplorerParameters(storeParameters.getProviderName()); |
448 |
ToolsLocator.getDynObjectManager().copy(storeParameters, explorerParameters); |
449 |
DataServerExplorer explorer = manager.openServerExplorer(storeParameters.getProviderName(), explorerParameters); |
450 |
return (JDBCServerExplorer) explorer;
|
451 |
|
452 |
} |
453 |
|
454 |
public boolean existsDALInfo() { |
455 |
try {
|
456 |
JDBCStoreParameters storeParameters = getParameters(); |
457 |
JDBCServerExplorer explorer = getExplorer(); |
458 |
ResourcesStorage resources = explorer.getResourcesStorage(storeParameters); |
459 |
return resources.exists("dal"); |
460 |
} catch (Exception ex) { |
461 |
//FIXME: LOGGER
|
462 |
return false; |
463 |
} |
464 |
} |
465 |
|
466 |
public void removeDALInfo() { |
467 |
try {
|
468 |
JDBCStoreParameters storeParameters = getParameters(); |
469 |
JDBCServerExplorer explorer = getExplorer(); |
470 |
ResourcesStorage resources = explorer.getResourcesStorage(storeParameters); |
471 |
if(resources.exists("dal")) { |
472 |
resources.remove("dal");
|
473 |
JDBCStoreParameters params = (JDBCStoreParameters) explorer.get(storeParameters.getTable()); |
474 |
this.value = params;
|
475 |
this.featureType = null; |
476 |
} |
477 |
DisposeUtils.disposeQuietly(explorer); |
478 |
} catch (Exception ex) { |
479 |
throw new RuntimeException("Can't remove DAL resource.", ex); |
480 |
} |
481 |
|
482 |
} |
483 |
|
484 |
public int getGeometryType(){ |
485 |
return this.getValue().getGeometryType(); |
486 |
} |
487 |
|
488 |
public int getGeometrySubtype(){ |
489 |
return this.getValue().getGeometrySubtype(); |
490 |
} |
491 |
|
492 |
public void setGeometryType(int geomType){ |
493 |
this.getValue().setGeometryType(geomType);
|
494 |
} |
495 |
|
496 |
public void setGeometrySubtype(int geomSubtype){ |
497 |
this.getValue().setGeometrySubtype(geomSubtype);
|
498 |
} |
499 |
|
500 |
} |