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