Statistics
| Revision:

root / trunk / libraries / libFMap_dataDB / src / org / gvsig / data / datastores / vectorial / db / sde / SDEExplorer.java @ 20855

History | View | Annotate | Download (9.74 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
                try{
134
                table.delete();
135
                }catch (Exception e) {
136
                        System.out.println("Table does not exist");
137
                }
138
                table.create(columns);
139
                SeLayer layer=new SeLayer(conn);
140
                layer.setTableName(sdeParam.getTableName());
141
                layer.setSpatialColumnName("SHAPE");
142
                layer.setShapeTypes(SeLayer.SE_NIL_TYPE_MASK);
143
                layer.create(3, 4);
144
                return sdeParam;
145
            } catch (SeException e) {
146
                        throw new InitializeWriterException(DATAEXPLORER_NAME,e);
147
                }
148
          }
149

    
150
    /**
151
     * DOCUMENT ME!
152
     *
153
     * @return DOCUMENT ME!
154
     */
155
    public INewDataStoreParameters createNewDataStoreParameter() {
156
        return new SDENewStoreParameter((IDataStoreParameters) this.newStoreParamters());
157
    }
158

    
159
    /**
160
     * DOCUMENT ME!
161
     *
162
     * @param dsp DOCUMENT ME!
163
     *
164
     * @throws ReadException DOCUMENT ME!
165
     */
166
    public void remove(IDataStoreParameters dsp) throws ReadException {
167
        SeConnection conn = this.getConnection();
168
        this.remove(dsp, conn);
169
    }
170

    
171
    /**
172
     * DOCUMENT ME!
173
     *
174
     * @param dsp DOCUMENT ME!
175
     * @param conn DOCUMENT ME!
176
     *
177
     * @throws ReadException DOCUMENT ME!
178
     */
179
    protected void remove(IDataStoreParameters dsp, SeConnection conn)
180
        throws ReadException {
181
            try {
182
                SeTable table = new SeTable(conn,((SDEStoreParameters)dsp).tableID());
183
                table.delete();
184
        } catch (SeException e) {
185
                        throw new ReadException(this.getClass().getName(),e);
186
                }
187
    }
188

    
189
    /**
190
     * DOCUMENT ME!
191
     *
192
     * @return DOCUMENT ME!
193
     */
194
    public boolean canCreate() {
195
        return true;
196
    }
197

    
198
    /**
199
     * DOCUMENT ME!
200
     *
201
     * @return DOCUMENT ME!
202
     */
203
    public String getName() {
204
        return DATAEXPLORER_NAME;
205
    }
206

    
207
    /**
208
     * DOCUMENT ME!
209
     *
210
     * @return DOCUMENT ME!
211
     */
212
    public String getDataStoreName() {
213
        return SDEStore.DATASTORE_NAME;
214
    }
215

    
216
    /**
217
     * DOCUMENT ME!
218
     *
219
     * @param parameters DOCUMENT ME!
220
     *
221
     * @throws InitializeException DOCUMENT ME!
222
     */
223
    public void init(IDataExplorerParameters parameters)
224
        throws InitializeException {
225
            SDEResource tmpResource = new SDEResource((SDEExplorerParameters)parameters);
226
                SDEResource theResource;
227
                ResourceManager resMan = ResourceManager.getResourceManager();
228

    
229
                try {
230
                        theResource = (SDEResource)resMan.addResource(tmpResource);
231
                } catch (DataException e1) {
232
                        throw new InitializeException(this.getName(),e1);
233
                }
234
            this.parameters = (SDEExplorerParameters) parameters;
235
            this.resource=theResource;
236
     }
237

    
238
    private SDEStoreParameters newStoreParamters() {
239
        DataManager manager = DataManager.getManager();
240
        SDEStoreParameters param = (SDEStoreParameters) manager.createDataStoreParameters(getDataStoreName());
241
        this.parameters.fillStoreParameters((DBParameters) param);
242

    
243
        return param;
244
    }
245

    
246
    /**
247
     * DOCUMENT ME!
248
     *
249
     * @return DOCUMENT ME!
250
     *
251
     * @throws ReadException DOCUMENT ME!
252
     */
253
    public IDataStoreParameters[] list() throws ReadException {
254
        return this.list(this.parameters.isShowInformationDBTables());
255
    }
256

    
257
    /**
258
     * DOCUMENT ME!
259
     *
260
     * @param showInformationDBTables DOCUMENT ME!
261
     *
262
     * @return DOCUMENT ME!
263
     *
264
     * @throws ReadException DOCUMENT ME!
265
     */
266
    public IDataStoreParameters[] list(boolean showInformationDBTables)
267
        throws ReadException {
268
        SeConnection conn = this.getConnection();
269
        ArrayList paramList = new ArrayList();
270
        SDEStoreParameters sdeparam = null;
271

    
272
        try {
273
            Vector theLayers = conn.getLayers();
274

    
275
            for (int i = 0; i < theLayers.size(); i++) {
276
                sdeparam = this.newStoreParamters();
277

    
278
                SeLayer layer = (SeLayer) theLayers.elementAt(i);
279
                sdeparam.setTableName(layer.getTableName());
280
                sdeparam.setFields(new String[] { "*" });
281
                sdeparam.setGeometryField(layer.getSpatialColumn());
282
                paramList.add(sdeparam);
283
            }
284

    
285
            return (IDataStoreParameters[]) paramList.toArray(new IDataStoreParameters[0]);
286
        } catch (SeException e) {
287
            throw new ReadException(this.getClass().getName(), e);
288
        }
289
    }
290

    
291
    /**
292
     * DOCUMENT ME!
293
     *
294
     * @param dsp DOCUMENT ME!
295
     *
296
     * @return DOCUMENT ME!
297
     *
298
     * @throws ReadException DOCUMENT ME!
299
     */
300
    public IFeatureType[] getFeatureTypes(IDataStoreParameters dsp)
301
        throws ReadException {
302
        return new IFeatureType[] {
303
            SDEUtils.getFeatureType(this.getConnection(),
304
                (SDEStoreParameters) dsp)
305
        };
306
    }
307

    
308
    /**
309
     * DOCUMENT ME!
310
     *
311
     * @return DOCUMENT ME!
312
     */
313
    public String getDefaultSchema() {
314
        return this.defaultSchema;
315
    }
316

    
317
        public void dispose() throws DataException {
318
                ResourceManager resMan = ResourceManager.getResourceManager();
319

    
320
            try {
321
                        resMan.remove(this.resource);
322
                } catch (DataException e1) {
323
                        throw new CloseException(this.getName(),e1);
324
                } catch (KeyException e) {
325
                        throw new CloseException(this.getName(),e);
326
                }
327
        }
328
}