svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.oracle / src / org / gvsig / oracle / extension / ExportToOracle.java @ 30231
History | View | Annotate | Download (10.3 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* Prodevelop Integraci?n de Tecnolog?as SL
|
34 |
* Conde Salvatierra de ?lava , 34-10
|
35 |
* 46004 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963 510 612
|
39 |
* +34 963 510 968
|
40 |
* gis@prodevelop.es
|
41 |
* http://www.prodevelop.es
|
42 |
*/
|
43 |
package org.gvsig.oracle.extension; |
44 |
|
45 |
import java.awt.Component; |
46 |
|
47 |
import javax.swing.JComponent; |
48 |
import javax.swing.JOptionPane; |
49 |
|
50 |
import org.gvsig.andami.PluginServices; |
51 |
import org.gvsig.andami.messages.NotificationManager; |
52 |
import org.gvsig.fmap.dal.DALLocator; |
53 |
import org.gvsig.fmap.dal.DataManager; |
54 |
import org.gvsig.fmap.dal.DataServerExplorer; |
55 |
import org.gvsig.fmap.dal.DataTypes; |
56 |
import org.gvsig.fmap.dal.exception.DataException; |
57 |
import org.gvsig.fmap.dal.exception.ReadException; |
58 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
59 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
60 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
61 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
62 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
63 |
import org.gvsig.fmap.dal.feature.Feature; |
64 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
65 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
66 |
import org.gvsig.fmap.dal.feature.FeatureType; |
67 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
68 |
import org.gvsig.fmap.dal.store.oracle.OracleServerExplorer; |
69 |
import org.gvsig.fmap.dal.store.oracle.OracleServerExplorerParameters; |
70 |
import org.gvsig.fmap.dal.store.oracle.OracleStoreParameters; |
71 |
import org.gvsig.fmap.dal.store.oracle.OracleValues; |
72 |
import org.gvsig.fmap.mapcontext.MapContext; |
73 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
74 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
75 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
76 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
77 |
import org.gvsig.geodb.vectorialdb.wizard.MyExplorer; |
78 |
import org.gvsig.oracle.gui.OracleConnectionChooserPanel; |
79 |
import org.gvsig.utils.PostProcessSupport; |
80 |
import org.jfree.util.Log; |
81 |
import org.opengis.feature.FeatureAttributeDescriptor; |
82 |
|
83 |
/**
|
84 |
* Writes a layer as an Oracle table.
|
85 |
*
|
86 |
* @author jldominguez, vsanjaime
|
87 |
*
|
88 |
*/
|
89 |
public class ExportToOracle { |
90 |
|
91 |
/**
|
92 |
* Constructor
|
93 |
*/
|
94 |
public ExportToOracle() {
|
95 |
System.out.println("Start export process..."); |
96 |
} |
97 |
|
98 |
/**
|
99 |
* run export process
|
100 |
*
|
101 |
* @param mapContext
|
102 |
* @param layer
|
103 |
*/
|
104 |
public void toOracle(MapContext mapContext, FLyrVect layer) { |
105 |
|
106 |
// name of oracle table
|
107 |
String tableName = this.getNewOracleTableName(); |
108 |
if (tableName == null) { |
109 |
return;
|
110 |
} |
111 |
|
112 |
// name of oracle table
|
113 |
String pkFieldName = ""; |
114 |
try {
|
115 |
pkFieldName = this.getNewOracleTablePK(layer);
|
116 |
} catch (Exception e) { |
117 |
Log.warn("There are not primakey fields in this data store");
|
118 |
pkFieldName = null;
|
119 |
} |
120 |
|
121 |
// Selected explorer (connection)
|
122 |
MyExplorer explorer = this.getSelectedExplorer();
|
123 |
|
124 |
if (explorer == null) { |
125 |
return;
|
126 |
} |
127 |
|
128 |
//
|
129 |
OracleServerExplorerParameters explorerParams = (OracleServerExplorerParameters) explorer |
130 |
.getDbSeverExplorerParameters(); |
131 |
|
132 |
this.createNewOracleStore(mapContext, layer, explorerParams, tableName,
|
133 |
pkFieldName); |
134 |
} |
135 |
|
136 |
/**
|
137 |
* create new oracle store
|
138 |
*
|
139 |
* @param mapContext
|
140 |
* @param layer
|
141 |
* @param exParams
|
142 |
* @param tableName
|
143 |
* @param pkName
|
144 |
*/
|
145 |
private void createNewOracleStore(MapContext mapContext, FLyrVect layer, |
146 |
OracleServerExplorerParameters explorerParams, String tableName,
|
147 |
String pkName) {
|
148 |
try {
|
149 |
|
150 |
DataManager dataManager = DALLocator.getDataManager(); |
151 |
|
152 |
DataServerExplorer explorer = dataManager |
153 |
.createServerExplorer(explorerParams); |
154 |
OracleServerExplorer oraExplorer = (OracleServerExplorer) explorer; |
155 |
|
156 |
NewFeatureStoreParameters newOracleStoreParams = (NewFeatureStoreParameters) oraExplorer |
157 |
.getAddParameters(); |
158 |
|
159 |
((OracleStoreParameters) newOracleStoreParams).setTable(tableName); |
160 |
|
161 |
boolean exportOk = export(oraExplorer, newOracleStoreParams, layer |
162 |
.getFeatureStore(), pkName); |
163 |
if (exportOk) {
|
164 |
openLayer(newOracleStoreParams, tableName, mapContext); |
165 |
} |
166 |
|
167 |
} catch (DataException e) {
|
168 |
NotificationManager.addError(e.getMessage(), e); |
169 |
} catch (ValidateDataParametersException e) {
|
170 |
NotificationManager.addError(e.getMessage(), e); |
171 |
e.printStackTrace(); |
172 |
} |
173 |
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* show layer in the view
|
178 |
*
|
179 |
* @param newParams
|
180 |
* @param name
|
181 |
* @param mapContext
|
182 |
* @throws ReadException
|
183 |
*/
|
184 |
private void openLayer(NewFeatureStoreParameters newParams, String name, |
185 |
MapContext mapContext) throws ReadException {
|
186 |
int res = JOptionPane.showConfirmDialog((JComponent) PluginServices |
187 |
.getMDIManager().getActiveWindow(), PluginServices.getText( |
188 |
this, "insertar_en_la_vista_la_capa_creada"), PluginServices |
189 |
.getText(this, "insertar_capa"), JOptionPane.YES_NO_OPTION); |
190 |
|
191 |
if (res == JOptionPane.YES_OPTION) { |
192 |
PostProcessSupport.executeCalls(); |
193 |
try {
|
194 |
FLayer newLayer = LayerFactory.getInstance().createLayer(name, |
195 |
newParams); |
196 |
|
197 |
mapContext.getLayers().addLayer(newLayer); |
198 |
} catch (LoadLayerException e) {
|
199 |
throw new ReadException("Load layer", e); |
200 |
} |
201 |
} |
202 |
} |
203 |
|
204 |
/**
|
205 |
* Create new fields and fill data
|
206 |
*
|
207 |
* @param explorer
|
208 |
* @param params
|
209 |
* @param set
|
210 |
* @param featureStore
|
211 |
* @param pkName
|
212 |
* @return
|
213 |
*/
|
214 |
private boolean export(OracleServerExplorer explorer, |
215 |
NewFeatureStoreParameters params, FeatureStore featureStore, |
216 |
String pkName) {
|
217 |
|
218 |
DisposableIterator it1 = null;
|
219 |
try {
|
220 |
EditableFeatureType efType = featureStore.getDefaultFeatureType() |
221 |
.getEditable(); |
222 |
|
223 |
// if GID field not exists, create it
|
224 |
boolean gidFound = false; |
225 |
|
226 |
for (int i = 0; i < efType.size(); i++) { |
227 |
EditableFeatureAttributeDescriptor edesc = (EditableFeatureAttributeDescriptor) efType |
228 |
.getAttributeDescriptor(i); |
229 |
if (edesc.getName().compareTo(
|
230 |
OracleValues.DEFAULT_ID_FIELD_CASE_SENSITIVE) == 0) {
|
231 |
gidFound = true;
|
232 |
break;
|
233 |
} |
234 |
} |
235 |
|
236 |
if (!gidFound) {
|
237 |
EditableFeatureAttributeDescriptor gid = efType.add( |
238 |
OracleValues.DEFAULT_ID_FIELD_CASE_SENSITIVE, |
239 |
DataTypes.INT); |
240 |
gid.setIsAutomatic(true);
|
241 |
gid.setSize(7);
|
242 |
gid.setPrecision(0);
|
243 |
} |
244 |
|
245 |
if (pkName != null) { |
246 |
EditableFeatureAttributeDescriptor pk = efType.add(pkName, |
247 |
DataTypes.LONG); |
248 |
pk.setIsPrimaryKey(true);
|
249 |
pk.setIsAutomatic(true);
|
250 |
} |
251 |
|
252 |
efType |
253 |
.setDefaultGeometryAttributeName(OracleValues.DEFAULT_GEO_FIELD); |
254 |
|
255 |
params.setDefaultFeatureType(efType.getNotEditableCopy()); |
256 |
|
257 |
explorer.add(params, true);
|
258 |
|
259 |
DataManager manager = DALLocator.getDataManager(); |
260 |
FeatureStore target = (FeatureStore) manager.createStore(params); |
261 |
FeatureType targetType = target.getDefaultFeatureType(); |
262 |
|
263 |
target.edit(FeatureStore.MODE_APPEND); |
264 |
|
265 |
FeatureSet set = featureStore.getFeatureSet(); |
266 |
|
267 |
it1 = set.iterator(); |
268 |
while (it1.hasNext()) {
|
269 |
Feature feature = (Feature) it1.next(); |
270 |
EditableFeature efeat = target.createNewFeature(targetType, |
271 |
feature); |
272 |
target.insert(efeat); |
273 |
} |
274 |
set.dispose(); |
275 |
target.finishEditing(); |
276 |
target.dispose(); |
277 |
return true; |
278 |
} catch (Exception e) { |
279 |
NotificationManager.showMessageError(e.getLocalizedMessage(), e); |
280 |
return false; |
281 |
} finally {
|
282 |
if (it1 != null) { |
283 |
it1.dispose(); |
284 |
} |
285 |
} |
286 |
|
287 |
} |
288 |
|
289 |
/**
|
290 |
* get new oracle table name
|
291 |
*
|
292 |
* @return
|
293 |
*/
|
294 |
private String getNewOracleTableName() { |
295 |
|
296 |
String tableName = ""; |
297 |
boolean valid_name = false; |
298 |
|
299 |
while (!valid_name) {
|
300 |
tableName = JOptionPane.showInputDialog(((Component) PluginServices |
301 |
.getMainFrame()), PluginServices.getText(this,
|
302 |
"intro_tablename"));
|
303 |
valid_name = ((tableName == null) || ((tableName.length() <= (OracleValues.MAX_ID_LENGTH - 3)) |
304 |
&& (tableName.indexOf(" ") == -1) && (tableName.length() > 0))); |
305 |
|
306 |
if (!valid_name) {
|
307 |
if (tableName.length() > (OracleValues.MAX_ID_LENGTH - 3)) { |
308 |
JOptionPane.showMessageDialog(null, PluginServices.getText( |
309 |
this, "nombre_demasiado_largo"), PluginServices |
310 |
.getText(this, "error"), JOptionPane.ERROR_MESSAGE); |
311 |
} else {
|
312 |
JOptionPane.showMessageDialog(null, PluginServices.getText( |
313 |
this, "nombre_no_valido"), PluginServices.getText( |
314 |
this, "error"), JOptionPane.ERROR_MESSAGE); |
315 |
} |
316 |
} |
317 |
} |
318 |
return tableName.toUpperCase();
|
319 |
} |
320 |
|
321 |
/**
|
322 |
* get new oracle table pk field
|
323 |
*
|
324 |
* @return
|
325 |
* @throws DataException
|
326 |
* @throws ReadException
|
327 |
*/
|
328 |
private String getNewOracleTablePK(FLyrVect layer) throws ReadException, |
329 |
DataException { |
330 |
|
331 |
FeatureStore store = layer.getFeatureStore(); |
332 |
FeatureType type = store.getDefaultFeatureType(); |
333 |
FeatureAttributeDescriptor[] pks = (FeatureAttributeDescriptor[]) type |
334 |
.getPrimaryKey(); |
335 |
|
336 |
String pkName = null; |
337 |
if (pks == null || pks.length < 1) { |
338 |
pkName = JOptionPane.showInputDialog(PluginServices.getText(this, |
339 |
"input_pk_field_name"));
|
340 |
} |
341 |
return pkName;
|
342 |
|
343 |
} |
344 |
|
345 |
/**
|
346 |
* get parameters of selected explorer
|
347 |
*
|
348 |
* @return
|
349 |
*/
|
350 |
private MyExplorer getSelectedExplorer() {
|
351 |
|
352 |
OracleConnectionChooserPanel dlg = new OracleConnectionChooserPanel();
|
353 |
PluginServices.getMDIManager().addWindow(dlg); |
354 |
dlg.validate(); |
355 |
|
356 |
if (!dlg.isOkPressed()) {
|
357 |
return null; |
358 |
} |
359 |
return dlg.getSelectedServerExplorer();
|
360 |
} |
361 |
} |