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 @ 45647
History | View | Annotate | Download (15.9 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.tools.ToolsLocator; |
42 |
import org.gvsig.tools.dispose.DisposeUtils; |
43 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
44 |
import org.gvsig.tools.util.ContainerUtils; |
45 |
import org.gvsig.tools.util.LabeledValue; |
46 |
import org.gvsig.tools.util.LabeledValueImpl; |
47 |
import org.gvsig.tools.util.PropertiesSupport; |
48 |
import org.gvsig.tools.util.PropertiesSupportHelper; |
49 |
|
50 |
/**
|
51 |
*
|
52 |
* @author fdiaz
|
53 |
*/
|
54 |
public class TableInfo extends LabeledValueImpl<JDBCStoreParameters> implements PropertiesSupport { |
55 |
|
56 |
private final ListSelectionModel columnChecksModel; |
57 |
private FeatureType featureType;
|
58 |
private int geomFieldSelected = -1; |
59 |
private int idFieldSelected = -1; |
60 |
private ComboBoxModel<String> idFieldComboModel; |
61 |
private ComboBoxModel<String> geomFieldComboModel; |
62 |
private List<FeatureAttributeDescriptor> attributeDescriptors; |
63 |
private ListModel<LabeledValue<FeatureAttributeDescriptor>> columnsListModel; |
64 |
private Expression filter; |
65 |
private IProjection projection;
|
66 |
private boolean selected; |
67 |
private String documentName; |
68 |
private boolean isView; |
69 |
private Boolean readOnly; |
70 |
private boolean requireGeometry; |
71 |
|
72 |
private PropertiesSupportHelper propertiesHelper;
|
73 |
private final String id; |
74 |
|
75 |
public TableInfo(String id, JDBCStoreParameters parameters, boolean requireGeometry, boolean isView) { |
76 |
super(getLabelForTable(parameters), parameters);
|
77 |
this.id = id;
|
78 |
this.columnChecksModel = new DefaultListSelectionModel(); |
79 |
this.selected = false; |
80 |
this.documentName = parameters.getTable();
|
81 |
this.projection = parameters.getCRS();
|
82 |
this.isView = isView;
|
83 |
this.readOnly = null; |
84 |
this.requireGeometry = requireGeometry;
|
85 |
this.propertiesHelper = new PropertiesSupportHelper(); |
86 |
} |
87 |
|
88 |
private static String getLabelForTable(JDBCStoreParameters parameters) { |
89 |
String schema = parameters.getSchema();
|
90 |
String tableName = parameters.getTable();
|
91 |
if (StringUtils.isBlank(schema)) {
|
92 |
return tableName;
|
93 |
} |
94 |
return schema + "." + tableName; |
95 |
} |
96 |
|
97 |
public void fetch(JDBCStoreParameters parameters) { |
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.selected = selected;
|
145 |
} |
146 |
|
147 |
public ListSelectionModel getColumnChecksModel() { |
148 |
return this.columnChecksModel; |
149 |
} |
150 |
|
151 |
public JDBCStoreParameters getParameters() {
|
152 |
JDBCStoreParameters p = this.getValue();
|
153 |
StringBuilder fields = new StringBuilder(); |
154 |
List<FeatureAttributeDescriptor> attributes = this.getAttributeDescriptors(); |
155 |
boolean allSelected = true; |
156 |
for (int i = 0; i < attributes.size(); i++) { |
157 |
if (this.columnChecksModel.isSelectedIndex(i)) { |
158 |
if (fields.length() > 0) { |
159 |
fields.append(",");
|
160 |
} |
161 |
fields.append(attributes.get(i).getName()); |
162 |
} else {
|
163 |
allSelected = false;
|
164 |
} |
165 |
} |
166 |
if (!allSelected) {
|
167 |
p.setFields(fields.toString()); |
168 |
} |
169 |
if(this.idFieldSelected >= 0){ |
170 |
p.setPkFields(this.getFieldId());
|
171 |
} |
172 |
p.setCRS(this.getProjection());
|
173 |
p.setDefaultGeometryField(this.getGeomField());
|
174 |
if (!ExpressionUtils.isEmpty(this.filter)) { |
175 |
p.setBaseFilter(this.filter.getPhrase());
|
176 |
} else {
|
177 |
p.setBaseFilter(null);
|
178 |
} |
179 |
return p;
|
180 |
} |
181 |
|
182 |
public void setProjection(IProjection projection) { |
183 |
this.projection = projection;
|
184 |
} |
185 |
|
186 |
public IProjection getProjection() {
|
187 |
return projection;
|
188 |
} |
189 |
|
190 |
public String getFieldId() { |
191 |
if (this.idFieldSelected < 0) { |
192 |
return null; |
193 |
} |
194 |
return this.getIdFieldComboModel().getElementAt(this.idFieldSelected); |
195 |
} |
196 |
|
197 |
public String getGeomField() { |
198 |
if (this.geomFieldSelected < 0) { |
199 |
return null; |
200 |
} |
201 |
return this.geomFieldComboModel.getElementAt(this.geomFieldSelected); |
202 |
} |
203 |
|
204 |
public FeatureType getFeatureType() {
|
205 |
if (this.featureType == null) { |
206 |
DataManager dataManager = DALLocator.getDataManager(); |
207 |
try {
|
208 |
JDBCStoreParameters params = this.getValue();
|
209 |
FeatureStore store = (FeatureStore) dataManager.openStore(params.getDataStoreName(), params); |
210 |
this.featureType = store.getDefaultFeatureType();
|
211 |
} catch (Exception ex) { |
212 |
AbstractWizardDB.LOGGER.trace("Can't get feature type.", ex); // To allow set break points |
213 |
} |
214 |
} |
215 |
return this.featureType; |
216 |
} |
217 |
|
218 |
public ComboBoxModel getGeomFieldComboModel() { |
219 |
if (this.geomFieldComboModel == null) { |
220 |
DefaultComboBoxModel<String> geomModel = new DefaultComboBoxModel<>(); |
221 |
geomModel.addElement(" ");
|
222 |
int geomIndex = -1; |
223 |
int n = 1; |
224 |
for (FeatureAttributeDescriptor attr : this.getAttributeDescriptors()) { |
225 |
if (geomIndex < 0 && attr.getType() == DataTypes.GEOMETRY) { |
226 |
geomIndex = n; |
227 |
} |
228 |
int dataType = attr.getType();
|
229 |
if (dataType == DataTypes.GEOMETRY || dataType == DataTypes.BYTEARRAY || dataType == DataTypes.STRING) {
|
230 |
geomModel.addElement(attr.getName()); |
231 |
n++; |
232 |
} |
233 |
} |
234 |
if (geomIndex < 0) { |
235 |
geomIndex = 0;
|
236 |
} |
237 |
this.geomFieldComboModel = geomModel;
|
238 |
this.geomFieldSelected = geomIndex;
|
239 |
} |
240 |
return this.geomFieldComboModel; |
241 |
} |
242 |
|
243 |
public int getGeomFieldSelected() { |
244 |
return this.geomFieldSelected; |
245 |
} |
246 |
|
247 |
public ComboBoxModel<String> getIdFieldComboModel() { |
248 |
if (this.idFieldComboModel == null) { |
249 |
StringBuilder pkName = new StringBuilder(); |
250 |
for (FeatureAttributeDescriptor attr : this.getAttributeDescriptors()) { |
251 |
if (attr.isPrimaryKey()) {
|
252 |
if (!StringUtils.isBlank(pkName)) {
|
253 |
pkName.append(",");
|
254 |
} |
255 |
pkName.append(attr.getName()); |
256 |
} |
257 |
} |
258 |
DefaultComboBoxModel<String> idsModel = new DefaultComboBoxModel<>(); |
259 |
idsModel.addElement(" ");
|
260 |
int idsIndex = -1; |
261 |
int n = 1; |
262 |
if (!StringUtils.isBlank(pkName) && StringUtils.contains(pkName, "/")) { |
263 |
idsModel.addElement(pkName.toString()); |
264 |
idsIndex = n++; |
265 |
} |
266 |
for (FeatureAttributeDescriptor attr : getAttributeDescriptors()) {
|
267 |
if (idsIndex < 0 && attr.isPrimaryKey()) { |
268 |
idsIndex = n; |
269 |
} |
270 |
idsModel.addElement(attr.getName()); |
271 |
n++; |
272 |
} |
273 |
if (idsIndex < 0) { |
274 |
idsIndex = 0;
|
275 |
} |
276 |
this.idFieldComboModel = idsModel;
|
277 |
this.idFieldSelected = idsIndex;
|
278 |
} |
279 |
return this.idFieldComboModel; |
280 |
} |
281 |
|
282 |
public List<FeatureAttributeDescriptor> getAttributeDescriptors() { |
283 |
if (this.attributeDescriptors == null) { |
284 |
List<FeatureAttributeDescriptor> attrs = new ArrayList<>(); |
285 |
for (FeatureAttributeDescriptor attr : this.getFeatureType()) { |
286 |
attrs.add(attr); |
287 |
} |
288 |
attrs.sort((FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) -> o1.getName().compareTo(o2.getName())); |
289 |
this.columnChecksModel.setSelectionInterval(0, attrs.size()); |
290 |
this.attributeDescriptors = attrs;
|
291 |
} |
292 |
return this.attributeDescriptors; |
293 |
} |
294 |
|
295 |
public ListModel<LabeledValue<FeatureAttributeDescriptor>> getColumnsListModel() { |
296 |
if (this.columnsListModel == null) { |
297 |
DefaultListModel<LabeledValue<FeatureAttributeDescriptor>> model = new DefaultListModel<>(); |
298 |
for (FeatureAttributeDescriptor attr : this.getAttributeDescriptors()) { |
299 |
model.addElement(new LabeledValueImpl<>(attr.getName() + " [" + attr.getDataTypeName() + "]", attr)); |
300 |
} |
301 |
this.columnsListModel = model;
|
302 |
} |
303 |
return this.columnsListModel; |
304 |
} |
305 |
|
306 |
public FeatureAttributeDescriptor getAttributeDescriptor(String attrName) { |
307 |
return this.getFeatureType().getAttributeDescriptor(attrName); |
308 |
} |
309 |
|
310 |
public int getIdFieldSelected() { |
311 |
return this.idFieldSelected; |
312 |
} |
313 |
|
314 |
public Expression getFilter() { |
315 |
return this.filter; |
316 |
} |
317 |
|
318 |
public void setFilter(Expression filter) { |
319 |
this.filter = filter;
|
320 |
} |
321 |
|
322 |
void setIdFieldSelected(int selectedIndex) { |
323 |
this.idFieldSelected = selectedIndex;
|
324 |
} |
325 |
|
326 |
void setIdFieldSelected(String selected) { |
327 |
ComboBoxModel<String> model = getIdFieldComboModel(); |
328 |
for (int i = 0; i < model.getSize(); i++) { |
329 |
if(StringUtils.equalsIgnoreCase(selected, model.getElementAt(i))){
|
330 |
this.idFieldSelected = i;
|
331 |
return;
|
332 |
} |
333 |
} |
334 |
this.idFieldSelected = -1; |
335 |
} |
336 |
|
337 |
|
338 |
void setGeomFieldSelected(int selectedIndex) { |
339 |
this.geomFieldSelected = selectedIndex;
|
340 |
} |
341 |
|
342 |
void setGeomFieldSelected(String selected) { |
343 |
ComboBoxModel<String> model = getGeomFieldComboModel(); |
344 |
for (int i = 0; i < model.getSize(); i++) { |
345 |
if(StringUtils.equalsIgnoreCase(selected, model.getElementAt(i))){
|
346 |
this.geomFieldSelected = i;
|
347 |
return;
|
348 |
} |
349 |
} |
350 |
this.geomFieldSelected = -1; |
351 |
} |
352 |
|
353 |
public boolean requireGeometry() { |
354 |
return requireGeometry;
|
355 |
} |
356 |
|
357 |
public boolean hasValidValues() { |
358 |
if (this.getGeomFieldSelected() < 0 && requireGeometry()) { |
359 |
return false; |
360 |
} |
361 |
JDBCStoreParameters p = this.getParameters();
|
362 |
try {
|
363 |
p.validate(); |
364 |
return true; |
365 |
} catch (ValidateDataParametersException ex) {
|
366 |
return false; |
367 |
} |
368 |
} |
369 |
|
370 |
public boolean isView() { |
371 |
return this.isView; |
372 |
} |
373 |
|
374 |
public boolean isReadOnly() { |
375 |
if (this.readOnly != null) { |
376 |
return this.readOnly; |
377 |
} |
378 |
JDBCStoreParameters params = this.getParameters();
|
379 |
DataManager manager = DALLocator.getDataManager(); |
380 |
FeatureStore store = null;
|
381 |
try {
|
382 |
store = (FeatureStore) manager.openStore(params.getProviderName(), params); |
383 |
this.readOnly = !store.allowWrite();
|
384 |
return this.readOnly; |
385 |
} catch (Exception ex) { |
386 |
AbstractWizardDB.LOGGER.warn("Can't get if store allow write", ex);
|
387 |
this.readOnly = false; |
388 |
return this.readOnly; |
389 |
} finally {
|
390 |
DisposeUtils.disposeQuietly(store); |
391 |
} |
392 |
} |
393 |
|
394 |
@Override
|
395 |
public Object getProperty(String name) { |
396 |
return this.propertiesHelper.getProperty(name); |
397 |
} |
398 |
|
399 |
@Override
|
400 |
public void setProperty(String name, Object value) { |
401 |
this.propertiesHelper.setProperty(name, value);
|
402 |
} |
403 |
|
404 |
@Override
|
405 |
public Map<String, Object> getProperties() { |
406 |
return this.propertiesHelper.getProperties(); |
407 |
} |
408 |
|
409 |
private JDBCServerExplorer getExplorer() throws DataException, ValidateDataParametersException { |
410 |
DataManager manager = DALLocator.getDataManager(); |
411 |
JDBCStoreParameters storeParameters = getParameters(); |
412 |
DataServerExplorerParameters explorerParameters = manager.createServerExplorerParameters(storeParameters.getProviderName()); |
413 |
ToolsLocator.getDynObjectManager().copy(storeParameters, explorerParameters); |
414 |
DataServerExplorer explorer = manager.openServerExplorer(storeParameters.getProviderName(), explorerParameters); |
415 |
return (JDBCServerExplorer) explorer;
|
416 |
|
417 |
} |
418 |
|
419 |
public boolean existsDALInfo() { |
420 |
try {
|
421 |
JDBCStoreParameters storeParameters = getParameters(); |
422 |
JDBCServerExplorer explorer = getExplorer(); |
423 |
ResourcesStorage resources = explorer.getResourcesStorage(storeParameters); |
424 |
return resources.exists("dal"); |
425 |
} catch (Exception ex) { |
426 |
//FIXME: LOGGER
|
427 |
return false; |
428 |
} |
429 |
} |
430 |
|
431 |
public void removeDALInfo() { |
432 |
try {
|
433 |
JDBCStoreParameters storeParameters = getParameters(); |
434 |
JDBCServerExplorer explorer = getExplorer(); |
435 |
ResourcesStorage resources = explorer.getResourcesStorage(storeParameters); |
436 |
if(resources.exists("dal")) { |
437 |
resources.remove("dal");
|
438 |
JDBCStoreParameters params = (JDBCStoreParameters) explorer.get(storeParameters.getTable()); |
439 |
this.value = params;
|
440 |
} |
441 |
DisposeUtils.disposeQuietly(explorer); |
442 |
} catch (Exception ex) { |
443 |
throw new RuntimeException("Can't remove DAL resource.", ex); |
444 |
} |
445 |
|
446 |
} |
447 |
} |