Statistics
| Revision:

root / trunk / extensions / extHyperlink / src / org / gvsig / hyperlink / layers / VectLayerManager.java @ 28132

History | View | Annotate | Download (7.03 KB)

1
package org.gvsig.hyperlink.layers;
2

    
3
import java.awt.geom.Point2D;
4
import java.io.File;
5
import java.net.URI;
6
import java.net.URISyntaxException;
7
import java.util.ArrayList;
8
import java.util.BitSet;
9
import java.util.Map;
10

    
11
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
12
import com.hardcode.gdbms.engine.data.DataSource;
13
import com.iver.andami.PluginServices;
14
import com.iver.andami.messages.NotificationManager;
15
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
16
import com.iver.cit.gvsig.fmap.layers.FBitSet;
17
import com.iver.cit.gvsig.fmap.layers.FLayer;
18
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
19
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
20
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
21
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
22

    
23
public class VectLayerManager implements ILinkLayerManager {
24
        private FLyrVect _layer = null;
25

    
26
        public URI[] getLink(Point2D point, double tolerance, String fieldName, String fileExtension) {
27
                FLyrVect lyrVect = (FLyrVect) _layer;
28
                FBitSet newBitSet;
29
                BitSet bitset;
30
                ArrayList uriList;
31

    
32
                //Construimos el BitSet (V?ctor con componentes BOOLEAN) con la consulta que
33
                //hacemos a la capa.
34

    
35
                try {
36
                        newBitSet = lyrVect.queryByPoint(point, tolerance);
37
                        bitset = newBitSet;
38
                } catch (ReadDriverException e1) {
39
                        PluginServices.getLogger().error(e1);
40
                        return null;
41
                } catch (VisitorException e1) {
42
                        PluginServices.getLogger().error(e1);
43
                        return null;
44
                }
45

    
46
                //Si el bitset creado no est? vac?o creamos el vector de URLS correspondientes
47
                //a la consulta que hemos hecho.
48

    
49
                if (bitset!=null){
50
                        try {
51
                                if (lyrVect instanceof AlphanumericData) {
52

    
53
                                        DataSource ds = ((AlphanumericData) lyrVect).getRecordset();
54
                                        ds.start();
55
                                        //boolean exist=false;
56
                                        int idField;
57
                                        //Creo el vector de URL?s con la misma longitud que el bitset
58
                                        uriList = new ArrayList();
59

    
60
                                        //Consigo el identificador del campo pasandole como par?metro el
61
                                        //nombre del campo del ?nlace
62
                                        idField = ds.getFieldIndexByName(fieldName);
63
                                        if (idField != -1){
64
                                                //Recorremos el BitSet siguiendo el ejmplo de la clase que se
65
                                                //proporciona en la API
66
                                                int i = 0;
67
                                                for (int j = bitset.nextSetBit(0); j >= 0;
68
                                                j = bitset.nextSetBit(j + 1)){
69
                                                        //Creamos el fichero con el nombre del campo y la extensi?n.
70
                                                        String fieldValue=ds.getFieldValue(j, idField).toString();
71
                                                        if (!fieldValue.equals("")) {
72
                                                                try {
73
                                                                        uriList.add(getURI(fieldValue, fileExtension));
74
                                                                } catch (URISyntaxException e) {
75
                                                                        NotificationManager.addWarning(PluginServices.getText(this, "Hyperlink__field_value_is_not_valid_file"), e);
76
                                                                }
77
                                                        }
78
                                                }
79
                                                ds.stop();
80
                                                return (URI[]) uriList.toArray(new URI[0]);
81
                                        }
82
                                        ds.stop();
83
                                }else {
84
                                        PluginServices.getLogger().error("Hyperlink error. FLyrVect class hierarchy changed??");
85
                                }
86
                        } catch (ReadDriverException e) {
87
                                PluginServices.getLogger().error(e);
88
                        }
89
                }
90
                return new URI[0];
91
        }
92

    
93
        protected URI getURI(String baseURI, String extension) throws URISyntaxException {
94
                String stringURI;
95
                if (extension.equals("")) {
96
                        stringURI = baseURI;
97
                }
98
                else if (extension.startsWith(".")) {
99
                        stringURI = baseURI+extension;
100
                }
101
                else {
102
                        stringURI = baseURI+"."+extension;
103
                }
104
                File file = new File(stringURI);
105
                if (file.exists()) {
106
                        return file.toURI();
107
                }
108
                else {
109
                        return new URI(stringURI);
110
                }
111
        }
112

    
113
        public FLayer getLayer() {
114
                return _layer;
115
        }
116

    
117
        public void setLayer(FLayer layer) throws IncompatibleLayerException {
118
                try {
119
                        _layer = (FLyrVect) layer;
120
                }
121
                catch (ClassCastException ex) {
122
                        throw new IncompatibleLayerException(ex);
123
                }
124
        }
125

    
126
        public Object create() {
127
                return this;
128
        }
129

    
130
        public Object create(Object[] args) {
131
                return this;
132
        }
133

    
134
        public Object create(Map args) {
135
                return this;
136
        }
137

    
138
        public URI[][] getLink(Point2D point, double tolerance, String[] fieldName, String fileExtension) {
139
                FLyrVect lyrVect = (FLyrVect) _layer;
140
                FBitSet newBitSet;
141
                BitSet bitset;
142
                URI uri[][]= null;
143

    
144
                //Construimos el BitSet (V?ctor con componentes BOOLEAN) con la consulta que
145
                //hacemos a la capa.
146
                        try {
147
                                newBitSet = lyrVect.queryByPoint(point, tolerance);
148
                        } catch (ReadDriverException e1) {
149
                                PluginServices.getLogger().error(e1);
150
                                return null;
151
                        } catch (VisitorException e1) {
152
                                PluginServices.getLogger().error(e1);
153
                                return null;
154
                        }
155
                        bitset = newBitSet;
156

    
157
                //Si el bitset creado no est? vac?o creamos el vector de URLS correspondientes
158
                //a la consulta que hemos hecho.
159

    
160
                if (bitset!=null){
161
                        try {
162
                                if (lyrVect instanceof AlphanumericData) {
163

    
164
                                        DataSource ds = ((AlphanumericData) lyrVect).getRecordset();
165
                                        ds.start();
166
                                        //boolean exist=false;
167
                                        int idField;
168
                                        //Creo el vector de URL?s con la misma longitud que el bitset
169
                                        uri = new URI[bitset.length()][fieldName.length];
170

    
171
                                        //Recorremos el BitSet siguiendo el ejmplo de la clase que se
172
                                        //proporciona en la API
173
                                        for (int geomNumber = bitset.nextSetBit(0); geomNumber >= 0;
174
                                                geomNumber = bitset.nextSetBit(geomNumber + 1))
175
                                        {
176
                                                for (int fieldCount=0; fieldCount<fieldName.length; fieldCount++) {
177
                                                        // get the field ID using the field name 
178
                                                        idField = ds.getFieldIndexByName(fieldName[fieldCount]);
179
                                                        if (idField!=-1) {
180
                                                                String auxField=ds.getFieldValue(geomNumber, idField).toString();
181
                                                                if(auxField.startsWith("http:/")){
182
                                                                        try {
183
                                                                                uri[geomNumber][fieldCount] = new URI(auxField);
184
                                                                        } catch (URISyntaxException e) {
185
                                                                                PluginServices.getLogger().error(e);
186
                                                                        }
187
                                                                }
188
                                                                else{
189
                                                                        File file =new File(ds.getFieldValue(geomNumber, idField).toString());
190
                                                                        uri[geomNumber][fieldCount] = file.toURI();
191
                                                                }
192
                                                        }
193
                                                        else {
194
                                                                PluginServices.getLogger().error("Hyperlink error. Field "+fieldName[fieldCount]+"doesn't exist!!");
195
                                                                uri[geomNumber][fieldCount] = null;
196
                                                        }
197
                                                }
198

    
199
                                        }
200
                                        ds.stop();
201
                                        return uri;
202
                                }else {
203
                                        PluginServices.getLogger().error("Hyperlink error. FLyrVect class hierarchy changed??");
204
                                }
205
                        } catch (ReadDriverException e) {
206
                                PluginServices.getLogger().error(e);
207
                        }
208
                }
209
                return new URI[0][0];
210
        }
211

    
212
        public String[] getFieldCandidates() {
213
                ReadableVectorial reader = _layer.getSource();
214
                try {
215
                        SelectableDataSource dataSource = reader.getRecordset();
216
                        ArrayList fields = new ArrayList();
217
                        int fieldType;
218
                        for (int i=0; i<dataSource.getFieldCount(); i++)  {
219
                                fieldType = dataSource.getFieldType(i);
220
                                if (fieldType==java.sql.Types.VARCHAR
221
                                                || fieldType==java.sql.Types.LONGVARCHAR
222
                                                || fieldType==java.sql.Types.CHAR
223
                                                || fieldType==java.sql.Types.BIGINT
224
                                                || fieldType==java.sql.Types.INTEGER
225
                                                || fieldType==java.sql.Types.NUMERIC
226
                                                || fieldType==java.sql.Types.SMALLINT
227
                                                || fieldType==java.sql.Types.TINYINT) {
228
                                        fields.add(dataSource.getFieldName(i));
229
                                }
230
                        }
231
                        return (String[]) fields.toArray(new String[0]);
232
                } catch (ReadDriverException e) {
233
                        NotificationManager.addError(PluginServices.getText(this,"Error reading layer fields"), e);
234
                }
235
                return new String[0];
236
        }
237

    
238
}