Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / layers / FLayerJDBCVectorial.java @ 10661

History | View | Annotate | Download (5 KB)

1
package com.iver.cit.gvsig.fmap.layers;
2

    
3
import org.cresques.cts.IProjection;
4

    
5
import com.hardcode.driverManager.DriverLoadException;
6
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
7
import com.iver.cit.gvsig.exceptions.layers.DriverLayerException;
8
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
9
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
10
import com.iver.cit.gvsig.exceptions.layers.NameLayerException;
11
import com.iver.cit.gvsig.exceptions.layers.ProjectionLayerException;
12
import com.iver.cit.gvsig.exceptions.layers.XMLLayerException;
13
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
14
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver;
15
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
16
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
17
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
18
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
19
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
20
import com.iver.utiles.XMLEntity;
21

    
22
public class FLayerJDBCVectorial  extends FLyrVect {
23
        private boolean loaded = false;
24

    
25
        private VectorialDatabaseDriver dbDriver = null;
26

    
27
        /* Esto deberia ir en el FLyrDefault */
28
        public void setProjectionByName(String projectionName) throws Exception{
29
                IProjection proj = CRSFactory.getCRS(projectionName);
30
                if (proj == null) {
31
                        throw new Exception("No se ha encontrado la proyeccion: "+ projectionName);
32
                }
33
                this.setProjection(proj);
34

    
35
        }
36

    
37

    
38
        public void setDriver(VectorialDatabaseDriver driver) {
39
                this.dbDriver = driver;
40
        }
41

    
42
        public void setDriverByName(String driverName) throws ReadDriverException {
43
                try {
44
                        this.setDriver(
45
                          (VectorialDatabaseDriver)LayerFactory.getDM().getDriver(driverName)
46
                        );
47
                } catch (DriverLoadException e) {
48
                        throw new ReadDriverException(getName(),e);
49
                }
50
        }
51

    
52

    
53
        public VectorialDatabaseDriver getDriver() {
54
                return this.dbDriver;
55
        }
56
        /* FIXME: esto tendria que tener declarado un throws de algo*/
57
        public void wakeUp() throws LoadLayerException {
58
                if (!loaded) {
59
                        this.load();
60
                }
61

    
62
        }
63

    
64

    
65
        public void load() throws LoadLayerException {
66
                if (this.getName() == null || this.getName().length() == 0) {
67
                        this.setAvailable(false);
68
                        throw new NameLayerException(this.getName(),null);
69
                }
70
                if (this.dbDriver == null) {
71
                        this.setAvailable(false);
72
                        //TODO: traducir???
73
                        throw new DriverLayerException(this.getName(),null);
74
                }
75
                if (this.getProjection() == null) {
76
                        this.setAvailable(false);
77
                        //TODO: traducir???
78
                        throw new ProjectionLayerException(this.getName(),null);
79
                }
80

    
81
//                try {
82
                        try {
83
                                ((DefaultDBDriver)this.dbDriver).load();
84
                        } catch (ReadDriverException e1) {
85
                                throw new LoadLayerException(this.getName(),e1);
86
                        }
87
//                } catch (Exception e) {
88
//                        this.setAvailable(false);
89
//                        throw new DriverIOException(e);
90
//                }
91

    
92
//                try {
93
                        VectorialDBAdapter dbAdapter = new VectorialDBAdapter();
94
                        dbAdapter.setDriver(this.dbDriver);
95
                        this.setSource(dbAdapter);
96

    
97
//                } catch (Exception e) {
98
//                        this.setAvailable(false);
99
//                        throw new DriverIOException(e);
100
//                }
101

    
102
//                try {
103
                        try {
104
                                this.putLoadSelection();
105
                                this.putLoadLegend();
106
                                this.initializeLegendDefault();
107
                        } catch (XMLException e) {
108
                                this.setAvailable(false);
109
                                throw new XMLLayerException(this.getName(),e);
110
                        } catch (LegendLayerException e) {
111
                                this.setAvailable(false);
112
                                throw new LegendLayerException(this.getName(),e);
113
                        } catch (ReadDriverException e) {
114
                                this.setAvailable(false);
115
                                throw new LoadLayerException(this.getName(),e);
116
                        }
117
//                } catch (Exception e) {
118
//                        this.setAvailable(false);
119
//                        //TODO: traducir???
120
//                        throw new DriverIOException(e);
121
//                }
122
                this.cleanLoadOptions();
123
        }
124

    
125
        /* Esto deberia ir en FLyrVect */
126
        private void initializeLegendDefault() throws ReadDriverException, LegendLayerException {
127
                if (this.getLegend() == null) {
128
            if (this.getRecordset().getDriver() instanceof WithDefaultLegend) {
129
                WithDefaultLegend aux = (WithDefaultLegend) this.getRecordset().getDriver();
130
                this.setLegend((VectorialLegend) aux.getDefaultLegend());
131
            } else {
132
                this.setLegend(LegendFactory.createSingleSymbolLegend(
133
                        this.getShapeType()));
134
            }
135
                }
136
        }
137

    
138
        public void setXMLEntity(XMLEntity xml) throws XMLException {
139
        IProjection proj = null;
140
        if (xml.contains("proj")) {
141
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
142
        }
143
        else
144
        {
145
            proj = this.getMapContext().getViewPort().getProjection();
146
        }
147
                this.setName(xml.getName());
148
                this.setProjection(proj);
149
        String driverName = xml.getStringProperty("db");
150
        VectorialDatabaseDriver driver;
151
        try {
152

    
153
            driver = (VectorialDatabaseDriver) LayerFactory.getDM().getDriver(driverName);
154
            //Hay que separar la carga de los datos del XMLEntity del load.
155
            driver.setXMLEntity(xml.getChild(2));
156
            this.setDriver(driver);
157

    
158
        } catch (Exception e){
159
                throw new XMLException(e);
160
        }
161
        super.setXMLEntityNew(xml);
162

    
163

    
164
        }
165

    
166
}