svn-gvsig-desktop / trunk / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / sde / SDEExplorer.java @ 20841
History | View | Annotate | Download (9.44 KB)
1 |
|
---|---|
2 |
/* gvSIG. Geographic Information System of the Valencian Government
|
3 |
*
|
4 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
5 |
* of the Valencian Government (CIT)
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU General Public License
|
9 |
* as published by the Free Software Foundation; either version 2
|
10 |
* of the License, or (at your option) any later version.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
* GNU General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License
|
18 |
* along with this program; if not, write to the Free Software
|
19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
20 |
* MA 02110-1301, USA.
|
21 |
*
|
22 |
*/
|
23 |
|
24 |
/*
|
25 |
* AUTHORS (In addition to CIT):
|
26 |
* ${year} IVER T.I. S.A. {{Task}}
|
27 |
*/
|
28 |
|
29 |
package org.gvsig.data.datastores.vectorial.db.sde; |
30 |
|
31 |
import java.security.KeyException; |
32 |
import java.util.ArrayList; |
33 |
import java.util.Vector; |
34 |
|
35 |
import org.gvsig.data.DataManager; |
36 |
import org.gvsig.data.IDataExplorerParameters; |
37 |
import org.gvsig.data.IDataStoreParameters; |
38 |
import org.gvsig.data.INewDataStoreParameters; |
39 |
import org.gvsig.data.ResourceManager; |
40 |
import org.gvsig.data.datastores.vectorial.db.DBAttributeDescriptor; |
41 |
import org.gvsig.data.datastores.vectorial.db.DBExplorer; |
42 |
import org.gvsig.data.datastores.vectorial.db.DBFeatureType; |
43 |
import org.gvsig.data.datastores.vectorial.db.DBParameters; |
44 |
import org.gvsig.data.exception.CloseException; |
45 |
import org.gvsig.data.exception.DataException; |
46 |
import org.gvsig.data.exception.InitializeException; |
47 |
import org.gvsig.data.exception.InitializeWriterException; |
48 |
import org.gvsig.data.exception.ReadException; |
49 |
import org.gvsig.data.vectorial.IFeatureType; |
50 |
import org.gvsig.data.vectorial.INewFeatureStoreParameters; |
51 |
|
52 |
import com.esri.sde.sdk.client.SeColumnDefinition; |
53 |
import com.esri.sde.sdk.client.SeConnection; |
54 |
import com.esri.sde.sdk.client.SeException; |
55 |
import com.esri.sde.sdk.client.SeLayer; |
56 |
import com.esri.sde.sdk.client.SeTable; |
57 |
|
58 |
|
59 |
/**
|
60 |
* DOCUMENT ME!
|
61 |
*
|
62 |
* @author Vicente Caballero Navarro
|
63 |
*/
|
64 |
public class SDEExplorer extends DBExplorer { |
65 |
/**
|
66 |
* DOCUMENT ME!
|
67 |
*/
|
68 |
public static String DATAEXPLORER_NAME = "SDEExplorer"; |
69 |
private SDEExplorerParameters parameters;
|
70 |
private String defaultSchema; |
71 |
private SDEResource resource;
|
72 |
/**
|
73 |
* DOCUMENT ME!
|
74 |
*
|
75 |
* @return DOCUMENT ME!
|
76 |
*
|
77 |
* @throws InitializeException DOCUMENT ME!
|
78 |
*/
|
79 |
protected SeConnection getConnection() throws ReadException { |
80 |
return this.resource.getConnection();//SDEUtils.getConnection(host, port, db, dbuser, dbpass); |
81 |
} |
82 |
|
83 |
/**
|
84 |
* DOCUMENT ME!
|
85 |
*
|
86 |
* @param ndsp DOCUMENT ME!
|
87 |
*
|
88 |
* @return DOCUMENT ME!
|
89 |
*
|
90 |
* @throws InitializeException DOCUMENT ME!
|
91 |
* @throws InitializeWriterException DOCUMENT ME!
|
92 |
*/
|
93 |
public IDataStoreParameters add(INewFeatureStoreParameters ndsp)
|
94 |
throws InitializeException, InitializeWriterException {
|
95 |
SeConnection conn; |
96 |
try {
|
97 |
conn = this.getConnection();
|
98 |
} catch (ReadException e) {
|
99 |
throw new InitializeException(DATAEXPLORER_NAME,e); |
100 |
} |
101 |
IDataStoreParameters params = this.add(ndsp, conn);
|
102 |
|
103 |
return params;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* DOCUMENT ME!
|
108 |
*
|
109 |
* @param ndsp DOCUMENT ME!
|
110 |
* @param conn DOCUMENT ME!
|
111 |
*
|
112 |
* @return DOCUMENT ME!
|
113 |
*
|
114 |
* @throws InitializeException DOCUMENT ME!
|
115 |
* @throws InitializeWriterException DOCUMENT ME!
|
116 |
*/
|
117 |
protected IDataStoreParameters add(INewFeatureStoreParameters ndsp,
|
118 |
SeConnection conn) |
119 |
throws InitializeException, InitializeWriterException {
|
120 |
try {
|
121 |
if (!ndsp.isValid()){
|
122 |
throw new InitializeException(this.getName(),new Exception("Parameters not valid")); |
123 |
} |
124 |
SDEStoreParameters sdeParam = (SDEStoreParameters)ndsp.getDataStoreParameters(); |
125 |
DBFeatureType fType = (DBFeatureType)ndsp.getFeatureType(); |
126 |
SeTable table = new SeTable(conn,sdeParam.tableID());
|
127 |
DBAttributeDescriptor[] fads=(DBAttributeDescriptor[])fType.toArray(new DBAttributeDescriptor[0]); |
128 |
SeColumnDefinition[] columns=new SeColumnDefinition[fads.length]; |
129 |
for (int j = 0; j < fads.length; j++) { |
130 |
SeColumnDefinition column=new SeColumnDefinition(fads[j].getName(),SDETypes.fieldTypeToInt(fads[j].getDataType()),fads[j].getSize(),fads[j].getPrecision(),fads[j].isAllowNull());
|
131 |
columns[j]=column; |
132 |
} |
133 |
table.create(columns); |
134 |
return sdeParam;
|
135 |
} catch (SeException e) {
|
136 |
throw new InitializeWriterException(DATAEXPLORER_NAME,e); |
137 |
} |
138 |
} |
139 |
|
140 |
/**
|
141 |
* DOCUMENT ME!
|
142 |
*
|
143 |
* @return DOCUMENT ME!
|
144 |
*/
|
145 |
public INewDataStoreParameters createNewDataStoreParameter() {
|
146 |
return new SDENewStoreParameter((IDataStoreParameters) this.newStoreParamters()); |
147 |
} |
148 |
|
149 |
/**
|
150 |
* DOCUMENT ME!
|
151 |
*
|
152 |
* @param dsp DOCUMENT ME!
|
153 |
*
|
154 |
* @throws ReadException DOCUMENT ME!
|
155 |
*/
|
156 |
public void remove(IDataStoreParameters dsp) throws ReadException { |
157 |
SeConnection conn = this.getConnection();
|
158 |
this.remove(dsp, conn);
|
159 |
} |
160 |
|
161 |
/**
|
162 |
* DOCUMENT ME!
|
163 |
*
|
164 |
* @param dsp DOCUMENT ME!
|
165 |
* @param conn DOCUMENT ME!
|
166 |
*
|
167 |
* @throws ReadException DOCUMENT ME!
|
168 |
*/
|
169 |
protected void remove(IDataStoreParameters dsp, SeConnection conn) |
170 |
throws ReadException {
|
171 |
try {
|
172 |
SeTable table = new SeTable(conn,((SDEStoreParameters)dsp).tableID());
|
173 |
table.delete(); |
174 |
} catch (SeException e) {
|
175 |
throw new ReadException(this.getClass().getName(),e); |
176 |
} |
177 |
} |
178 |
|
179 |
/**
|
180 |
* DOCUMENT ME!
|
181 |
*
|
182 |
* @return DOCUMENT ME!
|
183 |
*/
|
184 |
public boolean canCreate() { |
185 |
return true; |
186 |
} |
187 |
|
188 |
/**
|
189 |
* DOCUMENT ME!
|
190 |
*
|
191 |
* @return DOCUMENT ME!
|
192 |
*/
|
193 |
public String getName() { |
194 |
return DATAEXPLORER_NAME;
|
195 |
} |
196 |
|
197 |
/**
|
198 |
* DOCUMENT ME!
|
199 |
*
|
200 |
* @return DOCUMENT ME!
|
201 |
*/
|
202 |
public String getDataStoreName() { |
203 |
return SDEStore.DATASTORE_NAME;
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* DOCUMENT ME!
|
208 |
*
|
209 |
* @param parameters DOCUMENT ME!
|
210 |
*
|
211 |
* @throws InitializeException DOCUMENT ME!
|
212 |
*/
|
213 |
public void init(IDataExplorerParameters parameters) |
214 |
throws InitializeException {
|
215 |
SDEResource tmpResource = new SDEResource((SDEExplorerParameters)parameters);
|
216 |
SDEResource theResource; |
217 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
218 |
|
219 |
try {
|
220 |
theResource = (SDEResource)resMan.addResource(tmpResource); |
221 |
} catch (DataException e1) {
|
222 |
throw new InitializeException(this.getName(),e1); |
223 |
} |
224 |
this.parameters = (SDEExplorerParameters) parameters;
|
225 |
this.resource=theResource;
|
226 |
} |
227 |
|
228 |
private SDEStoreParameters newStoreParamters() {
|
229 |
DataManager manager = DataManager.getManager(); |
230 |
SDEStoreParameters param = (SDEStoreParameters) manager.createDataStoreParameters(getDataStoreName()); |
231 |
this.parameters.fillStoreParameters((DBParameters) param);
|
232 |
|
233 |
return param;
|
234 |
} |
235 |
|
236 |
/**
|
237 |
* DOCUMENT ME!
|
238 |
*
|
239 |
* @return DOCUMENT ME!
|
240 |
*
|
241 |
* @throws ReadException DOCUMENT ME!
|
242 |
*/
|
243 |
public IDataStoreParameters[] list() throws ReadException { |
244 |
return this.list(this.parameters.isShowInformationDBTables()); |
245 |
} |
246 |
|
247 |
/**
|
248 |
* DOCUMENT ME!
|
249 |
*
|
250 |
* @param showInformationDBTables DOCUMENT ME!
|
251 |
*
|
252 |
* @return DOCUMENT ME!
|
253 |
*
|
254 |
* @throws ReadException DOCUMENT ME!
|
255 |
*/
|
256 |
public IDataStoreParameters[] list(boolean showInformationDBTables) |
257 |
throws ReadException {
|
258 |
SeConnection conn = this.getConnection();
|
259 |
ArrayList paramList = new ArrayList(); |
260 |
SDEStoreParameters sdeparam = null;
|
261 |
|
262 |
try {
|
263 |
Vector theLayers = conn.getLayers();
|
264 |
|
265 |
for (int i = 0; i < theLayers.size(); i++) { |
266 |
sdeparam = this.newStoreParamters();
|
267 |
|
268 |
SeLayer layer = (SeLayer) theLayers.elementAt(i); |
269 |
sdeparam.setTableName(layer.getTableName()); |
270 |
sdeparam.setFields(new String[] { "*" }); |
271 |
sdeparam.setGeometryField(layer.getSpatialColumn()); |
272 |
paramList.add(sdeparam); |
273 |
} |
274 |
|
275 |
return (IDataStoreParameters[]) paramList.toArray(new IDataStoreParameters[0]); |
276 |
} catch (SeException e) {
|
277 |
throw new ReadException(this.getClass().getName(), e); |
278 |
} |
279 |
} |
280 |
|
281 |
/**
|
282 |
* DOCUMENT ME!
|
283 |
*
|
284 |
* @param dsp DOCUMENT ME!
|
285 |
*
|
286 |
* @return DOCUMENT ME!
|
287 |
*
|
288 |
* @throws ReadException DOCUMENT ME!
|
289 |
*/
|
290 |
public IFeatureType[] getFeatureTypes(IDataStoreParameters dsp) |
291 |
throws ReadException {
|
292 |
return new IFeatureType[] { |
293 |
SDEUtils.getFeatureType(this.getConnection(),
|
294 |
(SDEStoreParameters) dsp) |
295 |
}; |
296 |
} |
297 |
|
298 |
/**
|
299 |
* DOCUMENT ME!
|
300 |
*
|
301 |
* @return DOCUMENT ME!
|
302 |
*/
|
303 |
public String getDefaultSchema() { |
304 |
return this.defaultSchema; |
305 |
} |
306 |
|
307 |
public void dispose() throws DataException { |
308 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
309 |
|
310 |
try {
|
311 |
resMan.remove(this.resource);
|
312 |
} catch (DataException e1) {
|
313 |
throw new CloseException(this.getName(),e1); |
314 |
} catch (KeyException e) { |
315 |
throw new CloseException(this.getName(),e); |
316 |
} |
317 |
} |
318 |
} |