root / branches / v2_0_0_prep / extensions / extGeoDB / src / org / gvsig / geodb / ExportToDB.java @ 29628
History | View | Annotate | Download (9.94 KB)
1 |
package org.gvsig.geodb; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
|
5 |
import javax.swing.JComponent; |
6 |
import javax.swing.JOptionPane; |
7 |
|
8 |
import org.gvsig.andami.PluginServices; |
9 |
import org.gvsig.andami.messages.NotificationManager; |
10 |
import org.gvsig.andami.plugins.Extension; |
11 |
import org.gvsig.app.project.documents.view.IProjectView; |
12 |
import org.gvsig.app.project.documents.view.gui.View; |
13 |
import org.gvsig.fmap.dal.DALLocator; |
14 |
import org.gvsig.fmap.dal.DataManager; |
15 |
import org.gvsig.fmap.dal.DataTypes; |
16 |
import org.gvsig.fmap.dal.exception.DataException; |
17 |
import org.gvsig.fmap.dal.exception.ReadException; |
18 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
19 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
20 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
21 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
22 |
import org.gvsig.fmap.dal.feature.Feature; |
23 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
24 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
25 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
26 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
27 |
import org.gvsig.fmap.dal.feature.FeatureType; |
28 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
29 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
30 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
31 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
32 |
import org.gvsig.fmap.dal.store.mysql.MySQLServerExplorer; |
33 |
import org.gvsig.fmap.dal.store.postgresql.PostgreSQLServerExplorer; |
34 |
import org.gvsig.fmap.geom.Geometry; |
35 |
import org.gvsig.fmap.mapcontext.MapContext; |
36 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
37 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
38 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
39 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
40 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
41 |
import org.gvsig.geodb.vectorialdb.ConnectionSettings; |
42 |
import org.gvsig.geodb.vectorialdb.DlgConnection; |
43 |
import org.gvsig.tools.evaluator.AbstractEvaluator; |
44 |
import org.gvsig.tools.evaluator.EvaluatorData; |
45 |
import org.gvsig.tools.evaluator.EvaluatorException; |
46 |
import org.gvsig.utils.PostProcessSupport; |
47 |
|
48 |
|
49 |
public class ExportToDB extends Extension { |
50 |
/**
|
51 |
* @see org.gvsig.andami.plugins.IExtension#initialize()
|
52 |
*/
|
53 |
public void initialize() { |
54 |
} |
55 |
|
56 |
/**
|
57 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
58 |
*/
|
59 |
public void execute(String actionCommand) { |
60 |
org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
61 |
.getActiveWindow(); |
62 |
|
63 |
if (f instanceof View) { |
64 |
View vista = (View) f; |
65 |
IProjectView model = vista.getModel(); |
66 |
MapContext mapa = model.getMapContext(); |
67 |
FLayers layers = mapa.getLayers(); |
68 |
FLayer[] actives = layers.getActives();
|
69 |
try {
|
70 |
// NOTA: SI HAY UNA SELECCI?N, SOLO SE SALVAN LOS SELECCIONADOS
|
71 |
for (int i = 0; i < actives.length; i++) { |
72 |
if (actives[i] instanceof FLyrVect) { |
73 |
FLyrVect lv = (FLyrVect) actives[i]; |
74 |
// saveToDXF(mapa,lv);
|
75 |
|
76 |
|
77 |
|
78 |
/////////
|
79 |
|
80 |
long numSelec = ((FeatureSelection)lv.getFeatureStore().getSelection()).getSize();
|
81 |
if (numSelec > 0) { |
82 |
int resp = JOptionPane.showConfirmDialog( |
83 |
(Component) PluginServices.getMainFrame(),
|
84 |
PluginServices.getText(this,"se_van_a_guardar_") + numSelec |
85 |
+ PluginServices.getText(this,"features_desea_continuar"), |
86 |
PluginServices.getText(this,"export_to"), JOptionPane.YES_NO_OPTION); |
87 |
if (resp != JOptionPane.YES_OPTION) { |
88 |
continue;
|
89 |
} |
90 |
} // if numSelec > 0
|
91 |
// if (actionCommand.equals("SHP")) {
|
92 |
// saveToSHP(mapa, lv);
|
93 |
// }
|
94 |
// if (actionCommand.equals("DXF")) {
|
95 |
// saveToDXF(mapa, lv);
|
96 |
// }
|
97 |
if (actionCommand.equals("POSTGIS")) { |
98 |
saveToJDBC(mapa, lv, PostgreSQLServerExplorer.NAME); |
99 |
} |
100 |
if (actionCommand.equals("MYSQL")) { |
101 |
saveToJDBC(mapa, lv, MySQLServerExplorer.NAME); |
102 |
} |
103 |
// if (actionCommand.equals("GML")) {
|
104 |
// saveToGml(mapa, lv);
|
105 |
// }
|
106 |
} // actives[i]
|
107 |
} // for
|
108 |
} catch (Exception e) { |
109 |
NotificationManager.showMessageError(e.getMessage(),e); |
110 |
} |
111 |
} |
112 |
} |
113 |
|
114 |
public void saveToJDBC(MapContext mapContext, FLyrVect layer, |
115 |
String explorerName) {
|
116 |
try {
|
117 |
String tableName = JOptionPane.showInputDialog(PluginServices |
118 |
.getText(this, "intro_tablename")); |
119 |
if (tableName == null) { |
120 |
return;
|
121 |
} |
122 |
FeatureAttributeDescriptor[] pks = layer.getFeatureStore()
|
123 |
.getDefaultFeatureType() |
124 |
.getPrimaryKey(); |
125 |
String pkName = null; |
126 |
if (pks == null || pks.length < 1) { |
127 |
pkName = JOptionPane.showInputDialog(PluginServices.getText(this,"input_pk_field_name")); |
128 |
} |
129 |
|
130 |
tableName = tableName.toLowerCase(); |
131 |
DlgConnection dlg = new DlgConnection();
|
132 |
dlg.setModal(true);
|
133 |
dlg.setVisible(true);
|
134 |
ConnectionSettings cs = dlg.getConnSettings(); |
135 |
if (cs == null) { |
136 |
return;
|
137 |
} |
138 |
|
139 |
DataManager datamanager=DALLocator.getDataManager(); |
140 |
|
141 |
JDBCServerExplorerParameters explorerParam = (JDBCServerExplorerParameters) datamanager |
142 |
.createServerExplorerParameters(explorerName); |
143 |
|
144 |
explorerParam.setHost(cs.getHost()); |
145 |
if (cs.getPort() != null && cs.getPort().length() > 0) { |
146 |
explorerParam.setPort(Integer.valueOf(cs.getPort()));
|
147 |
} |
148 |
explorerParam.setDBName(cs.getDb()); |
149 |
explorerParam.setUser(cs.getUser()); |
150 |
explorerParam.setPassword(cs.getPassw()); |
151 |
if (cs.getSchema() != null && cs.getSchema().length() > 0) { |
152 |
explorerParam.setSchema(cs.getSchema()); |
153 |
} |
154 |
|
155 |
JDBCServerExplorer explorer = (JDBCServerExplorer) datamanager |
156 |
.createServerExplorer(explorerParam); |
157 |
|
158 |
NewFeatureStoreParameters newStoreParams = (NewFeatureStoreParameters) explorer |
159 |
.getAddParameters(); |
160 |
|
161 |
((DBStoreParameters) newStoreParams).setTable(tableName); |
162 |
|
163 |
if (export(explorer, newStoreParams, null, layer.getFeatureStore(),pkName)) { |
164 |
openLayer(newStoreParams, tableName, mapContext); |
165 |
} |
166 |
|
167 |
|
168 |
|
169 |
} catch (DataException e) {
|
170 |
NotificationManager.addError(e.getMessage(), e); |
171 |
} catch (ValidateDataParametersException e) {
|
172 |
NotificationManager.addError(e.getMessage(), e); |
173 |
} |
174 |
|
175 |
} |
176 |
|
177 |
|
178 |
|
179 |
class GeometryTypeEvaluator extends AbstractEvaluator{ |
180 |
private FeatureType featureType=null; |
181 |
private int type=0; |
182 |
private FeatureSelection selection;
|
183 |
private boolean hasSelection=false; |
184 |
|
185 |
public GeometryTypeEvaluator(FeatureType ft, int t, FeatureSelection selection) { |
186 |
featureType=ft; |
187 |
type=t; |
188 |
this.selection=selection;
|
189 |
hasSelection=selection.getSelectedCount()>0;
|
190 |
this.getFieldsInfo().addFieldValue(
|
191 |
ft.getDefaultGeometryAttributeName()); |
192 |
} |
193 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
194 |
if (hasSelection){
|
195 |
if (!selection.isSelected((Feature)data)){
|
196 |
return false; |
197 |
} |
198 |
} |
199 |
Geometry geometry=(Geometry) data.getDataValue(featureType.getDefaultGeometryAttributeName()); |
200 |
return new Boolean(geometry.getType()==type); |
201 |
} |
202 |
|
203 |
public String getName() { |
204 |
return "GeometryTypeEvaluator"; |
205 |
} |
206 |
|
207 |
} |
208 |
|
209 |
|
210 |
|
211 |
private void openLayer(NewFeatureStoreParameters newParams, String name, MapContext mapContext) throws ReadException { |
212 |
int res = JOptionPane.showConfirmDialog( |
213 |
(JComponent) PluginServices.getMDIManager().getActiveWindow()
|
214 |
, PluginServices.getText(this, "insertar_en_la_vista_la_capa_creada"), |
215 |
PluginServices.getText(this,"insertar_capa"), |
216 |
JOptionPane.YES_NO_OPTION);
|
217 |
|
218 |
if (res == JOptionPane.YES_OPTION){ |
219 |
PostProcessSupport.executeCalls(); |
220 |
LayerFactory layerFactory=LayerFactory.getInstance(); |
221 |
try {
|
222 |
FLayer newLayer = layerFactory.getInstance().createLayer( |
223 |
name,newParams); |
224 |
|
225 |
mapContext.getLayers().addLayer(newLayer); |
226 |
} catch (LoadLayerException e) {
|
227 |
throw new ReadException("Load layer",e); |
228 |
} |
229 |
} |
230 |
|
231 |
} |
232 |
|
233 |
private boolean export(JDBCServerExplorer explorer, |
234 |
NewFeatureStoreParameters params, FeatureSet set, |
235 |
FeatureStore featureStore, String pkName) {
|
236 |
DisposableIterator it1 = null;
|
237 |
try {
|
238 |
EditableFeatureType fType = featureStore.getDefaultFeatureType() |
239 |
.getEditable(); |
240 |
if (pkName != null) { |
241 |
EditableFeatureAttributeDescriptor pk = fType.add(pkName, |
242 |
DataTypes.LONG); |
243 |
pk.setIsPrimaryKey(true);
|
244 |
pk.setIsAutomatic(true);
|
245 |
} |
246 |
|
247 |
params.setDefaultFeatureType(fType); |
248 |
explorer.add(params, true);
|
249 |
|
250 |
DataManager manager = DALLocator.getDataManager(); |
251 |
FeatureStore target = (FeatureStore) manager |
252 |
.createStore(params); |
253 |
FeatureType targetType = target.getDefaultFeatureType(); |
254 |
|
255 |
target.edit(FeatureStore.MODE_APPEND); |
256 |
if (set == null) { |
257 |
set = featureStore.getFeatureSet(); |
258 |
} |
259 |
it1 = set.iterator(); |
260 |
while (it1.hasNext()) {
|
261 |
Feature feature = (Feature) it1.next(); |
262 |
target.insert(target.createNewFeature(targetType, feature)); |
263 |
} |
264 |
set.dispose(); |
265 |
target.finishEditing(); |
266 |
target.dispose(); |
267 |
return true; |
268 |
} catch (Exception e) { |
269 |
NotificationManager.showMessageError(e.getLocalizedMessage(),e); |
270 |
return false; |
271 |
} finally {
|
272 |
if (it1 != null) { |
273 |
it1.dispose(); |
274 |
} |
275 |
} |
276 |
|
277 |
} |
278 |
/**
|
279 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
280 |
*/
|
281 |
public boolean isEnabled() { |
282 |
org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices |
283 |
.getMDIManager().getActiveWindow(); |
284 |
|
285 |
if (f == null) { |
286 |
return false; |
287 |
} |
288 |
|
289 |
if (!(f instanceof View)) { |
290 |
return false; |
291 |
} |
292 |
|
293 |
FLayers layers = ((View) f).getMapControl().getMapContext().getLayers();
|
294 |
FLayer[] actives = layers.getActives();
|
295 |
if (actives.length != 1) { |
296 |
return false; |
297 |
} |
298 |
if (!(actives[0] instanceof FLyrVect)) { |
299 |
return false; |
300 |
} |
301 |
return true; |
302 |
|
303 |
} |
304 |
|
305 |
/**
|
306 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
307 |
*/
|
308 |
public boolean isVisible() { |
309 |
org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
310 |
.getActiveWindow(); |
311 |
|
312 |
if (f == null) { |
313 |
return false; |
314 |
} |
315 |
|
316 |
if (f instanceof View) { |
317 |
return true; |
318 |
} |
319 |
return false; |
320 |
} |
321 |
|
322 |
// private int findFileByName(FieldDescription[] fields, String fieldName){
|
323 |
// for (int i=0; i < fields.length; i++)
|
324 |
// {
|
325 |
// FieldDescription f = fields[i];
|
326 |
// if (f.getFieldName().equalsIgnoreCase(fieldName))
|
327 |
// {
|
328 |
// return i;
|
329 |
// }
|
330 |
// }
|
331 |
//
|
332 |
// return -1;
|
333 |
//
|
334 |
// }
|
335 |
|
336 |
} |