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 @ 45635
History | View | Annotate | Download (11.4 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.List; |
10 |
import java.util.Map; |
11 |
import javax.swing.ComboBoxModel; |
12 |
import javax.swing.DefaultComboBoxModel; |
13 |
import javax.swing.DefaultListModel; |
14 |
import javax.swing.DefaultListSelectionModel; |
15 |
import javax.swing.ListModel; |
16 |
import javax.swing.ListSelectionModel; |
17 |
import org.apache.commons.lang3.StringUtils; |
18 |
import org.cresques.cts.IProjection; |
19 |
import org.gvsig.expressionevaluator.Expression; |
20 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
21 |
import org.gvsig.fmap.dal.DALLocator; |
22 |
import org.gvsig.fmap.dal.DataManager; |
23 |
import org.gvsig.fmap.dal.DataTypes; |
24 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
25 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
26 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
27 |
import org.gvsig.fmap.dal.feature.FeatureType; |
28 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
29 |
import org.gvsig.tools.dispose.DisposeUtils; |
30 |
import org.gvsig.tools.util.LabeledValue; |
31 |
import org.gvsig.tools.util.LabeledValueImpl; |
32 |
import org.gvsig.tools.util.PropertiesSupport; |
33 |
import org.gvsig.tools.util.PropertiesSupportHelper; |
34 |
|
35 |
/**
|
36 |
*
|
37 |
* @author fdiaz
|
38 |
*/
|
39 |
public class TableInfo extends LabeledValueImpl<JDBCStoreParameters> implements PropertiesSupport { |
40 |
|
41 |
private final ListSelectionModel columnChecksModel; |
42 |
private FeatureType featureType;
|
43 |
private int geomFieldSelected = -1; |
44 |
private int idFieldSelected = -1; |
45 |
private ComboBoxModel<String> idFieldComboModel; |
46 |
private ComboBoxModel<String> geomFieldComboModel; |
47 |
private List<FeatureAttributeDescriptor> attributeDescriptors; |
48 |
private ListModel<LabeledValue<FeatureAttributeDescriptor>> columnsListModel; |
49 |
private Expression filter; |
50 |
private IProjection projection;
|
51 |
private boolean selected; |
52 |
private String documentName; |
53 |
private boolean isView; |
54 |
private Boolean readOnly; |
55 |
private boolean requireGeometry; |
56 |
|
57 |
private PropertiesSupportHelper propertiesHelper;
|
58 |
private final String id; |
59 |
|
60 |
public TableInfo(String id, JDBCStoreParameters parameters, boolean requireGeometry, boolean isView) { |
61 |
super(getLabelForTable(parameters), parameters);
|
62 |
this.id = id;
|
63 |
this.columnChecksModel = new DefaultListSelectionModel(); |
64 |
this.selected = false; |
65 |
this.documentName = parameters.getTable();
|
66 |
this.projection = parameters.getCRS();
|
67 |
this.isView = isView;
|
68 |
this.readOnly = null; |
69 |
this.requireGeometry = requireGeometry;
|
70 |
this.propertiesHelper = new PropertiesSupportHelper(); |
71 |
} |
72 |
|
73 |
private static String getLabelForTable(JDBCStoreParameters parameters) { |
74 |
String schema = parameters.getSchema();
|
75 |
String tableName = parameters.getTable();
|
76 |
if (StringUtils.isBlank(schema)) {
|
77 |
return tableName;
|
78 |
} |
79 |
return schema + "." + tableName; |
80 |
} |
81 |
|
82 |
public void fetch(JDBCStoreParameters parameters) { |
83 |
this.projection = parameters.getCRS();
|
84 |
} |
85 |
|
86 |
public String getId() { |
87 |
return id;
|
88 |
} |
89 |
|
90 |
public String getDocumentName() { |
91 |
return this.documentName; |
92 |
} |
93 |
|
94 |
public void setDocumentName(String name) { |
95 |
this.documentName = name;
|
96 |
} |
97 |
|
98 |
public boolean isSelected() { |
99 |
return selected;
|
100 |
} |
101 |
|
102 |
public void setSelected(boolean selected) { |
103 |
this.selected = selected;
|
104 |
} |
105 |
|
106 |
public ListSelectionModel getColumnChecksModel() { |
107 |
return this.columnChecksModel; |
108 |
} |
109 |
|
110 |
public JDBCStoreParameters getParameters() {
|
111 |
JDBCStoreParameters p = this.getValue();
|
112 |
StringBuilder fields = new StringBuilder(); |
113 |
List<FeatureAttributeDescriptor> attributes = this.getAttributeDescriptors(); |
114 |
boolean allSelected = true; |
115 |
for (int i = 0; i < attributes.size(); i++) { |
116 |
if (this.columnChecksModel.isSelectedIndex(i)) { |
117 |
if (fields.length() > 0) { |
118 |
fields.append(",");
|
119 |
} |
120 |
fields.append(attributes.get(i).getName()); |
121 |
} else {
|
122 |
allSelected = false;
|
123 |
} |
124 |
} |
125 |
if (!allSelected) {
|
126 |
p.setFields(fields.toString()); |
127 |
} |
128 |
p.setPkFields(this.getFieldId());
|
129 |
p.setCRS(this.getProjection());
|
130 |
p.setDefaultGeometryField(this.getGeomField());
|
131 |
if (!ExpressionUtils.isEmpty(this.filter)) { |
132 |
p.setBaseFilter(this.filter.toString());
|
133 |
} else {
|
134 |
p.setBaseFilter(null);
|
135 |
} |
136 |
return p;
|
137 |
} |
138 |
|
139 |
public void setProjection(IProjection projection) { |
140 |
this.projection = projection;
|
141 |
} |
142 |
|
143 |
public IProjection getProjection() {
|
144 |
return projection;
|
145 |
} |
146 |
|
147 |
public String getFieldId() { |
148 |
if (this.idFieldSelected < 0) { |
149 |
return null; |
150 |
} |
151 |
return this.getIdFieldComboModel().getElementAt(this.idFieldSelected); |
152 |
} |
153 |
|
154 |
public String getGeomField() { |
155 |
if (this.geomFieldSelected < 0) { |
156 |
return null; |
157 |
} |
158 |
return this.geomFieldComboModel.getElementAt(this.geomFieldSelected); |
159 |
} |
160 |
|
161 |
public FeatureType getFeatureType() {
|
162 |
if (this.featureType == null) { |
163 |
DataManager dataManager = DALLocator.getDataManager(); |
164 |
try {
|
165 |
JDBCStoreParameters params = this.getValue();
|
166 |
FeatureStore store = (FeatureStore) dataManager.openStore(params.getDataStoreName(), params); |
167 |
this.featureType = store.getDefaultFeatureType();
|
168 |
} catch (Exception ex) { |
169 |
AbstractWizardDB.LOGGER.trace("Can't get feature type.", ex); // To allow set break points |
170 |
} |
171 |
} |
172 |
return this.featureType; |
173 |
} |
174 |
|
175 |
public ComboBoxModel getGeomFieldComboModel() { |
176 |
if (this.geomFieldComboModel == null) { |
177 |
DefaultComboBoxModel<String> geomModel = new DefaultComboBoxModel<>(); |
178 |
geomModel.addElement(" ");
|
179 |
int geomIndex = -1; |
180 |
int n = 1; |
181 |
for (FeatureAttributeDescriptor attr : this.getAttributeDescriptors()) { |
182 |
if (geomIndex < 0 && attr.getType() == DataTypes.GEOMETRY) { |
183 |
geomIndex = n; |
184 |
} |
185 |
int dataType = attr.getType();
|
186 |
if (dataType == DataTypes.GEOMETRY || dataType == DataTypes.BYTEARRAY || dataType == DataTypes.STRING) {
|
187 |
geomModel.addElement(attr.getName()); |
188 |
n++; |
189 |
} |
190 |
} |
191 |
if (geomIndex < 0) { |
192 |
geomIndex = 0;
|
193 |
} |
194 |
this.geomFieldComboModel = geomModel;
|
195 |
this.geomFieldSelected = geomIndex;
|
196 |
} |
197 |
return this.geomFieldComboModel; |
198 |
} |
199 |
|
200 |
public int getGeomFieldSelected() { |
201 |
return this.geomFieldSelected; |
202 |
} |
203 |
|
204 |
public ComboBoxModel<String> getIdFieldComboModel() { |
205 |
if (this.idFieldComboModel == null) { |
206 |
StringBuilder pkName = new StringBuilder(); |
207 |
for (FeatureAttributeDescriptor attr : this.getAttributeDescriptors()) { |
208 |
if (attr.isPrimaryKey()) {
|
209 |
if (!StringUtils.isBlank(pkName)) {
|
210 |
pkName.append(",");
|
211 |
} |
212 |
pkName.append(attr.getName()); |
213 |
} |
214 |
} |
215 |
DefaultComboBoxModel<String> idsModel = new DefaultComboBoxModel<>(); |
216 |
idsModel.addElement(" ");
|
217 |
int idsIndex = -1; |
218 |
int n = 1; |
219 |
if (!StringUtils.isBlank(pkName) && StringUtils.contains(pkName, "/")) { |
220 |
idsModel.addElement(pkName.toString()); |
221 |
idsIndex = n++; |
222 |
} |
223 |
for (FeatureAttributeDescriptor attr : getAttributeDescriptors()) {
|
224 |
if (idsIndex < 0 && attr.isPrimaryKey()) { |
225 |
idsIndex = n; |
226 |
} |
227 |
idsModel.addElement(attr.getName()); |
228 |
n++; |
229 |
} |
230 |
if (idsIndex < 0) { |
231 |
idsIndex = 0;
|
232 |
} |
233 |
this.idFieldComboModel = idsModel;
|
234 |
this.idFieldSelected = idsIndex;
|
235 |
} |
236 |
return this.idFieldComboModel; |
237 |
} |
238 |
|
239 |
public List<FeatureAttributeDescriptor> getAttributeDescriptors() { |
240 |
if (this.attributeDescriptors == null) { |
241 |
List<FeatureAttributeDescriptor> attrs = new ArrayList<>(); |
242 |
for (FeatureAttributeDescriptor attr : this.getFeatureType()) { |
243 |
attrs.add(attr); |
244 |
} |
245 |
attrs.sort((FeatureAttributeDescriptor o1, FeatureAttributeDescriptor o2) -> o1.getName().compareTo(o2.getName())); |
246 |
this.columnChecksModel.setSelectionInterval(0, attrs.size()); |
247 |
this.attributeDescriptors = attrs;
|
248 |
} |
249 |
return this.attributeDescriptors; |
250 |
} |
251 |
|
252 |
public ListModel<LabeledValue<FeatureAttributeDescriptor>> getColumnsListModel() { |
253 |
if (this.columnsListModel == null) { |
254 |
DefaultListModel<LabeledValue<FeatureAttributeDescriptor>> model = new DefaultListModel<>(); |
255 |
for (FeatureAttributeDescriptor attr : this.getAttributeDescriptors()) { |
256 |
model.addElement(new LabeledValueImpl<>(attr.getName() + " [" + attr.getDataTypeName() + "]", attr)); |
257 |
} |
258 |
this.columnsListModel = model;
|
259 |
} |
260 |
return this.columnsListModel; |
261 |
} |
262 |
|
263 |
public FeatureAttributeDescriptor getAttributeDescriptor(String attrName) { |
264 |
return this.getFeatureType().getAttributeDescriptor(attrName); |
265 |
} |
266 |
|
267 |
public int getIdFieldSelected() { |
268 |
return this.idFieldSelected; |
269 |
} |
270 |
|
271 |
public Expression getFilter() { |
272 |
return this.filter; |
273 |
} |
274 |
|
275 |
public void setFilter(Expression filter) { |
276 |
this.filter = filter;
|
277 |
} |
278 |
|
279 |
void setIdFieldSelected(int selectedIndex) { |
280 |
this.idFieldSelected = selectedIndex;
|
281 |
} |
282 |
|
283 |
void setGeomFieldSelected(int selectedIndex) { |
284 |
this.geomFieldSelected = selectedIndex;
|
285 |
} |
286 |
|
287 |
public boolean requireGeometry() { |
288 |
return requireGeometry;
|
289 |
} |
290 |
|
291 |
public boolean hasValidValues() { |
292 |
if (this.getGeomFieldSelected() < 0 && requireGeometry()) { |
293 |
return false; |
294 |
} |
295 |
JDBCStoreParameters p = this.getParameters();
|
296 |
try {
|
297 |
p.validate(); |
298 |
return true; |
299 |
} catch (ValidateDataParametersException ex) {
|
300 |
return false; |
301 |
} |
302 |
} |
303 |
|
304 |
public boolean isView() { |
305 |
return this.isView; |
306 |
} |
307 |
|
308 |
public boolean isReadOnly() { |
309 |
if (this.readOnly != null) { |
310 |
return this.readOnly; |
311 |
} |
312 |
JDBCStoreParameters params = this.getParameters();
|
313 |
DataManager manager = DALLocator.getDataManager(); |
314 |
FeatureStore store = null;
|
315 |
try {
|
316 |
store = (FeatureStore) manager.openStore(params.getProviderName(), params); |
317 |
this.readOnly = !store.allowWrite();
|
318 |
return this.readOnly; |
319 |
} catch (Exception ex) { |
320 |
AbstractWizardDB.LOGGER.warn("Can't get if store allow write", ex);
|
321 |
this.readOnly = false; |
322 |
return this.readOnly; |
323 |
} finally {
|
324 |
DisposeUtils.disposeQuietly(store); |
325 |
} |
326 |
} |
327 |
|
328 |
@Override
|
329 |
public Object getProperty(String name) { |
330 |
return this.propertiesHelper.getProperty(name); |
331 |
} |
332 |
|
333 |
@Override
|
334 |
public void setProperty(String name, Object value) { |
335 |
this.propertiesHelper.setProperty(name, value);
|
336 |
} |
337 |
|
338 |
@Override
|
339 |
public Map<String, Object> getProperties() { |
340 |
return this.propertiesHelper.getProperties(); |
341 |
} |
342 |
|
343 |
} |