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