Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLayerVectorialDB.java @ 11971

History | View | Annotate | Download (6.84 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 com.iver.cit.gvsig.fmap.layers;
44

    
45
import com.hardcode.driverManager.DriverLoadException;
46
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
47

    
48
import com.iver.cit.gvsig.exceptions.layers.DriverLayerException;
49
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
50
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
51
import com.iver.cit.gvsig.exceptions.layers.NameLayerException;
52
import com.iver.cit.gvsig.exceptions.layers.ProjectionLayerException;
53
import com.iver.cit.gvsig.exceptions.layers.XMLLayerException;
54
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
55
import com.iver.cit.gvsig.fmap.drivers.DefaultJDBCDriver;
56
import com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver;
57
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
58
import com.iver.cit.gvsig.fmap.rendering.IVectorialLegend;
59
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
60
import com.iver.cit.gvsig.fmap.rendering.styling.AttrInTableLabeling;
61
import com.iver.cit.gvsig.fmap.rendering.styling.ILabelingStrategy;
62

    
63
import com.iver.utiles.XMLEntity;
64

    
65
import org.cresques.cts.IProjection;
66

    
67

    
68
public class FLayerVectorialDB extends FLyrVect {
69
    private boolean loaded = false;
70
    private IVectorialDatabaseDriver dbDriver = null;
71

    
72
    /* Esto deberia ir en el FLyrDefault */
73
    public void setProjectionByName(String projectionName)
74
        throws Exception {
75
        IProjection proj = CRSFactory.getCRS(projectionName);
76

    
77
        if (proj == null) {
78
            throw new Exception("No se ha encontrado la proyeccion: " +
79
                projectionName);
80
        }
81

    
82
        this.setProjection(proj);
83
    }
84

    
85
    public void setDriver(IVectorialDatabaseDriver driver) {
86
        this.dbDriver = driver;
87
    }
88

    
89
    public void setDriverByName(String driverName) throws ReadDriverException {
90
                try {
91
                        this.setDriver(
92
                          (IVectorialDatabaseDriver)LayerFactory.getDM().getDriver(driverName)
93
                        );
94
                } catch (DriverLoadException e) {
95
                        throw new ReadDriverException(getName(),e);
96
                }
97
        }
98

    
99
    public IVectorialDatabaseDriver getDriver() {
100
        return this.dbDriver;
101
    }
102

    
103
    /* FIXME: esto tendria que tener declarado un throws de algo*/
104
        public void wakeUp() throws LoadLayerException {
105
                if (!loaded) {
106
                        this.load();
107
                }
108

    
109
        }
110

    
111

    
112
    public void load() throws LoadLayerException {
113
                if (this.getName() == null || this.getName().length() == 0) {
114
                        this.setAvailable(false);
115
                        throw new NameLayerException(this.getName(),null);
116
                }
117
                if (this.dbDriver == null) {
118
                        this.setAvailable(false);
119
                        //TODO: traducir???
120
                        throw new DriverLayerException(this.getName(),null);
121
                }
122
                if (this.getProjection() == null) {
123
                        this.setAvailable(false);
124
                        //TODO: traducir???
125
                        throw new ProjectionLayerException(this.getName(),null);
126
                }
127

    
128
//                try {
129
                        try {
130
                                ((DefaultJDBCDriver)this.dbDriver).load();
131
                        } catch (ReadDriverException e1) {
132
                                throw new LoadLayerException(this.getName(),e1);
133
                        }
134
//                } catch (Exception e) {
135
//                        this.setAvailable(false);
136
//                        throw new DriverIOException(e);
137
//                }
138

    
139
//                try {
140
                        VectorialDBAdapter dbAdapter = new VectorialDBAdapter();
141
                        dbAdapter.setDriver(this.dbDriver);
142
                        this.setSource(dbAdapter);
143

    
144
//                } catch (Exception e) {
145
//                        this.setAvailable(false);
146
//                        throw new DriverIOException(e);
147
//                }
148

    
149
//                try {
150
                        try {
151
                                this.putLoadSelection();
152
                                this.putLoadLegend();
153
                                this.initializeLegendDefault();
154
                        } catch (XMLException e) {
155
                                this.setAvailable(false);
156
                                throw new XMLLayerException(this.getName(),e);
157
                        } catch (LegendLayerException e) {
158
                                this.setAvailable(false);
159
                                throw new LegendLayerException(this.getName(),e);
160
                        } catch (ReadDriverException e) {
161
                                this.setAvailable(false);
162
                                throw new LoadLayerException(this.getName(),e);
163
                        }
164
//                } catch (Exception e) {
165
//                        this.setAvailable(false);
166
//                        //TODO: traducir???
167
//                        throw new DriverIOException(e);
168
//                }
169
                this.cleanLoadOptions();
170
        }
171

    
172
    /* Esto deberia ir en FLyrVect */
173
        private void initializeLegendDefault() throws ReadDriverException, LegendLayerException {
174
                if (this.getLegend() == null) {
175
            if (this.getRecordset().getDriver() instanceof WithDefaultLegend) {
176
                WithDefaultLegend aux = (WithDefaultLegend) this.getRecordset().getDriver();
177
                this.setLegend((IVectorialLegend) aux.getDefaultLegend());
178

    
179
                ILabelingStrategy labeler = aux.getDefaultLabelingStrategy();
180
                if (labeler instanceof AttrInTableLabeling) {
181
                        ((AttrInTableLabeling) labeler).setLayer(this);
182
                }
183

    
184
                this.setLabelingStrategy(labeler);
185

    
186
            } else {
187
                this.setLegend(LegendFactory.createSingleSymbolLegend(
188
                        this.getShapeType()));
189
            }
190
                }
191
        }
192

    
193

    
194
    public void setXMLEntity(XMLEntity xml) throws XMLException {
195
        IProjection proj = null;
196

    
197
        if (xml.contains("proj")) {
198
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
199
        }
200
        else {
201
            proj = this.getMapContext().getViewPort().getProjection();
202
        }
203

    
204
        this.setName(xml.getName());
205
        this.setProjection(proj);
206

    
207
        String driverName = xml.getStringProperty("db");
208
        IVectorialDatabaseDriver driver;
209

    
210
        try {
211
            driver = (IVectorialDatabaseDriver) LayerFactory.getDM()
212
                                                           .getDriver(driverName);
213

    
214
            //Hay que separar la carga de los datos del XMLEntity del load.
215
            driver.setXMLEntity(xml.getChild(2));
216
            this.setDriver(driver);
217
        }
218
        catch (Exception e) {
219
            throw new XMLException(e);
220
        }
221

    
222
        super.setXMLEntityNew(xml);
223
    }
224
}